From e90d38d551b6174944c27f8e8f5933a70189c553 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Tue, 11 Jun 2024 13:28:44 -0400 Subject: [PATCH] fix scope visitors --- packages/macros/src/glimmer/ast-transform.ts | 58 +++++++++----------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/packages/macros/src/glimmer/ast-transform.ts b/packages/macros/src/glimmer/ast-transform.ts index 67086fa30..c13341834 100644 --- a/packages/macros/src/glimmer/ast-transform.ts +++ b/packages/macros/src/glimmer/ast-transform.ts @@ -72,18 +72,7 @@ export function makeFirstTransform(opts: FirstTransformParams) { name: '@embroider/macros/first', visitor: { - [rootVisitorKey(env)]: { - enter(node: any) { - if (node.blockParams.length > 0) { - scopeStack.push(node.blockParams); - } - }, - exit(node: any) { - if (node.blockParams.length > 0) { - scopeStack.pop(); - } - }, - }, + ...scopeVisitors(env, scopeStack), SubExpression(node: any, walker: { parent: { node: any } }) { if (node.path.type !== 'PathExpression') { return; @@ -169,18 +158,7 @@ export function makeSecondTransform() { name: '@embroider/macros/second', visitor: { - [rootVisitorKey(env)]: { - enter(node: any) { - if (node.blockParams.length > 0) { - scopeStack.push(node.blockParams); - } - }, - exit(node: any) { - if (node.blockParams.length > 0) { - scopeStack.pop(); - } - }, - }, + ...scopeVisitors(env, scopeStack), BlockStatement(node: any) { if (node.path.type !== 'PathExpression') { return; @@ -292,16 +270,30 @@ function inScope(scopeStack: string[][], name: string) { function headOf(path: any) { if (!path) return; - return 'head' in path ? path.head : path.parts[0]; + return 'head' in path ? path.head.name : path.parts[0]; } -/** - * Template is available in ember-source 3.17+ - * Program is deprecated in ember-source 5.9+ - */ -function rootVisitorKey(env: any) { - let hasTemplate = 'template' in env.syntax.builders; - let rootKey = hasTemplate ? 'Template' : 'Program'; +function scopeVisitors(env: any, scopeStack: string[][]) { + function enter(node: any) { + if (node.blockParams.length > 0) { + scopeStack.push(node.blockParams); + } + } + function exit(node: any) { + if (node.blockParams.length > 0) { + scopeStack.pop(); + } + } - return rootKey; + let hasTemplate = 'template' in env.syntax.builders; + if (hasTemplate) { + return { + Template: { enter, exit }, + Block: { enter, exit }, + }; + } else { + return { + Program: { enter, exit }, + }; + } }