eslint: AssertionError: ImportDeclaration should appear when the mode is ES6 and in the module context

“version”: “1.8.0” “escope”: “^3.2.0”

This issue is coming from escope in general (feel free to redirect there instead), though not sure which config option in eslint exists to enable modules mode (other than ecmaFeatures: { modules: true } which we already use).

We use ESLintTester (which seems is deprecated now, so maybe issue can be related as well). Trying to test the:

import Foo from 'Foo';

// Or type

import type Bar from 'Bar';

Fails with the assertion in the escope:

- AssertionError: ImportDeclaration should appear when the mode is ES6 and in the module context.
        at Referencer.ImportDeclaration (scripts/lint/eslint/node_modules/eslint/node_modules/escope/lib/referencer.js:711:17)
        at Referencer.Visitor.visit (scripts/lint/eslint/node_modules/eslint/node_modules/escope/node_modules/esrecurse/esrecurse.js:109:34)
        at Referencer.Visitor.visitChildren (scripts/lint/eslint/node_modules/eslint/node_modules/escope/node_modules/esrecurse/esrecurse.js:88:38)
        at Referencer.Program (scripts/lint/eslint/node_modules/eslint/node_modules/escope/lib/referencer.js:539:22)
        at Referencer.Visitor.visit (scripts/lint/eslint/node_modules/eslint/node_modules/escope/node_modules/esrecurse/esrecurse.js:109:34)
        at Object.analyze (scripts/lint/eslint/node_modules/eslint/node_modules/escope/lib/index.js:130:16)
        at EventEmitter.module.exports.api.verify (scripts/lint/eslint/node_modules/eslint/lib/eslint.js:736:35)
        at runRuleForItem (scripts/lint/eslint/node_modules/eslint/lib/testers/rule-tester.js:265:34)
        at testInvalidTemplate (scripts/lint/eslint/node_modules/eslint/lib/testers/rule-tester.js:302:26)
        at Spec.<anonymous> (scripts/lint/eslint/node_modules/eslint/lib/testers/rule-tester.js:370:21)

Is it a known issue? I didn’t find other config options to allow import in the tests.

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (3 by maintainers)

Commits related to this issue

Most upvoted comments

I follow doc to add following options in .eslintrc.json file, and the issue is gone

  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "modules": true
    }
  }

https://eslint.org/docs/user-guide/configuring#specifying-parser-options

adding the following lines in .eslintrc fixed the issue in my side.

"parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "module",
        "ecmaFeatures": {
            "jsx": true
        }
    }

I got the same issue, I can’t find any babel-eslint@6 in stable version so I update to babel-eslint@6.0.0-beta.6, that solve the problem.

In case this bites someone else, I had forgotten to move ecmaFeatures (at the top level) to parserOptions.ecmaFeatures in my config. For some reason, the former still works but not in all scenarios.

thanks @gasolin ! your fix worked for me (babel-eslint 6)

For others seeing this error message, did you just upgrade eslint to 2.3.0? There’s an odd issue with babel-eslint depending on a module eslint no longer requires, https://github.com/eslint/eslint/issues/5476 npm install estraverse-fb will fix your issue in the short term.

Posting this here because it would have saved me some grief, since I did not come across this thread https://github.com/AtomLinter/linter-eslint/issues/469 first.