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 Oct 1, 2018
1 parent 14f4e46 commit fe78ac8
Show file tree
Hide file tree
Showing 277 changed files with 1,102 additions and 44 deletions.
20 changes: 18 additions & 2 deletions lib/cli-engine.js
Expand Up @@ -48,6 +48,7 @@ const resolver = new ModuleResolver();
* @property {string[]} envs An array of environments to load.
* @property {string[]} extensions An array of file extensions to check.
* @property {boolean|Function} fix Execute in autofix mode. If a function, should return a boolean.
* @property {string[]} fixTypes Array of rule types to apply fixes for.
* @property {string[]} globals An array of global variables to declare.
* @property {boolean} ignore False disables use of .eslintignore.
* @property {string} ignorePath The ignore file to use instead of .eslintignore.
Expand Down Expand Up @@ -174,7 +175,6 @@ function processText(text, configHelper, filename, fix, allowInlineConfig, repor
}

const autofixingEnabled = typeof fix !== "undefined" && (!processor || processor.supportsAutofix);

const fixedResult = linter.verifyAndFix(text, config, {
filename: effectiveFilename,
allowInlineConfig,
Expand All @@ -183,7 +183,6 @@ function processText(text, configHelper, filename, fix, allowInlineConfig, repor
preprocess: processor && (rawText => processor.preprocess(rawText, effectiveFilename)),
postprocess: processor && (problemLists => processor.postprocess(problemLists, effectiveFilename))
});

const stats = calculateStatsPerFile(fixedResult.messages);

const result = {
Expand Down Expand Up @@ -429,6 +428,23 @@ class CLIEngine {
*/
this._lintResultCache = new LintResultCache(cacheFile, this.config);
}

// setup special filter for fixes
if (this.options.fixTypes && this.options.fixTypes.length > 0) {

// convert to Set for faster lookup
const fixTypes = new Set(this.options.fixTypes);

// save original value of options.fix in case it's a function
const originalFix = this.options.fix;

this.options.fix = (lintResult) => {
const rules = this.getRules();
const matches = fixTypes.has(rules.get(lintResult.ruleId).meta.type);
return matches && (typeof originalFix === "function" ? originalFix(lintResult) : true);
};
}

}

getRules() {
Expand Down
7 changes: 6 additions & 1 deletion lib/cli.js
Expand Up @@ -64,6 +64,7 @@ function translateOptions(cliOptions) {
cacheFile: cliOptions.cacheFile,
cacheLocation: cliOptions.cacheLocation,
fix: (cliOptions.fix || cliOptions.fixDryRun) && (cliOptions.quiet ? quietFixPredicate : true),
fixTypes: cliOptions.fixType,
allowInlineConfig: cliOptions.inlineConfig,
reportUnusedDisableDirectives: cliOptions.reportUnusedDisableDirectives
};
Expand Down Expand Up @@ -187,8 +188,12 @@ const cli = {
return 2;
}

const engine = new CLIEngine(translateOptions(currentOptions));
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));
const report = useStdin ? engine.executeOnText(text, currentOptions.stdinFilename, true) : engine.executeOnFiles(files);

