Simplify the creation an index file for your ESLint plugin
npm install create-eslint-index> Simplify the creation an index file for your ESLint plugin
```
$ npm install --save create-eslint-index req-all
With ESLint 3, a new rule format has been introduced. This new format makes it easier to attach metadata to each rule, which can then be used to generate recommended configurations and/or rule lists in the README automatically.
The new rule format looks like the following:
`js
'use strict';
const create = (context) => {
// ...
};
module.exports = {
create,
meta: {
docs: {
recommended: 'error',
description: 'Description'
}
}
};
`
`js
const createIndex = require('create-eslint-index');
const reqAll = require('req-all');
const rules = reqAll('rules', {camelize: false}); // Loads all rules from the rules folder and puts them in an object.
const recommendedRules = createIndex.createConfig({
plugin: ' prefix
field: 'meta.docs.recommended'
}, rules);
module.exports = {
rules,
configs: {
recommended: {
rules: recommendedRules
}
}
};
`
``
$ npm install --save-dev inject-in-tag
package.json:`json`
{
"scripts": {
"update-md": "inject-in-tag ./rule-description.js README.md"
}
}
README.md:`md$3
`
rule-description.js:`js
const reqAll = require('req-all');
const createIndex = require('create-eslint-index');
const index = require('./');
const rules = reqAll('rules', {camelize: false});
const settings = {
descriptionField: 'meta.docs.description',
docPath: 'docs/rules'
};
module.exports = {
RULES: \n${createIndex.createRulesDescription(settings, rules)}\n`
};
Result in the README:
`md$3
- rule-1 - This is rule 1
- rule-2 - This is rule 2
`
Creates a recommended setting object
#### settings (object)string
- settings.plugin (): Name of your plugin, without the "eslint-plugin-" prefix. If your plugin name is "eslint-plugin-foo", this should then evaluate to "foo".string
- settings.path (): Path to get to the recommended value of a rule.
#### rules (object)
Object containing each rule, where the key is the name of the rule and the value is the exported content of the file rule.
#### Example
See the example above for a more practical illustration.
`js
const rules = [
'rule-1': {
create() {},
meta: {
docs: {
recommended: 'error',
description: 'This is rule 1'
}
}
},
'rule-2': {
create() {},
meta: {
docs: {
recommended: ['error', 'option'],
description: 'This is rule 2'
}
}
},
];
createIndex.createConfig({
plugin: 'foo',
path: 'meta.docs.recommended'
}, rules);
/* =>
{
'foo/rule-1': 'error',
'foo/rule-2': ['error', 'option']
}
*/
`
Creates a list of rules and their description, as one big string.
#### settings (object)string
- settings.docPath (): Relative path to the documentation folder containing Markdown files matching your rules.string
- settings.descriptionField (): Path to get to the description value of a rule.
#### rules (object)
Object containing each rule, where the key is the name of the rule and the value is the exported content of the file rule.
#### Example
Let's assume rules is declared as in the previous example.
`js
const rules = reqAll('rules', {camelize: false});
const settings = {
descriptionField: 'meta.docs.description',
docPath: 'docs/rules'
};
console.log(createIndex.createRulesDescription(settings, rules));
/* =>
- rule-1 - This is rule 1
- rule-2 - This is rule 2
*/
``
MIT © Jeroen Engels