-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
RFC#997 - {{on}} as keyword #21068
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
NullVoxPopuli
merged 8 commits into
main
from
nvp/on-as-keyword-tremplate-the-transform-way
Apr 6, 2026
Merged
RFC#997 - {{on}} as keyword #21068
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7843f42
Add 'on' keyword support and related tests
NullVoxPopuli f1797a1
Remove globalThis for keyword resolution in runtime template() (#21254)
NullVoxPopuli-ai-agent ebd0447
Revert unnecessary change to normalize.ts (#21255)
NullVoxPopuli-ai-agent e6fa2b5
Remove redundant condition
NullVoxPopuli 51caaea
Add another test for no eval and no scope
NullVoxPopuli c0739a9
Evaluator is now no longer needed
NullVoxPopuli 5403b05
Rebase cleanup
NullVoxPopuli 6bbf661
Remove unneeded Object.keys keywords
NullVoxPopuli 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
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
44 changes: 44 additions & 0 deletions
44
packages/@ember/template-compiler/lib/plugins/auto-import-builtins.ts
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { AST, ASTPlugin } from '@glimmer/syntax'; | ||
| import type { EmberASTPluginEnvironment } from '../types'; | ||
| import { isPath, trackLocals } from './utils'; | ||
|
|
||
| /** | ||
| @module ember | ||
| */ | ||
|
|
||
| /** | ||
| A Glimmer2 AST transformation that makes importable keywords work | ||
|
|
||
| @private | ||
| @class TransformActionSyntax | ||
| */ | ||
|
|
||
| export default function autoImportBuiltins(env: EmberASTPluginEnvironment): ASTPlugin { | ||
| let { hasLocal, visitor } = trackLocals(env); | ||
|
|
||
| return { | ||
| name: 'auto-import-built-ins', | ||
|
|
||
| visitor: { | ||
| ...visitor, | ||
| ElementModifierStatement(node: AST.ElementModifierStatement) { | ||
| if (isOn(node, hasLocal)) { | ||
| if (env.meta?.jsutils) { | ||
| node.path.original = env.meta.jsutils.bindImport('@ember/modifier', 'on', node, { | ||
| name: 'on', | ||
| }); | ||
| } else if (env.meta?.emberRuntime) { | ||
| node.path.original = env.meta.emberRuntime.lookupKeyword('on'); | ||
| } | ||
| } | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function isOn( | ||
| node: AST.ElementModifierStatement | AST.MustacheStatement | AST.SubExpression, | ||
| hasLocal: (k: string) => boolean | ||
| ): node is AST.ElementModifierStatement & { path: AST.PathExpression } { | ||
| return isPath(node.path) && node.path.original === 'on' && !hasLocal('on'); | ||
| } |
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
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 |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import { precompile as glimmerPrecompile } from '@glimmer/compiler'; | |
| import type { SerializedTemplateWithLazyBlock } from '@glimmer/interfaces'; | ||
| import { setComponentTemplate } from '@glimmer/manager'; | ||
| import { templateFactory } from '@glimmer/opcode-compiler'; | ||
| import compileOptions from './compile-options'; | ||
| import compileOptions, { keywords, RUNTIME_KEYWORDS_NAME } from './compile-options'; | ||
| import type { EmberPrecompileOptions } from './types'; | ||
|
|
||
| type ComponentClass = abstract new (...args: any[]) => object; | ||
|
|
@@ -237,38 +237,54 @@ export function template( | |
| templateString: string, | ||
| providedOptions?: BaseTemplateOptions | BaseClassTemplateOptions<any> | ||
| ): object { | ||
| const options: EmberPrecompileOptions = { strictMode: true, ...providedOptions }; | ||
| const evaluate = buildEvaluator(options); | ||
| const options = { strictMode: true, ...providedOptions }; | ||
|
|
||
| const evaluate = buildEvaluator(options); | ||
| const normalizedOptions = compileOptions(options); | ||
| const component = normalizedOptions.component ?? templateOnly(); | ||
|
|
||
| const source = glimmerPrecompile(templateString, normalizedOptions); | ||
| const template = templateFactory(evaluate(`(${source})`) as SerializedTemplateWithLazyBlock); | ||
| const wire = evaluate(`(${source})`) as SerializedTemplateWithLazyBlock; | ||
|
|
||
| const template = templateFactory(wire); | ||
|
|
||
| setComponentTemplate(template, component); | ||
|
|
||
| return component; | ||
| } | ||
|
|
||
| const evaluator = (source: string) => { | ||
| return new Function(`return ${source}`)(); | ||
| }; | ||
| /** | ||
| * Builds the source wireformat JSON block | ||
| * | ||
| * @param options | ||
| * @returns | ||
| */ | ||
| function buildEvaluator(options: Partial<EmberPrecompileOptions>) { | ||
| if (options.eval) { | ||
| const userEval = options.eval; | ||
|
|
||
| function buildEvaluator(options: Partial<EmberPrecompileOptions> | undefined) { | ||
| if (options === undefined) { | ||
| return evaluator; | ||
| } | ||
| // Wrap the compiled source in a function that receives the keywords | ||
| // container as a parameter. The user's eval evaluates this in the | ||
| // caller's scope, so local variables (like `handleClick`) are captured | ||
| // via closure, while `__keywords__` comes from the function parameter. | ||
| return (source: string) => { | ||
| let wrapperFn = userEval(`(function(${RUNTIME_KEYWORDS_NAME}){ return (${source}); })`) as ( | ||
| ...args: unknown[] | ||
| ) => unknown; | ||
|
|
||
| if (options.eval) { | ||
| return options.eval; | ||
| return wrapperFn(keywords); | ||
| }; | ||
| } else { | ||
| const scope = options.scope?.(); | ||
| let scope = options.scope?.(); | ||
|
|
||
| if (!scope) { | ||
| return evaluator; | ||
| return (source: string) => { | ||
| return new Function(RUNTIME_KEYWORDS_NAME, `return (${source})`)(keywords); | ||
| }; | ||
| } | ||
|
|
||
| scope = Object.assign({ [RUNTIME_KEYWORDS_NAME]: keywords }, scope); | ||
|
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. @ef4 we did it! |
||
|
|
||
| return (source: string) => { | ||
| let hasThis = Object.prototype.hasOwnProperty.call(scope, 'this'); | ||
| let thisValue = hasThis ? (scope as { this?: unknown }).this : undefined; | ||
|
|
||
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
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
88 changes: 88 additions & 0 deletions
88
packages/@glimmer-workspace/integration-tests/test/keywords/on-runtime-test.ts
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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { castToBrowser } from '@glimmer/debug-util'; | ||
| import { | ||
| GlimmerishComponent, | ||
| jitSuite, | ||
| RenderTest, | ||
| test, | ||
| } from '@glimmer-workspace/integration-tests'; | ||
|
|
||
| import { template } from '@ember/template-compiler/runtime'; | ||
|
|
||
| class KeywordOn extends RenderTest { | ||
| static suiteName = 'keyword modifier: on (runtime)'; | ||
|
|
||
| @test | ||
| 'explicit scope'(assert: Assert) { | ||
| let handleClick = () => { | ||
| assert.step('success'); | ||
| }; | ||
|
|
||
| const compiled = template('<button {{on "click" handleClick}}>Click</button>', { | ||
| strictMode: true, | ||
| scope: () => ({ | ||
| handleClick, | ||
| }), | ||
| }); | ||
|
|
||
| this.renderComponent(compiled); | ||
|
|
||
| castToBrowser(this.element, 'div').querySelector('button')!.click(); | ||
| assert.verifySteps(['success']); | ||
| } | ||
|
|
||
| @test | ||
| 'implicit scope'(assert: Assert) { | ||
| let handleClick = () => { | ||
| assert.step('success'); | ||
| }; | ||
|
|
||
| hide(handleClick); | ||
|
|
||
| const compiled = template('<button {{on "click" handleClick}}>Click</button>', { | ||
| strictMode: true, | ||
| eval() { | ||
| return eval(arguments[0]); | ||
| }, | ||
| }); | ||
|
|
||
| this.renderComponent(compiled); | ||
|
|
||
| castToBrowser(this.element, 'div').querySelector('button')!.click(); | ||
| assert.verifySteps(['success']); | ||
| } | ||
|
|
||
| @test | ||
| 'no eval and no scope'(assert: Assert) { | ||
| class Foo extends GlimmerishComponent { | ||
| static { | ||
| template('<button {{on "click" this.handleClick}}>Click</button>', { | ||
| strictMode: true, | ||
| component: this, | ||
| }); | ||
| } | ||
|
|
||
| handleClick = () => assert.step('success'); | ||
| } | ||
|
|
||
| this.renderComponent(Foo); | ||
|
|
||
| castToBrowser(this.element, 'div').querySelector('button')!.click(); | ||
| assert.verifySteps(['success']); | ||
| } | ||
| } | ||
|
|
||
| jitSuite(KeywordOn); | ||
|
|
||
| /** | ||
| * This function is used to hide a variable from the transpiler, so that it | ||
| * doesn't get removed as "unused". It does not actually do anything with the | ||
| * variable, it just makes it be part of an expression that the transpiler | ||
| * won't remove. | ||
| * | ||
| * It's a bit of a hack, but it's necessary for testing. | ||
| * | ||
| * @param variable The variable to hide. | ||
| */ | ||
| const hide = (variable: unknown) => { | ||
| new Function(`return (${JSON.stringify(variable)});`); | ||
| }; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.