-
Notifications
You must be signed in to change notification settings - Fork 137
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 support for the helper
helper
#1120
Changes from all commits
97d10f3
42d8dc4
6bedb73
f9f8f6f
fbf17fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,10 @@ export function makeResolverTransform(resolver: Resolver) { | |
handleComponentHelper(node.params[0], resolver, filename, scopeStack); | ||
return; | ||
} | ||
if (node.path.original === 'helper' && node.params.length > 0) { | ||
handleDynamicHelper(node.params[0], resolver, filename); | ||
return; | ||
} | ||
resolver.resolveSubExpression(node.path.original, filename, node.path.loc); | ||
}, | ||
MustacheStatement(node: ASTv1.MustacheStatement) { | ||
|
@@ -97,6 +101,10 @@ export function makeResolverTransform(resolver: Resolver) { | |
handleComponentHelper(node.params[0], resolver, filename, scopeStack); | ||
return; | ||
} | ||
if (node.path.original === 'helper' && node.params.length > 0) { | ||
handleDynamicHelper(node.params[0], resolver, filename); | ||
return; | ||
} | ||
let hasArgs = node.params.length > 0 || node.hash.pairs.length > 0; | ||
let resolution = resolver.resolveMustache(node.path.original, hasArgs, filename, node.path.loc); | ||
if (resolution && resolution.type === 'component') { | ||
|
@@ -320,3 +328,16 @@ function handleComponentHelper( | |
|
||
resolver.resolveComponentHelper(locator, moduleName, param.loc, impliedBecause); | ||
} | ||
|
||
function handleDynamicHelper(param: ASTv1.Node, resolver: Resolver, moduleName: string): void { | ||
switch (param.type) { | ||
case 'StringLiteral': | ||
resolver.resolveDynamicHelper({ type: 'literal', path: param.value }, moduleName, param.loc); | ||
break; | ||
case 'TextNode': | ||
resolver.resolveDynamicHelper({ type: 'literal', path: param.chars }, moduleName, param.loc); | ||
break; | ||
default: | ||
resolver.resolveDynamicHelper({ type: 'other' }, moduleName, param.loc); | ||
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 Do we actually want to handle this case? The contextual helpers RFCs mentions that passing a helper reference into the helper should be supported so that extra arguments can be added: https://emberjs.github.io/rfcs/0432-contextual-helpers.html I left it out in the draft PR since Ember itself verifies that the helper isn't dynamic. I think Embroider only needs to do anything if users pass in a helper name? I think the code snippet in the RFC would throw an error with the current implementation? {{#let (helper "join-words" separator=",") as |join|}}
{{#let (helper join "foo") as |foo|}}
{{#let (helper foo "bar") as |foo-bar|}}
{{foo-bar "baz"}}
{{/let}}
{{/let}}
{{/let}} 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. Yes, this also generates an error, it's just done from the resolver instead of directly here because the resolver keeps track of all the errors. 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. But I think it shouldn't throw an error for that code snippet? Passing in a helper reference should work, according to the RFC, but with the currently merged implementation it will throw an error I think? Unless I misunderstood the code, I think at the moment, errors will be thrown under Embroider while it works as expected in a classic build. |
||
} | ||
} |
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.
Silly question, but how is it possible to pass a
TextNode
to the helper keyword? There is probably a legit use-case I'm missing, but I can't seem to create that scenario in AST-explorer 😄.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.
Maybe it's not? I was copying the earlier work on the component helper and that does have handling for TextNode, and I don't recall if that's because we found a case or because some typescript types implied it was possible.