Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Apr 13, 2024
1 parent 0ae952b commit adc809b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
44 changes: 26 additions & 18 deletions __tests__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,14 +996,14 @@ describe('htmlbars-inline-precompile', function () {
expect(transformed).toContain(`window.define('my-app/components/thing', thing)`);
});

it('can emit side-effectful import', function () {
let compatTransform: ExtendedPluginBuilder = () => {
it('update scope correctly', function () {
let compatTransform: ExtendedPluginBuilder = (env) => {
return {
name: 'compat-transform',
visitor: {
ElementNode(node) {
ElementNode(node, path) {
if (node.tag === 'Thing') {
node.tag = 'NewThing';
node.tag = env.meta.jsutils.bindExpression('Thing', path, { nameHint: 'NewThing' });
}
},
},
Expand All @@ -1014,15 +1014,14 @@ describe('htmlbars-inline-precompile', function () {

let transformed = transform(stripIndent`
import { precompileTemplate } from '@ember/template-compilation';
let NewThing = '';
export default function() {
const template = precompileTemplate('<Thing />');
}
`);

expect(transformed).toEqualCode(`
import { precompileTemplate } from '@ember/template-compilation';
let NewThing = '';
let NewThing = Thing;
export default function () {
const template = precompileTemplate("<NewThing />", {
scope: () => ({
Expand All @@ -1033,34 +1032,44 @@ describe('htmlbars-inline-precompile', function () {
});

it('updates scope correctly when renamed', function () {
let renameTransform: ExtendedPluginBuilder = () => {
let importTransform: ExtendedPluginBuilder = (env) => {
return {
name: 'compat-transform',
visitor: {
ElementNode(node) {
ElementNode(node, path) {
if (node.tag === 'Thing') {
env.meta.jsutils.bindImport('the-thing', 'Thing', path);
}
},
},
};
};

let renameTransform: ExtendedPluginBuilder = (env) => {
return {
name: 'compat-transform',
visitor: {
ElementNode(node, path) {
if (node.tag === 'Thing') {
node.tag = 'NewThing';
node.tag = env.meta.jsutils.bindExpression('Thing', path, { nameHint: 'NewThing' });
}
},
},
};
};

plugins = [[HTMLBarsInlinePrecompile, { targetFormat: 'hbs', transforms: [] }]];
plugins = [[HTMLBarsInlinePrecompile, { targetFormat: 'hbs', transforms: [importTransform] }]];

let transformed = transform(stripIndent`
import { precompileTemplate } from '@ember/template-compilation';
let Thing = '';
let NewThing = '';
export default function() {
const template = precompileTemplate('<Thing />');
}
`);

expect(transformed).toEqualCode(`
import { precompileTemplate } from '@ember/template-compilation';
let Thing = '';
let NewThing = '';
import { Thing } from "the-thing";
export default function () {
const template = precompileTemplate("<Thing />", {
scope: () => ({
Expand All @@ -1075,8 +1084,8 @@ describe('htmlbars-inline-precompile', function () {

expect(transformed).toEqualCode(`
import { precompileTemplate } from '@ember/template-compilation';
let Thing = '';
let NewThing = '';
import { Thing } from "the-thing";
let NewThing = Thing;
export default function () {
const template = precompileTemplate("<NewThing />", {
scope: () => ({
Expand All @@ -1086,7 +1095,7 @@ describe('htmlbars-inline-precompile', function () {
}`);
});

it('updates scope correctly when renamed', function () {
it('can emit side-effectful import', function () {
let compatTransform: ExtendedPluginBuilder = (env) => {
return {
name: 'compat-transform',
Expand Down Expand Up @@ -1218,7 +1227,6 @@ describe('htmlbars-inline-precompile', function () {
const template = precompileTemplate("<Message @text={{two}} />", {
moduleName: 'customModuleName',
scope: () => ({
Message,
two
})
});
Expand Down
2 changes: 1 addition & 1 deletion src/js-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class JSUtils {
t.variableDeclarator(t.identifier(identifier), importedIdentifier),
])
);
declaration.scope.registerBinding('let', declaration.get('declarations.0') as NodePath
declaration.scope.registerBinding('let', declaration.get('declarations.0') as NodePath);
}
this.#scopeLocals.add(identifier);
return identifier;
Expand Down
4 changes: 2 additions & 2 deletions src/scope-locals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import { ASTPluginEnvironment, NodeVisitor } from '@glimmer/syntax';
import { astNodeHasBinding } from './hbs-utils';

export class ScopeLocals {
constructor(jsPath: NodePath<t.Expression>, checkJsScope: Boolean = false) {
constructor(jsPath: NodePath<t.Expression>, checkJsScope = false) {
this.#jsPath = jsPath;
this.#checkJsScope = checkJsScope;
}

#mapping: Record<string, string> = {};
#locals: string[] = [];
#jsPath: NodePath<t.Expression>;
#checkJsScope: Boolean;
#checkJsScope: boolean;

get locals() {
return this.#locals;
Expand Down

0 comments on commit adc809b

Please sign in to comment.