Skip to content

Commit

Permalink
New: Add --fix-type option to CLI (fixes #10855)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Sep 21, 2018
1 parent 14f4e46 commit 837cb42
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/cli.js
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions lib/options.js
Expand Up @@ -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"
},
Expand Down
53 changes: 53 additions & 0 deletions 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();
};

0 comments on commit 837cb42

Please sign in to comment.