angular - ng lint 返回错误错误 Angular 8
问题描述
当我运行 ng lint 时,突然出现所有这些错误错误
Forbidden 'var' keyword, use 'let' or 'const' instead
但我知道一个事实是我没有var
在这些文件中的任何地方使用,当我运行它时,它也会用letng lint --fix
替换exp
inexport
和fun
infunction
export function
letport letction
我不知道这如何开始发生或为什么开始发生
这是我的 tslint 文件
{
"rulesDirectory": ["node_modules/codelyzer"],
"rules": {
"arrow-return-shorthand": true,
"callable-types": true,
"class-name": true,
"comment-format": [true],
"curly": true,
"eofline": true,
"forin": true,
"import-blacklist": [true],
"import-spacing": true,
"interface-over-type-literal": true,
"label-position": true,
"max-line-length": [false],
"member-access": false,
"member-ordering": [
true,
{
"order": ["static-field", "instance-field", "static-method", "instance-method"]
}
],
"no-arg": true,
"no-bitwise": true,
"no-console": [true, "debug", "info", "time", "timeEnd", "trace"],
"no-construct": true,
"no-debugger": true,
"no-duplicate-super": true,
"no-empty": false,
"no-empty-interface": true,
"no-eval": false,
"no-inferrable-types": [true, "ignore-params"],
"no-misused-new": true,
"no-non-null-assertion": true,
"no-shadowed-variable": false,
"no-string-literal": false,
"no-string-throw": true,
"no-switch-case-fall-through": true,
"no-trailing-whitespace": true,
"no-unnecessary-initializer": true,
"no-unused-expression": false,
"no-var-keyword": true,
"object-literal-sort-keys": false,
"one-line": [true, "check-open-brace", "check-catch", "check-else", "check-whitespace"],
"prefer-const": false,
"quotemark": [true, "single"],
"radix": false,
"semicolon": [true, "always"],
"triple-equals": [true, "allow-null-check"],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}
],
"unified-signatures": true,
"variable-name": false,
"whitespace": [true, "check-branch", "check-decl", "check-operator", "check-separator", "check-type"],
"directive-selector": [false, "attribute", "app", "camelCase"],
"component-selector": [false, "element", "app", "kebab-case"],
"no-input-rename": true,
"no-output-rename": true,
"use-life-cycle-interface": true,
"use-pipe-transform-interface": true,
"component-class-suffix": true,
"directive-class-suffix": true,
"no-access-missing-member": false
}
}
在我的 package.json
"typescript": "3.4.5",
"codelyzer": "^5.1.0",
"tslint": "5.17.0",
我似乎也收到了很多警告,比如
The 'no-arg' rule threw an error in '/angular/src/shared/helpers/FormattedStringValueExtracter.ts':
这就是那个文件
class ExtractionResult {
public IsMatch: boolean;
public Matches: any[];
constructor(isMatch: boolean) {
this.IsMatch = isMatch;
this.Matches = [];
}
}
enum FormatStringTokenType {
ConstantText,
DynamicValue
}
class FormatStringToken {
public Text: string;
public Type: FormatStringTokenType;
constructor(text: string, type: FormatStringTokenType) {
this.Text = text;
this.Type = type;
}
}
class FormatStringTokenizer {
Tokenize(format: string, includeBracketsForDynamicValues: boolean = false): FormatStringToken[] {
const tokens: FormatStringToken[] = [];
let currentText = '';
let inDynamicValue = false;
for (let i = 0; i < format.length; i++) {
const c = format[i];
switch (c) {
case '{':
if (inDynamicValue) {
throw new Error(
'Incorrect syntax at char ' + i + '! format string can not contain nested dynamic value expression!'
);
}
inDynamicValue = true;
if (currentText.length > 0) {
tokens.push(new FormatStringToken(currentText, FormatStringTokenType.ConstantText));
currentText = '';
}
break;
case '}':
if (!inDynamicValue) {
throw new Error(
'Incorrect syntax at char ' + i + '! These is no opening brackets for the closing bracket }.'
);
}
inDynamicValue = false;
if (currentText.length <= 0) {
throw new Error('Incorrect syntax at char ' + i + '! Brackets does not containt any chars.');
}
let dynamicValue = currentText;
if (includeBracketsForDynamicValues) {
dynamicValue = '{' + dynamicValue + '}';
}
tokens.push(new FormatStringToken(dynamicValue, FormatStringTokenType.DynamicValue));
currentText = '';
break;
default:
currentText += c;
break;
}
}
if (inDynamicValue) {
throw new Error('There is no closing } char for an opened { char.');
}
if (currentText.length > 0) {
tokens.push(new FormatStringToken(currentText, FormatStringTokenType.ConstantText));
}
return tokens;
}
}
export class FormattedStringValueExtracter {
Extract(str: string, format: string): ExtractionResult {
if (str === format) {
return new ExtractionResult(true);
}
const formatTokens = new FormatStringTokenizer().Tokenize(format);
if (!formatTokens) {
return new ExtractionResult(str === '');
}
const result = new ExtractionResult(true);
for (let i = 0; i < formatTokens.length; i++) {
const currentToken = formatTokens[i];
const previousToken = i > 0 ? formatTokens[i - 1] : null;
if (currentToken.Type === FormatStringTokenType.ConstantText) {
if (i === 0) {
if (str.indexOf(currentToken.Text) !== 0) {
result.IsMatch = false;
return result;
}
str = str.substr(currentToken.Text.length, str.length - currentToken.Text.length);
} else {
const matchIndex = str.indexOf(currentToken.Text);
if (matchIndex < 0) {
result.IsMatch = false;
return result;
}
result.Matches.push({ name: previousToken.Text, value: str.substr(0, matchIndex) });
str = str.substring(0, matchIndex + currentToken.Text.length);
}
}
}
const lastToken = formatTokens[formatTokens.length - 1];
if (lastToken.Type === FormatStringTokenType.DynamicValue) {
result.Matches.push({ name: lastToken.Text, value: str });
}
return result;
}
IsMatch(str: string, format: string): string[] {
const result = new FormattedStringValueExtracter().Extract(str, format);
if (!result.IsMatch) {
return [];
}
const values = [];
for (let i = 0; i < result.Matches.length; i++) {
values.push(result.Matches[i].value);
}
return values;
}
}
当我查看它说它有问题的任何文件时,没有警告或错误......当我实际上但var
在一个文件中它不再给我任何警告......在VSCODE的问题面板中
任何帮助,将不胜感激!!这个真的想不通!
编辑
我也看到了类似的错误
TypeError: Cannot read property 'pos' of undefined
at cb (/node_modules/tslint/lib/rules/oneLineRule.js
好像 tslint 有什么问题???
编辑
这是我的角度版本
Angular CLI: 8.1.2
Node: 10.16.0OS: linux x64
Angular: 8.1.2
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router, service-worker
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.800.6
@angular-devkit/build-angular 0.800.6
@angular-devkit/build-optimizer 0.800.6
@angular-devkit/build-webpack 0.800.6
@angular-devkit/core 8.1.2
@angular-devkit/schematics 7.3.9
@angular/cdk 7.3.7
@angular/http 7.2.15
@angular/pwa 0.12.4
@ngtools/webpack 8.0.6
@schematics/angular 7.2.4
@schematics/update 0.801.2
rxjs 6.5.2
typescript 3.4.5
解决方案
我使用最新版本的 CLI 和框架 (v8.2.0) 启动了一个新的本地项目,然后按原样添加您的文件。运行npm run lint
只给了我一个 linting 错误
ERROR: /<...>/file.ts:145:9 - Expected a 'for-of' loop instead of a 'for' loop with this simple iteration
所以也许你遇到的问题可以通过更新 cli/framework 版本来解决。如果不是,那么也许检查您的angular.json
文件以确保您的项目使用您期望的 ts 配置,这是我的默认安装
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
推荐阅读
- python - 使用 'is' 来检查 IDE 和终端之间的 int 身份执行不同
- mysql - Mysql工作台逆向工程
- javascript - 如何在 React 中更新我的帖子的评论
- bash - 删除 bash 中最后一次出现的字符串
- eclipse - HANA Eclipse - 项目资源管理器“没有连接到数据库”
- javascript - 如何在 Cypress 中测试 Slate JS 行为
- php - 如何在 PHP 数组中找到填充的索引?
- python - 使用 mypy 时在 python 中正确键入异常/错误元组
- html - 为什么在使用“padding: 10px;!imprtant;”时我的导航栏上没有颜色
- webpack - Webpack 无法解析相对于包路径的模块