From 837cb423aeef3799510f8b4ae51843f7909484e8 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 19 Sep 2018 08:24:18 -0700 Subject: [PATCH] New: Add --fix-type option to CLI (fixes #10855) --- lib/cli.js | 18 +++++++++++++ lib/options.js | 7 +++++ tools/update-rule-types.js | 53 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 tools/update-rule-types.js diff --git a/lib/cli.js b/lib/cli.js index f854015fe9c..39f4d9a953d 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -187,8 +187,26 @@ const cli = { return 2; } + if (currentOptions.fixType && !currentOptions.fix && !currentOptions.fixDryRun) { + log.error("The --fix-type option requires either --fix or --fix-dry-run."); + return 2; + } + const engine = new CLIEngine(translateOptions(currentOptions)); + // must be assigned after CLIEngine creation to use getRules() + if (currentOptions.fixType) { + + // convert to Set for faster lookup + const fixTypes = new Set(currentOptions.fixType); + + engine.options.fix = (lintResult) => { + const rules = engine.getRules(); + const matches = fixTypes.has(rules.get(lintResult.ruleId).meta.type); + return matches && (currentOptions.quiet ? quietFixPredicate(lintResult) : true); + }; + } + const report = useStdin ? engine.executeOnText(text, currentOptions.stdinFilename, true) : engine.executeOnFiles(files); if (currentOptions.fix) { diff --git a/lib/options.js b/lib/options.js index 9265d151d5c..5f2e06d627a 100644 --- a/lib/options.js +++ b/lib/options.js @@ -97,6 +97,13 @@ module.exports = optionator({ default: false, description: "Automatically fix problems without saving the changes to the file system" }, + { + option: "fix-type", + type: "Array", + enum: [ "problem", "suggestion", "style" ], + default: "problem,suggestion,style", + description: "Specify the types of fixes to apply" + }, { heading: "Ignoring files" }, diff --git a/tools/update-rule-types.js b/tools/update-rule-types.js new file mode 100644 index 00000000000..cafe4efaca7 --- /dev/null +++ b/tools/update-rule-types.js @@ -0,0 +1,53 @@ +/** + * 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"; + +module.exports = (fileInfo, api) => { + const j = api.jscodeshift; + const source = fileInfo.source; + + const nodes = j(source).find(j.ObjectExpression).filter((p) => { + return p.node.properties.some(node => node.key.name === "meta"); + }); + + return nodes.replaceWith((p) => { + const metaNode = p.node.properties.find(node => node.key.name === "meta"); + 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; + + switch (categoryNode.value) { + case "Stylistic Issues": + ruleType = "style"; + break; + + case "Possible Errors": + ruleType = "problem"; + break; + + default: + ruleType = "suggestion"; + } + + if (typeNode) { + console.log("Type already there."); + } else { + const newProp = j.property( + "init", + j.identifier("type"), + j.literal(ruleType) + ); + p.node.properties[0].value.properties.unshift(newProp); + } + + return p.node; + }).toSource(); +}; \ No newline at end of file