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

add template only name #58

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
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"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be PascalCase, since it's what would get shown in the inspector, and how it would be invoked by a consumer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inspector would auto transform it to pascal case

Copy link
Contributor Author

@patricklx patricklx Jun 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

inspector:
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it also doesn't matter if it starts with lower case. so my-template-only-2 would also be shown as MyTemplateOnly2

`);
});

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
39 changes: 34 additions & 5 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 @@ -496,14 +497,27 @@ function insertCompiledTemplate<EnvSpecificOptions>(

let expression = t.callExpression(templateFactoryIdentifier, [templateExpression]);

let assignment = target.parent;
let assignmentName: t.StringLiteral = t.stringLiteral(state.filename);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can leak whole filesystem paths, which is both bloat and information leak. Also, it's not even a good answer, because you can have many components in this same file and they will all look as if they're the same component in the inspector, leading to confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, should be the same as below, babel.types.identifier('undefined')

if (assignment.type === 'AssignmentExpression' && assignment.left.type === 'Identifier') {
assignmentName = t.stringLiteral(assignment.left.name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using local variables like this is also likely to mislead, because nothing is stopping people from reusing the same local variable names in different modules that interact with each other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think its the best we can do right now. at least better than unknown template.
how about basename of file + variable name?

}
if (assignment.type === 'VariableDeclarator' && assignment.id.type === 'Identifier') {
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'),
[]
),
t.callExpression(i.import('@ember/component/template-only', 'default', 'templateOnly'), [
t.identifier('undefined'),
assignmentName,
]),
]);
}
return expression;
Expand Down Expand Up @@ -606,14 +620,29 @@ function updateCallForm<EnvSpecificOptions>(
convertStrictMode(babel, target);
removeEvalAndScope(target);
target.node.arguments = target.node.arguments.slice(0, 2);

let assignment = target.parent;
let assignmentName: Babel.types.Identifier | Babel.types.StringLiteral =
babel.types.identifier('undefined');
if (assignment.type === 'AssignmentExpression' && assignment.left.type === 'Identifier') {
assignmentName = babel.types.stringLiteral(assignment.left.name);
}
if (assignment.type === 'VariableDeclarator' && assignment.id.type === 'Identifier') {
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);
state.util.replaceWith(target, (i) =>
babel.types.callExpression(i.import('@ember/component', 'setComponentTemplate'), [
target.node,
backingClass?.node ??
babel.types.callExpression(
i.import('@ember/component/template-only', 'default', 'templateOnly'),
[]
[babel.types.identifier('undefined'), assignmentName]
),
])
);
Expand Down
Loading