Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed an issue that code was broken by autofix of require-prop-type-constructor #1009

Merged
merged 1 commit into from Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}
]
}
]
})