`Tslint --fix` does not autofix but instead generates lint problems as console errors

Jonathan002 picture Jonathan002 · Aug 15, 2017 · Viewed 8.6k times · Source

I'm using the Angular starter kit

and I'm trying to get tslint to autofix all my lint problems with the --fix flag.

I'm running the script: npm run tslint --fix src/**/*.ts

It just generates the same error that I'm already being told about in tslint and not autofixing it:

console output:

ERROR: src/app/app-routing.module.ts[10, 5]: comment must start with a space
ERROR: src/app/app-routing.module.ts[2, 20]: Too many spaces before 'from'

Am I missing something that allows it to implement the changes?

My versions are:

"tslint": "^5.6.0"  
"codelyzer": "^3.1.2"

Question: How can I get tslint to implement autofix to my lint errors?

Answer

Mike Patrick picture Mike Patrick · Nov 4, 2017

Unfortunately, not all linting violations are auto-fixable. You can see which rules are auto-fixable here by looking for the Has Fixer tag.

My guess is that "comment must start with a space" is governed by the comment-format rule, which is not auto-fixable.

I'm not sure which rule is causing your second error, but it is most likely also not auto-fixable.

Here's a snippet you can run tslint --fix against to verify that some violations are fixed, and others are not.

//no var keyword (comment does not start with space)
var x: string = 'x';
console.log(x);

// array-type
let y: String[] = [];
console.log(y);

// ban-single-arg-parens
['1', '2'].filter((arg) => {
    console.log(arg);
});

// semicolon
let z: string = ''
console.log(z);

// no unused variable
let a: string = '';

// trailing comma
let list = ['1', '2', ];

// missing trailing comma
let obj = [
    1,
    2
];

Rules to include when linting the above file:

"semicolon": [true, "always"],
"trailing-comma": [true, {"multiline": "always", "singleline": "never"}],
"array-type": [true, "array-generic"],
"arrow-parens": [true, "ban-single-arg-parens"],

It's tempting to think that all whitespace errors would be auto-fixable, and perhaps they should be. Sadly, they're not.