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

[BUGFIX] Contextual component variable transformation #230

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions transforms/angle-brackets/telemetry/mock-invokables.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,7 @@ module.exports = {
'app/helpers/nested/helper': {
type: 'Helper',
},
'app/components/power-select': {
type: 'Component',
},
};
11 changes: 10 additions & 1 deletion transforms/angle-brackets/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const HTML_ATTRIBUTES = ['class', 'placeholder', 'required'];
const BUILT_IN_COMPONENTS = ['link-to', 'input', 'textarea'];

let inAttr = false;
let havingBlockParams = false;
let currentBlockParams = [];

function isAttribute(key) {
return HTML_ATTRIBUTES.includes(key) || isDataAttribute(key);
Expand Down Expand Up @@ -435,16 +437,23 @@ function transformToAngleBracket(fileInfo, config, invokableData) {
node.loc.source !== '(synthetic)' &&
!shouldIgnoreMustacheStatement(tagName, config, invokableData);
const isNestedComponent = isNestedComponentTagName(tagName);
const isNotBlockParamValue =
!havingBlockParams && !currentBlockParams.includes(node.path.original);

if (
isValidMustache &&
(node.hash.pairs.length > 0 || node.params.length > 0 || isNestedComponent)
(node.hash.pairs.length > 0 || node.params.length > 0 || isNestedComponent) &&
isNotBlockParamValue
) {
return transformNode(node, fileInfo, config);
}
},
BlockStatement(node) {
let tagName = `${node.path.original}`;
havingBlockParams = node.program.blockParams.length > 0;
if (havingBlockParams) {
currentBlockParams = node.program.blockParams;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this doesn't work for nested block params

Copy link
Member Author

Choose a reason for hiding this comment

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

@Turbo87 Can you give me an example?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the corresponding template-lint rule has something like this implemented, or you can have a look at the implicit-this codemod

}
if (
!shouldIgnoreMustacheStatement(node.path.original, config, invokableData) ||
isWallStreet(tagName)
Expand Down
20 changes: 20 additions & 0 deletions transforms/angle-brackets/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1164,3 +1164,23 @@ test('No telemetry', () => {
"
`);
});

test('contextual-component-variable', () => {
let input = `
{{#power-select search=(perform searchThings)
selected=selectedResult
placeholder="Enter the thing..."
loadingMessage="Searching..."
onchange=(perform selectThing) as |fooResult|}}
<span class="select-description">{{fooResult.attributes.thing-desc}}</span>
{{/power-select}}
`;

expect(runTest('contextual-component-variable.hbs', input)).toMatchInlineSnapshot(`
"
<PowerSelect @search={{perform searchThings}} @selected={{selectedResult}} @placeholder=\\"Enter the thing...\\" @loadingMessage=\\"Searching...\\" @onchange={{perform selectThing}} as |fooResult|>
<span class=\\"select-description\\">{{fooResult.attributes.thing-desc}}</span>
</PowerSelect>
"
`);
});