@metamask/eslint-plugin-design-tokens 0.1.0 → 1.1.0

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
@@ -7,11 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [1.1.0]
11
+
12
+ ### Added
13
+
14
+ - Adding `prefer-theme-color-classnames` rule ([#22](https://github.com/MetaMask/eslint-plugin-design-tokens/pull/22))
15
+ - Updating PR template to align better with mobile and extension ([#19](https://github.com/MetaMask/eslint-plugin-design-tokens/pull/19))
16
+ - Updating readme rule table with color-no-hex rule ([#18](https://github.com/MetaMask/eslint-plugin-design-tokens/pull/18))
17
+
18
+ ## [1.0.0]
19
+
20
+ ### Added
21
+
22
+ - Adding `color-no-hex` rule ([#11](https://github.com/MetaMask/eslint-plugin-design-tokens/pull/11))
23
+
10
24
  ## [0.1.0]
11
25
 
12
26
  ### Changed
13
27
 
14
28
  - Initial release
15
29
 
16
- [Unreleased]: https://github.com/MetaMask/eslint-plugin-design-tokens/compare/v0.1.0...HEAD
30
+ [Unreleased]: https://github.com/MetaMask/eslint-plugin-design-tokens/compare/v1.1.0...HEAD
31
+ [1.1.0]: https://github.com/MetaMask/eslint-plugin-design-tokens/compare/v1.0.0...v1.1.0
32
+ [1.0.0]: https://github.com/MetaMask/eslint-plugin-design-tokens/compare/v0.1.0...v1.0.0
17
33
  [0.1.0]: https://github.com/MetaMask/eslint-plugin-design-tokens/releases/tag/v0.1.0
package/README.md CHANGED
@@ -60,9 +60,11 @@ Then configure the rules you want to use within `rules` property of your `.eslin
60
60
 
61
61
  > Remember that all rules from this plugin are prefixed by `"@metamask/design-tokens/"`
62
62
 
63
- | Name                                                                              | Description |
64
- | :-------------------------------------------------------------------------------- | :-------------------------------------------------------------------------------- |
65
- | [no-deprecated-classnames](docs/rules/no-deprecated-classnames.md) | Enforce usage of up-to-date TailwindCSS class names, disallowing deprecated ones. |
63
+ | Name                                                                              | Description |
64
+ | :-------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- |
65
+ | [no-deprecated-classnames](docs/rules/no-deprecated-classnames.md) | Enforce usage of up-to-date TailwindCSS class names, disallowing deprecated ones. |
66
+ | [color-no-hex](docs/rules/color-no-hex.md) | Prevent the use of hex color values. |
67
+ | [prefer-theme-color-classnames](docs/rules/prefer-theme-color-classnames.md) | Encourage the use of theme color class names instead of literal color class names. |
66
68
 
67
69
  ## Contributing
68
70
 
package/dist/index.js CHANGED
@@ -4,4 +4,6 @@ exports.rules = void 0;
4
4
  const rules_1 = require("./rules");
5
5
  exports.rules = {
6
6
  'no-deprecated-classnames': rules_1.noDeprecatedClassnames,
7
+ 'color-no-hex': rules_1.colorNoHex,
8
+ 'prefer-theme-color-classnames': rules_1.preferThemeColorClassnames,
7
9
  };
package/dist/index.mjs CHANGED
@@ -6,7 +6,7 @@ var noDeprecatedClassnames = {
6
6
  description: "No deprecated classnames allowed",
7
7
  recommended: false,
8
8
  // Make sure to replace the URL with the actual location of your rule's documentation
9
- url: "https://github.com/MetaMask/eslint-plugin-design-tokens?tab=readme-ov-file#eslint-plugin-design-tokens-"
9
+ url: "https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/no-deprecated-classnames.md"
10
10
  },
11
11
  // Define the schema to accept an object of deprecated classnames
12
12
  schema: [
@@ -51,9 +51,156 @@ var noDeprecatedClassnames = {
51
51
  }
52
52
  };
53
53
 
54
+ // src/rules/prefer-theme-color-classnames.ts
55
+ var preferThemeColorClassnames = {
56
+ meta: {
57
+ type: "suggestion",
58
+ docs: {
59
+ description: "Encourage the use of theme color class names instead of literal color class names.",
60
+ recommended: true,
61
+ url: "https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/prefer-theme-color-classnames.md"
62
+ },
63
+ // Adding a schema to accept options
64
+ schema: [
65
+ {
66
+ type: "object",
67
+ properties: {
68
+ discouragedColors: {
69
+ type: "array",
70
+ items: {
71
+ type: "string",
72
+ minLength: 1
73
+ // This ensures that the string is not empty but does not prevent whitespace-only strings.
74
+ },
75
+ default: [
76
+ "blue",
77
+ "red",
78
+ "gray",
79
+ "slateGray",
80
+ "green",
81
+ "orange",
82
+ "black",
83
+ "white",
84
+ "indigo",
85
+ "yellow",
86
+ "purple",
87
+ "pink",
88
+ "teal"
89
+ ]
90
+ }
91
+ },
92
+ additionalProperties: false
93
+ }
94
+ ],
95
+ messages: {
96
+ discouragedColorUsage: `'{{className}}' usage of literal color class names is discouraged. Consider using theme color class names instead.`
97
+ }
98
+ },
99
+ create(context) {
100
+ const options = context.options[0] || {};
101
+ const discouragedColors = options.discouragedColors || [
102
+ "blue",
103
+ "red",
104
+ "gray",
105
+ "slateGray",
106
+ "green",
107
+ "orange",
108
+ "black",
109
+ "white",
110
+ "indigo",
111
+ "yellow",
112
+ "purple",
113
+ "pink",
114
+ "teal"
115
+ ];
116
+ const validColors = discouragedColors.filter(
117
+ (color) => color.trim().length > 0
118
+ );
119
+ if (validColors.length === 0) {
120
+ return {};
121
+ }
122
+ const colorRegex = new RegExp(
123
+ `\\b(\\w+-)?(${discouragedColors.join("|")})-?\\d*\\b`,
124
+ "iu"
125
+ );
126
+ function checkLiteralOrTemplateValue(value, node) {
127
+ if (!value.trim()) {
128
+ return;
129
+ }
130
+ const matches = value.match(colorRegex);
131
+ if (matches?.[0]) {
132
+ context.report({
133
+ node,
134
+ messageId: "discouragedColorUsage",
135
+ data: {
136
+ className: matches[0]
137
+ }
138
+ });
139
+ }
140
+ }
141
+ return {
142
+ Literal(node) {
143
+ if (typeof node.value === "string") {
144
+ checkLiteralOrTemplateValue(node.value, node);
145
+ }
146
+ },
147
+ TemplateLiteral(node) {
148
+ node.quasis.forEach((quasi) => {
149
+ checkLiteralOrTemplateValue(quasi.value.raw, quasi);
150
+ });
151
+ }
152
+ };
153
+ }
154
+ };
155
+
156
+ // src/rules/color-no-hex.ts
157
+ var hexColorRegex = /(#\p{Hex_Digit}{3,6})\b/u;
158
+ var colorNoHex = {
159
+ meta: {
160
+ type: "problem",
161
+ docs: {
162
+ description: "Prevent the use of hex color values.",
163
+ recommended: true,
164
+ url: "https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/color-no-hex.md"
165
+ },
166
+ schema: []
167
+ // This rule does not take any options
168
+ },
169
+ create(context) {
170
+ return {
171
+ Literal(node) {
172
+ if (typeof node.value === "string") {
173
+ const matches = node.value.match(hexColorRegex);
174
+ if (matches) {
175
+ const hexValue = matches[1];
176
+ context.report({
177
+ node,
178
+ message: `'${hexValue}' Hex color values are not allowed. Consider using design tokens instead. For support reach out to the design system team #metamask-design-system on Slack.`
179
+ });
180
+ }
181
+ }
182
+ },
183
+ TemplateLiteral(node) {
184
+ node.quasis.forEach((part) => {
185
+ const matches = part.value.raw.match(hexColorRegex);
186
+ if (matches) {
187
+ const hexValue = matches[1];
188
+ context.report({
189
+ node: part,
190
+ message: `'${hexValue}' Hex color values are not allowed. Consider using design tokens instead. For support reach out to the design system team #metamask-design-system on Slack.`
191
+ });
192
+ }
193
+ });
194
+ }
195
+ };
196
+ }
197
+ };
198
+
54
199
  // src/index.ts
