Skip to content

Commit f03b7a1

Browse files
committed
fix
1 parent 3f01117 commit f03b7a1

File tree

1 file changed

+13
-65
lines changed

1 file changed

+13
-65
lines changed

src/plugin.ts

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -427,26 +427,20 @@ function buildPrecompileOptions<EnvSpecificOptions>(
427427
}
428428

429429
function remapAndBindIdentifiers(target: NodePath, babel: typeof Babel, scopeLocals: ScopeLocals) {
430-
babel.traverse(
431-
target.node,
432-
{
433-
Identifier(path: NodePath<t.Identifier>) {
434-
if (scopeLocals.has(path.node.name) && path.node.name !== scopeLocals.get(path.node.name)) {
435-
// this identifier has different names in hbs vs js, so we need to
436-
// replace the hbs name in the template compiler output with the js
437-
// name
438-
path.replaceWith(babel.types.identifier(scopeLocals.get(path.node.name)));
439-
}
440-
// this is where we tell babel's scope system about the new reference we
441-
// just introduced. @babel/plugin-transform-typescript in particular
442-
// cares a lot about those references being present.
443-
path.scope.getBinding(path.node.name)?.reference(path);
444-
},
430+
target.traverse({
431+
Identifier(path: NodePath<t.Identifier>) {
432+
if (scopeLocals.has(path.node.name) && path.node.name !== scopeLocals.get(path.node.name)) {
433+
// this identifier has different names in hbs vs js, so we need to
434+
// replace the hbs name in the template compiler output with the js
435+
// name
436+
path.replaceWith(babel.types.identifier(scopeLocals.get(path.node.name)));
437+
}
438+
// this is where we tell babel's scope system about the new reference we
439+
// just introduced. @babel/plugin-transform-typescript in particular
440+
// cares a lot about those references being present.
441+
path.scope.getBinding(path.node.name)?.reference(path);
445442
},
446-
target.scope,
447-
{},
448-
target.parentPath ?? undefined
449-
);
443+
});
450444
}
451445

452446
function insertCompiledTemplate<EnvSpecificOptions>(
@@ -482,13 +476,6 @@ function insertCompiledTemplate<EnvSpecificOptions>(
482476
return;
483477
}
484478
} else {
485-
// The emitted `scope: () => []` here could potentially be wrong,
486-
// as it does not know about the values in JS Scope.
487-
// the scope that we pass to precompile tells the compiler what to expect will be
488-
// in scope at runtime.
489-
// but when we emit the final scope array, we need to make sure we map back to
490-
// the assignments / renames in the scope-bag from the pre-wirenformat
491-
// (which can be seen in target.toString())
492479
precompileResultString = opts.compiler.precompile(template, options);
493480
}
494481

@@ -500,8 +487,6 @@ function insertCompiledTemplate<EnvSpecificOptions>(
500487
let templateExpression = (precompileResultAST.program.body[0] as t.VariableDeclaration)
501488
.declarations[0].init as t.Expression;
502489

503-
updateGlimmerScopeWithBabelScope(babel, templateExpression, scopeLocals);
504-
505490
t.addComment(
506491
templateExpression,
507492
'leading',
@@ -674,43 +659,6 @@ function buildScope(babel: typeof Babel, locals: ScopeLocals) {
674659
);
675660
}
676661

677-
// templateExpression.properties[]:
678-
// [0]: key.value = id
679-
// [1]: key.value = block
680-
// [2]: key.value = moduleName
681-
// [3]: key.value = scope <-- this is what needs updating
682-
// [4]: key.value = isStrictMode
683-
function updateGlimmerScopeWithBabelScope(
684-
babel: typeof Babel,
685-
templateExpression: t.Expression,
686-
scopeLocals: ScopeLocals
687-
) {
688-
let t = babel.types;
689-
690-
if (t.isObjectExpression(templateExpression)) {
691-
let scopeObjectProperty = templateExpression.properties.find((property) => {
692-
if (t.isObjectProperty(property)) {
693-
return t.isStringLiteral(property.key) && property.key.value === 'scope';
694-
}
695-
return false;
696-
});
697-
698-
if (t.isObjectProperty(scopeObjectProperty)) {
699-
let scopeValue = scopeObjectProperty.value;
700-
701-
if (t.isArrowFunctionExpression(scopeValue)) {
702-
if (t.isArrayExpression(scopeValue.body)) {
703-
scopeValue.body.elements.map((element) => {
704-
if (t.isIdentifier(element)) {
705-
element.name = scopeLocals.get(element.name);
706-
}
707-
});
708-
}
709-
}
710-
}
711-
}
712-
}
713-
714662
// this is responsible both for adjusting the AST for our scope argument *and*
715663
// ensuring that babel's scope system will see that these new identifiers
716664
// reference their bindings. @babel/plugin-transform-typescript in particular

0 commit comments

Comments
 (0)