forked from patternfly/pf-codemods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-replace-prop-v5.js
75 lines (69 loc) · 2.45 KB
/
generate-replace-prop-v5.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
const path = require('path');
const fs = require('fs');
const componentName = process.argv[2]
const oldPropName = process.argv[3]
const newPropName = process.argv[4]
const referencePR = process.argv[5]
const lowerCasedComponentName = componentName[0].toLowerCase() + componentName.substring(1);
const newRuleName = `${lowerCasedComponentName}-remove-${oldPropName}`
if (!componentName || !oldPropName || !newPropName || !referencePR) {
console.log('usage: node generate [componentName] [oldPropName] [newPropName] [referencePR]');
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 { renameProp } = require('../../helpers');
// https://github.com/patternfly/patternfly-react/pull/${referencePR}
module.exports = {
meta: { fixable: 'code' },
create: renameProp(
'${componentName}',
{'${oldPropName}': '${newPropName}'},
node => \`${oldPropName} prop has been removed for \${node.name.name} and replaced with the ${newPropName} prop.\`
),
};
`);
// 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: \`import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${newPropName} />\`,
},
{
// No @patternfly/react-core import
code: \`<${componentName} ${oldPropName} />\`,
}
],
invalid: [
{
code: \`import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${oldPropName} />\`,
output: \`import { ${componentName} } from '@patternfly/react-core'; <${componentName} ${newPropName} />\`,
errors: [{
message: \`${oldPropName} prop has been removed for ${componentName} and replaced with the ${newPropName} prop.\`,
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};`)
);