admin管理员组

文章数量:1279015

I've got some js code that, during tests gets loaded in multiple parts, and gets concatenated and uglified for prod.

I have a config file that defines a variable myConfig and have the business-logic script, that expects myConfig to be set and globally available.

This is working fine, and there are no errors during dev or prod.

The problem is that I'd like to use a no-undef eslint rule to catch other variables and function from missing declarations. So I'm looking for ways to define a set of expected variables.

Is there a way to define such variables?

I've got some js code that, during tests gets loaded in multiple parts, and gets concatenated and uglified for prod.

I have a config file that defines a variable myConfig and have the business-logic script, that expects myConfig to be set and globally available.

This is working fine, and there are no errors during dev or prod.

The problem is that I'd like to use a no-undef eslint rule to catch other variables and function from missing declarations. So I'm looking for ways to define a set of expected variables.

Is there a way to define such variables?

Share Improve this question asked Aug 29, 2018 at 17:54 DanielDaniel 35.7k17 gold badges114 silver badges161 bronze badges 2
  • 1 If you are already packing and uglifying scripts, can't you just import your configuration script in your other scripts? Don't rely on global variables because that will only pollute the global namespace and make testing your code more difficult. – Derek 朕會功夫 Commented Aug 29, 2018 at 17:56
  • @Derek. I'm glad you can adhere to such nice absolutes in the projects you are creating. Some of us however don't have the luxury of creating projects from scratch and instead maintain legacy code. It's always refreshing to hear that the premise of the question is wrong rather than offering anything useful. – Daniel Commented Oct 8, 2019 at 20:33
Add a ment  | 

1 Answer 1

Reset to default 13

From the rule documentation:

Any reference to an undeclared variable causes a warning, unless the variable is explicitly mentioned in a /*global ...*/ ment, or specified in the globals key in the configuration file. A mon use case for these is if you intentionally use globals that are defined elsewhere (e.g. in a script sourced from HTML).

Further,

Specifying Globals

The no-undef rule will warn on variables that are accessed but not defined within the same file. If you are using global variables inside of a file then it’s worthwhile to define those globals so that ESLint will not warn about their usage. You can define global variables either using ments inside of a file or in the configuration file.

To specify globals using a ment inside of your JavaScript file, use the following format:

/* global var1, var2 */

This defines two global variables, var1 and var2. If you want to optionally specify that these global variables should never be written to (only read), then you can set each with a false flag:

/* global var1:false, var2:false */

To configure global variables inside of a configuration file, use the globals key and indicate the global variables you want to use. Set each global variable name equal to true to allow the variable to be overwritten or false to disallow overwriting. For example:

{
    "globals": {
        "var1": true,
        "var2": false
   }
}

And in YAML:

---
  globals:
    var1: true
    var2: false

These examples allow var1 to be overwritten in your code, but disallow it for var2.

本文标签: javascriptSet ignore list for noundef (Defining expected variables from other sources)Stack Overflow