@metamask/eslint-plugin-design-tokens 1.0.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,6 +7,14 @@ 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
+
10
18
  ## [1.0.0]
11
19
 
12
20
  ### Added
@@ -19,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
19
27
 
20
28
  - Initial release
21
29
 
22
- [Unreleased]: https://github.com/MetaMask/eslint-plugin-design-tokens/compare/v1.0.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
23
32
  [1.0.0]: https://github.com/MetaMask/eslint-plugin-design-tokens/compare/v0.1.0...v1.0.0
24
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
@@ -5,4 +5,5 @@ const rules_1 = require("./rules");
5
5
  exports.rules = {
6
6
  'no-deprecated-classnames': rules_1.noDeprecatedClassnames,
7
7
  'color-no-hex': rules_1.colorNoHex,
8
+ 'prefer-theme-color-classnames': rules_1.preferThemeColorClassnames,
8
9
  };
package/dist/index.mjs CHANGED
@@ -51,6 +51,108 @@ 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
+
54
156
  // src/rules/color-no-hex.ts
55
157
  var hexColorRegex = /(#\p{Hex_Digit}{3,6})\b/u;
56
158
  var colorNoHex = {
@@ -97,7 +199,8 @@ var colorNoHex = {
97
199
  // src/index.ts
98
200
  var rules = {
99
201
  "no-deprecated-classnames": noDeprecatedClassnames,
100
- "color-no-hex": colorNoHex
202
+ "color-no-hex": colorNoHex,
203
+ "prefer-theme-color-classnames": preferThemeColorClassnames
101
204
  };
102
205
  export {
103
206
  rules
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.colorNoHex = 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; } });
6
8
  var color_no_hex_1 = require("./color-no-hex");
7
9
  Object.defineProperty(exports, "colorNoHex", { enumerable: true, get: function () { return color_no_hex_1.colorNoHex; } });
@@ -51,6 +51,108 @@ 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
+
54
156
  // src/rules/color-no-hex.ts
55
157
  var hexColorRegex = /(#\p{Hex_Digit}{3,6})\b/u;
56
158
  var colorNoHex = {
@@ -95,5 +197,6 @@ var colorNoHex = {
95
197
  };
96
198
  export {
97
199
  colorNoHex,
98
- noDeprecatedClassnames
200
+ noDeprecatedClassnames,
201
+ preferThemeColorClassnames
99
202
  };
@@ -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": "1.0.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": {