From 04794a68e9039892e39423cb71e22dab92f0bdde Mon Sep 17 00:00:00 2001 From: chenwanyue Date: Fri, 5 Jul 2024 11:00:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20props=E6=94=AF=E6=8C=81ts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/rules/valid-properties.md | 10 ++++++++++ lib/rules/valid-properties.js | 3 ++- tests/lib/rules/valid-properties.js | 10 +++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/rules/valid-properties.md b/docs/rules/valid-properties.md index c80a5c1..56b98c0 100644 --- a/docs/rules/valid-properties.md +++ b/docs/rules/valid-properties.md @@ -35,6 +35,16 @@ description: 校验properties有效值 } } }) + // ✓ PropType ts + createComponent({ + properties: { + propsA: { + type: String as PropType, + value: "" + }, + propsB: Object as PropType + } + }) // ✗ BAD createComponent({ diff --git a/lib/rules/valid-properties.js b/lib/rules/valid-properties.js index ea20bcd..1cd097d 100644 --- a/lib/rules/valid-properties.js +++ b/lib/rules/valid-properties.js @@ -82,7 +82,7 @@ function validProp(node, context, allowKeys) { } }) } - } else if (node.value.type !== 'Identifier') { + } else if (!(node.value.type === 'Identifier' || node.value.type ==='TSAsExpression')) { return context.report({ node, message: "Invalid value for '{{propName}}'.", @@ -142,6 +142,7 @@ module.exports = { return utils.defineMpxVisitor(context, { onMpxObjectEnter(obj) { for (const prop of utils.getComponentPropsFromOptions(obj)) { + console.log('cwy-prop', prop, obj) prop.type === 'object' && validProp(prop.node, context, allowKeys) } } diff --git a/tests/lib/rules/valid-properties.js b/tests/lib/rules/valid-properties.js index 3c999c4..08cd4f7 100644 --- a/tests/lib/rules/valid-properties.js +++ b/tests/lib/rules/valid-properties.js @@ -7,7 +7,7 @@ // ------------------------------------------------------------------------------ // Requirements // ------------------------------------------------------------------------------ - +const tsParser = require.resolve('@typescript-eslint/parser') const rule = require('../../../lib/rules/valid-properties') const RuleTester = require('eslint').RuleTester @@ -42,11 +42,15 @@ ruleTester.run('valid-properties', rule, { type: String, optionalTypes:[Number], observer: ()=>{} + }, + propsD: Object as PropType, + propsE: { + type: Object as Obj } - } }) - ` + `, + parser: tsParser }, { filename: 'test.mpx', From 1c8b3810df4de2cb3a9212543c3e7eda4c3f3d35 Mon Sep 17 00:00:00 2001 From: chenwanyue <527202897@qq.com> Date: Fri, 5 Jul 2024 11:26:33 +0800 Subject: [PATCH 2/3] fix: lint --- lib/rules/valid-properties.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/rules/valid-properties.js b/lib/rules/valid-properties.js index 1cd097d..3948e6c 100644 --- a/lib/rules/valid-properties.js +++ b/lib/rules/valid-properties.js @@ -82,7 +82,9 @@ function validProp(node, context, allowKeys) { } }) } - } else if (!(node.value.type === 'Identifier' || node.value.type ==='TSAsExpression')) { + } else if ( + !(node.value.type === 'Identifier' || node.value.type === 'TSAsExpression') + ) { return context.report({ node, message: "Invalid value for '{{propName}}'.", @@ -142,7 +144,6 @@ module.exports = { return utils.defineMpxVisitor(context, { onMpxObjectEnter(obj) { for (const prop of utils.getComponentPropsFromOptions(obj)) { - console.log('cwy-prop', prop, obj) prop.type === 'object' && validProp(prop.node, context, allowKeys) } } From 5721775f1ffd82f963428705a01acf366398d4bf Mon Sep 17 00:00:00 2001 From: chenwanyue <527202897@qq.com> Date: Fri, 5 Jul 2024 11:54:51 +0800 Subject: [PATCH 3/3] fix: lint --- lib/rules/valid-properties.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/valid-properties.js b/lib/rules/valid-properties.js index 7c10718..3948e6c 100644 --- a/lib/rules/valid-properties.js +++ b/lib/rules/valid-properties.js @@ -83,7 +83,7 @@ function validProp(node, context, allowKeys) { }) } } else if ( - !(node.value.type === 'Identifier' || node.value.type === 'TSAsExpression') + !(node.value.type === 'Identifier' || node.value.type === 'TSAsExpression') ) { return context.report({ node,