Skip to content

Commit

Permalink
Fix false positives for script setup with ts literal type in `vue/req…
Browse files Browse the repository at this point in the history
…uire-valid-default-prop` (#1854)

* Fix false positives for script setup with ts literal type in `vue/require-valid-default-prop`

* remove dupe test

* Add testcase

* Update tests/lib/rules/require-valid-default-prop.js

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>
  • Loading branch information
ota-meshi and FloEdelmann committed Apr 20, 2022
1 parent a473a0d commit 62e2620
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
30 changes: 14 additions & 16 deletions lib/utils/ts-ast-utils.js
Expand Up @@ -260,23 +260,21 @@ function inferRuntimeType(context, node, checked = new Set()) {
return ['Array']

case 'TSLiteralType':
switch (node.literal.type) {
//@ts-ignore ?
case 'StringLiteral':
return ['String']
//@ts-ignore ?
case 'BooleanLiteral':
return ['Boolean']
//@ts-ignore ?
case 'NumericLiteral':
//@ts-ignore ?
// eslint-disable-next-line no-fallthrough
case 'BigIntLiteral':
return ['Number']
default:
return [`null`]
if (node.literal.type === 'Literal') {
switch (typeof node.literal.value) {
case 'boolean':
return ['Boolean']
case 'string':
return ['String']
case 'number':
case 'bigint':
return ['Number']
}
if (node.literal.value instanceof RegExp) {
return ['RegExp']
}
}

return [`null`]
case 'TSTypeReference':
if (node.typeName.type === 'Identifier') {
const variable = findVariable(context.getScope(), node.typeName.name)
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/require-valid-default-prop.js
Expand Up @@ -252,6 +252,29 @@ ruleTester.run('require-valid-default-prop', rule, {
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
parser: require.resolve('@typescript-eslint/parser'),
errors: errorMessage('function')
},
{
// https://github.com/vuejs/eslint-plugin-vue/issues/1853
filename: 'test.vue',
code: `<script setup lang="ts">
export interface SomePropInterface {
someProp?: false | string;
str?: 'foo' | 'bar';
num?: 1 | 2;
}
withDefaults(defineProps<SomePropInterface>(), {
someProp: false,
str: 'foo',
num: 1
});
</script>`,
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
parser: require.resolve('@typescript-eslint/parser')
},
parser: require.resolve('vue-eslint-parser')
}
],

Expand Down

0 comments on commit 62e2620

Please sign in to comment.