简体   繁体   English

`Tslint --fix`不会自动修复,而是生成lint问题作为控制台错误

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

I'm using the Angular starter kit 我正在使用Angular入门套件

and I'm trying to get tslint to autofix all my lint problems with the --fix flag. 我试图让tslint使用--fix标志自动修复我所有的lint问题。

I'm running the script: npm run tslint --fix src/**/*.ts 我正在运行脚本: 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: 它只会生成相同的错误,我已经在tslint告知它而不是自动修复它:

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? 问题:如何通过tslint实现自动修复到我的lint错误?

Unfortunately, not all linting violations are auto-fixable. 不幸的是,并非所有linting违规都是可自动修复的。 You can see which rules are auto-fixable here by looking for the Has Fixer tag. 您可以通过查找Has Fixer标记来查看哪些规则可自动 Has Fixer

My guess is that "comment must start with a space" is governed by the comment-format rule , which is not auto-fixable. 我的猜测是“注释必须以空格开头”由comment-format规则控制 ,该规则不可自动修复。

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. 这是一个代码片段,您可以运行tslint --fix来验证某些违规是否已修复,而其他违规则不是。

//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: lint上述文件时要包含的规则:

"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. 可悲的是,他们不是。

Update library tslint and codelyzer to latest. 将库tslintcodelyzer更新到最新版本。

and then use this command: 然后使用此命令:

tslint --fix src/**/*.ts -t verbose without using npm run tslint --fix src/**/*.ts -t verbose不使用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: 您也可以将它添加到package.json scripts ,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM