Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support module expression scopes #16091

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions eslint/babel-eslint-parser/src/analyze-scope.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,29 @@ class PatternVisitor extends OriginalPatternVisitor {

class Referencer extends OriginalReferencer {
#client;
visitedTopLevel = false;

constructor(options, scopeManager, client) {
super(options, scopeManager);
this.#client = client;
}

Program(node) {
if (this.visitedTopLevel) {
// Program nodes are also currently used nested in module expressions, leading to an error as only one global scope can exist
return;
}

this.visitedTopLevel = true;
super.Program(node);
}

ModuleExpression(node) {
this.scopeManager.__nestModuleScope(node);
this.visitChildren(node);
this.close(node);
}

// inherits.
visitPattern(node, options, callback) {
if (!node) {
Expand Down
16 changes: 16 additions & 0 deletions eslint/babel-eslint-parser/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,3 +987,19 @@ describe("Babel and Espree", () => {
});
});
});

describe("scope", () => {
it("should nest a module scope for module expressions", () => {
const code = "(module {})";
const options = {
babelOptions: { parserOpts: { plugins: ["moduleBlocks"] } },
};
const { ast, scopeManager } = parseForESLint(code, options);
const moduleExpression = ast.body[0].expression;
scopeManager.acquire(moduleExpression);

expect(scopeManager.acquire(moduleExpression)).toMatchObject({
type: "module",
});
});
});