mirror of
https://github.com/remarkjs/remark-lint.git
synced 2024-09-11 19:57:15 +03:00
Refactor additional, fileless, rules support
This commit is contained in:
parent
29965a3899
commit
6d2ba6557e
17
doc/rules.md
17
doc/rules.md
@ -71,10 +71,19 @@ be null or undefined in order to be ignored.
|
||||
|
||||
### reset
|
||||
|
||||
By default, all rules are turned on unless explicitly
|
||||
set to `false`. When `reset: true`, the opposite is true:
|
||||
all rules are turned off, unless when given a non-nully and
|
||||
non-false value.
|
||||
````md
|
||||
<!-- Explicitly activate rules: -->
|
||||
```json
|
||||
{
|
||||
"reset": true,
|
||||
"final-newline": true
|
||||
}
|
||||
```
|
||||
````
|
||||
|
||||
By default, all rules are turned on unless explicitly set to `false`.
|
||||
When `reset: true`, the opposite is true: all rules are turned off,
|
||||
unless when given a non-nully and non-false value.
|
||||
|
||||
Options: `boolean`, default: `false`.
|
||||
|
||||
|
@ -1,6 +0,0 @@
|
||||
module.exports = {
|
||||
'reset': {
|
||||
description: 'By default, all rules are turned on unless explicitly set to `false`.\nWhen `reset: true`, the opposite is true: all rules are turned off,\nunless when given a non-nully and non-false value.',
|
||||
example: 'reset: true\n<!-- Now you need to explicitly activate rules -->\nfinal-newline: true\n'
|
||||
}
|
||||
};
|
6
script/additional.json
Executable file
6
script/additional.json
Executable file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"reset": {
|
||||
"description": "By default, all rules are turned on unless explicitly set to `false`.\nWhen `reset: true`, the opposite is true: all rules are turned off,\nunless when given a non-nully and non-false value.\n\nOptions: `boolean`, default: `false`.",
|
||||
"example": " <!-- Explicitly activate rules: -->\n ```json\n {\n \"reset\": true,\n \"final-newline\": true\n }\n ```\n"
|
||||
}
|
||||
}
|
@ -19,7 +19,13 @@ var dox = require('dox');
|
||||
var mdast = require('mdast');
|
||||
var toc = require('mdast-toc');
|
||||
var rules = require('../lib/rules');
|
||||
var metaRules = require('../lib/rules/meta');
|
||||
var additional = require('./additional.json');
|
||||
|
||||
/*
|
||||
* Methods.
|
||||
*/
|
||||
|
||||
var exists = fs.existsSync;
|
||||
|
||||
function find(tags, key) {
|
||||
var value = null;
|
||||
@ -104,51 +110,22 @@ children.push({
|
||||
}]
|
||||
});
|
||||
|
||||
/*
|
||||
* Add `reset` docs.
|
||||
*/
|
||||
|
||||
children.push({
|
||||
'type': 'heading',
|
||||
'depth': 3,
|
||||
'children': [{
|
||||
'type': 'text',
|
||||
'value': 'reset'
|
||||
}]
|
||||
}, {
|
||||
'type': 'paragraph',
|
||||
'children': [{
|
||||
'type': 'text',
|
||||
'value': 'By default, all rules are turned on unless explicitly\n' +
|
||||
'set to `false`. When `reset: true`, the opposite is true:\n' +
|
||||
'all rules are turned off, unless when given a non-nully and\n' +
|
||||
'non-false value.'
|
||||
}]
|
||||
}, {
|
||||
'type': 'paragraph',
|
||||
'children': [{
|
||||
'type': 'text',
|
||||
'value': 'Options: `boolean`, default: `false`.'
|
||||
}]
|
||||
});
|
||||
|
||||
/*
|
||||
* Add rules.
|
||||
*/
|
||||
|
||||
Object.keys(rules)
|
||||
.concat(Object.keys(metaRules))
|
||||
.sort()
|
||||
Object.keys(additional).sort()
|
||||
.concat(Object.keys(rules).sort())
|
||||
.forEach(function (ruleId) {
|
||||
var description;
|
||||
var filePath;
|
||||
var example;
|
||||
var code;
|
||||
var rule;
|
||||
var tags;
|
||||
|
||||
try {
|
||||
filePath = path.join('lib', 'rules', ruleId + '.js');
|
||||
filePath = path.join('lib', 'rules', ruleId + '.js');
|
||||
|
||||
if (exists(filePath)) {
|
||||
code = fs.readFileSync(filePath, 'utf-8');
|
||||
tags = dox.parseComments(code)[0].tags;
|
||||
description = find(tags, 'fileoverview');
|
||||
@ -156,23 +133,13 @@ Object.keys(rules)
|
||||
|
||||
if (!description) {
|
||||
throw new Error(ruleId + ' is missing a `@fileoverview`');
|
||||
} else {
|
||||
description = description.string;
|
||||
}
|
||||
|
||||
if (example) {
|
||||
example = example.string;
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
// Handle file-less rules
|
||||
if (e.code === 'ENOENT') {
|
||||
rule = metaRules[ruleId];
|
||||
description = rule.description;
|
||||
example = rule.example;
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
description = description.string;
|
||||
example = example && example.string;
|
||||
} else {
|
||||
description = additional[ruleId].description;
|
||||
example = additional[ruleId].example;
|
||||
}
|
||||
|
||||
children.push({
|
||||
|
Loading…
Reference in New Issue
Block a user