Skip to content

Commit 2e53047

Browse files
committed
Add the new rule to the index
1 parent 46f2dfb commit 2e53047

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"eslint-config-airbnb-base": "^11.0.0",
3939
"eslint-plugin-import": "^2.2.0",
4040
"jest": "^18.1.0",
41+
"jscodeshift": "^0.3.30",
4142
"minimist": "^1.2.0",
4243
"rimraf": "^2.5.2",
4344
"to-ast": "^1.0.0"

scripts/addRuleToIndex.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
export const parser = 'flow';
2+
3+
export default function transformer(file, api, options) {
4+
const j = api.jscodeshift;
5+
const s = j(file.source);
6+
const {
7+
ruleName,
8+
rulePath,
9+
} = options || {};
10+
11+
const nameSort = (a, b) => {
12+
const aName = a.key.value;
13+
const bName = b.key.value;
14+
if (aName < bName) {
15+
return -1;
16+
}
17+
if (bName < aName) {
18+
return 1;
19+
}
20+
return 0;
21+
};
22+
23+
let changesMade = 0;
24+
25+
const rulePathInSrc = './' + rulePath.match(/src\/(.*)\.js/)[1];
26+
27+
changesMade += s.find(j.Identifier, {
28+
name: 'rules'
29+
}).forEach((path, index) => {
30+
// Add rule path.
31+
if (index === 0) {
32+
path.parentPath.value.value.properties.unshift(
33+
j.property(
34+
'init',
35+
j.literal(ruleName),
36+
j.callExpression(
37+
j.identifier('require'),
38+
[
39+
j.literal(rulePathInSrc),
40+
]
41+
),
42+
)
43+
);
44+
path.parentPath.value.value.properties.sort(nameSort);
45+
}
46+
// Set default reporting to error.
47+
if (index === 1) {
48+
path.parentPath.value.value.properties.unshift(
49+
j.property(
50+
'init',
51+
j.literal('jsx-a11y/' + ruleName),
52+
j.literal('error'),
53+
)
54+
);
55+
path.parentPath.value.value.properties.sort(nameSort);
56+
}
57+
}).length;
58+
59+
60+
if (changesMade === 0) {
61+
return null;
62+
}
63+
64+
return s.toSource({
65+
quote: 'single',
66+
trailingComma: true,
67+
});
68+
}

scripts/create-rule.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const argv = require('minimist')(process.argv.slice(2)); // eslint-disable-line
44
const ruleBoilerplateGenerator = require('./boilerplate/rule');
55
const testBoilerplateGenerator = require('./boilerplate/test');
66
const docBoilerplateGenerator = require('./boilerplate/doc');
7+
const exec = require('child_process').exec;
78

89
const ruleName = argv._[0];
910
const author = argv.author || '$AUTHOR';
@@ -29,3 +30,24 @@ const docBoilerplate = docBoilerplateGenerator(ruleName);
2930
fs.writeFileSync(rulePath, ruleBoilerplate);
3031
fs.writeFileSync(testPath, testBoilerplate);
3132
fs.writeFileSync(docsPath, docBoilerplate);
33+
34+
// Add the rule to the index
35+
exec([
36+
'./node_modules/jscodeshift/bin/jscodeshift.sh',
37+
'./src/index.js',
38+
'-t ./scripts/addRuleToIndex.js',
39+
'-p',
40+
'--extensions js',
41+
'--parser flow',
42+
`--ruleName=${ruleName}`,
43+
`--rulePath=${rulePath}`,
44+
].join(' '),
45+
(error, stdout, stderr) => {
46+
if (error) {
47+
console.error(`exec error: ${error}`);
48+
return;
49+
}
50+
console.log(`stdout: ${stdout}`);
51+
console.log(`stderr: ${stderr}`);
52+
}
53+
);

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = {
1717
'img-has-alt': require('./rules/img-has-alt'),
1818
'img-redundant-alt': require('./rules/img-redundant-alt'),
1919
'label-has-for': require('./rules/label-has-for'),
20-
lang: require('./rules/lang'),
20+
'lang': require('./rules/lang'),
2121
'mouse-events-have-key-events': require('./rules/mouse-events-have-key-events'),
2222
'no-access-key': require('./rules/no-access-key'),
2323
'no-autofocus': require('./rules/no-autofocus'),
@@ -28,7 +28,7 @@ module.exports = {
2828
'onclick-has-role': require('./rules/onclick-has-role'),
2929
'role-has-required-aria-props': require('./rules/role-has-required-aria-props'),
3030
'role-supports-aria-props': require('./rules/role-supports-aria-props'),
31-
scope: require('./rules/scope'),
31+
'scope': require('./rules/scope'),
3232
'tabindex-no-positive': require('./rules/tabindex-no-positive'),
3333
},
3434
configs: {

0 commit comments

Comments
 (0)