Skip to content

Commit

Permalink
Fixed a bug that source code is broken by autofix of require-prop-typ…
Browse files Browse the repository at this point in the history
…e-constructor. (#1009)
  • Loading branch information
ota-meshi committed Dec 26, 2019
1 parent acb48eb commit 4476263
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
13 changes: 11 additions & 2 deletions lib/rules/require-prop-type-constructor.js
Expand Up @@ -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)
}
}

Expand Down
105 changes: 104 additions & 1 deletion tests/lib/rules/require-prop-type-constructor.js
Expand Up @@ -17,7 +17,7 @@ const RuleTester = require('eslint').RuleTester

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2018,
ecmaVersion: 2020,
sourceType: 'module'
}
})
Expand Down Expand Up @@ -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
}
]
}
]
})

0 comments on commit 4476263

Please sign in to comment.