1 |
|
2 |
|
3 |
|
4 | import _ from 'lodash';
|
5 | import glob from 'glob';
|
6 | import path from 'path';
|
7 | import fs from 'fs';
|
8 |
|
9 | const formatCodeSnippet = (setup) => {
|
10 | const paragraphs = [];
|
11 |
|
12 | if (setup.options) {
|
13 | paragraphs.push('// Options: ' + JSON.stringify(setup.options));
|
14 | }
|
15 |
|
16 | paragraphs.push(setup.code);
|
17 |
|
18 | if (setup.errors) {
|
19 | paragraphs.push('// Message: ' + setup.errors[0].message);
|
20 | }
|
21 |
|
22 | return paragraphs.join('\n');
|
23 | };
|
24 |
|
25 | const getAssertions = () => {
|
26 | const assertionFiles = glob.sync(path.resolve(__dirname, './../tests/rules/assertions/*.js'));
|
27 |
|
28 | const assertionNames = _.map(assertionFiles, (filePath) => {
|
29 | return path.basename(filePath, '.js');
|
30 | });
|
31 |
|
32 | const assertionCodes = _.map(assertionFiles, (filePath) => {
|
33 | const codes = require(filePath).default;
|
34 |
|
35 | return {
|
36 | valid: _.map(codes.valid, formatCodeSnippet),
|
37 | invalid: _.map(codes.invalid, formatCodeSnippet)
|
38 | };
|
39 | });
|
40 |
|
41 | return _.zipObject(assertionNames, assertionCodes);
|
42 | };
|
43 |
|
44 | const updateDocuments = (assertions) => {
|
45 | const readmeDocumentPath = path.join(__dirname, './../README.md');
|
46 |
|
47 | let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8');
|
48 |
|
49 | documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => {
|
50 | let ruleName,
|
51 | ruleAssertions;
|
52 |
|
53 | ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1];
|
54 |
|
55 | ruleAssertions = assertions[ruleName];
|
56 |
|
57 | if (!ruleAssertions) {
|
58 | throw new Error('No assertions available for rule "' + ruleName + '".');
|
59 |
|
60 | return assertionsBlock;
|
61 | }
|
62 |
|
63 | return 'The following patterns are considered problems:\n\n```js\n' + ruleAssertions.invalid.join('\n\n') + '\n```\n\nThe following patterns are not considered problems:\n\n```js\n' + ruleAssertions.valid.join('\n\n') + '\n```\n';
|
64 | });
|
65 |
|
66 | fs.writeFileSync(readmeDocumentPath, documentBody);
|
67 | };
|
68 |
|
69 | updateDocuments(getAssertions());
|