{"record":{"id":"bebbf3ff8d443e86","repo":"facebook/flow","slug":"importdeclaration-should-appear-when-the-mode-is-e","errorCode":null,"errorMessage":"ImportDeclaration should appear when the mode is ES6 and in the module context.","messagePattern":"ImportDeclaration should appear when the mode is ES6 and in the module context\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/flow-eslint/src/scope-manager/referencer/Referencer.js","lineNumber":668,"sourceCode":"    this.visitFunction(node);\n  }\n\n  FunctionExpression(node: FunctionExpression): void {\n    this.visitFunction(node);\n  }\n\n  Identifier(node: Identifier): void {\n    this.currentScope().referenceValue(node);\n    this.visitType(node.typeAnnotation);\n  }\n\n  ImportAttribute(_: ImportAttribute): void {\n    // import assertions are module metadata and thus have no variables to reference\n  }\n\n  ImportDeclaration(node: ImportDeclaration): void {\n    if (!this.scopeManager.isES6() || !this.scopeManager.isModule()) {\n      throw new Error(\n        'ImportDeclaration should appear when the mode is ES6 and in the module context.',\n      );\n    }\n\n    ImportVisitor.visit(this, node);\n  }\n\n  JSXAttribute(node: JSXAttribute): void {\n    this.visit(node.value);\n  }\n\n  JSXClosingElement(node: JSXClosingElement): void {\n    /**\n     * Note that this was not previously considered to be a reference and that\n     * other scope analyzers do not count them either: e.g. TypeScript-eslint\n     * https://fburl.com/4q93a3x3\n     *\n     * We are considering this a reference because it technically includes an","sourceCodeStart":650,"sourceCodeEnd":686,"githubUrl":"https://github.com/facebook/flow/blob/d1341dac899a79c027762f6b423d896045287620/packages/flow-eslint/src/scope-manager/referencer/Referencer.js#L650-L686","documentation":"In flow-eslint's scope Referencer (an eslint-scope port), visiting an ImportDeclaration requires the scope manager to have been created with ecmaVersion >= 6 (isES6) and sourceType 'module' (isModule). If the analyzed source contains an import statement but analysis was not configured for ES modules, this invariant throws.","triggerScenarios":"Running flow-eslint or equivalent scope analysis with parserOptions missing `sourceType: 'module'` (default is 'script') or with ecmaVersion below 2015, while the analyzed code contains import/export statements.","commonSituations":"Missing or partial parserOptions in .eslintrc; lint setups switched to ESM files without config updates; embedding the Referencer in custom tooling and forgetting to pass the parser's options to the scope manager.","solutions":["Set `parserOptions: { ecmaVersion: 'latest', sourceType: 'module' }` in your eslint/analysis config","If the file really is a CommonJS script, remove the import statements rather than changing the config","When constructing the scope analysis programmatically, pass the same ecmaVersion/sourceType you passed the parser"],"exampleFix":"// before (.eslintrc.js)\nmodule.exports = { rules: { /* ... */ } };\n\n// after (.eslintrc.js)\nmodule.exports = {\n  parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },\n  rules: { /* ... */ },\n};","handlingStrategy":"validation","validationCode":"function isEsmAnalysisConfig(parserOptions: {\n  ecmaVersion?: number | 'latest';\n  sourceType?: string;\n}): boolean {\n  const ecma = parserOptions?.ecmaVersion ?? 5;\n  const ecmaNum = ecma === 'latest' ? 2022 : Number(ecma);\n  return parserOptions?.sourceType === 'module' && ecmaNum >= 2015;\n}","typeGuard":"function canAnalyzeImports(opts: {ecmaVersion?: unknown; sourceType?: unknown}): boolean {\n  const ecma = opts.ecmaVersion == null ? 5 : opts.ecmaVersion === 'latest' ? 2022 : Number(opts.ecmaVersion);\n  return opts.sourceType === 'module' && ecma >= 2015;\n}","tryCatchPattern":null,"preventionTips":["Always set sourceType and ecmaVersion when linting ESM","Verify effective config with `eslint --print-config file.js`","Pass the exact parserOptions object to the scope analysis when embedding the Referencer"],"tags":["eslint","scope-analysis","parser-options","esm","import"],"backgroundTag":"eslint-parser-options-mismatch","analyzedSha":"d1341dac899a79c027762f6b423d896045287620","analyzedAt":"2026-08-17T00:07:02.212Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}