Lint it your Way
Lint it your Way
Code Style
— It easy while you work alone.
— But then your team start to grow …
— And config became more complex …
… and more …
— Configuration became nightmare …
… and even more …
… and then you realize, that you need custom rules.
Let's forbis string classes! CSS-Modules all the things!
npm i -g yo
npm i -g generator-eslint
mkdir company-lint
cd company-lint
yo eslint:plugin
yo eslint:rule
const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
};
var ruleTester = new RuleTester({parserOptions});
valid: [{ code: `
import React, { PureComponent } from 'react';
import styles from './Cart.css';
class Cart extends PureComponent {
render() {
return (<p className={styles.Cart}>…</p>)
}
}
export default Cart;
`}]
invalid: [{code: `
import React, { PureComponent } from 'react';
class Cart extends PureComponent {
render() {
return (<p className='Cart'>…</p>)
}
}
export default Cart;`,
errors: [{
message: "You should use css modules instead",
type: "JSXAttribute"
}]}]
module.exports = {
create: function(context) {
return {
"selector": handler,
}
},
};
https://astexplorer.net/
"JSXAttribute": function (node) {
if (
(node.name.name === 'className')
&& (node.value.type === 'Literal')
) {
context.report({
node,
message: 'You should use css modules instead'
})
}
},
npm i
npm t
npm publish
npm i -D company-lint
// .eslintrc
"plugins": [
"company-lint",
],
"rules": {
"company-lint/no-string-classname": 2,
},
Let's lint commit message!
npm init
npm i -S command-line-args
// index.js
#! /usr/bin/env node
// index.js
#! /usr/bin/env node
const fs = require('fs');
const filePath = process.argv[2];
if (!fs.existsSync(filePath)) {
console.log('Can not read commit message');
process.exit(1);
}
const message = fs.readFileSync(filePath);
const commandLineArgs = require('command-line-args');
const optionDefinitions = [{
name: 'regexp',
alias: 'r',
type: String,
defaultValue: '^[A-Z]+-[0-9]+\s-\s[\W\w\s]{5,}[\s]*$' },
];
const cli = commandLineArgs(optionDefinitions);
const commonRegexp = new RegExp(
cli.regexp,
'ig'
);
if (!commonRegexp.test(message)) {
console.log("Wrong commit message format");
process.exit(3);
}
process.exit(0);
// package.json
"name": "my-message-hook",
"main": "index.js",
"bin": {"commit-msg": "index.js"},
"scripts": {
"release": "npm version patch &&
git push --tags &&
npm publish --access public"
}
$ npm i -S my-message-hook
$ npm i -D husky
// package.json
"scripts": {
"commitmsg": "commit-msg
${GIT_PARAMS}
-r MyRegExp"
},
Anton Nemtsev
http://silentimp.info/
@silentimp
thesilentimp@gmail.com
skype: ravencry