if (currentOptions.fix) {
Expand Down
5 changes: 5 additions & 0 deletions lib/options.js
Expand Up @@ -97,6 +97,11 @@ module.exports = optionator({
default: false,
description: "Automatically fix problems without saving the changes to the file system"
},
{
option: "fix-type",
type: "Array",
description: "Specify the types of fixes to apply"
},
{
heading: "Ignoring files"
},
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/accessor-pairs.js
Expand Up @@ -72,12 +72,15 @@ function isPropertyDescriptor(node) {

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "enforce getter and setter pairs in objects",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/accessor-pairs"
},

schema: [{
type: "object",
properties: {
Expand All @@ -90,6 +93,7 @@ module.exports = {
},
additionalProperties: false
}],

messages: {
getter: "Getter is not present.",
setter: "Setter is not present."
Expand Down
5 changes: 5 additions & 0 deletions lib/rules/array-bracket-newline.js
Expand Up @@ -13,13 +13,17 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce linebreaks after opening and before closing array brackets",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/array-bracket-newline"
},

fixable: "whitespace",

schema: [
{
oneOf: [
Expand All @@ -42,6 +46,7 @@ module.exports = {
]
}
],

messages: {
unexpectedOpeningLinebreak: "There should be no linebreak after '['.",
unexpectedClosingLinebreak: "There should be no linebreak before ']'.",
Expand Down
5 changes: 5 additions & 0 deletions lib/rules/array-bracket-spacing.js
Expand Up @@ -12,13 +12,17 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce consistent spacing inside array brackets",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/array-bracket-spacing"
},

fixable: "whitespace",

schema: [
{
enum: ["always", "never"]
Expand All @@ -39,6 +43,7 @@ module.exports = {
additionalProperties: false
}
],

messages: {
unexpectedSpaceAfter: "There should be no space after '{{tokenValue}}'.",
unexpectedSpaceBefore: "There should be no space before '{{tokenValue}}'.",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/array-callback-return.js
Expand Up @@ -141,6 +141,8 @@ function isCallbackOfArrayMethod(node) {

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "enforce `return` statements in callbacks of array methods",
category: "Best Practices",
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/array-element-newline.js
Expand Up @@ -13,13 +13,17 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce line breaks after each array element",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/array-element-newline"
},

fixable: "whitespace",

schema: [
{
oneOf: [
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/arrow-body-style.js
Expand Up @@ -16,6 +16,8 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "require braces around arrow function bodies",
category: "ECMAScript 6",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/arrow-parens.js
Expand Up @@ -16,6 +16,8 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "require parentheses around arrow function arguments",
category: "ECMAScript 6",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/arrow-spacing.js
Expand Up @@ -16,6 +16,8 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "enforce consistent spacing before and after the arrow in arrow functions",
category: "ECMAScript 6",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/block-scoped-var.js
Expand Up @@ -10,6 +10,8 @@

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "enforce the use of variables within the scope they are defined",
category: "Best Practices",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/block-spacing.js
Expand Up @@ -13,6 +13,8 @@ const util = require("../util/ast-utils");

module.exports = {
meta: {
type: "style",

docs: {
description: "disallow or enforce spaces inside of blocks after opening block and before closing block",
category: "Stylistic Issues",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/brace-style.js
Expand Up @@ -13,6 +13,8 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce consistent brace style for blocks",
category: "Stylistic Issues",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/callback-return.js
Expand Up @@ -10,6 +10,8 @@

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "require `return` statements after callbacks",
category: "Node.js and CommonJS",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/camelcase.js
Expand Up @@ -11,6 +11,8 @@

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce camelcase naming convention",
category: "Stylistic Issues",
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/capitalized-comments.js
Expand Up @@ -108,13 +108,17 @@ function createRegExpForIgnorePatterns(normalizedOptions) {

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce or disallow capitalization of the first letter of a comment",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/capitalized-comments"
},

fixable: "code",

schema: [
{ enum: ["always", "never"] },
{
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/class-methods-use-this.js
Expand Up @@ -11,12 +11,15 @@

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "enforce that class methods utilize `this`",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/class-methods-use-this"
},

schema: [{
type: "object",
properties: {
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/comma-dangle.js
Expand Up @@ -76,13 +76,17 @@ function normalizeOptions(optionValue) {

module.exports = {
meta: {
type: "style",

docs: {
description: "require or disallow trailing commas",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/comma-dangle"
},

fixable: "code",

schema: {
definitions: {
value: {
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/comma-spacing.js
Expand Up @@ -12,6 +12,8 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce consistent spacing before and after commas",
category: "Stylistic Issues",
Expand Down
5 changes: 5 additions & 0 deletions lib/rules/comma-style.js
Expand Up @@ -13,13 +13,17 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce consistent comma style",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/comma-style"
},

fixable: "code",

schema: [
{
enum: ["first", "last"]
Expand All @@ -37,6 +41,7 @@ module.exports = {
additionalProperties: false
}
],

messages: {
unexpectedLineBeforeAndAfterComma: "Bad line breaking before and after ','.",
expectedCommaFirst: "',' should be placed first.",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/complexity.js
Expand Up @@ -20,6 +20,8 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "enforce a maximum cyclomatic complexity allowed in a program",
category: "Best Practices",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/computed-property-spacing.js
Expand Up @@ -12,6 +12,8 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce consistent spacing inside computed property brackets",
category: "Stylistic Issues",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/consistent-return.js
Expand Up @@ -53,6 +53,8 @@ function isClassConstructor(node) {

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "require `return` statements to either always or never specify values",
category: "Best Practices",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/consistent-this.js
Expand Up @@ -10,6 +10,8 @@

module.exports = {
meta: {
type: "style",

docs: {
description: "enforce consistent naming when capturing the current execution context",
category: "Stylistic Issues",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/constructor-super.js
Expand Up @@ -92,6 +92,8 @@ function isPossibleConstructor(node) {

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "require `super()` calls in constructors",
category: "ECMAScript 6",
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/curly.js
Expand Up @@ -16,6 +16,8 @@ const astUtils = require("../util/ast-utils");

module.exports = {
meta: {
type: "suggestion",

docs: {
description: "enforce consistent brace style for all control statements",
category: "Best Practices",
Expand Down

0 comments on commit fe78ac8

Please sign in to comment.