Skip to content

Commit 92bf746

Browse files
suchitadoshi1987tylerturdenpants
authored andcommitted
Fix issues when converting dasherized curly component invocation into angle bracket invocation (#183)
* Fix issues when converting dasherized curly component invocation into angle bracket invocation * remove stale console * fix indents * fix prettier * refactor to remove the need of transformNestedTagName function * unify capitalizedT
1 parent 76f8257 commit 92bf746

2 files changed

Lines changed: 36 additions & 19 deletions

File tree

transforms/angle-brackets/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,3 +1023,23 @@ test('hyphens with nested usage', () => {
10231023
"
10241024
`);
10251025
});
1026+
1027+
test('wallstreet.hbs', () => {
1028+
let input = `
1029+
{{#foo-bar$baz-bang/foo-bar::bang}}
1030+
<div ...attributes>
1031+
{{foo bar="baz"}}
1032+
</div>
1033+
{{/foo-bar$baz-bang/foo-bar::bang}}
1034+
`;
1035+
1036+
expect(runTest('wallstreet.hbs', input)).toMatchInlineSnapshot(`
1037+
"
1038+
<FooBar$BazBang::FooBar::Bang>
1039+
<div ...attributes>
1040+
<Foo @bar=\\"baz\\" />
1041+
</div>
1042+
</FooBar$BazBang::FooBar::Bang>
1043+
"
1044+
`);
1045+
});

transforms/angle-brackets/transform.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,31 @@ function isNestedComponentTagName(tagName) {
2828
}
2929

3030
/**
31-
* Returns a capitalized tagname for angle brackets syntax
31+
* Returns a transformed capitalized tagname for angle brackets syntax
3232
* {{my-component}} => MyComponent
3333
*/
34-
function capitalizedTagName(tagname) {
35-
return tagname
36-
.split('-')
37-
.map(s => {
38-
if (!s) return '-';
39-
return s[0].toUpperCase() + s.slice(1);
40-
})
41-
.join('');
42-
}
43-
4434
function transformTagName(tagName) {
35+
const SIMPLE_DASHERIZE_REGEXP = /[a-z]|\/|-/g;
36+
const ALPHA = /[A-Za-z0-9]/;
37+
4538
if (tagName.includes('.')) {
4639
return tagName;
4740
}
4841

49-
if (isNestedComponentTagName(tagName)) {
50-
return transformNestedTagName(tagName);
51-
}
42+
tagName = tagName.replace(SIMPLE_DASHERIZE_REGEXP, (char, index) => {
43+
if (char === '/') {
44+
return '::';
45+
}
5246

53-
return capitalizedTagName(tagName);
54-
}
47+
if (index === 0 || !ALPHA.test(tagName[index - 1])) {
48+
return char.toUpperCase();
49+
}
50+
51+
// Remove all occurances of '-'s from the tagName that aren't starting with `-`
52+
return char === '-' ? '' : char.toLowerCase();
53+
});
5554

56-
function transformNestedTagName(tagName) {
57-
const paths = tagName.split('/');
58-
return paths.map(name => capitalizedTagName(name)).join('::');
55+
return tagName;
5956
}
6057

6158
function transformNestedSubExpression(subExpression) {

0 commit comments

Comments
 (0)