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

Address these issues in new apps (5.9): #1967

Merged
Merged
Changes from all 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
76 changes: 46 additions & 30 deletions packages/macros/src/glimmer/ast-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,16 @@ export function makeFirstTransform(opts: FirstTransformParams) {
name: '@embroider/macros/first',

visitor: {
Program: {
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;
}
if (inScope(scopeStack, node.path.parts[0])) {

if (inScope(scopeStack, headOf(node.path))) {
return;
}

if (node.path.original === 'macroGetOwnConfig') {
return literal(
getConfig(node, opts.configs, opts.packageRoot, moduleName, true, packageCache),
Expand Down Expand Up @@ -120,7 +111,8 @@ export function makeFirstTransform(opts: FirstTransformParams) {
if (node.path.type !== 'PathExpression') {
return;
}
if (inScope(scopeStack, node.path.parts[0])) {

if (inScope(scopeStack, headOf(node.path))) {
return;
}
if (node.path.original === 'macroGetOwnConfig') {
Expand Down Expand Up @@ -166,23 +158,13 @@ export function makeSecondTransform() {
name: '@embroider/macros/second',

visitor: {
Program: {
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;
}
if (inScope(scopeStack, node.path.parts[0])) {

if (inScope(scopeStack, headOf(node.path))) {
return;
}
if (node.path.original === 'if') {
Expand All @@ -196,7 +178,8 @@ export function makeSecondTransform() {
if (node.path.type !== 'PathExpression') {
return;
}
if (inScope(scopeStack, node.path.parts[0])) {

if (inScope(scopeStack, headOf(node.path))) {
return;
}
if (node.path.original === 'if') {
Expand Down Expand Up @@ -234,7 +217,8 @@ export function makeSecondTransform() {
if (modifier.path.type !== 'PathExpression') {
return true;
}
if (inScope(scopeStack, modifier.path.parts[0])) {

if (inScope(scopeStack, headOf(node.path))) {
return true;
}
if (modifier.path.original === 'macroMaybeAttrs') {
Expand All @@ -248,7 +232,8 @@ export function makeSecondTransform() {
if (node.path.type !== 'PathExpression') {
return;
}
if (inScope(scopeStack, node.path.parts[0])) {

if (inScope(scopeStack, headOf(node.path))) {
return;
}
if (node.path.original === 'if') {
Expand Down Expand Up @@ -281,3 +266,34 @@ function inScope(scopeStack: string[][], name: string) {
}
return false;
}

function headOf(path: any) {
if (!path) return;

return 'head' in path ? path.head.name : path.parts[0];
}

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();
}
}

let hasTemplate = 'template' in env.syntax.builders;
if (hasTemplate) {
return {
Template: { enter, exit },
Block: { enter, exit },
};
} else {
return {
Program: { enter, exit },
};
}
}
Loading