Skip to content

Commit 4476263

Browse files
authored
Fixed a bug that source code is broken by autofix of require-prop-type-constructor. (#1009)
1 parent acb48eb commit 4476263

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed

lib/rules/require-prop-type-constructor.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,23 @@ module.exports = {
3535

3636
create (context) {
3737
const fix = node => fixer => {
38+
let newText
3839
if (node.type === 'Literal') {
39-
return fixer.replaceText(node, node.value)
40+
if (typeof node.value !== 'string') {
41+
return undefined
42+
}
43+
newText = node.value
4044
} else if (
4145
node.type === 'TemplateLiteral' &&
4246
node.expressions.length === 0 &&
4347
node.quasis.length === 1
4448
) {
45-
return fixer.replaceText(node, node.quasis[0].value.cooked)
49+
newText = node.quasis[0].value.cooked
50+
} else {
51+
return undefined
52+
}
53+
if (newText) {
54+
return fixer.replaceText(node, newText)
4655
}
4756
}
4857

tests/lib/rules/require-prop-type-constructor.js

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const RuleTester = require('eslint').RuleTester
1717

1818
const ruleTester = new RuleTester({
1919
parserOptions: {
20-
ecmaVersion: 2018,
20+
ecmaVersion: 2020,
2121
sourceType: 'module'
2222
}
2323
})
@@ -237,6 +237,109 @@ ruleTester.run('require-prop-type-constructor', rule, {
237237
line: 4
238238
}],
239239
parser: require.resolve('@typescript-eslint/parser')
240+
},
241+
{
242+
filename: 'LiteralsComponent.vue',
243+
code: `
244+
export default {
245+
props: {
246+
str: 'String',
247+
str2: 'a',
248+
emptyStr: '',
249+
number: 1000,
250+
binumber: 0b10000000000000000000000000000000,
251+
hexnumber: 0x123456789ABCDEF,
252+
exp1: 1E3,
253+
exp2: 2e6,
254+
exp3: 0.1e2,
255+
bigInt: 9007199254740991n,
256+
boolean: true,
257+
'null': null,
258+
regex: /a/,
259+
template: \`String\`,
260+
emptyTemplate: \`\`,
261+
}
262+
}
263+
`,
264+
output: `
265+
export default {
266+
props: {
267+
str: String,
268+
str2: a,
269+
emptyStr: '',
270+
number: 1000,
271+
binumber: 0b10000000000000000000000000000000,
272+
hexnumber: 0x123456789ABCDEF,
273+
exp1: 1E3,
274+
exp2: 2e6,
275+
exp3: 0.1e2,
276+
bigInt: 9007199254740991n,
277+
boolean: true,
278+
'null': null,
279+
regex: /a/,
280+
template: String,
281+
emptyTemplate: \`\`,
282+
}
283+
}
284+
`,
285+
errors: [
286+
{
287+
message: 'The "str" property should be a constructor.',
288+
line: 4
289+
},
290+
{
291+
message: 'The "str2" property should be a constructor.',
292+
line: 5
293+
},
294+
{
295+
message: 'The "emptyStr" property should be a constructor.',
296+
line: 6
297+
},
298+
{
299+
message: 'The "number" property should be a constructor.',
300+
line: 7
301+
},
302+
{
303+
message: 'The "binumber" property should be a constructor.',
304+
line: 8
305+
},
306+
{
307+
message: 'The "hexnumber" property should be a constructor.',
308+
line: 9
309+
},
310+
{
311+
message: 'The "exp1" property should be a constructor.',
312+
line: 10
313+
},
314+
{
315+
message: 'The "exp2" property should be a constructor.',
316+
line: 11
317+
},
318+
{
319+
message: 'The "exp3" property should be a constructor.',
320+
line: 12
321+
},
322+
{
323+
message: 'The "bigInt" property should be a constructor.',
324+
line: 13
325+
},
326+
{
327+
message: 'The "boolean" property should be a constructor.',
328+
line: 14
329+
},
330+
{
331+
message: 'The "regex" property should be a constructor.',
332+
line: 16
333+
},
334+
{
335+
message: 'The "template" property should be a constructor.',
336+
line: 17
337+
},
338+
{
339+
message: 'The "emptyTemplate" property should be a constructor.',
340+
line: 18
341+
}
342+
]
240343
}
241344
]
242345
})

0 commit comments

Comments
 (0)