]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tools/update-rule-types.js
first commit
[pve-eslint.git] / eslint / tools / update-rule-types.js
1 /**
2 * JSCodeShift script to update meta.type in rules.
3 * Run over the rules directory only. Use this command:
4 *
5 * jscodeshift -t tools/update-rule-types.js lib/rules/
6 * @author Nicholas C. Zakas
7 */
8 "use strict";
9
10 const path = require("path");
11 const ruleTypes = require("./rule-types.json");
12
13 module.exports = (fileInfo, api) => {
14 const j = api.jscodeshift;
15 const source = fileInfo.source;
16 const ruleName = path.basename(fileInfo.path, ".js");
17
18 // get the object literal representing the rule
19 const nodes = j(source).find(j.ObjectExpression).filter(p => p.node.properties.some(node => node.key.name === "meta"));
20
21 // updating logic
22 return nodes.replaceWith(p => {
23
24 // gather important nodes from the rule
25 const metaNode = p.node.properties.find(node => node.key.name === "meta");
26
27 // if there's no properties, just exit
28 if (!metaNode.value.properties) {
29 return p.node;
30 }
31
32 const typeNode = metaNode.value.properties.find(node => node.key.name === "type");
33 const docsNode = metaNode.value.properties.find(node => node.key.name === "docs");
34 const categoryNode = docsNode.value.properties.find(node => node.key.name === "category").value;
35
36 let ruleType;
37
38 // the rule-types.json file takes highest priority
39 if (ruleName in ruleTypes) {
40 ruleType = ruleTypes[ruleName];
41 } else {
42
43 // otherwise fallback to category
44 switch (categoryNode.value) {
45 case "Stylistic Issues":
46 ruleType = "style";
47 break;
48
49 case "Possible Errors":
50 ruleType = "problem";
51 break;
52
53 default:
54 ruleType = "suggestion";
55 }
56 }
57
58 if (typeNode) {
59
60 // update existing type node
61 typeNode.value = j.literal(ruleType);
62 } else {
63
64 // add new type node if one doesn't exist
65 const newProp = j.property(
66 "init",
67 j.identifier("type"),
68 j.literal(ruleType)
69 );
70
71 p.node.properties[0].value.properties.unshift(newProp);
72 }
73
74 return p.node;
75 }).toSource();
76 };