eslint-plugin-mpx 0.2.16 → 0.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,6 +23,7 @@ module.exports = {
23
23
  'mpx/valid-wx-key': 'error',
24
24
  'mpx/valid-attribute-value': 'error',
25
25
  'mpx/valid-template-quote': 'error',
26
- 'mpx/valid-component-range': 'error'
26
+ 'mpx/valid-component-range': 'error',
27
+ 'mpx/valid-properties': 'error'
27
28
  }
28
29
  }
package/lib/index.js CHANGED
@@ -35,7 +35,8 @@ module.exports = {
35
35
  'valid-initdata': require('./rules/valid-initdata'),
36
36
  'no-deprecated-lifecycle': require('./rules/no-deprecated-lifecycle'),
37
37
  'no-deprecated-mpx-createfunction': require('./rules/no-deprecated-mpx-createfunction'),
38
- 'no-deprecated-watch-second-param': require('./rules/no-deprecated-watch-second-param')
38
+ 'no-deprecated-watch-second-param': require('./rules/no-deprecated-watch-second-param'),
39
+ 'valid-properties': require('./rules/valid-properties')
39
40
  },
40
41
  configs: {
41
42
  base: require('./configs/base'),
@@ -0,0 +1,151 @@
1
+ /**
2
+ * @author pagnkelly
3
+ * @copyright 2020 pagnkelly. All rights reserved.
4
+ * See LICENSE file in root directory for full license.
5
+ */
6
+ 'use strict'
7
+
8
+ // ------------------------------------------------------------------------------
9
+ // Requirements
10
+ // ------------------------------------------------------------------------------
11
+
12
+ const utils = require('../utils')
13
+
14
+ /**
15
+ * @typedef {import("../utils").ComponentObjectProp} ComponentObjectProp
16
+ */
17
+
18
+ // ------------------------------------------------------------------------------
19
+ // Rule Definition
20
+ // ------------------------------------------------------------------------------
21
+ // 校验properties是否符合预期
22
+ /** 默认允许的key */
23
+ const DEFAULT_KEYS = ['type', 'value', 'optionalTypes', 'observer']
24
+ /**
25
+ * check prop
26
+ * @param {Property} node
27
+ * @returns
28
+ */
29
+ /**
30
+ *
31
+ * @param {Property} node
32
+ * @param {RuleContext} context
33
+ * @param {string[]} allowKeys
34
+ * @returns
35
+ */
36
+ function validProp(node, context, allowKeys) {
37
+ if (!node) return
38
+ const sourceCode = context.getSourceCode()
39
+ const propName = sourceCode.getText(node.key)
40
+ if (!node.value) {
41
+ return context.report({
42
+ node,
43
+ message: "The value of '{{propName}}' cannot be empty.",
44
+ data: {
45
+ propName
46
+ }
47
+ })
48
+ }
49
+ if (node.value.type === 'ObjectExpression') {
50
+ if (!node.value.properties.length) {
51
+ return context.report({
52
+ node: node.value,
53
+ message: "The value of '{{propName}}' cannot be empty object.",
54
+ data: {
55
+ propName
56
+ }
57
+ })
58
+ }
59
+ let hasType = 0
60
+ node.value.properties.forEach((item) => {
61
+ if (item.type !== 'Property') return
62
+ const keyName = sourceCode.getText(item.key)
63
+ if (!allowKeys.includes(keyName)) {
64
+ context.report({
65
+ node: item,
66
+ message: "Property '{{propName}}' has invalid key '{{keyName}}'.",
67
+ data: {
68
+ propName,
69
+ keyName
70
+ }
71
+ })
72
+ } else if (keyName === 'type') {
73
+ hasType = 1
74
+ }
75
+ })
76
+ if (!hasType) {
77
+ context.report({
78
+ node: node.value,
79
+ message: "Property '{{propName}}' requires 'type' key.",
80
+ data: {
81
+ propName
82
+ }
83
+ })
84
+ }
85
+ } else if (node.value.type !== 'Identifier') {
86
+ return context.report({
87
+ node,
88
+ message: "Invalid value for '{{propName}}'.",
89
+ data: {
90
+ propName
91
+ }
92
+ })
93
+ }
94
+ }
95
+ module.exports = {
96
+ meta: {
97
+ type: 'problem',
98
+ docs: {
99
+ description:
100
+ 'enforce that a return statement is present in computed property',
101
+ categories: ['mpx-essential'],
102
+ url: 'https://mpx-ecology.github.io/eslint-plugin-mpx/rules/valid-properties.html'
103
+ },
104
+ fixable: null, // or "code" or "whitespace"
105
+ schema: [
106
+ {
107
+ type: 'object',
108
+ properties: {
109
+ allowKeys: {
110
+ type: 'array'
111
+ }
112
+ }
113
+ }
114
+ ]
115
+ },
116
+ /** @param {RuleContext} context */
117
+ create(context) {
118
+ const options = context.options[0] || {}
119
+ const allowKeys = options.allowKeys || DEFAULT_KEYS
120
+ const isScriptSetup = utils.isScriptSetup(context)
121
+
122
+ // ----------------------------------------------------------------------
123
+ // Public
124
+ // ----------------------------------------------------------------------
125
+
126
+ if (isScriptSetup) {
127
+ return utils.defineTemplateBodyVisitor(
128
+ context,
129
+ {},
130
+ {
131
+ /**
132
+ * @param {ObjectExpression} node
133
+ */
134
+ "CallExpression[callee.name='defineProps'] > ObjectExpression"(node) {
135
+ for (const prop of utils.getComponentPropsFromDefine(node)) {
136
+ prop.type === 'object' && validProp(prop.node, context, allowKeys)
137
+ }
138
+ }
139
+ }
140
+ )
141
+ } else {
142
+ return utils.defineMpxVisitor(context, {
143
+ onMpxObjectEnter(obj) {
144
+ for (const prop of utils.getComponentPropsFromOptions(obj)) {
145
+ prop.type === 'object' && validProp(prop.node, context, allowKeys)
146
+ }
147
+ }
148
+ })
149
+ }
150
+ }
151
+ }
@@ -137,7 +137,10 @@ module.exports = {
137
137
  node.parent.key.name.name
138
138
  )
139
139
  ) {
140
- if (node.parent.key.name.name === 'for-item') {
140
+ if (
141
+ node.parent.key.name.name === 'for-item' ||
142
+ node.parent.key.name.name === 'for-index'
143
+ ) {
141
144
  exposeSet.add(ref.id.name)
142
145
  }
143
146
  continue
@@ -803,6 +803,8 @@ module.exports = {
803
803
  * @return {(ComponentArrayProp | ComponentObjectProp | ComponentUnknownProp)[]} Array of component props
804
804
  */
805
805
  getComponentPropsFromOptions,
806
+
807
+ getComponentPropsFromDefine,
806
808
  /**
807
809
  * Get all computed properties by looking at all component's properties
808
810
  * @param {ObjectExpression} componentObject Object with component definition
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "eslint-plugin-mpx",
3
- "version": "0.2.16",
3
+ "version": "0.2.18",
4
4
  "description": "Official ESLint plugin for Mpx.js",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
7
7
  "start": "npm run test:base -- --watch --growl",
8
8
  "test": "mocha \"tests/lib/**/*.js\" --reporter dot",
9
- "test:only": "mocha \"tests/lib/rules/valid-initdata.js\" --reporter dot",
9
+ "test:only": "mocha \"tests/lib/rules/valid-properties.js\" --reporter dot",
10
10
  "debug": "mocha --inspect \"tests/lib/**/*.js\" --reporter dot --timeout 60000",
11
11
  "cover": "npm run cover:test && npm run cover:report",
12
12
  "cover:test": "nyc npm run test -- --timeout 60000",