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

Add config to enable transform on valueless attrs #241

Merged
merged 5 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions transforms/angle-brackets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function getOptions() {
options.skipFilesThatMatchRegex = new RegExp(config.skipFilesThatMatchRegex);
}

options.includeValuelessDataTestAttributes = !!config.includeValuelessDataTestAttributes;
options.skipBuiltInComponents = !!config.skipBuiltInComponents;
}

Expand Down
20 changes: 19 additions & 1 deletion transforms/angle-brackets/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ function hasValuelessDataParams(params) {
return getDataAttributesFromParams(params).length > 0;
}

/**
*
* data-* attributes are generally omitted,
* but this config allows including nodes with data-test-* attributes.
*/
function shouldSkipDataTestParams(params, includeValuelessDataTestAttributes) {
if (includeValuelessDataTestAttributes) {
const dataAttrs = getDataAttributesFromParams(params);
// This is true for nodes with data-* attributes too,
// as long as there is one with data-test-* attribute.
return !dataAttrs.some(attr => attr.original.startsWith('data-test-'));
}
return true;
}

function transformNodeAttributes(tagName, node, config) {
let attributes = transformAttrs(tagName, node.hash.pairs, config);
return node.params.concat(attributes);
Expand Down Expand Up @@ -338,7 +353,10 @@ function nodeHasPositionalParameters(node) {
}

function transformNode(node, fileInfo, config) {
if (hasValuelessDataParams(node.params)) {
if (
hasValuelessDataParams(node.params) &&
shouldSkipDataTestParams(node.params, config.includeValuelessDataTestAttributes)
) {
return;
}
let selfClosing = node.type !== 'BlockStatement';
Expand Down
99 changes: 99 additions & 0 deletions transforms/angle-brackets/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,105 @@ test('data-attributes', () => {
`);
});

test('data-test-attributes', () => {
let options = {
includeValuelessDataTestAttributes: true,
};
let input = `
{{x-foo data-foo=true}}
{{x-foo data-test-selector=true}}
{{x-foo data-test-selector=post.id}}
{{x-foo label="hi" data-test-selector=true}}
{{x-foo data-test-foo }}
{{#x-foo data-foo=true}}
block
{{/x-foo}}
{{#x-foo data-test-selector=true}}
block
{{/x-foo}}
{{#x-foo data-test-selector=post.id}}
block
{{/x-foo}}
{{#common/accordion-component data-test-accordion as |accordion|}}
block
{{/common/accordion-component}}
{{#link-to data-test-foo "posts"}}
Recent Posts
{{/link-to}}
{{#link-to data-test-foo this.dynamicPath (query-params direction="desc" showArchived=false)}}
Recent Posts
{{/link-to}}
{{#link-to data-test-foo data-foo this.dynamicPath (query-params direction="desc" showArchived=false)}}
Recent Posts
{{/link-to}}

{{x-foo
data-foo
name="Sophie"
}}
{{#x-foo data-foo}}
block
{{/x-foo}}
{{#common/accordion-component data-accordion as |accordion|}}
block
{{/common/accordion-component}}
{{#link-to data-foo "posts"}}
Recent Posts
{{/link-to}}
{{#link-to data-foo this.dynamicPath (query-params direction="desc" showArchived=false)}}
Recent Posts
{{/link-to}}
`;

expect(runTest('data-test-attributes.hbs', input, options)).toMatchInlineSnapshot(`
"
<XFoo @data-foo={{true}} />
<XFoo @data-test-selector={{true}} />
<XFoo @data-test-selector={{post.id}} />
<XFoo @label=\\"hi\\" @data-test-selector={{true}} />
<XFoo data-test-foo />
<XFoo @data-foo={{true}}>
block
</XFoo>
<XFoo @data-test-selector={{true}}>
block
</XFoo>
<XFoo @data-test-selector={{post.id}}>
block
</XFoo>
<Common::AccordionComponent data-test-accordion as |accordion|>
block
</Common::AccordionComponent>
<LinkTo @route=\\"posts\\" data-test-foo>
Recent Posts
</LinkTo>
<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>
Recent Posts
</LinkTo>

{{x-foo
data-foo
name=\\"Sophie\\"
}}
{{#x-foo data-foo}}
block
{{/x-foo}}
{{#common/accordion-component data-accordion as |accordion|}}
block
{{/common/accordion-component}}
{{#link-to data-foo \\"posts\\"}}
Recent Posts
{{/link-to}}
{{#link-to data-foo this.dynamicPath (query-params direction=\\"desc\\" showArchived=false)}}
Recent Posts
{{/link-to}}
"
`);
});

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