Александра Шинкевич (@neesoglasnaja)
Правила, по которым надо писать код
Автоматическая проверка кода
Работают, только если...
С настройки редактора
root = true
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4
[*.{md,jade}]
indent_style = tab
npm install htmlhint -g
htmlhint test.html
{
"tagname-lowercase": true,
"attr-lowercase": true,
"attr-value-double-quotes": true,
"doctype-first": true,
"tag-pair": true,
"spec-char-escape": true,
"id-unique": true,
"src-not-empty": true,
"attr-no-duplication": true,
"title-require": true
}
npm install --save-dev gulp-htmlhint
const htmlhint = require('gulp-htmlhint');
gulp
.src('./src/*.html')
.pipe(htmlhint());
npm install --save-dev grunt-htmlhint
grunt.loadNpmTasks('grunt-htmlhint');
...
htmlhint: {
options: {
htmlhintrc: '.htmlhintrc'
},
}
npm install --save-dev htmlhint-loader
module.exports = {
module: {
rules: [{
enforce: 'pre',
test: /\.html/,
loader: 'htmlhint-loader',
exclude: /node_modules/
}]
}
}
{
"extends": "stylelint-config-recommended",
"rules": {
"at-rule-no-unknown": [ true, {
"ignoreAtRules": ["extends"]
}],
"block-no-empty": null,
"unit-whitelist": ["em", "rem", "s"]
}
}
npm install -g stylelint
stylelint "styles/*.css"
npm install stylelint gulp-stylelint --save-dev
const stylelint = require('gulp-stylelint');
gulp
.src('./src/*.css')
.pipe(gulpStylelint({
reporters: [
{ formatter: 'string', console: true }
]
}));
npm install stylelint grunt-stylelint --save-dev
grunt.loadNpmTasks('grunt-stylelint');
...
grunt.initConfig({
stylelint: {
all: ['css/**/*.css', 'sass/**/*.scss']
}
});
npm install stylelint-webpack-plugin --save-dev
const StyleLintPlugin = require('stylelint-webpack-plugin');
module.exports = {
plugins: [
new StyleLintPlugin(options),
]
}
(master) Latest commit on 16 Feb 2017
npm install csscomb -g
csscomb assets/css
{
"always-semicolon": true,
"eof-newline": true,
"leading-zero": true,
"quotes": "single",
...,
"sort-order": [
[
"position",
"z-index"
], [
"display",
"visibility"
]
]
}
{
"parserOptions": {
"parser": "babel-eslint",
"sourceType": "module"
},
"env": { "browser": true },
"extends": ["airbnb-base"],
"plugins": ["vue"],
"rules": {
"semi": ["error", "always"],
"no-debugger": "warn"
}
}
npm install -g eslint
eslint --init
eslint scripts/*.js
npm install gulp-eslint --save-dev
const eslint = require('gulp-eslint');
gulp
.src('./scripts/*.js')
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
npm install grunt-eslint --save-dev
require('load-grunt-tasks')(grunt);
grunt.initConfig({
eslint: {
target: ['file.js']
}
});
npm install eslint eslint-loader --save-dev
module.exports = {
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
loader: "eslint-loader",
options: { emitError: true }
}]
}
}
#! /bin/bash
if !(npm run lint:js --silent)
exit 1;
fi
npm install --save-dev pre-commit
{
"name": "my-project",
"scripts": {
"lint:js": "eslint ./js/*.js",
"test": "echo '🤣 No' && exit 1"
},
"pre-commit": [
"lint:js",
"test"
]
}
Решение: понижать в local-dev-сборке «мешающие» свойства доwarn
.local-dev.eslintrc
extend
от существующей конфигурацииwarn
const isLocal = process.env.ENV_LOCAL === "local";
module.exports = {
module: {
rules: [{
...
loader: "eslint-loader",
options: {
configFile: isLocal ? ".local-dev.eslintrc" : ".eslintrc"
}
}]
}
}
Решение: делать проверки в CI
pipelines:
default:
- step:
caches:
- node
name: Source code linting
script:
- npm i
- npm run lint
git hooks
bit.ly/Standards-ChernivtsiJS
Александра Шинкевич (@neesoglasnaja)