我正在使用一些旧的 Sencha Touch 代码,这些代码是用旧式 JavaScript 编写的,我正在尝试清理它们。使用此代码很容易简单地拼写错误变量而看不到错误,因为没有任何内容被标记为全局或未定义。
我正在尝试使用 ESLint 来解决这个问题。 ESLint 正在有效地发现其他问题,例如 var
代替 let
或 const
,==
代替 ===
等。但是应该标记为未声明或全局的变量却被粗暴地忽略了。例如:
'use strict';
Ext.define('mycompany.view.blah.blah.blah', {
extend: 'Ext.Panel',
xtype: 'filterPanel',
config: {},
initialize: function () {
this.callParent(arguments);
console.log(foobar); // <-- Should be considered a disallowed global, or otherwise undeclared.
this.on('painted', this.onAfterRender, this);
this.on('hide', this.workorderFilterHidden, this);
我使用的是 Visual Studio 提供的默认 .eslintrc
文件,到目前为止仅进行了一些小的更改。有点长,不过我把它放出来供引用。
知道我可能会错过什么吗?我在默认规则中没有看到任何会击败“no-implicit-globals”规则的内容,并且我定义了一个允许的全局(Ext
),以防万一有必要使规则生效英寸。
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true // Allows support of JSX, but use of React plugin is required to support React semantics
}
},
// @typescript-eslint/parser is specified as a command line argument
"plugins": [
"node",
"promise",
"react",
"@typescript-eslint"
],
"env": {
"amd": true,
"browser": true,
"jquery": true,
"node": true,
"es6": true, // This enables ES6 global variables AND ES6 syntax
"worker": true
},
"rules": {
// The below are some, but not all, of the rules from eslint:recommended https://github.com/eslint/eslint/blob/master/conf/eslint-recommended.js (all errors set to warning)
"constructor-super": 1,
"for-direction": 1,
"getter-return": 1,
"no-async-promise-executor": 1,
"no-case-declarations": 1,
"no-class-assign": 1,
"no-compare-neg-zero": 1,
"no-cond-assign": 1,
"no-const-assign": 1,
"no-constant-condition": 1,
"no-control-regex": 1,
"no-debugger": 1,
"no-delete-var": 1,
"no-dupe-args": 1,
"no-dupe-class-members": 1,
"no-dupe-keys": 1,
"no-duplicate-case": 1,
"no-empty": 1,
"no-empty-character-class": 1,
"no-empty-pattern": 1,
"no-ex-assign": 1,
"no-extra-boolean-cast": 1,
"no-fallthrough": 1,
"no-func-assign": 1,
"no-global-assign": "error",
"no-implicit-globals": "error",
"no-inner-declarations": 1,
"no-invalid-regexp": 1,
"no-misleading-character-class": 1,
"no-mixed-spaces-and-tabs": 1,
"no-new-symbol": 1,
"no-obj-calls": 1,
"no-octal": 1,
"no-prototype-builtins": 1,
"no-redeclare": 1,
"no-regex-spaces": 1,
"no-self-assign": 1,
"no-shadow-restricted-names": 1,
"no-sparse-arrays": 1,
"no-this-before-super": 1,
"no-unexpected-multiline": 1,
"no-unreachable": 1,
"no-unsafe-finally": 1,
"no-unsafe-negation": 1,
"no-unused-labels": 1,
"no-useless-catch": 1,
"no-useless-escape": 1,
"no-var": "warn",
"no-with": 1,
"require-atomic-updates": 1,
"require-yield": 1,
"strict": "warn",
"use-isnan": 1,
"valid-typeof": 1,
// Other rules
"default-param-last": 1,
"eqeqeq": ["error", "always", {"null": "ignore"}],
// The below are some, but not all, of the rules from eslint-plugin-react:recommended https://github.com/yannickcr/eslint-plugin-react#recommended (all errors set to warn)
"react/display-name": 1,
"react/jsx-no-duplicate-props": 1,
"react/jsx-no-undef": 1,
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1,
"react/no-children-prop": 1,
"react/no-danger-with-children": 1,
"react/no-deprecated": 1,
"react/no-direct-mutation-state": 1,
"react/no-find-dom-node": 1,
"react/no-is-mounted": 1,
"react/no-render-return-value": 1,
"react/no-string-refs": 1,
"react/no-unescaped-entities": 1,
"react/no-unknown-property": 1,
"react/require-render-return": 1,
// Some additional React rules
"react/no-danger": 1,
"react/no-did-mount-set-state": 1,
"react/no-did-update-set-state": 1
},
"overrides": [
{
"files": [ "*.ts", "*.tsx" ],
"rules": {
// The below are all rules from @typescript-eslint/eslint:recommended https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/eslint-recommended.ts (all errors set to warn)
"getter-return": 0, //Checked by Typescript - ts(2378)
"no-dupe-args": 0, // Checked by Typescript - ts(2300)
"no-dupe-keys": 0, // Checked by Typescript - ts(1117)
"no-unreachable": 0, // Checked by Typescript - ts(7027)
"valid-typeof": 0, // Checked by Typescript - ts(2367)
"no-const-assign": 0, // Checked by Typescript - ts(2588)
"no-new-symbol": 0, // Checked by Typescript - ts(2588)
"no-this-before-super": 0, // Checked by Typescript - ts(2376)
"no-undef": 0, // This is checked by Typescript using the option `strictNullChecks`.
"no-dupe-class-members": 0, // This is already checked by Typescript.
"no-redeclare": 0, // This is already checked by Typescript.
// The below is some, but not all, of the rules from @typescript-eslint/recommended https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/recommended.json (all errors set to warn)
"@typescript-eslint/adjacent-overload-signatures": 1,
"@typescript-eslint/ban-ts-ignore": 1,
"@typescript-eslint/ban-types": 1,
"camelcase": 0,
"@typescript-eslint/camelcase": 1,
"@typescript-eslint/class-name-casing": 1,
"@typescript-eslint/consistent-type-assertions": 1,
"@typescript-eslint/interface-name-prefix": 1,
"@typescript-eslint/member-delimiter-style": 1,
"no-array-constructor": 0,
"@typescript-eslint/no-array-constructor": 1,
"no-empty-function": 0,
"@typescript-eslint/no-empty-function": 1,
"@typescript-eslint/no-empty-interface": 1,
"@typescript-eslint/no-explicit-any": 1,
"@typescript-eslint/no-inferrable-types": 1,
"@typescript-eslint/no-misused-new": 1,
"@typescript-eslint/no-namespace": 1,
"@typescript-eslint/no-non-null-assertion": 1,
"@typescript-eslint/no-this-alias": 1,
"no-unused-vars": 0,
"@typescript-eslint/no-unused-vars": 1,
"no-use-before-define": 0,
"@typescript-eslint/no-use-before-define": 1,
"@typescript-eslint/no-var-requires": 1,
"@typescript-eslint/prefer-namespace-keyword": 1,
"@typescript-eslint/triple-slash-reference": 1,
"@typescript-eslint/type-annotation-spacing": 1,
"no-var": 1,
"prefer-const": 1,
"prefer-rest-params": 1,
"prefer-spread": 1
}
}
],
"globals": {
"Ext": "readonly"
}
}
最佳答案
您缺少规则 no-undef .
Disallows the use of undeclared variables unless mentioned in
/*global */
comments.
尽管这条规则被标记为推荐,但我看到它被禁用的最常见原因是它不能很好地与 TypeScript 文件配合使用,因为 TypeScript 文件根本不需要它(您可以在 this FAQ 中阅读更多内容)。
因此,对于您的用例,可能的解决方案是仅针对 .js 和 .jsx 文件启用规则。您已经在 "overrides"
> "rules"
中禁用了该规则,因此您需要做的就是在常规 "rules"
中启用它> 上面的部分:
"no-undef": 1,
请注意no-implicit-globals
仅警告在全局作用域中声明的变量,而不警告出现在内部作用域中的隐式全局变量(例如,在函数体内)。
关于javascript - ESLint "no-implicit-globals"规则被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72104607/