Skip to content

Commit 448259d

Browse files
committed
style: fix bin style
1 parent 59673c4 commit 448259d

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

bin/readme-assertions.js

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,88 @@
11
/**
22
* This script is used to inline assertions into the README.md documents.
33
*/
4-
import _ from 'lodash';
5-
import glob from 'glob';
64
import path from 'path';
75
import fs from 'fs';
6+
import _ from 'lodash';
7+
import glob from 'glob';
88

9-
const formatCodeSnippet = (setup) => {
10-
const paragraphs = [];
9+
const trimCode = (code) => {
10+
let lines = _.trim(code).split('\n');
1111

12-
paragraphs.push(trimCode(setup.code));
12+
const indendation = lines[lines.length - 1].match(/^\s+/);
1313

14-
if (setup.settings) {
15-
paragraphs.push('// Settings: ' + JSON.stringify(setup.settings));
16-
}
14+
const indentSize = indendation ? indendation[0].length : 0;
1715

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;
2019
}
2120

22-
if (setup.errors) {
23-
paragraphs.push('// Message: ' + setup.errors[0].message);
24-
}
21+
return line.slice(indentSize);
22+
});
2523

26-
return paragraphs.join('\n');
24+
return lines.join('\n');
2725
};
2826

29-
const getAssertions = () => {
30-
const assertionFiles = glob.sync(path.resolve(__dirname, '../test/rules/assertions/*.js'));
27+
const formatCodeSnippet = (setup) => {
28+
const paragraphs = [];
3129

32-
const assertionNames = _.map(assertionFiles, (filePath) => {
33-
return path.basename(filePath, '.js');
34-
});
30+
paragraphs.push(trimCode(setup.code));
3531

36-
const assertionCodes = _.map(assertionFiles, (filePath) => {
37-
let codes;
32+
if (setup.settings) {
33+
paragraphs.push('// Settings: ' + JSON.stringify(setup.settings));
34+
}
3835

39-
codes = require(filePath).default;
36+
if (setup.options) {
37+
paragraphs.push('// Options: ' + JSON.stringify(setup.options));
38+
}
4039

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+
}
4643

47-
return _.zipObject(assertionNames, assertionCodes);
44+
return paragraphs.join('\n');
4845
};
4946

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'));
5449

55-
const indentSize = indendation ? indendation[0].length : 0;
50+
const assertionNames = _.map(assertionFiles, (filePath) => {
51+
return path.basename(filePath, '.js');
52+
});
5653

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;
6157

62-
return line.slice(indentSize);
63-
});
58+
return {
59+
invalid: _.map(codes.invalid, formatCodeSnippet),
60+
valid: _.map(codes.valid, formatCodeSnippet)
61+
};
62+
});
6463

65-
return lines.join('\n');
64+
return _.zipObject(assertionNames, assertionCodes);
6665
};
6766

6867
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');
7269

73-
documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => {
74-
let ruleName,
75-
ruleAssertions;
70+
let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8');
7671

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];
7875

79-
ruleAssertions = assertions[ruleName];
76+
if (!ruleAssertions) {
77+
throw new Error('No assertions available for rule "' + ruleName + '".');
8078

81-
if (!ruleAssertions) {
82-
throw new Error('No assertions available for rule "' + ruleName + '".');
83-
84-
return assertionsBlock;
85-
}
79+
return assertionsBlock;
80+
}
8681

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+
});
8984

90-
fs.writeFileSync(readmeDocumentPath, documentBody);
85+
fs.writeFileSync(readmeDocumentPath, documentBody);
9186
};
9287

9388
updateDocuments(getAssertions());

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"eslint": "^4.19.1",
2222
"eslint-config-canonical": "^9.3.1",
2323
"gitdown": "^2.5.1",
24+
"glob": "^7.1.2",
2425
"globby": "^6.1.0",
2526
"mocha": "^3.5.3",
2627
"semantic-release": "^8.0.3"

0 commit comments

Comments
 (0)