55
200
  var rules = {
56
- "no-deprecated-classnames": noDeprecatedClassnames
201
+ "no-deprecated-classnames": noDeprecatedClassnames,
202
+ "color-no-hex": colorNoHex,
203
+ "prefer-theme-color-classnames": preferThemeColorClassnames
57
204
  };
58
205
  export {
59
206
  rules
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.colorNoHex = void 0;
4
+ /**
5
+ * Regular expression to match hex color values.
6
+ * Added capturing group to extract the hex value.
7
+ * Using 'u' flag for Unicode support.
8
+ */
9
+ const hexColorRegex = /(#\p{Hex_Digit}{3,6})\b/u;
10
+ /**
11
+ * Rule to prevent the use of hex color values.
12
+ */
13
+ exports.colorNoHex = {
14
+ meta: {
15
+ type: 'problem',
16
+ docs: {
17
+ description: 'Prevent the use of hex color values.',
18
+ recommended: true,
19
+ url: 'https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/color-no-hex.md',
20
+ },
21
+ schema: [], // This rule does not take any options
22
+ },
23
+ create(context) {
24
+ return {
25
+ Literal(node) {
26
+ if (typeof node.value === 'string') {
27
+ const matches = node.value.match(hexColorRegex);
28
+ if (matches) {
29
+ const hexValue = matches[1]; // Type assertion to string
30
+ context.report({
31
+ node,
32
+ message: `'${hexValue}' Hex color values are not allowed. Consider using design tokens instead. For support reach out to the design system team #metamask-design-system on Slack.`,
33
+ });
34
+ }
35
+ }
36
+ },
37
+ TemplateLiteral(node) {
38
+ node.quasis.forEach((part) => {
39
+ const matches = part.value.raw.match(hexColorRegex);
40
+ if (matches) {
41
+ const hexValue = matches[1]; // Type assertion to string
42
+ context.report({
43
+ node: part,
44
+ message: `'${hexValue}' Hex color values are not allowed. Consider using design tokens instead. For support reach out to the design system team #metamask-design-system on Slack.`,
45
+ });
46
+ }
47
+ });
48
+ },
49
+ };
50
+ },
51
+ };
@@ -0,0 +1,45 @@
1
+ // src/rules/color-no-hex.ts
2
+ var hexColorRegex = /(#\p{Hex_Digit}{3,6})\b/u;
3
+ var colorNoHex = {
4
+ meta: {
5
+ type: "problem",
6
+ docs: {
7
+ description: "Prevent the use of hex color values.",
8
+ recommended: true,
9
+ url: "https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/color-no-hex.md"
10
+ },
11
+ schema: []
12
+ // This rule does not take any options
13
+ },
14
+ create(context) {
15
+ return {
16
+ Literal(node) {
17
+ if (typeof node.value === "string") {
18
+ const matches = node.value.match(hexColorRegex);
19
+ if (matches) {
20
+ const hexValue = matches[1];
21
+ context.report({
22
+ node,
23
+ message: `'${hexValue}' Hex color values are not allowed. Consider using design tokens instead. For support reach out to the design system team #metamask-design-system on Slack.`
24
+ });
25
+ }
26
+ }
27
+ },
28
+ TemplateLiteral(node) {
29
+ node.quasis.forEach((part) => {
30
+ const matches = part.value.raw.match(hexColorRegex);
31
+ if (matches) {
32
+ const hexValue = matches[1];
33
+ context.report({
34
+ node: part,
35
+ message: `'${hexValue}' Hex color values are not allowed. Consider using design tokens instead. For support reach out to the design system team #metamask-design-system on Slack.`
36
+ });
37
+ }
38
+ });
39
+ }
40
+ };
41
+ }
42
+ };
43
+ export {
44
+ colorNoHex
45
+ };
@@ -1,5 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.noDeprecatedClassnames = void 0;
3
+ exports.colorNoHex = exports.preferThemeColorClassnames = exports.noDeprecatedClassnames = void 0;
4
4
  var no_deprecated_classnames_1 = require("./no-deprecated-classnames");
5
5
  Object.defineProperty(exports, "noDeprecatedClassnames", { enumerable: true, get: function () { return no_deprecated_classnames_1.noDeprecatedClassnames; } });
6
+ var prefer_theme_color_classnames_1 = require("./prefer-theme-color-classnames");
7
+ Object.defineProperty(exports, "preferThemeColorClassnames", { enumerable: true, get: function () { return prefer_theme_color_classnames_1.preferThemeColorClassnames; } });
8
+ var color_no_hex_1 = require("./color-no-hex");
9
+ Object.defineProperty(exports, "colorNoHex", { enumerable: true, get: function () { return color_no_hex_1.colorNoHex; } });
@@ -6,7 +6,7 @@ var noDeprecatedClassnames = {
6
6
  description: "No deprecated classnames allowed",
7
7
  recommended: false,
8
8
  // Make sure to replace the URL with the actual location of your rule's documentation
9
- url: "https://github.com/MetaMask/eslint-plugin-design-tokens?tab=readme-ov-file#eslint-plugin-design-tokens-"
9
+ url: "https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/no-deprecated-classnames.md"
10
10
  },
11
11
  // Define the schema to accept an object of deprecated classnames
12
12
  schema: [
@@ -50,6 +50,153 @@ var noDeprecatedClassnames = {
50
50
  };
51
51
  }
52
52
  };
53
+
54
+ // src/rules/prefer-theme-color-classnames.ts
55
+ var preferThemeColorClassnames = {
56
+ meta: {
57
+ type: "suggestion",
58
+ docs: {
59
+ description: "Encourage the use of theme color class names instead of literal color class names.",
60
+ recommended: true,
61
+ url: "https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/prefer-theme-color-classnames.md"
62
+ },
63
+ // Adding a schema to accept options
64
+ schema: [
65
+ {
66
+ type: "object",
67
+ properties: {
68
+ discouragedColors: {
69
+ type: "array",
70
+ items: {
71
+ type: "string",
72
+ minLength: 1
73
+ // This ensures that the string is not empty but does not prevent whitespace-only strings.
74
+ },
75
+ default: [
76
+ "blue",
77
+ "red",
78
+ "gray",
79
+ "slateGray",
80
+ "green",
81
+ "orange",
82
+ "black",
83
+ "white",
84
+ "indigo",
85
+ "yellow",
86
+ "purple",
87
+ "pink",
88
+ "teal"
89
+ ]
90
+ }
91
+ },
92
+ additionalProperties: false
93
+ }
94
+ ],
95
+ messages: {
96
+ discouragedColorUsage: `'{{className}}' usage of literal color class names is discouraged. Consider using theme color class names instead.`
97
+ }
98
+ },
99
+ create(context) {
100
+ const options = context.options[0] || {};
101
+ const discouragedColors = options.discouragedColors || [
102
+ "blue",
103
+ "red",
104
+ "gray",
105
+ "slateGray",
106
+ "green",
107
+ "orange",
108
+ "black",
109
+ "white",
110
+ "indigo",
111
+ "yellow",
112
+ "purple",
113
+ "pink",
114
+ "teal"
115
+ ];
116
+ const validColors = discouragedColors.filter(
117
+ (color) => color.trim().length > 0
118
+ );
119
+ if (validColors.length === 0) {
120
+ return {};
121
+ }
122
+ const colorRegex = new RegExp(
123
+ `\\b(\\w+-)?(${discouragedColors.join("|")})-?\\d*\\b`,
124
+ "iu"
125
+ );
126
+ function checkLiteralOrTemplateValue(value, node) {
127
+ if (!value.trim()) {
128
+ return;
129
+ }
130
+ const matches = value.match(colorRegex);
131
+ if (matches?.[0]) {
132
+ context.report({
133
+ node,
134
+ messageId: "discouragedColorUsage",
135
+ data: {
136
+ className: matches[0]
137
+ }
138
+ });
139
+ }
140
+ }
141
+ return {
142
+ Literal(node) {
143
+ if (typeof node.value === "string") {
144
+ checkLiteralOrTemplateValue(node.value, node);
145
+ }
146
+ },
147
+ TemplateLiteral(node) {
148
+ node.quasis.forEach((quasi) => {
149
+ checkLiteralOrTemplateValue(quasi.value.raw, quasi);
150
+ });
151
+ }
152
+ };
153
+ }
154
+ };
155
+
156
+ // src/rules/color-no-hex.ts
157
+ var hexColorRegex = /(#\p{Hex_Digit}{3,6})\b/u;
158
+ var colorNoHex = {
159
+ meta: {
160
+ type: "problem",
161
+ docs: {
162
+ description: "Prevent the use of hex color values.",
163
+ recommended: true,
164
+ url: "https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/color-no-hex.md"
165
+ },
166
+ schema: []
167
+ // This rule does not take any options
168
+ },
169
+ create(context) {
170
+ return {
171
+ Literal(node) {
172
+ if (typeof node.value === "string") {
173
+ const matches = node.value.match(hexColorRegex);
174
+ if (matches) {
175
+ const hexValue = matches[1];
176
+ context.report({
177
+ node,
178
+ message: `'${hexValue}' Hex color values are not allowed. Consider using design tokens instead. For support reach out to the design system team #metamask-design-system on Slack.`
179
+ });
180
+ }
181
+ }
182
+ },
183
+ TemplateLiteral(node) {
184
+ node.quasis.forEach((part) => {
185
+ const matches = part.value.raw.match(hexColorRegex);
186
+ if (matches) {
187
+ const hexValue = matches[1];
188
+ context.report({
189
+ node: part,
190
+ message: `'${hexValue}' Hex color values are not allowed. Consider using design tokens instead. For support reach out to the design system team #metamask-design-system on Slack.`
191
+ });
192
+ }
193
+ });
194
+ }
195
+ };
196
+ }
197
+ };
53
198
  export {
54
- noDeprecatedClassnames
199
+ colorNoHex,
200
+ noDeprecatedClassnames,
201
+ preferThemeColorClassnames
55
202
  };
@@ -13,7 +13,7 @@ exports.noDeprecatedClassnames = {
13
13
  description: 'No deprecated classnames allowed',
14
14
  recommended: false,
15
15
  // Make sure to replace the URL with the actual location of your rule's documentation
16
- url: 'https://github.com/MetaMask/eslint-plugin-design-tokens?tab=readme-ov-file#eslint-plugin-design-tokens-',
16
+ url: 'https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/no-deprecated-classnames.md',
17
17
  },
18
18
  // Define the schema to accept an object of deprecated classnames
19
19
  schema: [
@@ -6,7 +6,7 @@ var noDeprecatedClassnames = {
6
6
  description: "No deprecated classnames allowed",
7
7
  recommended: false,
8
8
  // Make sure to replace the URL with the actual location of your rule's documentation
9
- url: "https://github.com/MetaMask/eslint-plugin-design-tokens?tab=readme-ov-file#eslint-plugin-design-tokens-"
9
+ url: "https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/no-deprecated-classnames.md"
10
10
  },
11
11
  // Define the schema to accept an object of deprecated classnames
12
12
  schema: [
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.preferThemeColorClassnames = void 0;
4
+ /**
5
+ * Rule to encourage the use of theme color class names instead of literal color class names.
6
+ */
7
+ exports.preferThemeColorClassnames = {
8
+ meta: {
9
+ type: 'suggestion',
10
+ docs: {
11
+ description: 'Encourage the use of theme color class names instead of literal color class names.',
12
+ recommended: true,
13
+ url: 'https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/prefer-theme-color-classnames.md',
14
+ },
15
+ // Adding a schema to accept options
16
+ schema: [
17
+ {
18
+ type: 'object',
19
+ properties: {
20
+ discouragedColors: {
21
+ type: 'array',
22
+ items: {
23
+ type: 'string',
24
+ minLength: 1, // This ensures that the string is not empty but does not prevent whitespace-only strings.
25
+ },
26
+ default: [
27
+ 'blue',
28
+ 'red',
29
+ 'gray',
30
+ 'slateGray',
31
+ 'green',
32
+ 'orange',
33
+ 'black',
34
+ 'white',
35
+ 'indigo',
36
+ 'yellow',
37
+ 'purple',
38
+ 'pink',
39
+ 'teal',
40
+ ],
41
+ },
42
+ },
43
+ additionalProperties: false,
44
+ },
45
+ ],
46
+ messages: {
47
+ discouragedColorUsage: `'{{className}}' usage of literal color class names is discouraged. Consider using theme color class names instead.`,
48
+ },
49
+ },
50
+ create(context) {
51
+ const options = context.options[0] || {};
52
+ // Default to a list of discouraged color names if none are provided in the options.
53
+ const discouragedColors = options.discouragedColors || [
54
+ 'blue',
55
+ 'red',
56
+ 'gray',
57
+ 'slateGray',
58
+ 'green',
59
+ 'orange',
60
+ 'black',
61
+ 'white',
62
+ 'indigo',
63
+ 'yellow',
64
+ 'purple',
65
+ 'pink',
66
+ 'teal',
67
+ ];
68
+ // Validate discouragedColors to ensure no empty or whitespace-only strings
69
+ const validColors = discouragedColors.filter((color) => color.trim().length > 0);
70
+ if (validColors.length === 0) {
71
+ return {}; // Exit early if no valid colors are provided
72
+ }
73
+ // Create a regex that matches class names containing any of the discouraged color names.
74
+ const colorRegex = new RegExp(`\\b(\\w+-)?(${discouragedColors.join('|')})-?\\d*\\b`, 'iu');
75
+ /**
76
+ * Checks if a given string value contains discouraged literal color names in CSS class names and reports them.
77
+ * This function is designed to handle both literals and template literals, capturing classes that violate the rules.
78
+ *
79
+ * @param value - The string value to check against the defined regex for discouraged color names. This could be the raw text from a Literal node or a segment of a TemplateLiteral node in the AST (Abstract Syntax Tree).
80
+ * @param node - The AST node associated with the value; used for reporting the ESLint error. This node object is part of ESLint's traversal of JavaScript code and provides context like location and type, which are used in reporting and potentially fixing violations.
81
+ */
82
+ function checkLiteralOrTemplateValue(value, node) {
83
+ // Return early if the string to be checked is empty or only whitespace
84
+ if (!value.trim()) {
85
+ return;
86
+ }
87
+ const matches = value.match(colorRegex);
88
+ if (matches?.[0]) {
89
+ context.report({
90
+ node,
91
+ messageId: 'discouragedColorUsage',
92
+ data: {
93
+ className: matches[0],
94
+ },
95
+ });
96
+ }
97
+ }
98
+ return {
99
+ Literal(node) {
100
+ if (typeof node.value === 'string') {
101
+ checkLiteralOrTemplateValue(node.value, node);
102
+ }
103
+ },
104
+ TemplateLiteral(node) {
105
+ node.quasis.forEach((quasi) => {
106
+ checkLiteralOrTemplateValue(quasi.value.raw, quasi);
107
+ });
108
+ },
109
+ };
110
+ },
111
+ };
@@ -0,0 +1,104 @@
1
+ // src/rules/prefer-theme-color-classnames.ts
2
+ var preferThemeColorClassnames = {
3
+ meta: {
4
+ type: "suggestion",
5
+ docs: {
6
+ description: "Encourage the use of theme color class names instead of literal color class names.",
7
+ recommended: true,
8
+ url: "https://github.com/MetaMask/eslint-plugin-design-tokens/blob/main/docs/rules/prefer-theme-color-classnames.md"
9
+ },
10
+ // Adding a schema to accept options
11
+ schema: [
12
+ {
13
+ type: "object",
14
+ properties: {
15
+ discouragedColors: {
16
+ type: "array",
17
+ items: {
18
+ type: "string",
19
+ minLength: 1
20
+ // This ensures that the string is not empty but does not prevent whitespace-only strings.
21
+ },
22
+ default: [
23
+ "blue",
24
+ "red",
25
+ "gray",
26
+ "slateGray",
27
+ "green",
28
+ "orange",
29
+ "black",
30
+ "white",
31
+ "indigo",
32
+ "yellow",
33
+ "purple",
34
+ "pink",
35
+ "teal"
36
+ ]
37
+ }
38
+ },
39
+ additionalProperties: false
40
+ }
41
+ ],
42
+ messages: {
43
+ discouragedColorUsage: `'{{className}}' usage of literal color class names is discouraged. Consider using theme color class names instead.`
44
+ }
45
+ },
46
+ create(context) {
47
+ const options = context.options[0] || {};
48
+ const discouragedColors = options.discouragedColors || [
49
+ "blue",
50
+ "red",
51
+ "gray",
52
+ "slateGray",
53
+ "green",
54
+ "orange",
55
+ "black",
56
+ "white",
57
+ "indigo",
58
+ "yellow",
59
+ "purple",
60
+ "pink",
61
+ "teal"
62
+ ];
63
+ const validColors = discouragedColors.filter(
64
+ (color) => color.trim().length > 0
65
+ );
66
+ if (validColors.length === 0) {
67
+ return {};
68
+ }
69
+ const colorRegex = new RegExp(
70
+ `\\b(\\w+-)?(${discouragedColors.join("|")})-?\\d*\\b`,
71
+ "iu"
72
+ );
73
+ function checkLiteralOrTemplateValue(value, node) {
74
+ if (!value.trim()) {
75
+ return;
76
+ }
77
+ const matches = value.match(colorRegex);
78
+ if (matches?.[0]) {
79
+ context.report({
80
+ node,
81
+ messageId: "discouragedColorUsage",
82
+ data: {
83
+ className: matches[0]
84
+ }
85
+ });
86
+ }
87
+ }
88
+ return {
89
+ Literal(node) {
90
+ if (typeof node.value === "string") {
91
+ checkLiteralOrTemplateValue(node.value, node);
92
+ }
93
+ },
94
+ TemplateLiteral(node) {
95
+ node.quasis.forEach((quasi) => {
96
+ checkLiteralOrTemplateValue(quasi.value.raw, quasi);
97
+ });
98
+ }
99
+ };
100
+ }
101
+ };
102
+ export {
103
+ preferThemeColorClassnames
104
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/eslint-plugin-design-tokens",
3
- "version": "0.1.0",
3
+ "version": "1.1.0",
4
4
  "description": "An ESLint plugin designed to enforce best practices and consistency for using MetaMask design tokens across various projects",
5
5
  "homepage": "https://github.com/MetaMask/eslint-plugin-design-tokens#readme",
6
6
  "bugs": {