Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/pluginutils/src/attachScopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'sc
}
}

// static clause has its own function scope
if (node.type === 'StaticBlock') {
newScope = new Scope({
parent: scope,
block: false
});
}

// create new for scope
if (/For(?:In|Of)?Statement/.test(node.type)) {
newScope = new Scope({
Expand Down
22 changes: 22 additions & 0 deletions packages/pluginutils/test/attachScopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ test('supports catch without a parameter', () => {
}).not.toThrow();
});

test('supports static block', () => {
const ast = parse(
`
class Foo {
static {
let a = 10;
}
}
`,
{ ecmaVersion: 2022, sourceType: 'module' }
) as unknown as estree.Program;

const scope = attachScopes(ast, 'scope');
expect(scope.contains('a')).toBeFalsy();

const classDeclaration = ast.body[0] as estree.ClassDeclaration;
const classBody = classDeclaration.body;
const staticBlock = classBody.body[0] as estree.StaticBlock & { scope: AttachedScope };

expect(staticBlock.scope.contains('a')).toBeTruthy();
});

test('supports ForStatement', () => {
const ast = parse(
`
Expand Down
Loading