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

fix 🎬 2 #54

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
78 changes: 13 additions & 65 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,26 +427,20 @@ function buildPrecompileOptions<EnvSpecificOptions>(
}

function remapAndBindIdentifiers(target: NodePath, babel: typeof Babel, scopeLocals: ScopeLocals) {
babel.traverse(
target.node,
{
Identifier(path: NodePath<t.Identifier>) {
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<t.Identifier>) {
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<EnvSpecificOptions>(
Expand Down Expand Up @@ -482,13 +476,6 @@ function insertCompiledTemplate<EnvSpecificOptions>(
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);
}

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

updateGlimmerScopeWithBabelScope(babel, templateExpression, scopeLocals);

t.addComment(
templateExpression,
'leading',
Expand Down Expand Up @@ -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
Expand Down
Loading