|
1 | 1 | /**
|
2 | 2 | * This script is used to inline assertions into the README.md documents.
|
3 | 3 | */
|
4 |
| -import _ from 'lodash'; |
5 |
| -import glob from 'glob'; |
6 | 4 | import path from 'path';
|
7 | 5 | import fs from 'fs';
|
| 6 | +import _ from 'lodash'; |
| 7 | +import glob from 'glob'; |
8 | 8 |
|
9 |
| -const formatCodeSnippet = (setup) => { |
10 |
| - const paragraphs = []; |
| 9 | +const trimCode = (code) => { |
| 10 | + let lines = _.trim(code).split('\n'); |
11 | 11 |
|
12 |
| - paragraphs.push(trimCode(setup.code)); |
| 12 | + const indendation = lines[lines.length - 1].match(/^\s+/); |
13 | 13 |
|
14 |
| - if (setup.settings) { |
15 |
| - paragraphs.push('// Settings: ' + JSON.stringify(setup.settings)); |
16 |
| - } |
| 14 | + const indentSize = indendation ? indendation[0].length : 0; |
17 | 15 |
|
18 |
| - if (setup.options) { |
19 |
| - paragraphs.push('// Options: ' + JSON.stringify(setup.options)); |
| 16 | + lines = _.map(lines, (line, i) => { |
| 17 | + if (i === 0) { |
| 18 | + return line; |
20 | 19 | }
|
21 | 20 |
|
22 |
| - if (setup.errors) { |
23 |
| - paragraphs.push('// Message: ' + setup.errors[0].message); |
24 |
| - } |
| 21 | + return line.slice(indentSize); |
| 22 | + }); |
25 | 23 |
|
26 |
| - return paragraphs.join('\n'); |
| 24 | + return lines.join('\n'); |
27 | 25 | };
|
28 | 26 |
|
29 |
| -const getAssertions = () => { |
30 |
| - const assertionFiles = glob.sync(path.resolve(__dirname, '../test/rules/assertions/*.js')); |
| 27 | +const formatCodeSnippet = (setup) => { |
| 28 | + const paragraphs = []; |
31 | 29 |
|
32 |
| - const assertionNames = _.map(assertionFiles, (filePath) => { |
33 |
| - return path.basename(filePath, '.js'); |
34 |
| - }); |
| 30 | + paragraphs.push(trimCode(setup.code)); |
35 | 31 |
|
36 |
| - const assertionCodes = _.map(assertionFiles, (filePath) => { |
37 |
| - let codes; |
| 32 | + if (setup.settings) { |
| 33 | + paragraphs.push('// Settings: ' + JSON.stringify(setup.settings)); |
| 34 | + } |
38 | 35 |
|
39 |
| - codes = require(filePath).default; |
| 36 | + if (setup.options) { |
| 37 | + paragraphs.push('// Options: ' + JSON.stringify(setup.options)); |
| 38 | + } |
40 | 39 |
|
41 |
| - return { |
42 |
| - valid: _.map(codes.valid, formatCodeSnippet), |
43 |
| - invalid: _.map(codes.invalid, formatCodeSnippet) |
44 |
| - }; |
45 |
| - }); |
| 40 | + if (setup.errors) { |
| 41 | + paragraphs.push('// Message: ' + setup.errors[0].message); |
| 42 | + } |
46 | 43 |
|
47 |
| - return _.zipObject(assertionNames, assertionCodes); |
| 44 | + return paragraphs.join('\n'); |
48 | 45 | };
|
49 | 46 |
|
50 |
| -const trimCode = (code) => { |
51 |
| - let lines = _.trim(code).split('\n'); |
52 |
| - |
53 |
| - const indendation = lines[lines.length - 1].match(/^\s+/); |
| 47 | +const getAssertions = () => { |
| 48 | + const assertionFiles = glob.sync(path.resolve(__dirname, '../test/rules/assertions/*.js')); |
54 | 49 |
|
55 |
| - const indentSize = indendation ? indendation[0].length : 0; |
| 50 | + const assertionNames = _.map(assertionFiles, (filePath) => { |
| 51 | + return path.basename(filePath, '.js'); |
| 52 | + }); |
56 | 53 |
|
57 |
| - lines = _.map(lines, (line, i) => { |
58 |
| - if (i === 0) { |
59 |
| - return line; |
60 |
| - } |
| 54 | + const assertionCodes = _.map(assertionFiles, (filePath) => { |
| 55 | + // eslint-disable-next-line global-require, import/no-dynamic-require |
| 56 | + const codes = require(filePath).default; |
61 | 57 |
|
62 |
| - return line.slice(indentSize); |
63 |
| - }); |
| 58 | + return { |
| 59 | + invalid: _.map(codes.invalid, formatCodeSnippet), |
| 60 | + valid: _.map(codes.valid, formatCodeSnippet) |
| 61 | + }; |
| 62 | + }); |
64 | 63 |
|
65 |
| - return lines.join('\n'); |
| 64 | + return _.zipObject(assertionNames, assertionCodes); |
66 | 65 | };
|
67 | 66 |
|
68 | 67 | const updateDocuments = (assertions) => {
|
69 |
| - const readmeDocumentPath = path.join(__dirname, '../README.md'); |
70 |
| - |
71 |
| - let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8'); |
| 68 | + const readmeDocumentPath = path.join(__dirname, '../README.md'); |
72 | 69 |
|
73 |
| - documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => { |
74 |
| - let ruleName, |
75 |
| - ruleAssertions; |
| 70 | + let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8'); |
76 | 71 |
|
77 |
| - ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1]; |
| 72 | + documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => { |
| 73 | + const ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1]; |
| 74 | + const ruleAssertions = assertions[ruleName]; |
78 | 75 |
|
79 |
| - ruleAssertions = assertions[ruleName]; |
| 76 | + if (!ruleAssertions) { |
| 77 | + throw new Error('No assertions available for rule "' + ruleName + '".'); |
80 | 78 |
|
81 |
| - if (!ruleAssertions) { |
82 |
| - throw new Error('No assertions available for rule "' + ruleName + '".'); |
83 |
| - |
84 |
| - return assertionsBlock; |
85 |
| - } |
| 79 | + return assertionsBlock; |
| 80 | + } |
86 | 81 |
|
87 |
| - 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'; |
88 |
| - }); |
| 82 | + 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'; |
| 83 | + }); |
89 | 84 |
|
90 |
| - fs.writeFileSync(readmeDocumentPath, documentBody); |
| 85 | + fs.writeFileSync(readmeDocumentPath, documentBody); |
91 | 86 | };
|
92 | 87 |
|
93 | 88 | updateDocuments(getAssertions());
|
0 commit comments