Skip to content

Commit 62e2620

Browse files
Fix false positives for script setup with ts literal type in vue/require-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 <[email protected]> Co-authored-by: Flo Edelmann <[email protected]>
1 parent a473a0d commit 62e2620

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

lib/utils/ts-ast-utils.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,21 @@ function inferRuntimeType(context, node, checked = new Set()) {
260260
return ['Array']
261261

262262
case 'TSLiteralType':
263-
switch (node.literal.type) {
264-
//@ts-ignore ?
265-
case 'StringLiteral':
266-
return ['String']
267-
//@ts-ignore ?
268-
case 'BooleanLiteral':
269-
return ['Boolean']
270-
//@ts-ignore ?
271-
case 'NumericLiteral':
272-
//@ts-ignore ?
273-
// eslint-disable-next-line no-fallthrough
274-
case 'BigIntLiteral':
275-
return ['Number']
276-
default:
277-
return [`null`]
263+
if (node.literal.type === 'Literal') {
264+
switch (typeof node.literal.value) {
265+
case 'boolean':
266+
return ['Boolean']
267+
case 'string':
268+
return ['String']
269+
case 'number':
270+
case 'bigint':
271+
return ['Number']
272+
}
273+
if (node.literal.value instanceof RegExp) {
274+
return ['RegExp']
275+
}
278276
}
279-
277+
return [`null`]
280278
case 'TSTypeReference':
281279
if (node.typeName.type === 'Identifier') {
282280
const variable = findVariable(context.getScope(), node.typeName.name)

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

+23
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,29 @@ ruleTester.run('require-valid-default-prop', rule, {
252252
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
253253
parser: require.resolve('@typescript-eslint/parser'),
254254
errors: errorMessage('function')
255+
},
256+
{
257+
// https://github.com/vuejs/eslint-plugin-vue/issues/1853
258+
filename: 'test.vue',
259+
code: `<script setup lang="ts">
260+
export interface SomePropInterface {
261+
someProp?: false | string;
262+
str?: 'foo' | 'bar';
263+
num?: 1 | 2;
264+
}
265+
266+
withDefaults(defineProps<SomePropInterface>(), {
267+
someProp: false,
268+
str: 'foo',
269+
num: 1
270+
});
271+
</script>`,
272+
parserOptions: {
273+
ecmaVersion: 6,
274+
sourceType: 'module',
275+
parser: require.resolve('@typescript-eslint/parser')
276+
},
277+
parser: require.resolve('vue-eslint-parser')
255278
}
256279
],
257280

0 commit comments

Comments
 (0)