-
-
Notifications
You must be signed in to change notification settings - Fork 13
Assert the compiled scope independently of the wire format's shape #121
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -417,6 +417,24 @@ describe('htmlbars-inline-precompile', function () { | |
| `); | ||
| }); | ||
|
|
||
| it('puts a scope local into the compiled scope', async function () { | ||
| let transformed = await transform(` | ||
| import { Setup } from './foo.js'; | ||
| import { precompileTemplate } from '@ember/template-compilation'; | ||
| import { setComponentTemplate } from '@ember/component'; | ||
| import templateOnly from '@ember/component/template-only'; | ||
|
|
||
| export default setComponentTemplate(precompileTemplate("<Setup />", { | ||
| strictMode: true, | ||
| scope: () => ({ | ||
| Setup | ||
| }) | ||
| }), templateOnly()); | ||
| `); | ||
|
|
||
| expect(wireScope(transformed)).toEqual({ Setup: 'Setup' }); | ||
| }); | ||
|
|
||
| it('does not fully remove imports that have other imports', async function () { | ||
| let transformed = await transform(` | ||
| import { precompileTemplate, compileTemplate } from '@ember/template-compilation'; | ||
|
|
@@ -2537,6 +2555,60 @@ describe('htmlbars-inline-precompile', function () { | |
| }); | ||
| }); | ||
|
|
||
| // Reads the compiled scope back out of the wire format as a mapping from the | ||
| // name the template uses to the JS it resolves to. | ||
| // | ||
| // Older compilers emit the scope as an array (`scope: () => [Setup]`) and newer | ||
| // ones as an object (`scope: () => ({ Foo: Setup })`). Both carry the same | ||
| // information, but the array form leaves the names implicit: they're the | ||
| // `locals` we handed the compiler, in order. Reading those back off the | ||
| // precompile call puts both shapes into the same form, so a test can assert on | ||
| // the scope without caring which ember-source is installed. | ||
| function wireScope(src: string): Record<string, string> { | ||
| let t = babel.types; | ||
| let found: Record<string, string> | undefined; | ||
|
|
||
| babel.traverse(babel.parse(src, { configFile: false, babelrc: false })!, { | ||
| ObjectProperty(path) { | ||
| let { key, value } = path.node; | ||
| let isScopeKey = | ||
| (t.isIdentifier(key) && key.name === 'scope') || | ||
| (t.isStringLiteral(key) && key.value === 'scope'); | ||
| if (!isScopeKey || !t.isArrowFunctionExpression(value)) { | ||
| return; | ||
| } | ||
|
|
||
| let source = (node: babel.types.Node) => src.slice(node.start!, node.end!); | ||
|
|
||
| if (t.isObjectExpression(value.body)) { | ||
| found = Object.fromEntries( | ||
| value.body.properties.map((prop) => { | ||
| if (!t.isObjectProperty(prop)) { | ||
| throw new Error(`unexpected scope entry: ${prop.type}`); | ||
| } | ||
| let name = t.isIdentifier(prop.key) ? prop.key.name : source(prop.key); | ||
| return [name, source(prop.value)]; | ||
| }) | ||
| ); | ||
| } else if (t.isArrayExpression(value.body)) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. old |
||
| let locals = (precompileSpy.mock.lastCall?.at(-1) as { locals?: string[] })?.locals; | ||
| if (!locals) { | ||
| throw new Error('cannot name an array-form scope without the compiler options'); | ||
| } | ||
| found = Object.fromEntries( | ||
| value.body.elements.map((element, index) => [locals[index], source(element!)]) | ||
| ); | ||
| } | ||
| path.stop(); | ||
| }, | ||
| }); | ||
|
|
||
| if (!found) { | ||
| throw new Error(`no wire-format scope found in:\n${src}`); | ||
| } | ||
| return found; | ||
| } | ||
|
|
||
| // This takes out parts of ember's wire format that aren't our job and shouldn't | ||
| // break our tests if they change. | ||
| function normalizeWireFormat(src: string): string { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new