Skip to content

Commit 260428a

Browse files
committed
refactor: separate test and doc checks
1 parent 318f482 commit 260428a

File tree

7 files changed

+208
-187
lines changed

7 files changed

+208
-187
lines changed

CONTRIBUTING.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
When making a commit, the following Pre-Commit hooks run:
88

9-
* tests and docs checks
9+
* test and documentation checks
1010
* tests
1111
* lint
1212
* commit message validation (see "Commit Messages" below)
@@ -21,11 +21,11 @@ All commit messages must begin with one of the following prefixes:
2121
* `docs: `
2222
* `chore: `
2323

24-
The prefix is used to bump the correct segment of the version number automatically during deploy.
24+
The prefix is used to bump the correct segment of the version number during the automatic release.
2525

2626
## Tests
2727

28-
Run them with `npm t`.
28+
Run them with `npm test`.
2929

3030
## Lint
3131

@@ -50,4 +50,4 @@ Run with `npm run lint`.
5050

5151
A CI service will build and publish the new documentation.
5252

53-
Note: The section "The following patterns are considered problems:" and "The following patterns are not considered problems:" is **generated automatically** using the test cases.
53+
Note: Sections "The following patterns are considered problems:" and "The following patterns are not considered problems:" are **generated automatically** using the test cases.

bin/testsAndDocsCheck.js

-174
This file was deleted.

package.json

+9-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
"engines": {
3030
"node": ">=4"
3131
},
32+
"husky": {
33+
"hooks": {
34+
"post-commit": "npm run create-readme && git add README.md && git commit -m 'docs: generate docs' --no-verify",
35+
"pre-commit": "npm run check-docs && npm run check-tests && npm run lint && npm run test && npm run build && npm run format-json"
36+
}
37+
},
3238
"keywords": [
3339
"eslint",
3440
"plugin",
@@ -40,22 +46,17 @@
4046
"peerDependencies": {
4147
"eslint": ">=2.0.0"
4248
},
43-
"husky": {
44-
"hooks": {
45-
"post-commit": "npm run create-readme && git add README.md && git commit -m 'docs: generate docs' --no-verify",
46-
"pre-commit": "npm run tests-docs-check && npm run lint && npm run test && npm run build && npm run format-json"
47-
}
48-
},
4949
"repository": {
5050
"type": "git",
5151
"url": "https://github.com/gajus/eslint-plugin-flowtype"
5252
},
5353
"scripts": {
5454
"build": "rm -fr ./dist && babel ./src --out-dir ./dist --copy-files",
55+
"check-docs": "babel-node ./src/bin/checkDocs",
56+
"check-tests": "babel-node ./src/bin/checkTests",
5557
"create-readme": "gitdown ./.README/README.md --output-file ./README.md && npm run documentation-add-assertions",
56-
"documentation-add-assertions": "babel-node ./bin/readmeAssertions",
58+
"documentation-add-assertions": "babel-node ./bin/addAssertions",
5759
"format-json": "jsonlint --sort-keys --in-place --indent ' ' ./src/configs/recommended.json && echo '' >> ./src/configs/recommended.json",
58-
"tests-docs-check": "babel-node ./bin/testsAndDocsCheck",
5960
"lint": "eslint ./src ./tests",
6061
"test": "mocha --compilers js:babel-register ./tests/rules/index.js"
6162
},

bin/readmeAssertions.js renamed to src/bin/addAssertions.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#!/usr/bin/env node
2+
13
/**
2-
* This script is used to inline assertions into the README.md documents.
4+
* @file This script is used to inline assertions into the README.md documents.
35
*/
46

57
import path from 'path';

src/bin/checkDocs.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env node
2+
3+
// @flow
4+
5+
import fs from 'fs';
6+
import path from 'path';
7+
import {
8+
getRules,
9+
isFile
10+
} from './utilities';
11+
12+
const windows = (array, size) => {
13+
const output = [];
14+
15+
for (let ii = 0; ii < array.length - size + 1; ii++) {
16+
output.push(array.slice(ii, ii + size));
17+
}
18+
19+
return output;
20+
};
21+
22+
const getDocIndexRules = () => {
23+
const content = fs.readFileSync(path.resolve(__dirname, '../../.README/README.md'), 'utf-8');
24+
25+
const rules = content.split('\n').map((line) => {
26+
const match = /^{"gitdown": "include", "file": "([^"]+)"}$/.exec(line);
27+
28+
if (match === null) {
29+
return null;
30+
} else {
31+
return match[1].replace('./rules/', '').replace('.md', '');
32+
}
33+
}).filter((rule) => {
34+
return rule !== null;
35+
});
36+
37+
if (rules.length === 0) {
38+
throw new Error('Docs checker is broken - it could not extract rules from docs index file.');
39+
}
40+
41+
return rules;
42+
};
43+
44+
const hasCorrectAssertions = (docPath, name) => {
45+
const content = fs.readFileSync(docPath, 'utf-8');
46+
47+
const match = /<!-- assertions ([a-zA-Z]+) -->/.exec(content);
48+
49+
if (match === null) {
50+
return false;
51+
} else {
52+
return match[1] === name;
53+
}
54+
};
55+
56+
/**
57+
* Performed checks:
58+
* - file `/.README/rules/<rule>.md` exists
59+
* - file `/.README/rules/<rule>.md` contains correct assertions placeholder (`<!-- assertions ... -->`)
60+
* - rule is included in gitdown directive in `/.README/README.md`
61+
* - rules in `/.README/README.md` are alphabetically sorted
62+
*/
63+
const checkDocs = (rulesNames) => {
64+
const docIndexRules = getDocIndexRules();
65+
66+
const sorted = windows(docIndexRules, 2)
67+
.every((chunk) => {
68+
return chunk[0] < chunk[1];
69+
});
70+
71+
if (!sorted) {
72+
throw new Error('Rules are not alphabetically sorted in `.README/README.md` file.');
73+
}
74+
75+
const invalid = rulesNames.filter((names) => {
76+
const docPath = path.resolve(__dirname, '../../.README/rules', names[1] + '.md');
77+
const docExists = isFile(docPath);
78+
const inIndex = docIndexRules.indexOf(names[1]) !== -1;
79+
const hasAssertions = docExists ? hasCorrectAssertions(docPath, names[0]) : false;
80+
81+
return !(docExists && inIndex && hasAssertions);
82+
});
83+
84+
if (invalid.length > 0) {
85+
const invalidList = invalid
86+
.map((names) => {
87+
return names[0];
88+
}).join(', ');
89+
90+
throw new Error(
91+
'Docs checker encountered an error in: ' + invalidList + '. ' +
92+
'Make sure that for every rule you created documentation file with assertions placeholder in camelCase ' +
93+
'and included the file path in `.README/README.md` file.'
94+
);
95+
}
96+
};
97+
98+
checkDocs(getRules());

0 commit comments

Comments
 (0)