1

I have defined a writable global variable, AS, in .eslintrc.

AS is only declared once in the codebase.

ESLint is throwing the following no-redeclare error for the single declaration of the global variable:

Error - 'AS' is already defined as a built-in global variable. (no-redeclare)

Why is ESLint throwing a no-redeclare error when the variable is only declared once?

Is there a way to disable this automatically instead of manually disabling the line in the source file?

Brian Zelip
  • 2,909
  • 4
  • 33
  • 45
  • 2
    Eslint doesn't know the declaration you're linting *is* the one global declaration. It only knows there already is a global of that name (because you told it so), and assumes that your declaration collides with that. – Bergi Sep 04 '21 at 13:08
  • 1
    Look here: [Disallow Redeclaring Variables (no-redeclare)](https://eslint.org/docs/2.0.0/rules/no-redeclare) – Louys Patrice Bessette Sep 04 '21 at 13:10
  • 1
    Maybe the `AS` comes from some TypeScript? [Look here](https://stackoverflow.com/questions/55781559/what-does-the-as-keyword-do) – Louys Patrice Bessette Sep 04 '21 at 13:13
  • @LouysPatriceBessette - the ESLint docs you linked to don't seem to answer my question. – Brian Zelip Sep 04 '21 at 13:13
  • @LouysPatriceBessette, nope, no TypeScript. – Brian Zelip Sep 04 '21 at 13:14
  • The error means the `AS` is already declared somewhere for sure. – Louys Patrice Bessette Sep 04 '21 at 13:15
  • @LouysPatriceBessette it _is_ declared somewhere indeed. The error message points to the line in the file where the var is declared. That line is also the only line where the var is declared. – Brian Zelip Sep 04 '21 at 13:16
  • *`as` is contextual keyword in JavaScript - when the code runs into the syntax rule ImportClause* [Ref](https://stackoverflow.com/a/68674619/2159528) – Louys Patrice Bessette Sep 04 '21 at 13:30
  • @LouysPatriceBessette The OP's error message is about `AS`, not about `as` - and even a variable named `as` is totally valid. – Bergi Sep 04 '21 at 14:04
  • Eslint `no-declare` provide the `builtinGlobals` option to ignore the global variable. https://eslint.org/docs/rules/no-redeclare#builtinglobals – Sathik Basha May 24 '22 at 12:24

1 Answers1

0

As I understand, after you defined AS in ESLint as a global variable it considers it already defined so when it encounters var AS = ... or function AS() {} in your code it complains about this with no-redeclare

The solution (workaround) I found to this problem - in a browser environment - was to declare the variable (in my case it was a function) attached to the window object.

window.AS = ...

That will not be considered a re-declaration by ESLint v8.21.0.

szabgab
  • 6,202
  • 11
  • 50
  • 64