From b414a94eee83447c9672795e62fe05a53c35b50d Mon Sep 17 00:00:00 2001 From: chenwanyue Date: Thu, 19 Oct 2023 16:08:23 +0800 Subject: [PATCH] auto fix --- lib/configs/mpx-essential.js | 2 +- lib/index.js | 2 +- lib/rules/valid-properties.js | 95 ++++++++++++++--------------- lib/utils/index.js | 2 +- tests/lib/rules/valid-properties.js | 36 +++++------ 5 files changed, 68 insertions(+), 69 deletions(-) diff --git a/lib/configs/mpx-essential.js b/lib/configs/mpx-essential.js index 494dc6f..2a3237a 100644 --- a/lib/configs/mpx-essential.js +++ b/lib/configs/mpx-essential.js @@ -24,6 +24,6 @@ module.exports = { 'mpx/valid-attribute-value': 'error', 'mpx/valid-template-quote': 'error', 'mpx/valid-component-range': 'error', - 'mpx/valid-properties':'error' + 'mpx/valid-properties': 'error' } } diff --git a/lib/index.js b/lib/index.js index af6be28..469bb66 100644 --- a/lib/index.js +++ b/lib/index.js @@ -36,7 +36,7 @@ module.exports = { 'no-deprecated-lifecycle': require('./rules/no-deprecated-lifecycle'), 'no-deprecated-mpx-createfunction': require('./rules/no-deprecated-mpx-createfunction'), 'no-deprecated-watch-second-param': require('./rules/no-deprecated-watch-second-param'), - 'valid-properties': require('./rules/valid-properties'), + 'valid-properties': require('./rules/valid-properties') }, configs: { base: require('./configs/base'), diff --git a/lib/rules/valid-properties.js b/lib/rules/valid-properties.js index 2ad3c01..ea20bcd 100644 --- a/lib/rules/valid-properties.js +++ b/lib/rules/valid-properties.js @@ -3,13 +3,13 @@ * @copyright 2020 pagnkelly. All rights reserved. * See LICENSE file in root directory for full license. */ -"use strict" +'use strict' // ------------------------------------------------------------------------------ // Requirements // ------------------------------------------------------------------------------ -const utils = require("../utils") +const utils = require('../utils') /** * @typedef {import("../utils").ComponentObjectProp} ComponentObjectProp @@ -20,100 +20,94 @@ const utils = require("../utils") // ------------------------------------------------------------------------------ // 校验properties是否符合预期 /** 默认允许的key */ -const DEFAULT_KEYS = ["type", "value", "optionalTypes", "observer"] +const DEFAULT_KEYS = ['type', 'value', 'optionalTypes', 'observer'] /** * check prop - * @param {Property} node - * @returns + * @param {Property} node + * @returns */ /** - * - * @param {Property} node - * @param {RuleContext} context - * @param {string[]} allowKeys - * @returns + * + * @param {Property} node + * @param {RuleContext} context + * @param {string[]} allowKeys + * @returns */ function validProp(node, context, allowKeys) { - if(!node) return + if (!node) return const sourceCode = context.getSourceCode() const propName = sourceCode.getText(node.key) - if(!node.value) { + if (!node.value) { return context.report({ - node: node, - message: - "The value of '{{propName}}' cannot be empty.", + node, + message: "The value of '{{propName}}' cannot be empty.", data: { propName } }) } - if(node.value.type === "ObjectExpression"){ - if(!node.value.properties.length) { + if (node.value.type === 'ObjectExpression') { + if (!node.value.properties.length) { return context.report({ node: node.value, - message: - "The value of '{{propName}}' cannot be empty object.", + message: "The value of '{{propName}}' cannot be empty object.", data: { propName } }) } let hasType = 0 - node.value.properties.forEach((item)=>{ - if(item.type !== "Property") return + node.value.properties.forEach((item) => { + if (item.type !== 'Property') return const keyName = sourceCode.getText(item.key) - if(!allowKeys.includes(keyName)){ + if (!allowKeys.includes(keyName)) { context.report({ node: item, - message: - "Property '{{propName}}' has invalid key '{{keyName}}'.", + message: "Property '{{propName}}' has invalid key '{{keyName}}'.", data: { propName, keyName } }) - } else if(keyName === "type") { + } else if (keyName === 'type') { hasType = 1 } }) - if(!hasType) { + if (!hasType) { context.report({ node: node.value, - message: - "Property '{{propName}}' requires 'type' key.", + message: "Property '{{propName}}' requires 'type' key.", data: { propName } }) } - } else if(node.value.type !== "Identifier"){ + } else if (node.value.type !== 'Identifier') { return context.report({ node, - message: - "Invalid value for '{{propName}}'.", + message: "Invalid value for '{{propName}}'.", data: { propName } }) } - } module.exports = { meta: { - type: "problem", + type: 'problem', docs: { description: - "enforce that a return statement is present in computed property", - categories: ["mpx-essential"], - url: "https://mpx-ecology.github.io/eslint-plugin-mpx/rules/valid-properties.html" + 'enforce that a return statement is present in computed property', + categories: ['mpx-essential'], + url: 'https://mpx-ecology.github.io/eslint-plugin-mpx/rules/valid-properties.html' }, fixable: null, // or "code" or "whitespace" schema: [ { - type: "object", + type: 'object', properties: { allowKeys: { - type: "array" + type: 'array' } } } @@ -124,31 +118,34 @@ module.exports = { const options = context.options[0] || {} const allowKeys = options.allowKeys || DEFAULT_KEYS const isScriptSetup = utils.isScriptSetup(context) - + // ---------------------------------------------------------------------- // Public // ---------------------------------------------------------------------- if (isScriptSetup) { - return utils.defineTemplateBodyVisitor(context, {},{ - /** - * @param {ObjectExpression} node - */ - "CallExpression[callee.name='defineProps'] > ObjectExpression"(node) { - for (const prop of utils.getComponentPropsFromDefine(node)) { - prop.type === "object" && validProp(prop.node,context,allowKeys) + return utils.defineTemplateBodyVisitor( + context, + {}, + { + /** + * @param {ObjectExpression} node + */ + "CallExpression[callee.name='defineProps'] > ObjectExpression"(node) { + for (const prop of utils.getComponentPropsFromDefine(node)) { + prop.type === 'object' && validProp(prop.node, context, allowKeys) + } } } - }) + ) } else { return utils.defineMpxVisitor(context, { onMpxObjectEnter(obj) { for (const prop of utils.getComponentPropsFromOptions(obj)) { - prop.type === "object" && validProp(prop.node,context,allowKeys) + prop.type === 'object' && validProp(prop.node, context, allowKeys) } } }) } } - } diff --git a/lib/utils/index.js b/lib/utils/index.js index ad8c3bc..a20da8a 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -803,7 +803,7 @@ module.exports = { * @return {(ComponentArrayProp | ComponentObjectProp | ComponentUnknownProp)[]} Array of component props */ getComponentPropsFromOptions, - + getComponentPropsFromDefine, /** * Get all computed properties by looking at all component's properties diff --git a/tests/lib/rules/valid-properties.js b/tests/lib/rules/valid-properties.js index 3f30410..3c999c4 100644 --- a/tests/lib/rules/valid-properties.js +++ b/tests/lib/rules/valid-properties.js @@ -2,20 +2,20 @@ * @fileoverview Enforces that a return statement is present in computed property (valid-properties) * @author Armano */ -"use strict" +'use strict' // ------------------------------------------------------------------------------ // Requirements // ------------------------------------------------------------------------------ -const rule = require("../../../lib/rules/valid-properties") -const RuleTester = require("eslint").RuleTester +const rule = require('../../../lib/rules/valid-properties') +const RuleTester = require('eslint').RuleTester // const parserOptions = { // ecmaVersion: 2020, // sourceType: "module" // } -const mpxParser = require.resolve("mpx-eslint-parser") +const mpxParser = require.resolve('mpx-eslint-parser') // ------------------------------------------------------------------------------ // Tests // ------------------------------------------------------------------------------ @@ -23,13 +23,13 @@ const mpxParser = require.resolve("mpx-eslint-parser") const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, - sourceType: "module" + sourceType: 'module' } }) -ruleTester.run("valid-properties", rule, { +ruleTester.run('valid-properties', rule, { valid: [ { - filename: "test.mpx", + filename: 'test.mpx', code: ` createComponent({ properties: { @@ -47,8 +47,9 @@ ruleTester.run("valid-properties", rule, { } }) ` - },{ - filename: "test.mpx", + }, + { + filename: 'test.mpx', code: `