@hero-design/eslint-plugin 9.0.1-rc2.0 → 9.0.1

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.
package/CHANGELOG.md CHANGED
@@ -1,12 +1,10 @@
1
1
  # @hero-design/eslint-plugin
2
2
 
3
- ## 9.0.1-rc2.0
3
+ ## 9.0.1
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - [#2647](https://github.com/Thinkei/hero-design/pull/2647) [`96d0a79e0`](https://github.com/Thinkei/hero-design/commit/96d0a79e008db83c915bbadc8d70a5403d6bd4e3) Thanks [@ttkien](https://github.com/ttkien)! - add pre-commit
8
-
9
- - [#2647](https://github.com/Thinkei/hero-design/pull/2647) [`35fcc3812`](https://github.com/Thinkei/hero-design/commit/35fcc3812a090140848c80ed8d5f39026c4c5ee2) Thanks [@ttkien](https://github.com/ttkien)! - yarn install after bump version
7
+ - [#3187](https://github.com/Thinkei/hero-design/pull/3187) [`c580bccd6`](https://github.com/Thinkei/hero-design/commit/c580bccd6958dd5accc6f7c17192a822630e6536) Thanks [@vinhphan-eh](https://github.com/vinhphan-eh)! - Implement no-direct-color-palette-access rule
10
8
 
11
9
  ## 9.0.0
12
10
 
package/lib/index.js CHANGED
@@ -118,5 +118,11 @@ module.exports = {
118
118
  ],
119
119
  },
120
120
  },
121
+ recommendedReact: {
122
+ plugins: ['@hero-design'],
123
+ rules: {
124
+ '@hero-design/no-direct-color-palette-access': 'error'
125
+ }
126
+ }
121
127
  },
122
128
  };
@@ -4,6 +4,8 @@
4
4
  */
5
5
  'use strict';
6
6
 
7
+ const { findIndentifierPath } = require('../utils');
8
+
7
9
  //------------------------------------------------------------------------------
8
10
  // Rule Definition
9
11
  //------------------------------------------------------------------------------
