Skip to content

Commit

Permalink
Refactor additional, fileless, rules support
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 12, 2015
1 parent 29965a3 commit 6d2ba65
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 60 deletions.
17 changes: 13 additions & 4 deletions doc/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
6 changes: 0 additions & 6 deletions lib/rules/meta.js

This file was deleted.

6 changes: 6 additions & 0 deletions script/additional.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
67 changes: 17 additions & 50 deletions script/build-rule-documentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,75 +110,36 @@ 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');
example = find(tags, 'example');

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({
Expand Down

1 comment on commit 6d2ba65

@zcei
Copy link
Contributor

@zcei zcei commented on 6d2ba65 Jun 12, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly what I was talking about 👍
Good work!

Please sign in to comment.