Skip to content

Commit 5abd174

Browse files
committed
Address PR Feedback
1 parent b3991f2 commit 5abd174

2 files changed

Lines changed: 60 additions & 94 deletions

File tree

lib/rules/template-modifier-name-case.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
/* eslint-disable unicorn/consistent-function-scoping, eslint-plugin/no-unused-message-ids */
1+
/* eslint-disable unicorn/consistent-function-scoping */
22

3-
function dasherize(str) {
4-
return str.replaceAll(/([A-Z])/g, '-$1').toLowerCase();
3+
const SIMPLE_DASHERIZE_REGEXP = /[A-Z]/g;
4+
const ALPHA = /[A-Za-z]/;
5+
6+
function dasherize(key) {
7+
return key
8+
.replaceAll(SIMPLE_DASHERIZE_REGEXP, (char, index) => {
9+
if (index === 0 || !ALPHA.test(key[index - 1])) {
10+
return char.toLowerCase();
11+
}
12+
return `-${char.toLowerCase()}`;
13+
})
14+
.replaceAll('::', '/');
515
}
616

717
/** @type {import('eslint').Rule.RuleModule} */
@@ -18,7 +28,7 @@ module.exports = {
1828
schema: [],
1929
messages: {
2030
dasherized:
21-
'Use dasherized names for modifier invocation. Please replace `{{dasherizeModifierName}}` with `{{dasherizeModifierName}}`.',
31+
'Use dasherized names for modifier invocation. Please replace `{{modifierName}}` with `{{dasherizedName}}`.',
2232
},
2333
originallyFrom: {
2434
name: 'ember-template-lint',
@@ -29,11 +39,6 @@ module.exports = {
2939
},
3040

3141
create(context) {
32-
function generateErrorMessage(modifierName) {
33-
const dasherizedName = dasherize(modifierName);
34-
return `Use dasherized names for modifier invocation. Please replace \`${modifierName}\` with \`${dasherizedName}\`.`;
35-
}
36-
3742
function isModifierHelper(node) {
3843
return (
3944
node.path && node.path.type === 'GlimmerPathExpression' && node.path.original === 'modifier'
@@ -45,11 +50,12 @@ module.exports = {
4550
const modifierName = node.path?.original;
4651

4752
if (typeof modifierName === 'string' && modifierName !== dasherize(modifierName)) {
53+
const dasherizedName = dasherize(modifierName);
4854
context.report({
4955
node,
50-
message: generateErrorMessage(modifierName),
56+
messageId: 'dasherized',
57+
data: { modifierName, dasherizedName },
5158
fix(fixer) {
52-
const dasherizedName = dasherize(modifierName);
5359
return fixer.replaceTextRange(node.path.range, dasherizedName);
5460
},
5561
});
@@ -67,11 +73,12 @@ module.exports = {
6773
const modifierName = nameParam.value;
6874

6975
if (typeof modifierName === 'string' && modifierName !== dasherize(modifierName)) {
76+
const dasherizedName = dasherize(modifierName);
7077
context.report({
7178
node: nameParam,
72-
message: generateErrorMessage(modifierName),
79+
messageId: 'dasherized',
80+
data: { modifierName, dasherizedName },
7381
fix(fixer) {
74-
const dasherizedName = dasherize(modifierName);
7582
return fixer.replaceTextRange(nameParam.range, `"${dasherizedName}"`);
7683
},
7784
});
@@ -90,11 +97,12 @@ module.exports = {
9097
const modifierName = nameParam.value;
9198

9299
if (typeof modifierName === 'string' && modifierName !== dasherize(modifierName)) {
100+
const dasherizedName = dasherize(modifierName);
93101
context.report({
94102
node: nameParam,
95-
message: generateErrorMessage(modifierName),
103+
messageId: 'dasherized',
104+
data: { modifierName, dasherizedName },
96105
fix(fixer) {
97-
const dasherizedName = dasherize(modifierName);
98106
return fixer.replaceTextRange(nameParam.range, `"${dasherizedName}"`);
99107
},
100108
});
@@ -104,4 +112,4 @@ module.exports = {
104112
};
105113
},
106114
};
107-
/* eslint-enable unicorn/consistent-function-scoping, eslint-plugin/no-unused-message-ids */
115+
/* eslint-enable unicorn/consistent-function-scoping */

tests/lib/rules/template-modifier-name-case.js

Lines changed: 36 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -29,85 +29,62 @@ ruleTester.run('template-modifier-name-case', rule, {
2929
{
3030
code: '<template><div {{didInsert}}></div></template>',
3131
output: '<template><div {{did-insert}}></div></template>',
32-
errors: [
33-
{
34-
message:
35-
'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.',
36-
},
37-
],
32+
errors: [{ messageId: 'dasherized' }],
3833
},
3934
{
4035
code: '<template><div {{doSomething}}></div></template>',
4136
output: '<template><div {{do-something}}></div></template>',
42-
errors: [
43-
{
44-
message:
45-
'Use dasherized names for modifier invocation. Please replace `doSomething` with `do-something`.',
46-
},
47-
],
37+
errors: [{ messageId: 'dasherized' }],
4838
},
4939
{
5040
code: '<template><div {{fooBar}}></div></template>',
5141
output: '<template><div {{foo-bar}}></div></template>',
52-
errors: [
53-
{
54-
message:
55-
'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.',
56-
},
57-
],
42+
errors: [{ messageId: 'dasherized' }],
43+
},
44+
// PascalCase: index-0 guard prevents leading dash
45+
{
46+
code: '<template><div {{FooBar}}></div></template>',
47+
output: '<template><div {{foo-bar}}></div></template>',
48+
errors: [{ messageId: 'dasherized' }],
49+
},
50+
{
51+
code: '<template><div {{XFoo}}></div></template>',
52+
output: '<template><div {{x-foo}}></div></template>',
53+
errors: [{ messageId: 'dasherized' }],
54+
},
55+
// Namespaced :: → /
56+
{
57+
code: '<template><div {{Foo::barBaz}}></div></template>',
58+
output: '<template><div {{foo/bar-baz}}></div></template>',
59+
errors: [{ messageId: 'dasherized' }],
5860
},
5961
{
6062
code: '<template><div {{(modifier "didInsert")}}></div></template>',
6163
output: '<template><div {{(modifier "did-insert")}}></div></template>',
62-
errors: [
63-
{
64-
message:
65-
'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.',
66-
},
67-
],
64+
errors: [{ messageId: 'dasherized' }],
6865
},
6966

7067
{
7168
code: '<template><div class="monkey" {{didInsert "something" with="somethingElse"}}></div></template>',
7269
output:
7370
'<template><div class="monkey" {{did-insert "something" with="somethingElse"}}></div></template>',
74-
errors: [
75-
{
76-
message:
77-
'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.',
78-
},
79-
],
71+
errors: [{ messageId: 'dasherized' }],
8072
},
8173
{
8274
code: '<template><a href="#" onclick={{amazingActionThing "foo"}} {{doSomething}}></a></template>',
8375
output:
8476
'<template><a href="#" onclick={{amazingActionThing "foo"}} {{do-something}}></a></template>',
85-
errors: [
86-
{
87-
message:
88-
'Use dasherized names for modifier invocation. Please replace `doSomething` with `do-something`.',
89-
},
90-
],
77+
errors: [{ messageId: 'dasherized' }],
9178
},
9279
{
9380
code: '<template><div {{(modifier "fooBar")}}></div></template>',
9481
output: '<template><div {{(modifier "foo-bar")}}></div></template>',
95-
errors: [
96-
{
97-
message:
98-
'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.',
99-
},
100-
],
82+
errors: [{ messageId: 'dasherized' }],
10183
},
10284
{
10385
code: '<template><div {{(if this.foo (modifier "fooBar"))}}></div></template>',
10486
output: '<template><div {{(if this.foo (modifier "foo-bar"))}}></div></template>',
105-
errors: [
106-
{
107-
message:
108-
'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.',
109-
},
110-
],
87+
errors: [{ messageId: 'dasherized' }],
11188
},
11289
],
11390
});
@@ -138,52 +115,33 @@ hbsRuleTester.run('template-modifier-name-case', rule, {
138115
{
139116
code: '<div {{didInsert}}></div>',
140117
output: '<div {{did-insert}}></div>',
141-
errors: [
142-
{
143-
message:
144-
'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.',
145-
},
146-
],
118+
errors: [{ messageId: 'dasherized' }],
147119
},
148120
{
149121
code: '<div class="monkey" {{didInsert "something" with="somethingElse"}}></div>',
150122
output: '<div class="monkey" {{did-insert "something" with="somethingElse"}}></div>',
151-
errors: [
152-
{
153-
message:
154-
'Use dasherized names for modifier invocation. Please replace `didInsert` with `did-insert`.',
155-
},
156-
],
123+
errors: [{ messageId: 'dasherized' }],
124+
},
125+
// PascalCase: index-0 guard prevents leading dash
126+
{
127+
code: '<div {{FooBar}}></div>',
128+
output: '<div {{foo-bar}}></div>',
129+
errors: [{ messageId: 'dasherized' }],
157130
},
158131
{
159132
code: '<a href="#" onclick={{amazingActionThing "foo"}} {{doSomething}}></a>',
160133
output: '<a href="#" onclick={{amazingActionThing "foo"}} {{do-something}}></a>',
161-
errors: [
162-
{
163-
message:
164-
'Use dasherized names for modifier invocation. Please replace `doSomething` with `do-something`.',
165-
},
166-
],
134+
errors: [{ messageId: 'dasherized' }],
167135
},
168136
{
169137
code: '<div {{(modifier "fooBar")}}></div>',
170138
output: '<div {{(modifier "foo-bar")}}></div>',
171-
errors: [
172-
{
173-
message:
174-
'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.',
175-
},
176-
],
139+
errors: [{ messageId: 'dasherized' }],
177140
},
178141
{
179142
code: '<div {{(if this.foo (modifier "fooBar"))}}></div>',
180143
output: '<div {{(if this.foo (modifier "foo-bar"))}}></div>',
181-
errors: [
182-
{
183-
message:
184-
'Use dasherized names for modifier invocation. Please replace `fooBar` with `foo-bar`.',
185-
},
186-
],
144+
errors: [{ messageId: 'dasherized' }],
187145
},
188146
],
189147
});

0 commit comments

Comments
 (0)