@@ -46,27 +48,13 @@ module.exports = {
46
48
  const oldKeys = context.options[0].keys.map((k) => k.old);
47
49
  const newKeys = context.options[0].keys.map((k) => k.new);
48
50
 
49
- const findPath = (node, identifiers) => {
50
- const property = node.property;
51
-
52
- if (
53
- property != null &&
54
- property.type === 'Identifier' &&
55
- property.name != null
56
- ) {
57
- return findPath(node.parent, [...identifiers, property.name]);
58
- }
59
-
60
- return identifiers.join('.');
61
- };
62
-
63
51
  return {
64
52
  MemberExpression(node) {
65
53
  const object = node.object;
66
54
  if (object == null) return;
67
55
 
68
56
  if (object.type === 'Identifier' && object.name === 'theme') {
69
- const path = findPath(node, []);
57
+ const path = findIndentifierPath(node, []);
70
58
  const deprecatedKeyIndex = oldKeys.findIndex((k) => k === path);
71
59
 
72
60
  if (deprecatedKeyIndex >= 0) {
@@ -0,0 +1,56 @@
1
+ /**
2
+ * @fileoverview Disallow direct access of color palette.
3
+ */
4
+ 'use strict';
5
+
6
+ const { findIndentifierPath } = require('../utils');
7
+
8
+ //------------------------------------------------------------------------------
9
+ // Rule Definition
10
+ //------------------------------------------------------------------------------
11
+
12
+ /** @type {import('eslint').Rule.RuleModule} */
13
+ module.exports = {
14
+ meta: {
15
+ type: 'problem',
16
+ docs: {
17
+ description: 'Disallow direct access of color palette.',
18
+ },
19
+ messages: {
20
+ invalidColorAccess: 'Unexpected direct access of palette colors, use semantic colors instead.',
21
+ },
22
+ schema: [],
23
+ },
24
+
25
+ create(context) {
26
+ return {
27
+ MemberExpression(node) {
28
+ const object = node.object;
29
+ if (object == null) return;
30
+
31
+ // For example: theme.colors.pallete.redLight30
32
+ if (object.type === 'Identifier' && object.name === 'theme') {
33
+ const path = findIndentifierPath(node, []);
34
+
35
+ if (path.includes('colors.palette')) {
36
+ context.report({
37
+ node,
38
+ messageId: 'invalidColorAccess',
39
+ });
40
+ }
41
+ }
42
+ // For example: colors.pallete.redLight30
43
+ else if (object.type === 'Identifier' && object.name === 'colors') {
44
+ const path = findIndentifierPath(node, []);
45
+
46
+ if (path.includes('palette')) {
47
+ context.report({
48
+ node,
49
+ messageId: 'invalidColorAccess',
50
+ });
51
+ }
52
+ }
53
+ },
54
+ };
55
+ },
56
+ };
package/lib/utils.js ADDED
@@ -0,0 +1,17 @@
1
+ const findIndentifierPath = (node, identifiers) => {
2
+ const property = node.property;
3
+
4
+ if (
5
+ property != null &&
6
+ property.type === 'Identifier' &&
7
+ property.name != null
8
+ ) {
9
+ return findIndentifierPath(node.parent, [...identifiers, property.name]);
10
+ }
11
+
12
+ return identifiers.join('.');
13
+ };
14
+
15
+ module.exports = {
16
+ findIndentifierPath,
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hero-design/eslint-plugin",
3
- "version": "9.0.1-rc2.0",
3
+ "version": "9.0.1",
4
4
  "description": "Hero Design's eslint plugin",
5
5
  "keywords": [
6
6
  "eslint",
@@ -26,7 +26,7 @@
26
26
  "eslint-plugin-eslint-plugin": "^5.0.0",
27
27
  "eslint-plugin-node": "^11.1.0",
28
28
  "jest": "^29.2.1",
29
- "prettier-config-hd": "8.42.5-rc2.0"
29
+ "prettier-config-hd": "8.42.4"
30
30
  },
31
31
  "engines": {
32
32
  "node": "^14.17.0 || ^16.0.0 || >= 18.0.0"
@@ -0,0 +1,45 @@
1
+ const rule = require('../../../lib/rules/no-direct-color-palette-access');
2
+ const RuleTester = require('eslint').RuleTester;
3
+
4
+ //------------------------------------------------------------------------------
5
+ // Tests
6
+ //------------------------------------------------------------------------------
7
+
8
+ const config = {
9
+ parserOptions: {
10
+ sourceType: 'module',
11
+ ecmaVersion: 6,
12
+ ecmaFeatures: { jsx: true },
13
+ },
14
+ };
15
+
16
+ const ruleTester = new RuleTester();
17
+ ruleTester.run('no-direct-color-palette-access', rule, {
18
+ valid: [
19
+ {
20
+ code: 'const color = theme.colors.hoverDanger',
21
+ },
22
+ {
23
+ code: '<Box sx={{ backgroundColor: theme.colors.hoverDanger }} />',
24
+ },
25
+ ].map((test) => ({ ...test, ...config })),
26
+ invalid: [
27
+ {
28
+ code: 'const color = theme.colors.palette.redLight30',
29
+ errors: [{ messageId: 'invalidColorAccess' }],
30
+ },
31
+ {
32
+ code: 'const color = theme.colors.palette',
33
+ errors: [{ messageId: 'invalidColorAccess' }],
34
+ },
35
+ {
36
+ code: '<Box sx={{ backgroundColor: theme.colors.palette.redLight30 }} />',
37
+ errors: [{ messageId: 'invalidColorAccess' }],
38
+ },
39
+ {
40
+ code: `const colors = theme.colors;
41
+ const color = colors.palette.redLight30`,
42
+ errors: [{ messageId: 'invalidColorAccess' }],
43
+ }
44
+ ].map((test) => ({ ...test, ...config })),
45
+ });