未定义ESLint dollar($)。(没有undef)


107
$("#ID").hide();

我将ESLint添加到我的项目中。

一切都很好,除了symbol $

我得到错误: [eslint] '$' is not defined. (no-undef)

我的.eslintrc.json(注意:当有等效的javascript代码时,它还设置了其他规则以禁止jquery函数):

{
"env": {
    "browser": true,
    "commonjs": true,
    "es6": true
},
"extends": [
    "eslint:recommended"
],
"parserOptions": {
    "sourceType": "module"
},
"plugins": [
    "dollar-sign",
    "jquery"
],
"rules": {       
    "indent": [
        "error" ,
        "tab"
    ],
    "linebreak-style": [
        "error",
        "windows"
    ],
    "quotes": [
        "error",
        "double"
    ],
    "semi": [
        "error",
        "always"
    ],
    "jquery/no-ajax": 2,
    "jquery/no-animate": 2,
    "jquery/no-attr": 2,
    "jquery/no-bind": 2,
    "jquery/no-class": 2,
    "jquery/no-clone": 2,
    "jquery/no-closest": 2,
    "jquery/no-css": 2,
    "jquery/no-data": 2,
    "jquery/no-deferred": 2,
    "jquery/no-delegate": 2,
    "jquery/no-each": 2,
    "jquery/no-fade": 2,
    "jquery/no-filter": 2,
    "jquery/no-find": 2,
    "jquery/no-global-eval": 2,
    "jquery/no-has": 2,
    "jquery/no-hide": 2,
    "jquery/no-html": 2,
    "jquery/no-in-array": 2,
    "jquery/no-is": 2,
    "jquery/no-map": 2,
    "jquery/no-merge": 2,
    "jquery/no-param": 2,
    "jquery/no-parent": 2,
    "jquery/no-parents": 2,
    "jquery/no-parse-html": 2,
    "jquery/no-prop": 2,
    "jquery/no-proxy": 2,
    "jquery/no-serialize": 2,
    "jquery/no-show": 2,
    "jquery/no-sizzle": 2,
    "jquery/no-slide": 2,
    "jquery/no-text": 2,
    "jquery/no-toggle": 2,
    "jquery/no-trigger": 2,
    "jquery/no-trim": 2,
    "jquery/no-val": 2,
    "jquery/no-wrap": 2,
    "dollar-sign/dollar-sign": [
        2,
        "ignoreProperties"
    ]
}

您可以看到我添加了两个插件:eslint-plugin-dollar-sign和eslint-plugin-jquery。

为什么这个规则不起作用?

"dollar-sign/dollar-sign": [ 2, "ignoreProperties" ]

Answers:


211

你不见了

"env": {
  "browser": true,
  "commonjs": true,
  "es6": true,
  "jquery": true
},

$如果未jquery启用环境,则不会声明为全局。因此,您得到一个no-undef错误,表示您使用的是尚未声明的变量。



19

您还可以将此行添加到js文件的顶部:

/* global $ */

为防止对“ $”或任何其他全局变量(如“ varName”)发出警告:

/* global varName */

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.