diff --git a/src/plugin.ts b/src/plugin.ts index 5e0be23..0b724cc 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -427,26 +427,20 @@ function buildPrecompileOptions( } function remapAndBindIdentifiers(target: NodePath, babel: typeof Babel, scopeLocals: ScopeLocals) { - babel.traverse( - target.node, - { - Identifier(path: NodePath) { - if (scopeLocals.has(path.node.name) && path.node.name !== scopeLocals.get(path.node.name)) { - // this identifier has different names in hbs vs js, so we need to - // replace the hbs name in the template compiler output with the js - // name - path.replaceWith(babel.types.identifier(scopeLocals.get(path.node.name))); - } - // this is where we tell babel's scope system about the new reference we - // just introduced. @babel/plugin-transform-typescript in particular - // cares a lot about those references being present. - path.scope.getBinding(path.node.name)?.reference(path); - }, + target.traverse({ + Identifier(path: NodePath) { + if (scopeLocals.has(path.node.name) && path.node.name !== scopeLocals.get(path.node.name)) { + // this identifier has different names in hbs vs js, so we need to + // replace the hbs name in the template compiler output with the js + // name + path.replaceWith(babel.types.identifier(scopeLocals.get(path.node.name))); + } + // this is where we tell babel's scope system about the new reference we + // just introduced. @babel/plugin-transform-typescript in particular + // cares a lot about those references being present. + path.scope.getBinding(path.node.name)?.reference(path); }, - target.scope, - {}, - target.parentPath ?? undefined - ); + }); } function insertCompiledTemplate( @@ -482,13 +476,6 @@ function insertCompiledTemplate( return; } } else { - // The emitted `scope: () => []` here could potentially be wrong, - // as it does not know about the values in JS Scope. - // the scope that we pass to precompile tells the compiler what to expect will be - // in scope at runtime. - // but when we emit the final scope array, we need to make sure we map back to - // the assignments / renames in the scope-bag from the pre-wirenformat - // (which can be seen in target.toString()) precompileResultString = opts.compiler.precompile(template, options); } @@ -500,8 +487,6 @@ function insertCompiledTemplate( let templateExpression = (precompileResultAST.program.body[0] as t.VariableDeclaration) .declarations[0].init as t.Expression; - updateGlimmerScopeWithBabelScope(babel, templateExpression, scopeLocals); - t.addComment( templateExpression, 'leading', @@ -674,43 +659,6 @@ function buildScope(babel: typeof Babel, locals: ScopeLocals) { ); } -// templateExpression.properties[]: -// [0]: key.value = id -// [1]: key.value = block -// [2]: key.value = moduleName -// [3]: key.value = scope <-- this is what needs updating -// [4]: key.value = isStrictMode -function updateGlimmerScopeWithBabelScope( - babel: typeof Babel, - templateExpression: t.Expression, - scopeLocals: ScopeLocals -) { - let t = babel.types; - - if (t.isObjectExpression(templateExpression)) { - let scopeObjectProperty = templateExpression.properties.find((property) => { - if (t.isObjectProperty(property)) { - return t.isStringLiteral(property.key) && property.key.value === 'scope'; - } - return false; - }); - - if (t.isObjectProperty(scopeObjectProperty)) { - let scopeValue = scopeObjectProperty.value; - - if (t.isArrowFunctionExpression(scopeValue)) { - if (t.isArrayExpression(scopeValue.body)) { - scopeValue.body.elements.map((element) => { - if (t.isIdentifier(element)) { - element.name = scopeLocals.get(element.name); - } - }); - } - } - } - } -} - // this is responsible both for adjusting the AST for our scope argument *and* // ensuring that babel's scope system will see that these new identifiers // reference their bindings. @babel/plugin-transform-typescript in particular