Skip to content

Commit

Permalink
auto fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chenwanyue committed Oct 19, 2023
1 parent 397f8da commit b414a94
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 69 deletions.
2 changes: 1 addition & 1 deletion lib/configs/mpx-essential.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
95 changes: 46 additions & 49 deletions lib/rules/valid-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'
}
}
}
Expand All @@ -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)
}
}
})
}
}

}
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 19 additions & 17 deletions tests/lib/rules/valid-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
* @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
// ------------------------------------------------------------------------------

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: {
Expand All @@ -47,8 +47,9 @@ ruleTester.run("valid-properties", rule, {
}
})
`
},{
filename: "test.mpx",
},
{
filename: 'test.mpx',
code: `
<script setup>
const props = defineProps({
Expand All @@ -66,8 +67,8 @@ ruleTester.run("valid-properties", rule, {

invalid: [
{
filename: "test.mpx",
code: `
filename: 'test.mpx',
code: `
createComponent({
properties: {
propsA: {
Expand All @@ -88,9 +89,11 @@ ruleTester.run("valid-properties", rule, {
}
})
`,
options: [{
allowKeys:["type", "value","optionalTypes"]
}],
options: [
{
allowKeys: ['type', 'value', 'optionalTypes']
}
],
errors: [
{
message: "Property 'propsA' has invalid key 'default'.",
Expand Down Expand Up @@ -118,9 +121,9 @@ ruleTester.run("valid-properties", rule, {
}
]
},

{
filename: "test.mpx",
filename: 'test.mpx',
code: `
<script setup>
const props = defineProps({
Expand All @@ -144,6 +147,5 @@ ruleTester.run("valid-properties", rule, {
}
]
}

]
})

0 comments on commit b414a94

Please sign in to comment.