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

2

5

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?

Lazare answered 15/8, 2017 at 5:40 Comment(0)
F
6

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.

Fabre answered 4/11, 2017 at 21:14 Comment(1)
I'd think the best way would be to make a second linting file with only the rules you'd like to auto fix. Make a custom npm command npm run lint-fix which runs tslint --fix only with the specialized lint file. You can use the new command to auto fix, and you can use good ol ng lint just like normal still.Mehta
V
0

Update library tslint and codelyzer to latest.

and then use this command:

tslint --fix src/**/*.ts -t verbose without using npm run

after complete, it will show to you the unfixable problems so you have to fix it manually.

You can also add it to scripts in package.json like this:

"lint-fix": "tslint --fix src/**/*.ts -t verbose"

Victual answered 29/11, 2017 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.