From 1d58a2bf1716a7c5dd2bad68c15a7b04ba5a720f Mon Sep 17 00:00:00 2001 From: "marcin.piniarski" Date: Mon, 15 Jul 2024 19:03:09 +0200 Subject: [PATCH] feature: handle array as props list (#2465) --- lib/rules/define-props-declaration.js | 46 ++++++++++++++------- tests/lib/rules/define-props-declaration.js | 28 +++++++++++-- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/lib/rules/define-props-declaration.js b/lib/rules/define-props-declaration.js index 7f4e6348a..949b355c5 100644 --- a/lib/rules/define-props-declaration.js +++ b/lib/rules/define-props-declaration.js @@ -32,7 +32,9 @@ function* fixTypeBased(fixer, node, props, context) { } ) - const componentPropsTypeCode = `{ ${componentPropsTypes.join(PROPS_SEPARATOR)} }` + const componentPropsTypeCode = `{ ${componentPropsTypes.join( + PROPS_SEPARATOR + )} }` // remove defineProps function parameters yield fixer.replaceText(node.arguments[0], '') @@ -96,22 +98,34 @@ const mapNativeType = (nativeType) => { * @param {SourceCode} sourceCode */ function getComponentPropData(prop, sourceCode) { - if (prop.propName === null) { - throw new Error('Unexpected prop with null name.') - } - if (prop.type !== 'object') { - throw new Error(`Unexpected prop type: ${prop.type}.`) - } - const type = optionGetType(prop.value, sourceCode) - const required = optionGetRequired(prop.value) - const defaultValue = optionGetDefault(prop.value) - - return { - name: prop.propName, - type, - required, - defaultValue + if (prop.type === 'array') { + if (prop.node.type !== 'Identifier') { + throw new Error(`Unexpected prop type inside array: ${prop.node.type}`) + } + + return { + name: prop.node.name, + type: 'string', + required: false, + defaultValue: undefined + } + } else if (prop.type === 'object') { + if (prop.propName === null) { + throw new Error('Unexpected prop with null name.') + } + + const type = optionGetType(prop.value, sourceCode) + const required = optionGetRequired(prop.value) + const defaultValue = optionGetDefault(prop.value) + + return { + name: prop.propName, + type, + required, + defaultValue + } } + throw new Error(`Unexpected prop type: ${prop.type}.`) } /** diff --git a/tests/lib/rules/define-props-declaration.js b/tests/lib/rules/define-props-declaration.js index 4285f087d..38e700e65 100644 --- a/tests/lib/rules/define-props-declaration.js +++ b/tests/lib/rules/define-props-declaration.js @@ -365,7 +365,7 @@ tester.run('define-props-declaration', rule, { code: ` `, @@ -393,7 +393,7 @@ tester.run('define-props-declaration', rule, { code: ` `, @@ -633,6 +633,26 @@ tester.run('define-props-declaration', rule, { line: 3 } ] + }, + // array + { + filename: 'test.vue', + code: ` + + `, + output: ` + + `, + errors: [ + { + message: 'Use type-based declaration instead of runtime declaration.', + line: 3 + } + ] } ] })