From e0f14901228a49162d0ead7865e85d5d8304ec30 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 7 Jun 2024 21:38:56 -0400 Subject: [PATCH 1/5] Address these issues in new apps (5.9): DEPRECATION: The parts property on path nodes is deprecated, use head and tail instead DEPRECATION: The 'Program' visitor node is deprecated. Use 'Template' or 'Block' instead (node was 'Template') DEPRECATION: The parts property on path nodes is deprecated, use head and tail instead DEPRECATION: The 'Program' visitor node is deprecated. Use 'Template' or 'Block' instead (node was 'Template') DEPRECATION: The 'Program' visitor node is deprecated. Use 'Template' or 'Block' instead (node was 'Template') DEPRECATION: The 'Program' visitor node is deprecated. Use 'Template' or 'Block' instead (node was 'Template') DEPRECATION: The 'Program' visitor node is deprecated. Use 'Template' or 'Block' instead (node was 'Template') --- packages/macros/src/glimmer/ast-transform.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/macros/src/glimmer/ast-transform.ts b/packages/macros/src/glimmer/ast-transform.ts index 4e2e7e684..7727bde57 100644 --- a/packages/macros/src/glimmer/ast-transform.ts +++ b/packages/macros/src/glimmer/ast-transform.ts @@ -72,7 +72,7 @@ export function makeFirstTransform(opts: FirstTransformParams) { name: '@embroider/macros/first', visitor: { - Program: { + Block: { enter(node: any) { if (node.blockParams.length > 0) { scopeStack.push(node.blockParams); From 653ab871e2948cf4da962e8e09466d20e3bd2615 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:08:34 -0400 Subject: [PATCH 2/5] Handle the parts vs head deprecation --- packages/macros/src/glimmer/ast-transform.ts | 33 +++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/packages/macros/src/glimmer/ast-transform.ts b/packages/macros/src/glimmer/ast-transform.ts index 7727bde57..6c532a567 100644 --- a/packages/macros/src/glimmer/ast-transform.ts +++ b/packages/macros/src/glimmer/ast-transform.ts @@ -88,9 +88,13 @@ export function makeFirstTransform(opts: FirstTransformParams) { if (node.path.type !== 'PathExpression') { return; } - if (inScope(scopeStack, node.path.parts[0])) { + + let head = 'head' in node.path ? node.path.head : node.path.parts[0]; + + if (inScope(scopeStack, head)) { return; } + if (node.path.original === 'macroGetOwnConfig') { return literal( getConfig(node, opts.configs, opts.packageRoot, moduleName, true, packageCache), @@ -120,7 +124,10 @@ export function makeFirstTransform(opts: FirstTransformParams) { if (node.path.type !== 'PathExpression') { return; } - if (inScope(scopeStack, node.path.parts[0])) { + + let head = 'head' in node.path ? node.path.head : node.path.parts[0]; + + if (inScope(scopeStack, head)) { return; } if (node.path.original === 'macroGetOwnConfig') { @@ -166,7 +173,7 @@ export function makeSecondTransform() { name: '@embroider/macros/second', visitor: { - Program: { + Block: { enter(node: any) { if (node.blockParams.length > 0) { scopeStack.push(node.blockParams); @@ -182,7 +189,10 @@ export function makeSecondTransform() { if (node.path.type !== 'PathExpression') { return; } - if (inScope(scopeStack, node.path.parts[0])) { + + let head = 'head' in node.path ? node.path.head : node.path.parts[0]; + + if (inScope(scopeStack, head)) { return; } if (node.path.original === 'if') { @@ -196,7 +206,10 @@ export function makeSecondTransform() { if (node.path.type !== 'PathExpression') { return; } - if (inScope(scopeStack, node.path.parts[0])) { + + let head = 'head' in node.path ? node.path.head : node.path.parts[0]; + + if (inScope(scopeStack, head)) { return; } if (node.path.original === 'if') { @@ -234,7 +247,10 @@ export function makeSecondTransform() { if (modifier.path.type !== 'PathExpression') { return true; } - if (inScope(scopeStack, modifier.path.parts[0])) { + + let head = 'head' in node.path ? node.path.head : node.path.parts[0]; + + if (inScope(scopeStack, head)) { return true; } if (modifier.path.original === 'macroMaybeAttrs') { @@ -248,7 +264,10 @@ export function makeSecondTransform() { if (node.path.type !== 'PathExpression') { return; } - if (inScope(scopeStack, node.path.parts[0])) { + + let head = 'head' in node.path ? node.path.head : node.path.parts[0]; + + if (inScope(scopeStack, head)) { return; } if (node.path.original === 'if') { From bc7d79b044c56df97090be88f9fdf330c9f5f1a3 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:19:42 -0400 Subject: [PATCH 3/5] Handle undefined paths, and factor better --- packages/macros/src/glimmer/ast-transform.ts | 34 ++++++++------------ 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/packages/macros/src/glimmer/ast-transform.ts b/packages/macros/src/glimmer/ast-transform.ts index 6c532a567..27b6652b4 100644 --- a/packages/macros/src/glimmer/ast-transform.ts +++ b/packages/macros/src/glimmer/ast-transform.ts @@ -72,7 +72,7 @@ export function makeFirstTransform(opts: FirstTransformParams) { name: '@embroider/macros/first', visitor: { - Block: { + Template: { enter(node: any) { if (node.blockParams.length > 0) { scopeStack.push(node.blockParams); @@ -89,9 +89,7 @@ export function makeFirstTransform(opts: FirstTransformParams) { return; } - let head = 'head' in node.path ? node.path.head : node.path.parts[0]; - - if (inScope(scopeStack, head)) { + if (inScope(scopeStack, headOf(node.path))) { return; } @@ -125,9 +123,7 @@ export function makeFirstTransform(opts: FirstTransformParams) { return; } - let head = 'head' in node.path ? node.path.head : node.path.parts[0]; - - if (inScope(scopeStack, head)) { + if (inScope(scopeStack, headOf(node.path))) { return; } if (node.path.original === 'macroGetOwnConfig') { @@ -173,7 +169,7 @@ export function makeSecondTransform() { name: '@embroider/macros/second', visitor: { - Block: { + Template: { enter(node: any) { if (node.blockParams.length > 0) { scopeStack.push(node.blockParams); @@ -190,9 +186,7 @@ export function makeSecondTransform() { return; } - let head = 'head' in node.path ? node.path.head : node.path.parts[0]; - - if (inScope(scopeStack, head)) { + if (inScope(scopeStack, headOf(node.path))) { return; } if (node.path.original === 'if') { @@ -207,9 +201,7 @@ export function makeSecondTransform() { return; } - let head = 'head' in node.path ? node.path.head : node.path.parts[0]; - - if (inScope(scopeStack, head)) { + if (inScope(scopeStack, headOf(node.path))) { return; } if (node.path.original === 'if') { @@ -248,9 +240,7 @@ export function makeSecondTransform() { return true; } - let head = 'head' in node.path ? node.path.head : node.path.parts[0]; - - if (inScope(scopeStack, head)) { + if (inScope(scopeStack, headOf(node.path))) { return true; } if (modifier.path.original === 'macroMaybeAttrs') { @@ -265,9 +255,7 @@ export function makeSecondTransform() { return; } - let head = 'head' in node.path ? node.path.head : node.path.parts[0]; - - if (inScope(scopeStack, head)) { + if (inScope(scopeStack, headOf(node.path))) { return; } if (node.path.original === 'if') { @@ -300,3 +288,9 @@ function inScope(scopeStack: string[][], name: string) { } return false; } + +function headOf(path: any) { + if (!path) return; + + return 'head' in path ? path.head : path.parts[0]; +} From fd08c5ec8bf158de3673206adad716a3d732c51f Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:54:36 -0400 Subject: [PATCH 4/5] Use a dynamic visitor key --- packages/macros/src/glimmer/ast-transform.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/macros/src/glimmer/ast-transform.ts b/packages/macros/src/glimmer/ast-transform.ts index 27b6652b4..67086fa30 100644 --- a/packages/macros/src/glimmer/ast-transform.ts +++ b/packages/macros/src/glimmer/ast-transform.ts @@ -72,7 +72,7 @@ export function makeFirstTransform(opts: FirstTransformParams) { name: '@embroider/macros/first', visitor: { - Template: { + [rootVisitorKey(env)]: { enter(node: any) { if (node.blockParams.length > 0) { scopeStack.push(node.blockParams); @@ -169,7 +169,7 @@ export function makeSecondTransform() { name: '@embroider/macros/second', visitor: { - Template: { + [rootVisitorKey(env)]: { enter(node: any) { if (node.blockParams.length > 0) { scopeStack.push(node.blockParams); @@ -294,3 +294,14 @@ function headOf(path: any) { return 'head' in path ? path.head : 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'; + + return rootKey; +} From e90d38d551b6174944c27f8e8f5933a70189c553 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Tue, 11 Jun 2024 13:28:44 -0400 Subject: [PATCH 5/5] 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 }, + }; + } }