Skip to content

Commit 9fcf4a6

Browse files
Update: add fixer for test-case-shorthand-strings
1 parent e2e3de3 commit 9fcf4a6

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

Diff for: lib/rules/test-case-shorthand-strings.js

+11
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ module.exports = {
1919
recommended: false,
2020
},
2121
schema: [{ enum: ['as-needed', 'never', 'consistent', 'consistent-as-needed'] }],
22+
fixable: 'code',
2223
},
2324

2425
create (context) {
2526
const shorthandOption = context.options[0] || 'as-needed';
27+
const sourceCode = context.getSourceCode();
28+
2629
// ----------------------------------------------------------------------
2730
// Helpers
2831
// ----------------------------------------------------------------------
@@ -63,6 +66,14 @@ module.exports = {
6366
preferred: badCaseInfo.shorthand ? 'an object' : 'a string',
6467
actual: badCaseInfo.shorthand ? 'a string' : 'an object',
6568
},
69+
fix (fixer) {
70+
return fixer.replaceText(
71+
badCaseInfo.node,
72+
badCaseInfo.shorthand
73+
? `{code: ${sourceCode.getText(badCaseInfo.node)}}`
74+
: sourceCode.getText(badCaseInfo.node.properties[0].value)
75+
);
76+
},
6677
});
6778
});
6879
}

Diff for: tests/lib/rules/test-case-shorthand-strings.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -126,71 +126,79 @@ ruleTester.run('test-case-shorthand-strings', rule, {
126126
// as-needed
127127
{
128128
code: getTestCases(['{ code: "foo" }']),
129+
output: getTestCases(['"foo"']),
129130
errors: [EXPECTED_SHORTHAND_ERROR],
130131
},
131132
{
132133
code: getTestCases(['{ code: `foo` }']),
134+
output: getTestCases(['`foo`']),
133135
errors: [EXPECTED_SHORTHAND_ERROR],
134136
},
135137
{
136138
code: getTestCases(['"foo"', '{ code: `foo` }']),
139+
output: getTestCases(['"foo"', '`foo`']),
137140
errors: [EXPECTED_SHORTHAND_ERROR],
138141
},
139142
{
140143
code: getTestCases(['"foo"', '{ code: "foo" }', '{ code: `bar` }']),
144+
output: getTestCases(['"foo"', '"foo"', '`bar`']),
141145
errors: [EXPECTED_SHORTHAND_ERROR, EXPECTED_SHORTHAND_ERROR],
142146
},
143147

144148
// never
145149
{
146150
code: getTestCases(['"foo"']),
151+
output: getTestCases(['{code: "foo"}']),
147152
options: ['never'],
148153
errors: [UNEXPECTED_SHORTHAND_ERROR],
149154
},
150155
{
151156
code: getTestCases(['foo', '"bar"']),
152-
options: ['never'],
153-
errors: [UNEXPECTED_SHORTHAND_ERROR],
154-
},
155-
{
156-
code: getTestCases(['foo', '"bar"']),
157+
output: getTestCases(['foo', '{code: "bar"}']),
157158
options: ['never'],
158159
errors: [UNEXPECTED_SHORTHAND_ERROR],
159160
},
160161
{
161162
code: getTestCases(['`foo`']),
163+
output: getTestCases(['{code: `foo`}']),
162164
options: ['never'],
163165
errors: [UNEXPECTED_SHORTHAND_ERROR],
164166
},
165167
{
166168
code: getTestCases(['"foo"']) + getTestCases(['"foo"']),
169+
output: getTestCases(['{code: "foo"}']) + getTestCases(['{code: "foo"}']),
167170
options: ['never'],
168171
errors: [UNEXPECTED_SHORTHAND_ERROR, UNEXPECTED_SHORTHAND_ERROR],
169172
},
170173

171174
// consistent
172175
{
173176
code: getTestCases(['"foo"', '{ code: "bar" }', '{ code: "baz" }']),
177+
output: getTestCases(['"foo"', '"bar"', '"baz"']),
174178
errors: [EXPECTED_SHORTHAND_ERROR, EXPECTED_SHORTHAND_ERROR],
175179
},
176180
{
177181
code: getTestCases(['{ code: "bar" }', '"foo"', '{ code: "baz" }']),
182+
output: getTestCases(['"bar"', '"foo"', '"baz"']),
178183
errors: [EXPECTED_SHORTHAND_ERROR, EXPECTED_SHORTHAND_ERROR],
179184
},
180185

181186
// consistent-as-needed
182187
{
183188
code: getTestCases(['{ code: "foo" }', '{ code: "bar" }']),
189+
output: getTestCases(['"foo"', '"bar"']),
184190
options: ['consistent-as-needed'],
185191
errors: [EXPECTED_SHORTHAND_ERROR, EXPECTED_SHORTHAND_ERROR],
186192
},
187193
{
188194
code: getTestCases(['"foo"', '"bar"', '{ code: "baz", options: ["foo"] }']),
195+
output: getTestCases(['{code: "foo"}', '{code: "bar"}', '{ code: "baz", options: ["foo"] }']),
189196
options: ['consistent-as-needed'],
190197
errors: [UNEXPECTED_SHORTHAND_ERROR, UNEXPECTED_SHORTHAND_ERROR],
191198
},
192199
{
193200
code: getTestCases(['"foo"', '{ code: "baz", options: ["foo"] }', '"bar"']),
201+
output: getTestCases(['{code: "foo"}', '{ code: "baz", options: ["foo"] }', '{code: "bar"}']),
194202
options: ['consistent-as-needed'],
195203
errors: [UNEXPECTED_SHORTHAND_ERROR, UNEXPECTED_SHORTHAND_ERROR],
196204
},

0 commit comments

Comments
 (0)