Skip to content

Commit 6d79f93

Browse files
[Feature]: Add ability to convert only specific component invocations with each run (#437)
* config option to only operate on certain component names * add missing config setting and add documentation
1 parent 556b9e0 commit 6d79f93

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ Output:
179179
<SomeComponent data-test-foo={{true}} aria-label="bar" @foo={{true}} />
180180
```
181181

182+
### Converting specific components only
183+
184+
If you would like to only convert certain component invocations to use the angle brackets syntax, use the `components` configuration setting and specify component names. For example, with the configuration below, only the `{{baz}}` and `{{bat}}` components will be converted, leaving everything else intact.
185+
186+
**config/anglebrackets-codemod-config.json**
187+
188+
```js
189+
{
190+
"components": ["baz", "bat"]
191+
}
192+
```
193+
182194
## Debugging Workflow
183195

184196
Oftentimes, you want to debug the codemod or the transform to identify issues with the code or to understand

transforms/angle-brackets/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ function getOptions() {
1717
options.helpers = config.helpers;
1818
}
1919

20+
if (config.components) {
21+
options.components = config.components;
22+
}
23+
2024
if (config.skipAttributesThatMatchRegex) {
2125
options.skipAttributesThatMatchRegex = config.skipAttributesThatMatchRegex;
2226
}

transforms/angle-brackets/transform.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ function transformToAngleBracket(fileInfo, config, invokableData) {
445445
return {
446446
MustacheStatement(node) {
447447
const tagName = `${node.path && node.path.original}`;
448+
449+
if (config.components && !config.components.includes(tagName)) return;
450+
448451
// Don't change attribute statements
449452
const isValidMustache =
450453
node.loc.source !== '(synthetic)' &&
@@ -460,6 +463,9 @@ function transformToAngleBracket(fileInfo, config, invokableData) {
460463
},
461464
BlockStatement(node) {
462465
let tagName = `${node.path.original}`;
466+
467+
if (config.components && !config.components.includes(tagName)) return;
468+
463469
if (
464470
!shouldIgnoreMustacheStatement(node.path.original, config, invokableData) ||
465471
isWallStreet(tagName)

transforms/angle-brackets/transform.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,32 @@ test('custom-options', () => {
10501050
`);
10511051
});
10521052

1053+
test('specific-components', () => {
1054+
let input = `
1055+
{{some-component foo=true}}
1056+
{{my-component foo=true}}
1057+
{{x-foo foo=true}}
1058+
{{#my-card as |card|}}
1059+
{{card.title title="My Card Title"}}
1060+
{{/my-card}}
1061+
`;
1062+
1063+
let options = {
1064+
components: ['some-component', 'x-foo', 'my-card'],
1065+
};
1066+
1067+
expect(runTest('specific-components.hbs', input, options)).toMatchInlineSnapshot(`
1068+
"
1069+
<SomeComponent @foo={{true}} />
1070+
{{my-component foo=true}}
1071+
<XFoo @foo={{true}} />
1072+
<MyCard as |card|>
1073+
{{card.title title=\\"My Card Title\\"}}
1074+
</MyCard>
1075+
"
1076+
`);
1077+
});
1078+
10531079
test('skip-attributes', () => {
10541080
let input = `
10551081
{{some-component data-test-foo=true aria-label="bar" foo=true}}

0 commit comments

Comments
 (0)