Skip to content

Commit

Permalink
Merge pull request #1979 from embroider-build/ast-deprecation
Browse files Browse the repository at this point in the history
fix scope visitors
  • Loading branch information
NullVoxPopuli authored Jun 11, 2024
2 parents fd08c5e + e90d38d commit d9bfb72
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions packages/macros/src/glimmer/ast-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 },
};
}
}

0 comments on commit d9bfb72

Please sign in to comment.