diff --git a/lib/rules/require-prop-type-constructor.js b/lib/rules/require-prop-type-constructor.js index edde36f49..b0b312615 100644 --- a/lib/rules/require-prop-type-constructor.js +++ b/lib/rules/require-prop-type-constructor.js @@ -35,14 +35,23 @@ module.exports = { create (context) { const fix = node => fixer => { + let newText if (node.type === 'Literal') { - return fixer.replaceText(node, node.value) + if (typeof node.value !== 'string') { + return undefined + } + newText = node.value } else if ( node.type === 'TemplateLiteral' && node.expressions.length === 0 && node.quasis.length === 1 ) { - return fixer.replaceText(node, node.quasis[0].value.cooked) + newText = node.quasis[0].value.cooked + } else { + return undefined + } + if (newText) { + return fixer.replaceText(node, newText) } } diff --git a/tests/lib/rules/require-prop-type-constructor.js b/tests/lib/rules/require-prop-type-constructor.js index 70fc35e8e..103eb26a1 100644 --- a/tests/lib/rules/require-prop-type-constructor.js +++ b/tests/lib/rules/require-prop-type-constructor.js @@ -17,7 +17,7 @@ const RuleTester = require('eslint').RuleTester const ruleTester = new RuleTester({ parserOptions: { - ecmaVersion: 2018, + ecmaVersion: 2020, sourceType: 'module' } }) @@ -237,6 +237,109 @@ ruleTester.run('require-prop-type-constructor', rule, { line: 4 }], parser: require.resolve('@typescript-eslint/parser') + }, + { + filename: 'LiteralsComponent.vue', + code: ` + export default { + props: { + str: 'String', + str2: 'a', + emptyStr: '', + number: 1000, + binumber: 0b10000000000000000000000000000000, + hexnumber: 0x123456789ABCDEF, + exp1: 1E3, + exp2: 2e6, + exp3: 0.1e2, + bigInt: 9007199254740991n, + boolean: true, + 'null': null, + regex: /a/, + template: \`String\`, + emptyTemplate: \`\`, + } + } + `, + output: ` + export default { + props: { + str: String, + str2: a, + emptyStr: '', + number: 1000, + binumber: 0b10000000000000000000000000000000, + hexnumber: 0x123456789ABCDEF, + exp1: 1E3, + exp2: 2e6, + exp3: 0.1e2, + bigInt: 9007199254740991n, + boolean: true, + 'null': null, + regex: /a/, + template: String, + emptyTemplate: \`\`, + } + } + `, + errors: [ + { + message: 'The "str" property should be a constructor.', + line: 4 + }, + { + message: 'The "str2" property should be a constructor.', + line: 5 + }, + { + message: 'The "emptyStr" property should be a constructor.', + line: 6 + }, + { + message: 'The "number" property should be a constructor.', + line: 7 + }, + { + message: 'The "binumber" property should be a constructor.', + line: 8 + }, + { + message: 'The "hexnumber" property should be a constructor.', + line: 9 + }, + { + message: 'The "exp1" property should be a constructor.', + line: 10 + }, + { + message: 'The "exp2" property should be a constructor.', + line: 11 + }, + { + message: 'The "exp3" property should be a constructor.', + line: 12 + }, + { + message: 'The "bigInt" property should be a constructor.', + line: 13 + }, + { + message: 'The "boolean" property should be a constructor.', + line: 14 + }, + { + message: 'The "regex" property should be a constructor.', + line: 16 + }, + { + message: 'The "template" property should be a constructor.', + line: 17 + }, + { + message: 'The "emptyTemplate" property should be a constructor.', + line: 18 + } + ] } ] })