{"record":{"id":"3a7609776794e24f","repo":"facebook/flow","slug":"createtranslationcontext-module-scope-not-found","errorCode":null,"errorMessage":"createTranslationContext: Module scope not found","messagePattern":"createTranslationContext: Module scope not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/flow-api-translator/src/utils/TranslationUtils.js","lineNumber":39,"sourceCode":"export type TranslationContext = {\n  scopeManager: ScopeManager,\n  referenceMap: Map<Identifier | JSXIdentifier, Variable>,\n  variableMap: Map<Dep, Variable>,\n  recoverFromErrors: boolean,\n  mungeUnderscores: boolean,\n  code: string,\n};\n\nexport function createTranslationContext(\n  code: string,\n  scopeManager: ScopeManager,\n  {recoverFromErrors, mungeUnderscores = true}: TranslationOptions,\n): TranslationContext {\n  const referenceMap = new Map<Identifier | JSXIdentifier, Variable>();\n  const variableMap = new Map<Dep, Variable>();\n  const moduleScope = scopeManager.globalScope.childScopes[0];\n  if (moduleScope == null || moduleScope.type !== 'module') {\n    throw new Error('createTranslationContext: Module scope not found');\n  }\n  for (const variable of moduleScope.variables) {\n    for (const reference of variable.references) {\n      referenceMap.set(reference.identifier, variable);\n      variableMap.set(variable.name, variable);\n    }\n  }\n  return {\n    scopeManager,\n    referenceMap,\n    variableMap,\n    recoverFromErrors,\n    mungeUnderscores,\n    code,\n  };\n}\n","sourceCodeStart":21,"sourceCodeEnd":56,"githubUrl":"https://github.com/facebook/flow/blob/f88ac94bcf6992f5d5a158854d94613ebb92c6e6/packages/flow-api-translator/src/utils/TranslationUtils.js#L21-L56","documentation":"createTranslationContext (TranslationUtils.js) builds the translator's reference/variable maps from scopeManager.globalScope.childScopes[0] and requires that scope to be of type 'module'. If the first child scope is missing or is a function scope, the code was analyzed as a script rather than a module, and the context cannot be built.","triggerScenarios":"Invoking translator internals (flowToFlowDef/flowToFlowDef-family) with a ScopeManager produced from script-mode source: parsing with sourceType 'script', or 'unambiguous' resolution landing on script for a file without import/export and without module indicators. The public APIs parse with sourceType module, so this mainly hits direct internal callers.","commonSituations":"Feeding CommonJS-style files (no imports/exports) through a pipeline that parses with unambiguous/script; reusing a scopeManager from a different parser configuration; writing custom tooling on top of flow-api-translator internals.","solutions":["Parse and analyze the code with sourceType: 'module' so eslint-scope creates a module scope under the global scope.","Use the public translateFlowToFlowDef / translateFlowToJS APIs instead of internals; they enforce module parsing.","If a file has no module syntax, add an `export {}` or an @flow pragma plus module parse to force module mode.","Validate childScopes[0].type === 'module' before building the context and emit a clear 'parse as module' error."],"exampleFix":"// before\nconst scopeManager = analyze(parse(code, {sourceType: 'script'}));\nconst ctx = createTranslationContext(code, scopeManager, {recoverFromErrors: false});\n\n// after\nconst scopeManager = analyze(parse(code, {sourceType: 'module', flow: 'all'}));\nconst ctx = createTranslationContext(code, scopeManager, {recoverFromErrors: false});","handlingStrategy":"validation","validationCode":"// Confirm module scope exists before building a translation context\nfunction hasModuleScope(scopeManager) {\n  const first = scopeManager.globalScope.childScopes[0];\n  return first != null && first.type === 'module';\n}\nif (!hasModuleScope(scopeManager)) {\n  throw new Error('parse/analyze the code with sourceType: \"module\" before translation');\n}","typeGuard":"function isModuleScope(scope) {\n  return scope != null && scope.type === 'module';\n}","tryCatchPattern":null,"preventionTips":["Always parse with sourceType 'module' when feeding the translator.","Prefer the public translate* APIs which handle parsing configuration.","For module-less files, add `export {}` to force module mode."],"tags":["flow","api-translator","scope","sourcetype","module"],"backgroundTag":"missing-module-scope","analyzedSha":"f88ac94bcf6992f5d5a158854d94613ebb92c6e6","analyzedAt":"2026-08-20T10:41:37.992Z","contentChangedAt":"2026-08-20T10:41:37.992Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}