简体   繁体   English

Eslint angular 和 jasmine:未定义 no-undef

[英]Eslint angular and jasmine: is not defined no-undef

I am trying to lint my angular code using angular, eslint:recommended and the jasmine plugin recommended settings.我正在尝试使用 angular、eslint:recommended 和 jasmine 插件推荐的设置来整理我的 angular 代码。 However I get is not defined errors on the jasmine stuff.但是我得到的不是关于茉莉花的定义错误。 My eslint file looks like this:我的 eslint 文件如下所示:

{
  "plugins": ["jasmine"],
  "extends": ["angular", "eslint:recommended", 
  "plugin:jasmine/recommended"],
  "rules": {
   "quotes": [ 2, "single"],
   "strict": [2, "global"],
   "semi": ["error", "always"],
   "angular/on-watch": "warn"
  }
}

I get the following errors:我收到以下错误:

3:1 error 'describe' is not defined no-undef 3:1 错误 'describe' 未定义 no-undef

4:5 error 'it' is not defined no-undef 4:5 错误 'it' 未定义 no-undef

5:9 error 'expect' is not defined no-undef 5:9 错误 'expect' 未定义 no-undef

7:5 error 'it' is not defined no-undef 7:5 错误 'it' 未定义 no-undef

8:9 error 'expect' is not defined no-undef 8:9 错误 'expect' 未定义 no-undef

And in my package.json I have version 2.3.0 for eslint and 1.8.1 for eslint-plugin-jasmine .在我的package.json我有2.3.0版的eslint1.8.1eslint-plugin-jasmine I also have version 0.5.0 for eslint-config-angular and 1.0.0 for eslint-plugin-angular .我还有eslint-config-angular 0.5.0版本和eslint-plugin-angular 1.0.0版本。

I have also tried without specifying the "plugins": ["jasmine"] in my eslint file but then I get an error telling me the jasmine rules are not defined (eg Definition for rule 'jasmine/no-focused-tests' was not found ).我也试过在我的eslint文件中不指定"plugins": ["jasmine"]但后来我收到一个错误,告诉我没有定义 jasmine 规则(例如Definition for rule 'jasmine/no-focused-tests' was not found )。

Adding添加

"env": {
 "jasmine": true
}

solved the problem.解决了这个问题。 This was the suggestion that i got through the github page of the eslint jasmine plugin.这是我通过 eslint jasmine 插件的 github 页面获得的建议。

With this rule set, any variable non explicitly declared causes a warning.使用此规则集,任何未明确声明的变量都会导致警告。 You can set at the top of your spec file:您可以在规范文件的顶部设置:

/* global describe it expect */

You can add /* eslint-env jasmine */ to the top of your .spec.ts files.您可以将/* eslint-env jasmine */.spec.ts文件的顶部。 This is basically the same solution as the one proposed by Geert, but with a per-file solution.这与 Geert 提出的解决方案基本相同,但采用每个文件的解决方案。 The subtle difference is that it will still be an error to use some of the globally defined test methods like describe() and it() in your non-test files.细微的区别在于,在非测试文件中使用某些全局定义的测试方法(如describe()it()仍然会出错。

(There is probably a clever way to set up ESLint, so that you don't have add this line to all the .spec.ts files.) (可能有一种聪明的方法来设置 ESLint,这样您就不必将此行添加到所有.spec.ts文件中。)

The way I found to configure the validations for all Jasmine functions and do it in a single place, but still flag them when they are used in a "non-spec" file is as follows.我发现为所有 Jasmine 函数配置验证并在一个地方进行验证,但在“非规范”文件中使用它们时仍然标记它们的方法如下。

Have the common configuration defined at config level.在配置级别定义通用配置。 Then, for those specific configurations (such as Typescript or Jasmin) do an override.然后,对于那些特定的配置(例如 Typescript 或 Jasmin)进行覆盖。 That way, the "global configuration" is applied only to specific files.这样,“全局配置”仅适用于特定文件。

{
    "env": {
        "jasmine": true
    },
    "files": ["**/*.spec.js"]
}

Fragment of .eslintrc.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.

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