forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-rule-types.js
77 lines (60 loc) · 2.25 KB
/
update-rule-types.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
/**
* JSCodeShift script to update meta.type in rules.
* Run over the rules directory only. Use this command:
*
* jscodeshift -t tools/update-rule-types.js lib/rules/
*
* @author Nicholas C. Zakas
*/
"use strict";
const path = require("path");
const ruleTypes = require("./rule-types.json");
module.exports = (fileInfo, api) => {
const j = api.jscodeshift;
const source = fileInfo.source;
const ruleName = path.basename(fileInfo.path, ".js");
// get the object literal representing the rule
const nodes = j(source).find(j.ObjectExpression).filter(p => p.node.properties.some(node => node.key.name === "meta"));
// updating logic
return nodes.replaceWith(p => {
// gather important nodes from the rule
const metaNode = p.node.properties.find(node => node.key.name === "meta");
// if there's no properties, just exit
if (!metaNode.value.properties) {
return p.node;
}
const typeNode = metaNode.value.properties.find(node => node.key.name === "type");
const docsNode = metaNode.value.properties.find(node => node.key.name === "docs");
const categoryNode = docsNode.value.properties.find(node => node.key.name === "category").value;
let ruleType;
// the rule-types.json file takes highest priority
if (ruleName in ruleTypes) {
ruleType = ruleTypes[ruleName];
} else {
// otherwise fallback to category
switch (categoryNode.value) {
case "Stylistic Issues":
ruleType = "style";
break;
case "Possible Errors":
ruleType = "problem";
break;
default:
ruleType = "suggestion";
}
}
if (typeNode) {
// update existing type node
typeNode.value = j.literal(ruleType);
} else {
// add new type node if one doesn't exist
const newProp = j.property(
"init",
j.identifier("type"),
j.literal(ruleType)
);
p.node.properties[0].value.properties.unshift(newProp);
}
return p.node;
}).toSource();
};