Skip to content

Commit

Permalink
Bump ember-template-recast: fix valueless attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
lolmaus committed Nov 27, 2023
1 parent e86b127 commit b755fd4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
19 changes: 15 additions & 4 deletions transforms/angle-brackets/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ function shouldSkipFile(fileInfo, config) {
return false;
}

function transformAttrs(tagName, attrs, config) {
return attrs.map((a) => {
function transformAttrs(tagName, attrs, nodeParams, config) {
const newAttrs = attrs.map((a) => {
let _key = a.key;
let _valueType = a.value.type;
let _value;
Expand Down Expand Up @@ -174,6 +174,17 @@ function transformAttrs(tagName, attrs, config) {
}
return b.attr(_key, _value);
});

const newValuelessAttrs = nodeParams
.filter((param) => !!param.parts)
.filter((param) => isAttribute(param.parts[0]))
.map((param) => {
const attr = b.attr(param.parts[0], b.text(''));
attr.isValueless = true;
return attr;
});

return [...newAttrs, ...newValuelessAttrs];
}

function isQueryParam(param) {
Expand Down Expand Up @@ -299,7 +310,7 @@ function shouldSkipDataTestParams(params, includeValuelessDataTestAttributes) {
}

function transformNodeAttributes(tagName, node, config) {
let attributes = transformAttrs(tagName, node.hash.pairs, config);
let attributes = transformAttrs(tagName, node.hash.pairs, node.params, config);
return node.params.concat(attributes);
}

Expand Down Expand Up @@ -389,7 +400,7 @@ function transformNode(node, fileInfo, config) {
attributes = transformLinkToAttrs(node.params);
}

let namesParams = transformAttrs(tagName, node.hash.pairs, config);
let namesParams = transformAttrs(tagName, node.hash.pairs, node.params, config);
attributes = attributes.concat(namesParams);
} else {
if (nodeHasPositionalParameters(node)) {
Expand Down
25 changes: 20 additions & 5 deletions transforms/angle-brackets/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ test('data-test-attributes', () => {
<XFoo @data-test-selector={{true}} />
<XFoo @data-test-selector={{post.id}} />
<XFoo @label=\\"hi\\" @data-test-selector={{true}} />
<XFoo data-test-foo />
<XFoo data-test-foo />
<XFoo @data-foo={{true}}>
block
</XFoo>
Expand All @@ -210,16 +210,16 @@ test('data-test-attributes', () => {
<XFoo @data-test-selector={{post.id}}>
block
</XFoo>
<Common::AccordionComponent data-test-accordion as |accordion|>
<Common::AccordionComponent data-test-accordion as |accordion|>
block
</Common::AccordionComponent>
<LinkTo @route=\\"posts\\" data-test-foo>
<LinkTo @route=\\"posts\\" data-test-foo >
Recent Posts
</LinkTo>
<LinkTo @route={{this.dynamicPath}} @query={{hash direction=\\"desc\\" showArchived=false}} data-test-foo>
<LinkTo @route={{this.dynamicPath}} @query={{hash direction=\\"desc\\" showArchived=false}} data-test-foo >
Recent Posts
</LinkTo>
<LinkTo @route={{this.dynamicPath}} @query={{hash direction=\\"desc\\" showArchived=false}} data-test-foo data-foo>
<LinkTo @route={{this.dynamicPath}} @query={{hash direction=\\"desc\\" showArchived=false}} data-test-foo data-foo >
Recent Posts
</LinkTo>
Expand All @@ -243,6 +243,21 @@ test('data-test-attributes', () => {
`);
});

test('data-test-empty-attributes', () => {
let options = {
includeValuelessDataTestAttributes: true,
};
let input = `
{{x-foo data-test-foo }}
`;

expect(runTest('data-test-empty-attributes.hbs', input, options)).toMatchInlineSnapshot(`
"
<XFoo data-test-foo />
"
`);
});

test('deeply-nested-sub', () => {
let input = `
{{#some-component class=(concat foo (some-helper ted (some-dude bar (a b c)))) }}
Expand Down

0 comments on commit b755fd4

Please sign in to comment.