Skip to content

Commit 21d0c11

Browse files
suchitadoshi1987tylerturdenpants
authored andcommitted
Accept skipAttributesThatMatchRegex option values as an array of regex strings (#229)
* Accept the option as an array of regex strings * add test to cover invalid case
1 parent 905b914 commit 21d0c11

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ For example, with the configuration below, all attributes that matches either `/
138138
{
139139
"helpers": [],
140140
"skipBuiltInComponents": true,
141-
"skipAttributesThatMatchRegex": [/data-/gim, /aria-/gim]
141+
"skipAttributesThatMatchRegex": ["/data-/gim", "/aria-/gim"]
142142
}
143143
```
144144

transforms/angle-brackets/transform.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ function isAttribute(key) {
2525
*/
2626
function shouldSkipAttribute(key, config) {
2727
if (config.skipAttributesThatMatchRegex && config.skipAttributesThatMatchRegex.length) {
28-
return config.skipAttributesThatMatchRegex.some(rx => rx.test(key));
28+
return config.skipAttributesThatMatchRegex.some(rx => {
29+
// Get the user provided string and convert it to regex.
30+
const match = /^\/(.*)\/([a-z]*)$/.exec(rx);
31+
if (match) {
32+
const regex = new RegExp(match[1], match[2]);
33+
return regex.test(key);
34+
}
35+
});
2936
}
3037
return false;
3138
}

transforms/angle-brackets/transform.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ test('skip-attributes', () => {
957957
`;
958958

959959
let options = {
960-
skipAttributesThatMatchRegex: [/data-/gim, /aria-/gim],
960+
skipAttributesThatMatchRegex: ['/data-/gim', '/aria-/gim'],
961961
};
962962

963963
expect(runTest('ignore-attributes.hbs', input, options)).toMatchInlineSnapshot(`
@@ -967,6 +967,22 @@ test('skip-attributes', () => {
967967
`);
968968
});
969969

970+
test('skip-attributes with invalid regex', () => {
971+
let input = `
972+
{{some-component data-test-foo=true aria-label="bar" foo=true}}
973+
`;
974+
975+
let options = {
976+
skipAttributesThatMatchRegex: [null],
977+
};
978+
979+
expect(runTest('ignore-attributes.hbs', input, options)).toMatchInlineSnapshot(`
980+
"
981+
<SomeComponent @data-test-foo={{true}} @aria-label=\\"bar\\" @foo={{true}} />
982+
"
983+
`);
984+
});
985+
970986
test('regex-options', () => {
971987
let input = `
972988
{{some-component foo=true}}

0 commit comments

Comments
 (0)