Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Jun 25, 2024
1 parent 1fade50 commit c99903e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
26 changes: 13 additions & 13 deletions __tests__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('<HelloWorld @color={{"#ff0000"}} />', { scope: () => ({ HelloWorld }), strictMode: true }), templateOnly());
export default setComponentTemplate(precompileTemplate('<HelloWorld @color={{"#ff0000"}} />', { scope: () => ({ HelloWorld }), strictMode: true }), templateOnly(undefined, "foo-bar"));
`);
});

Expand Down Expand Up @@ -1599,7 +1599,7 @@ describe('htmlbars-inline-precompile', function () {
precompileTemplate("Icon", {
strictMode: true,
}),
templateOnly()
templateOnly(undefined, "Icon")
);
`);
});
Expand All @@ -1626,7 +1626,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: false, scope: () => ({ HelloWorld }) }), templateOnly());
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: false, scope: () => ({ HelloWorld }) }), templateOnly(undefined, "foo-bar"));
`);
});
});
Expand Down Expand Up @@ -1698,7 +1698,7 @@ describe('htmlbars-inline-precompile', function () {
scope: () => [HelloWorld],
isStrictMode: true,
}
), templateOnly());
), templateOnly(undefined, "foo-bar"));
`);
});

Expand Down Expand Up @@ -1897,7 +1897,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly());
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly(undefined, "foo-bar"));
`);
});

Expand Down Expand Up @@ -1928,7 +1928,7 @@ describe('htmlbars-inline-precompile', function () {
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
let div = 1;
export default setComponentTemplate(precompileTemplate('<div></div>', { strictMode: true, scope: () => ({ div })}), templateOnly());
export default setComponentTemplate(precompileTemplate('<div></div>', { strictMode: true, scope: () => ({ div })}), templateOnly(undefined, "foo-bar"));
`);
});

Expand All @@ -1955,7 +1955,7 @@ describe('htmlbars-inline-precompile', function () {
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
let hasBlock = 1;
export default setComponentTemplate(precompileTemplate('{{hasBlock "thing"}}', { strictMode: true, scope: () => ({ hasBlock }) }), templateOnly());
export default setComponentTemplate(precompileTemplate('{{hasBlock "thing"}}', { strictMode: true, scope: () => ({ hasBlock }) }), templateOnly(undefined, "foo-bar"));
`);
});

Expand All @@ -1980,7 +1980,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('{{hasBlock "thing"}}', { strictMode: true }), templateOnly());
export default setComponentTemplate(precompileTemplate('{{hasBlock "thing"}}', { strictMode: true }), templateOnly(undefined, "foo-bar"));
`);
});

Expand Down Expand Up @@ -2020,7 +2020,7 @@ describe('htmlbars-inline-precompile', function () {
isStrictMode: true,
}
),
templateOnly()
templateOnly(undefined, "foo-bar")
);
`);
});
Expand Down Expand Up @@ -2049,7 +2049,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly());
export default setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly(undefined, "foo-bar"));
`);
});

Expand Down Expand Up @@ -2081,7 +2081,7 @@ describe('htmlbars-inline-precompile', function () {
import templateOnly from "@ember/component/template-only";
export default function() {
let { HelloWorld } = globalThis;
return setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld })}), templateOnly());
return setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld })}), templateOnly(undefined, undefined));
}
`);
});
Expand Down Expand Up @@ -2133,7 +2133,7 @@ describe('htmlbars-inline-precompile', function () {
isStrictMode: true,
}
),
templateOnly()
templateOnly(undefined, "foo-bar")
);
`);
});
Expand Down Expand Up @@ -2166,7 +2166,7 @@ describe('htmlbars-inline-precompile', function () {
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
const MyComponent = setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly());
const MyComponent = setComponentTemplate(precompileTemplate('<HelloWorld />', { strictMode: true, scope: () => ({ HelloWorld }) }), templateOnly(undefined, "MyComponent"));
`);
});

Expand Down
35 changes: 22 additions & 13 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { EmberTemplateCompiler, PreprocessOptions } from './ember-template-
import { LegacyModuleName } from './public-types';
import { ScopeLocals } from './scope-locals';
import { ASTPluginBuilder, preprocess, print } from '@glimmer/syntax';
import { basename, extname } from 'path';

export * from './public-types';

Expand Down Expand Up @@ -497,23 +498,26 @@ function insertCompiledTemplate<EnvSpecificOptions>(
let expression = t.callExpression(templateFactoryIdentifier, [templateExpression]);

let assignment = target.parent;
let assignmentName: string | null = null;
let assignmentName: t.StringLiteral = t.stringLiteral(state.filename);
if (assignment.type === 'AssignmentExpression' && assignment.left.type === 'Identifier') {
assignmentName = assignment.left.name;
assignmentName = t.stringLiteral(assignment.left.name);
}

if (assignment.type === 'VariableDeclarator' && assignment.id.type === 'Identifier') {
assignmentName = assignment.id.name;
assignmentName = t.stringLiteral(assignment.id.name);
}
if (assignment.type === 'ExportDefaultDeclaration') {
const name = basename(state.filename).slice(0, -extname(state.filename).length);
assignmentName = t.stringLiteral(name);
}

if (config.rfc931Support) {
expression = t.callExpression(i.import('@ember/component', 'setComponentTemplate'), [
expression,
backingClass?.node ??
t.callExpression(
i.import('@ember/component/template-only', 'default', 'templateOnly'),
['@ember/component/template-only', assignmentName]
),
t.callExpression(i.import('@ember/component/template-only', 'default', 'templateOnly'), [
t.identifier('undefined'),
assignmentName,
]),
]);
}
return expression;
Expand Down Expand Up @@ -616,14 +620,19 @@ function updateCallForm<EnvSpecificOptions>(
convertStrictMode(babel, target);
removeEvalAndScope(target);
target.node.arguments = target.node.arguments.slice(0, 2);

let assignment = target.parent;
let assignmentName: string | null = null;
let assignmentName: Babel.types.Identifier | Babel.types.StringLiteral =
babel.types.identifier('undefined');
if (assignment.type === 'AssignmentExpression' && assignment.left.type === 'Identifier') {
assignmentName = assignment.left.name;
assignmentName = babel.types.stringLiteral(assignment.left.name);
}

if (assignment.type === 'VariableDeclarator' && assignment.id.type === 'Identifier') {
assignmentName = assignment.id.name;
assignmentName = babel.types.stringLiteral(assignment.id.name);
}
if (assignment.type === 'ExportDefaultDeclaration') {
const name = basename(state.filename).slice(0, -extname(state.filename).length);
assignmentName = babel.types.stringLiteral(name);
}

state.recursionGuard.add(target.node);
Expand All @@ -633,7 +642,7 @@ function updateCallForm<EnvSpecificOptions>(
backingClass?.node ??
babel.types.callExpression(
i.import('@ember/component/template-only', 'default', 'templateOnly'),
['@ember/component/template-only', assignmentName]
[babel.types.identifier('undefined'), assignmentName]
),
])
);
Expand Down

0 comments on commit c99903e

Please sign in to comment.