eslint-plugin-mpx 0.2.13 → 0.2.15

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.
@@ -22,8 +22,7 @@ module.exports = {
22
22
  'mpx/valid-swiper-item-style': 'error',
23
23
  'mpx/valid-wx-key': 'error',
24
24
  'mpx/valid-attribute-value': 'error',
25
- 'mpx/valid-template-quote': 'error'
26
-
27
- // 'mpx/valid-component-range': 'error' 默认不开
25
+ 'mpx/valid-template-quote': 'error',
26
+ 'mpx/valid-component-range': 'error'
28
27
  }
29
28
  }
@@ -32,33 +32,43 @@ module.exports = {
32
32
  return utils.defineTemplateBodyVisitor(context, {
33
33
  /** @param {VElement} node */
34
34
  "VElement[name='component']"(node) {
35
- if (!utils.hasAttribute(node, 'is')) {
36
- context.report({
35
+ const isNode = utils.getAttribute(node, 'is')
36
+ const rangeNode = utils.getAttributeAll(node, /^range(@[\w-|:]+)?$/)
37
+ if (!isNode) {
38
+ return context.report({
37
39
  node,
38
40
  loc: node.loc,
39
41
  message: "'component' element require 'is' attribute."
40
42
  })
41
- } else if (!utils.hasAttribute(node, 'range')) {
43
+ } else if (!rangeNode.length) {
42
44
  context.report({
43
45
  node,
44
46
  loc: node.loc,
45
47
  message:
46
48
  "'component' element require 'range' attribute to set the scope."
47
49
  })
50
+ } else {
51
+ // 最好有个兜底range
52
+ if (!utils.getAttribute(node, 'range')) {
53
+ context.report({
54
+ node,
55
+ loc: node.loc,
56
+ message: "'component' element require 'range' for backup."
57
+ })
58
+ }
48
59
  }
49
- // 暂时不判空了
50
- // [isNode, rangeNode].forEach((keyNode) => {
51
- // if (keyNode && utils.isEmptyValueDirective(keyNode, context)) {
52
- // context.report({
53
- // node,
54
- // loc: node.loc,
55
- // message: "'component' attribute '{{key}}' require valid value.",
56
- // data: {
57
- // key: keyNode.key.name
58
- // }
59
- // })
60
- // }
61
- // })
60
+ ;[isNode, ...rangeNode].forEach((keyNode) => {
61
+ if (keyNode && (!keyNode.value || !keyNode.value.value)) {
62
+ context.report({
63
+ node,
64
+ loc: node.loc,
65
+ message: "'component' attribute '{{key}}' require valid value.",
66
+ data: {
67
+ key: keyNode.key.name
68
+ }
69
+ })
70
+ }
71
+ })
62
72
  }
63
73
  })
64
74
  }
@@ -231,7 +231,9 @@ module.exports = {
231
231
  hasInitData = 2
232
232
  }
233
233
  } else {
234
- hasInitData = 2
234
+ if (hasInitData === 0) {
235
+ hasInitData = 2
236
+ }
235
237
  }
236
238
  })
237
239
  }
@@ -282,6 +282,10 @@ function wrapContextToOverrideTokenMethods(context, tokenStore, options) {
282
282
  getSourceCode() {
283
283
  return sourceCode
284
284
  },
285
+ // @ts-expect-error -- Added in ESLint v8.40
286
+ get sourceCode() {
287
+ return sourceCode
288
+ },
285
289
  getDeclaredVariables(node) {
286
290
  const scope = getContainerScope(node)
287
291
  if (scope) {
@@ -668,6 +672,7 @@ module.exports = {
668
672
  * @returns {VAttribute | null} The found attribute.
669
673
  */
670
674
  getAttribute,
675
+ getAttributeAll,
671
676
 
672
677
  /**
673
678
  * Get the directive list which has the given name.
@@ -1880,8 +1885,8 @@ function getScriptSetupElement(context) {
1880
1885
  /**
1881
1886
  * Check whether the given start tag has specific directive.
1882
1887
  * @param {VElement} node The start tag node to check.
1883
- * @param {string} name The attribute name to check.
1884
- * @param {string} [value] The attribute value to check.
1888
+ * @param {string | RegExp} name The attribute name to check.
1889
+ * @param {string | RegExp} [value] The attribute value to check.
1885
1890
  * @returns {boolean} `true` if the start tag has the attribute.
1886
1891
  */
1887
1892
  function hasAttribute(node, name, value) {
@@ -1890,8 +1895,8 @@ function hasAttribute(node, name, value) {
1890
1895
  /**
1891
1896
  * Get the attribute which has the given name.
1892
1897
  * @param {VElement} node The start tag node to check.
1893
- * @param {string} name The attribute name to check.
1894
- * @param {string} [value] The attribute value to check.
1898
+ * @param {string | RegExp} name The attribute name to check.
1899
+ * @param {string | RegExp} [value] The attribute value to check.
1895
1900
  * @returns {VAttribute | null} The found attribute.
1896
1901
  */
1897
1902
  function getAttribute(node, name, value) {
@@ -1902,16 +1907,57 @@ function getAttribute(node, name, value) {
1902
1907
  * @returns {node is VAttribute}
1903
1908
  */
1904
1909
  (node) => {
1905
- return (
1906
- !node.directive &&
1907
- node.key.name === name &&
1908
- (value === undefined ||
1909
- (node.value != null && node.value.value === value))
1910
- )
1910
+ return checkAttributes(node, name, value)
1911
1911
  }
1912
1912
  ) || null
1913
1913
  )
1914
1914
  }
1915
+ /**
1916
+ * Check attribute which has the given name.
1917
+ * @param {VAttribute | VDirective} node The start tag node to check.
1918
+ * @param {string | RegExp} name The attribute name to check.
1919
+ * @param {string | RegExp} [value] The attribute value to check.
1920
+ * @returns {boolean}
1921
+ */
1922
+ function checkAttributes(node, name, value) {
1923
+ return (
1924
+ !node.directive &&
1925
+ validEqual(name, node.key.name) &&
1926
+ (value === undefined ||
1927
+ (node.value != null && validEqual(value, node.value.value)))
1928
+ )
1929
+ }
1930
+ /**
1931
+ * Get all Attribute which has the given name.
1932
+ * @param {VElement} node The start tag node to check.
1933
+ * @param {string | RegExp} name The attribute name to check.
1934
+ * @param {string | RegExp} [value] The attribute value to check.
1935
+ * @returns {VAttribute[]} The found attribute.
1936
+ */
1937
+ function getAttributeAll(node, name, value) {
1938
+ return node.startTag.attributes.filter(
1939
+ /**
1940
+ * @param {VAttribute | VDirective} node
1941
+ * @returns {node is VAttribute}
1942
+ */
1943
+ (node) => {
1944
+ return checkAttributes(node, name, value)
1945
+ }
1946
+ )
1947
+ }
1948
+
1949
+ /**
1950
+ * Verify if the value meets expectations
1951
+ * @param {any} expectedValue Expected value, which can be RegExp
1952
+ * @param {any} value Value to be verified
1953
+ * @returns
1954
+ */
1955
+ function validEqual(expectedValue, value) {
1956
+ if (Object.prototype.toString.call(expectedValue) === '[object RegExp]')
1957
+ return expectedValue.test(value)
1958
+ return expectedValue === value
1959
+ }
1960
+
1915
1961
  /** @param {CallExpression} node */
1916
1962
  function isObjectArgument(node) {
1917
1963
  return (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-mpx",
3
- "version": "0.2.13",
3
+ "version": "0.2.15",
4
4
  "description": "Official ESLint plugin for Mpx.js",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {