Skip to content

Commit 7e06914

Browse files
authored
Add config to enable transform on valueless attrs (#241)
* Add config to enable transforming data-test-* attributes * Separate transformed nodes from skipped nodes and add test cases * Use params and startWith in shouldSkipDataTestParams * Add test case with both data-test and data-* attrs * Change "data-test" to "data-test-"
1 parent 0b06732 commit 7e06914

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

transforms/angle-brackets/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function getOptions() {
2525
options.skipFilesThatMatchRegex = new RegExp(config.skipFilesThatMatchRegex);
2626
}
2727

28+
options.includeValuelessDataTestAttributes = !!config.includeValuelessDataTestAttributes;
2829
options.skipBuiltInComponents = !!config.skipBuiltInComponents;
2930
}
3031

transforms/angle-brackets/transform.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,21 @@ function hasValuelessDataParams(params) {
280280
return getDataAttributesFromParams(params).length > 0;
281281
}
282282

283+
/**
284+
*
285+
* data-* attributes are generally omitted,
286+
* but this config allows including nodes with data-test-* attributes.
287+
*/
288+
function shouldSkipDataTestParams(params, includeValuelessDataTestAttributes) {
289+
if (includeValuelessDataTestAttributes) {
290+
const dataAttrs = getDataAttributesFromParams(params);
291+
// This is true for nodes with data-* attributes too,
292+
// as long as there is one with data-test-* attribute.
293+
return !dataAttrs.some(attr => attr.original.startsWith('data-test-'));
294+
}
295+
return true;
296+
}
297+
283298
function transformNodeAttributes(tagName, node, config) {
284299
let attributes = transformAttrs(tagName, node.hash.pairs, config);
285300
return node.params.concat(attributes);
@@ -338,7 +353,10 @@ function nodeHasPositionalParameters(node) {
338353
}
339354

340355
function transformNode(node, fileInfo, config) {
341-
if (hasValuelessDataParams(node.params)) {
356+
if (
357+
hasValuelessDataParams(node.params) &&
358+
shouldSkipDataTestParams(node.params, config.includeValuelessDataTestAttributes)
359+
) {
342360
return;
343361
}
344362
let selfClosing = node.type !== 'BlockStatement';

transforms/angle-brackets/transform.test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,105 @@ test('data-attributes', () => {
144144
`);
145145
});
146146

147+
test('data-test-attributes', () => {
148+
let options = {
149+
includeValuelessDataTestAttributes: true,
150+
};
151+
let input = `
152+
{{x-foo data-foo=true}}
153+
{{x-foo data-test-selector=true}}
154+
{{x-foo data-test-selector=post.id}}
155+
{{x-foo label="hi" data-test-selector=true}}
156+
{{x-foo data-test-foo }}
157+
{{#x-foo data-foo=true}}
158+
block
159+
{{/x-foo}}
160+
{{#x-foo data-test-selector=true}}
161+
block
162+
{{/x-foo}}
163+
{{#x-foo data-test-selector=post.id}}
164+
block
165+
{{/x-foo}}
166+
{{#common/accordion-component data-test-accordion as |accordion|}}
167+
block
168+
{{/common/accordion-component}}
169+
{{#link-to data-test-foo "posts"}}
170+
Recent Posts
171+
{{/link-to}}
172+
{{#link-to data-test-foo this.dynamicPath (query-params direction="desc" showArchived=false)}}
173+
Recent Posts
174+
{{/link-to}}
175+
{{#link-to data-test-foo data-foo this.dynamicPath (query-params direction="desc" showArchived=false)}}
176+
Recent Posts
177+
{{/link-to}}
178+
179+
{{x-foo
180+
data-foo
181+
name="Sophie"
182+
}}
183+
{{#x-foo data-foo}}
184+
block
185+
{{/x-foo}}
186+
{{#common/accordion-component data-accordion as |accordion|}}
187+
block
188+
{{/common/accordion-component}}
189+
{{#link-to data-foo "posts"}}
190+
Recent Posts
191+
{{/link-to}}
192+
{{#link-to data-foo this.dynamicPath (query-params direction="desc" showArchived=false)}}
193+
Recent Posts
194+
{{/link-to}}
195+
`;
196+
197+
expect(runTest('data-test-attributes.hbs', input, options)).toMatchInlineSnapshot(`
198+
"
199+
<XFoo @data-foo={{true}} />
200+
<XFoo @data-test-selector={{true}} />
201+
<XFoo @data-test-selector={{post.id}} />
202+
<XFoo @label=\\"hi\\" @data-test-selector={{true}} />
203+
<XFoo data-test-foo />
204+
<XFoo @data-foo={{true}}>
205+
block
206+
</XFoo>
207+
<XFoo @data-test-selector={{true}}>
208+
block
209+
</XFoo>
210+
<XFoo @data-test-selector={{post.id}}>
211+
block
212+
</XFoo>
213+
<Common::AccordionComponent data-test-accordion as |accordion|>
214+
block
215+
</Common::AccordionComponent>
216+
<LinkTo @route=\\"posts\\" data-test-foo>
217+
Recent Posts
218+
</LinkTo>
219+
<LinkTo @route={{this.dynamicPath}} @query={{hash direction=\\"desc\\" showArchived=false}} data-test-foo>
220+
Recent Posts
221+
</LinkTo>
222+
<LinkTo @route={{this.dynamicPath}} @query={{hash direction=\\"desc\\" showArchived=false}} data-test-foo data-foo>
223+
Recent Posts
224+
</LinkTo>
225+
226+
{{x-foo
227+
data-foo
228+
name=\\"Sophie\\"
229+
}}
230+
{{#x-foo data-foo}}
231+
block
232+
{{/x-foo}}
233+
{{#common/accordion-component data-accordion as |accordion|}}
234+
block
235+
{{/common/accordion-component}}
236+
{{#link-to data-foo \\"posts\\"}}
237+
Recent Posts
238+
{{/link-to}}
239+
{{#link-to data-foo this.dynamicPath (query-params direction=\\"desc\\" showArchived=false)}}
240+
Recent Posts
241+
{{/link-to}}
242+
"
243+
`);
244+
});
245+
147246
test('deeply-nested-sub', () => {
148247
let input = `
149248
{{#some-component class=(concat foo (some-helper ted (some-dude bar (a b c)))) }}

0 commit comments

Comments
 (0)