![](/img/trans.png)
[英]ESLint's "no-undef" rule is calling my use of Underscore an undefined variable
[英]Eslint angular and jasmine: is not defined no-undef
我正在嘗試使用 angular、eslint:recommended 和 jasmine 插件推薦的設置來整理我的 angular 代碼。 但是我得到的不是關於茉莉花的定義錯誤。 我的 eslint 文件如下所示:
{
"plugins": ["jasmine"],
"extends": ["angular", "eslint:recommended",
"plugin:jasmine/recommended"],
"rules": {
"quotes": [ 2, "single"],
"strict": [2, "global"],
"semi": ["error", "always"],
"angular/on-watch": "warn"
}
}
我收到以下錯誤:
3:1 錯誤 'describe' 未定義 no-undef
4:5 錯誤 'it' 未定義 no-undef
5:9 錯誤 'expect' 未定義 no-undef
7:5 錯誤 'it' 未定義 no-undef
8:9 錯誤 'expect' 未定義 no-undef
在我的package.json
我有2.3.0
版的eslint
和1.8.1
的eslint-plugin-jasmine
。 我還有eslint-config-angular
0.5.0
版本和eslint-plugin-angular
1.0.0
版本。
我也試過在我的eslint
文件中不指定"plugins": ["jasmine"]
但后來我收到一個錯誤,告訴我沒有定義 jasmine 規則(例如Definition for rule 'jasmine/no-focused-tests' was not found
)。
添加
"env": {
"jasmine": true
}
解決了這個問題。 這是我通過 eslint jasmine 插件的 github 頁面獲得的建議。
使用此規則集,任何未明確聲明的變量都會導致警告。 您可以在規范文件的頂部設置:
/* global describe it expect */
您可以將/* eslint-env jasmine */
到.spec.ts
文件的頂部。 這與 Geert 提出的解決方案基本相同,但采用每個文件的解決方案。 細微的區別在於,在非測試文件中使用某些全局定義的測試方法(如describe()
和it()
仍然會出錯。
(可能有一種聰明的方法來設置 ESLint,這樣您就不必將此行添加到所有.spec.ts
文件中。)
我發現為所有 Jasmine 函數配置驗證並在一個地方進行驗證,但在“非規范”文件中使用它們時仍然標記它們的方法如下。
在配置級別定義通用配置。 然后,對於那些特定的配置(例如 Typescript 或 Jasmin)進行覆蓋。 這樣,“全局配置”僅適用於特定文件。
{
"env": {
"jasmine": true
},
"files": ["**/*.spec.js"]
}
.eslintrc.js
片段:
/**
* @type {import("eslint").Linter.Config}
*/
module.exports = {
"env": {...},
"extends": [
"eslint:recommended"
],
"overrides": [
{
"extends": [
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"files": ["**/*.ts"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json",
"tsconfigRootDir": __dirname
},
"plugins": [
"@typescript-eslint"
]
},
// This is the important part
{
"env": {
"jasmine": true
},
"files": ["**/*.spec.js"]
}
],
"root": true,
"rules": {...}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.