forked from patternfly/pf-codemods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
80 lines (74 loc) · 2.17 KB
/
generate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const path = require('path');
const fs = require('fs');
const newRuleName = process.argv[2];
if (!newRuleName) {
console.log('usage: node generate [newRuleName]');
process.exit(1);
}
console.log('Generating rule', newRuleName);
// Write rule file
fs.writeFileSync(path.join(
__dirname,
'packages/eslint-plugin-pf-codemods/lib/rules/v5',
`${newRuleName}.js`
),
`const { getPackageImports } = require('../../helpers');
// https://github.com/patternfly/patternfly-react/pull/YOURNUMBERHERE
module.exports = {
create: function(context) {
const imports = getPackageImports(context, '@patternfly/react-core')
.filter(specifier => specifier.imported.name === 'YOURCOMPONENTNAME');
return imports.length == 0 ? {} : {
JSXOpeningElement(node) {
if (imports.map(imp => imp.local.name).includes(node.name.name)) {
const attribute = node.attributes.find(node => node.name && node.name.name === 'variant');
if (attribute) {
context.report({
node,
message: 'YOUR_MESSAGE_HERE',
fix(fixer) {
return fixer.replaceText(attribute, '');
}
});
}
}
}
};
}
};
`);
// Write test file
fs.writeFileSync(path.join(
__dirname,
'packages/eslint-plugin-pf-codemods/test/rules/v5',
`${newRuleName}.js`
),
`const ruleTester = require('../../ruletester');
const rule = require('../../../lib/rules/v5/${newRuleName}');
ruleTester.run("${newRuleName}", rule, {
valid: [
{
code: \`VALID_CODE_HERE\`,
}
],
invalid: [
{
code: \`import { } from '@patternfly/react-core';\`,
output: \`import { } from '@patternfly/react-core';\`,
errors: [{
message: \`YOUR_MESSAGE_HERE\`,
type: "JSXOpeningElement",
}]
},
]
});
`
);
// Update index file
const ruleIndexPath = path.join(__dirname, 'packages/eslint-plugin-pf-codemods/index.js');
const ruleIndex = fs.readFileSync(ruleIndexPath, 'utf8');
fs.writeFileSync(
ruleIndexPath,
// (ab)Use fact that `rules` object is at top of file
ruleIndex.replace("};", ` "${newRuleName}": require('./lib/rules/v5/${newRuleName}'),\n};`)
);