-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[Bugfix release] allow class-based helpers to work in strict-mode #19878
Merged
kategengler
merged 1 commit into
emberjs:master
from
NullVoxPopuli:smoke-test-and-repro-of-19877
Jan 7, 2022
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 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
78 changes: 78 additions & 0 deletions
78
packages/@ember/-internals/glimmer/tests/integration/custom-helper-test.js
This file contains 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { RenderingTestCase, moduleFor, strip } from 'internal-test-helpers'; | ||
|
||
import { precompileJSON } from '@glimmer/compiler'; | ||
import { getTemplateLocals } from '@glimmer/syntax'; | ||
import { createTemplateFactory } from '@ember/template-factory'; | ||
|
||
import { Helper } from '../../index'; | ||
import { setComponentTemplate } from '@glimmer/manager'; | ||
import { templateOnlyComponent } from '@glimmer/runtime'; | ||
|
||
// eslint-disable-next-line ember-internal/require-yuidoc-access | ||
/** | ||
* The template-compiler does not support strict mode at this time. | ||
* precompile from ember-template-compiler returns a string and | ||
* not a template-factory, so it doesn't help with strict-mode testing. | ||
* | ||
* We also can't import from `@ember/template-compiler` because it | ||
* doesn't exist to this kind of test, otherwise we'd be able to use | ||
* precompileTemplate, which would be perfect :D | ||
* | ||
* Copied(ish) from https://github.com/NullVoxPopuli/ember-repl/blob/main/addon/hbs.ts#L51 | ||
*/ | ||
function precompileTemplate(source, { moduleName, scope = {} }) { | ||
let locals = getTemplateLocals(source); | ||
|
||
let options = { | ||
strictMode: true, | ||
moduleName, | ||
locals, | ||
isProduction: false, | ||
meta: { moduleName }, | ||
}; | ||
|
||
// Copied from @glimmer/compiler/lib/compiler#precompile | ||
let [block, usedLocals] = precompileJSON(source, options); | ||
let usedScope = usedLocals.map((key) => scope[key]); | ||
|
||
let blockJSON = JSON.stringify(block); | ||
let templateJSONObject = { | ||
id: moduleName, | ||
block: blockJSON, | ||
moduleName: moduleName ?? '(unknown template module)', | ||
scope: () => usedScope, | ||
isStrictMode: true, | ||
}; | ||
|
||
let factory = createTemplateFactory(templateJSONObject); | ||
|
||
return factory; | ||
} | ||
|
||
moduleFor( | ||
'Custom Helper test', | ||
class extends RenderingTestCase { | ||
['@test works with strict-mode']() { | ||
class Custom extends Helper { | ||
compute([value]) { | ||
return `${value}-custom`; | ||
} | ||
} | ||
|
||
let template = strip` | ||
{{ (Custom 'my-test') }} | ||
`; | ||
|
||
let templateFactory = precompileTemplate(template, { | ||
moduleName: 'strict-mode', | ||
scope: { Custom }, | ||
}); | ||
|
||
let TestComponent = setComponentTemplate(templateFactory, templateOnlyComponent()); | ||
|
||
this.render(`<this.TestComponent />`, { TestComponent }); | ||
this.assertText('my-test-custom'); | ||
this.assertStableRerender(); | ||
} | ||
} | ||
); |
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.
when a class-based helper is used in strict mode, there is no class property