@atlaskit/eslint-plugin-design-system 4.14.1 → 4.15.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 +12 -0
- package/constellation/index/examples.mdx +1 -1
- package/constellation/index/usage.mdx +1 -1
- package/dist/cjs/index.js +0 -11
- package/dist/cjs/rules/ensure-design-token-usage/index.js +1 -29
- package/dist/cjs/rules/ensure-design-token-usage-spacing/index.js +59 -71
- package/dist/cjs/rules/ensure-design-token-usage-spacing/utils.js +18 -90
- package/dist/cjs/rules/icon-label/index.js +0 -20
- package/dist/cjs/rules/no-banned-imports/index.js +1 -7
- package/dist/cjs/rules/no-deprecated-apis/index.js +8 -16
- package/dist/cjs/rules/no-deprecated-design-token-usage/index.js +0 -10
- package/dist/cjs/rules/no-deprecated-imports/index.js +8 -24
- package/dist/cjs/rules/no-deprecated-imports/paths.js +4 -5
- package/dist/cjs/rules/no-unsafe-design-token-usage/index.js +0 -24
- package/dist/cjs/rules/use-visually-hidden/fix-jsx.js +0 -10
- package/dist/cjs/rules/use-visually-hidden/fix-vanilla.js +0 -8
- package/dist/cjs/rules/use-visually-hidden/index.js +7 -32
- package/dist/cjs/rules/use-visually-hidden/utils.js +3 -17
- package/dist/cjs/rules/utils/get-import-node-by-source.js +0 -2
- package/dist/cjs/rules/utils/get-is-exception.js +0 -15
- package/dist/cjs/rules/utils/is-color.js +4 -17
- package/dist/cjs/rules/utils/is-elevation.js +0 -3
- package/dist/cjs/rules/utils/is-node.js +8 -40
- package/dist/cjs/rules/utils/is-token.js +2 -8
- package/dist/cjs/rules/utils/jsx.js +0 -1
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/rules/ensure-design-token-usage/index.js +1 -30
- package/dist/es2019/rules/ensure-design-token-usage-spacing/index.js +57 -55
- package/dist/es2019/rules/ensure-design-token-usage-spacing/utils.js +15 -67
- package/dist/es2019/rules/icon-label/index.js +0 -19
- package/dist/es2019/rules/no-banned-imports/index.js +0 -5
- package/dist/es2019/rules/no-deprecated-apis/index.js +8 -16
- package/dist/es2019/rules/no-deprecated-design-token-usage/index.js +0 -8
- package/dist/es2019/rules/no-deprecated-imports/index.js +8 -20
- package/dist/es2019/rules/no-deprecated-imports/paths.js +4 -3
- package/dist/es2019/rules/no-unsafe-design-token-usage/index.js +0 -17
- package/dist/es2019/rules/use-visually-hidden/fix-jsx.js +0 -4
- package/dist/es2019/rules/use-visually-hidden/fix-vanilla.js +0 -3
- package/dist/es2019/rules/use-visually-hidden/index.js +5 -26
- package/dist/es2019/rules/use-visually-hidden/utils.js +4 -7
- package/dist/es2019/rules/utils/get-is-exception.js +0 -13
- package/dist/es2019/rules/utils/is-color.js +4 -10
- package/dist/es2019/rules/utils/is-elevation.js +0 -1
- package/dist/es2019/rules/utils/is-node.js +8 -20
- package/dist/es2019/rules/utils/is-token.js +0 -2
- package/dist/es2019/version.json +1 -1
- package/dist/esm/rules/ensure-design-token-usage/index.js +1 -23
- package/dist/esm/rules/ensure-design-token-usage-spacing/index.js +60 -64
- package/dist/esm/rules/ensure-design-token-usage-spacing/utils.js +18 -70
- package/dist/esm/rules/icon-label/index.js +0 -17
- package/dist/esm/rules/no-banned-imports/index.js +1 -4
- package/dist/esm/rules/no-deprecated-apis/index.js +8 -13
- package/dist/esm/rules/no-deprecated-design-token-usage/index.js +0 -6
- package/dist/esm/rules/no-deprecated-imports/index.js +9 -21
- package/dist/esm/rules/no-deprecated-imports/paths.js +4 -3
- package/dist/esm/rules/no-unsafe-design-token-usage/index.js +0 -15
- package/dist/esm/rules/use-visually-hidden/fix-jsx.js +0 -4
- package/dist/esm/rules/use-visually-hidden/fix-vanilla.js +0 -3
- package/dist/esm/rules/use-visually-hidden/index.js +7 -25
- package/dist/esm/rules/use-visually-hidden/utils.js +5 -9
- package/dist/esm/rules/utils/get-is-exception.js +0 -13
- package/dist/esm/rules/utils/is-color.js +4 -10
- package/dist/esm/rules/utils/is-elevation.js +0 -1
- package/dist/esm/rules/utils/is-node.js +8 -21
- package/dist/esm/rules/utils/is-token.js +2 -4
- package/dist/esm/version.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @atlaskit/eslint-plugin-design-system
|
|
2
2
|
|
|
3
|
+
## 4.15.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`18aeca8c199`](https://bitbucket.org/atlassian/atlassian-frontend/commits/18aeca8c199) - Internal change to update token references. There is no expected behaviour or visual change.
|
|
8
|
+
|
|
9
|
+
## 4.15.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [`17b3c102180`](https://bitbucket.org/atlassian/atlassian-frontend/commits/17b3c102180) - ensure-design-token-usage-spacing only lints on spacing properties by default, with typography properties enabled via config
|
|
14
|
+
|
|
3
15
|
## 4.14.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -9,7 +9,7 @@ import SectionMessage from '@atlaskit/section-message';
|
|
|
9
9
|
appearance="warning"
|
|
10
10
|
>
|
|
11
11
|
<p>
|
|
12
|
-
This ESLint plugin includes rules to help you use <a href="/foundations/design-tokens">design tokens</a>, which will soon be the new way to
|
|
12
|
+
This ESLint plugin includes rules to help you use <a href="/foundations/design-tokens">design tokens</a>, which will soon be the new way to apply color and spacing values.
|
|
13
13
|
<br/><br/>Do not use the design token-related rules until design tokens are available for you to adopt.
|
|
14
14
|
</p>
|
|
15
15
|
</SectionMessage>
|
|
@@ -5,7 +5,7 @@ order: 2
|
|
|
5
5
|
This plugin contains rules that should be used with the Atlassian Design System.
|
|
6
6
|
Where possible, these rules come with auto fixers.
|
|
7
7
|
|
|
8
|
-
You can read more about configuring
|
|
8
|
+
You can read more about configuring ESLint in their [documentation](https://eslint.org/docs/user-guide/configuring).
|
|
9
9
|
|
|
10
10
|
## Configuration
|
|
11
11
|
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,30 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.rules = exports.configs = void 0;
|
|
9
|
-
|
|
10
8
|
var _ensureDesignTokenUsage = _interopRequireDefault(require("./rules/ensure-design-token-usage"));
|
|
11
|
-
|
|
12
9
|
var _ensureDesignTokenUsageSpacing = _interopRequireDefault(require("./rules/ensure-design-token-usage-spacing"));
|
|
13
|
-
|
|
14
10
|
var _iconLabel = _interopRequireDefault(require("./rules/icon-label"));
|
|
15
|
-
|
|
16
11
|
var _noBannedImports = _interopRequireDefault(require("./rules/no-banned-imports"));
|
|
17
|
-
|
|
18
12
|
var _noDeprecatedApis = _interopRequireDefault(require("./rules/no-deprecated-apis"));
|
|
19
|
-
|
|
20
13
|
var _noDeprecatedDesignTokenUsage = _interopRequireDefault(require("./rules/no-deprecated-design-token-usage"));
|
|
21
|
-
|
|
22
14
|
var _noDeprecatedImports = _interopRequireDefault(require("./rules/no-deprecated-imports"));
|
|
23
|
-
|
|
24
15
|
var _noUnsafeDesignTokenUsage = _interopRequireDefault(require("./rules/no-unsafe-design-token-usage"));
|
|
25
|
-
|
|
26
16
|
var _useVisuallyHidden = _interopRequireDefault(require("./rules/use-visually-hidden"));
|
|
27
|
-
|
|
28
17
|
var rules = {
|
|
29
18
|
'ensure-design-token-usage': _ensureDesignTokenUsage.default,
|
|
30
19
|
'icon-label': _iconLabel.default,
|
|
@@ -4,25 +4,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
9
|
-
|
|
10
8
|
var _getIsException = require("../utils/get-is-exception");
|
|
11
|
-
|
|
12
9
|
var _isColor = require("../utils/is-color");
|
|
13
|
-
|
|
14
10
|
var _isElevation = require("../utils/is-elevation");
|
|
15
|
-
|
|
16
11
|
var _isNode = require("../utils/is-node");
|
|
17
|
-
|
|
18
12
|
var defaultConfig = {
|
|
19
13
|
shouldEnforceFallbacks: false
|
|
20
14
|
};
|
|
21
|
-
|
|
22
15
|
var getNodeColumn = function getNodeColumn(node) {
|
|
23
16
|
return node.loc ? node.loc.start.column : 0;
|
|
24
17
|
};
|
|
25
|
-
|
|
26
18
|
var getTokenSuggestion = function getTokenSuggestion(node, reference, config) {
|
|
27
19
|
return [{
|
|
28
20
|
shouldReturnSuggestion: !(0, _isNode.isDecendantOfGlobalToken)(node) && config.shouldEnforceFallbacks === false,
|
|
@@ -38,12 +30,10 @@ var getTokenSuggestion = function getTokenSuggestion(node, reference, config) {
|
|
|
38
30
|
}
|
|
39
31
|
}].filter(filterSuggestion);
|
|
40
32
|
};
|
|
41
|
-
|
|
42
33
|
var filterSuggestion = function filterSuggestion(_ref) {
|
|
43
34
|
var shouldReturnSuggestion = _ref.shouldReturnSuggestion;
|
|
44
35
|
return shouldReturnSuggestion;
|
|
45
36
|
};
|
|
46
|
-
|
|
47
37
|
var rule = {
|
|
48
38
|
meta: {
|
|
49
39
|
// We need to upgrade the version of ESLint.
|
|
@@ -66,7 +56,6 @@ var rule = {
|
|
|
66
56
|
if (!(0, _isNode.isDecendantOfStyleBlock)(node)) {
|
|
67
57
|
return;
|
|
68
58
|
}
|
|
69
|
-
|
|
70
59
|
if (node.type === 'Identifier' && (0, _isColor.isLegacyNamedColor)(node.name) && !isException(node)) {
|
|
71
60
|
context.report({
|
|
72
61
|
messageId: 'hardCodedColor',
|
|
@@ -80,9 +69,7 @@ var rule = {
|
|
|
80
69
|
if (isException(node) || (0, _isNode.isDecendantOfGlobalToken)(node) || (0, _isNode.isDecendantOfType)(node, 'ImportDeclaration') || (0, _isNode.isPropertyKey)(node) || (0, _isNode.isVariableName)(node)) {
|
|
81
70
|
return;
|
|
82
71
|
}
|
|
83
|
-
|
|
84
72
|
var isNodeLegacyColor = (0, _isColor.isLegacyColor)(node.name);
|
|
85
|
-
|
|
86
73
|
if (isNodeLegacyColor || (0, _isColor.isHardCodedColor)(node.name)) {
|
|
87
74
|
if (node.parent.type === 'MemberExpression') {
|
|
88
75
|
if (node.parent.object.type === 'Identifier') {
|
|
@@ -96,10 +83,8 @@ var rule = {
|
|
|
96
83
|
});
|
|
97
84
|
}
|
|
98
85
|
}
|
|
99
|
-
|
|
100
86
|
return;
|
|
101
87
|
}
|
|
102
|
-
|
|
103
88
|
context.report({
|
|
104
89
|
messageId: 'hardCodedColor',
|
|
105
90
|
node: node,
|
|
@@ -107,9 +92,7 @@ var rule = {
|
|
|
107
92
|
});
|
|
108
93
|
return;
|
|
109
94
|
}
|
|
110
|
-
|
|
111
95
|
var elevation = (0, _isElevation.isLegacyElevation)(node.name);
|
|
112
|
-
|
|
113
96
|
if (elevation) {
|
|
114
97
|
context.report({
|
|
115
98
|
messageId: 'legacyElevation',
|
|
@@ -121,7 +104,6 @@ var rule = {
|
|
|
121
104
|
if ((0, _isNode.isChildOfType)(node, 'TemplateLiteral') && node.range) {
|
|
122
105
|
return fixer.replaceTextRange([node.range[0] - 2, node.range[1] + 1], "background-color: ${token('".concat(elevation.background, "')};\n").concat(' '.repeat(getNodeColumn(node) - 2), "box-shadow: ${token('").concat(elevation.shadow, "')}"));
|
|
123
106
|
}
|
|
124
|
-
|
|
125
107
|
return null;
|
|
126
108
|
}
|
|
127
109
|
});
|
|
@@ -131,16 +113,15 @@ var rule = {
|
|
|
131
113
|
if (node.type !== 'TaggedTemplateExpression') {
|
|
132
114
|
return;
|
|
133
115
|
}
|
|
134
|
-
|
|
135
116
|
var value = node.quasi.quasis.map(function (q) {
|
|
136
117
|
return q.value.raw;
|
|
137
118
|
}).join('');
|
|
119
|
+
|
|
138
120
|
/**
|
|
139
121
|
* Attempts to remove all non-essential words & characters from a style block.
|
|
140
122
|
* Including selectors, queries and property names, leaving only values
|
|
141
123
|
* This is necessary to avoid cases where a property might have a color in its name ie. "white-space"
|
|
142
124
|
*/
|
|
143
|
-
|
|
144
125
|
var cssProperties = value.replace(/\n/g, '').split(/;|{|}/).map(function (el) {
|
|
145
126
|
return el.trim().split(':').pop() || '';
|
|
146
127
|
});
|
|
@@ -158,11 +139,9 @@ var rule = {
|
|
|
158
139
|
if (node.type !== 'Literal' || typeof node.value !== 'string') {
|
|
159
140
|
return;
|
|
160
141
|
}
|
|
161
|
-
|
|
162
142
|
if (!(0, _isNode.isDecendantOfStyleBlock)(node) && !(0, _isNode.isDecendantOfStyleJsxAttribute)(node)) {
|
|
163
143
|
return;
|
|
164
144
|
}
|
|
165
|
-
|
|
166
145
|
if (((0, _isColor.isHardCodedColor)(node.value) || (0, _isColor.includesHardCodedColor)(node.value)) && !isException(node)) {
|
|
167
146
|
context.report({
|
|
168
147
|
messageId: 'hardCodedColor',
|
|
@@ -176,15 +155,12 @@ var rule = {
|
|
|
176
155
|
if (node.type !== 'CallExpression' || node.callee.type !== 'Identifier') {
|
|
177
156
|
return;
|
|
178
157
|
}
|
|
179
|
-
|
|
180
158
|
if (!(0, _isNode.isDecendantOfStyleBlock)(node) && !(0, _isNode.isDecendantOfStyleJsxAttribute)(node)) {
|
|
181
159
|
return;
|
|
182
160
|
}
|
|
183
|
-
|
|
184
161
|
if (!(0, _isColor.isLegacyNamedColor)(node.callee.name) || (0, _isNode.isDecendantOfGlobalToken)(node) || isException(node)) {
|
|
185
162
|
return;
|
|
186
163
|
}
|
|
187
|
-
|
|
188
164
|
context.report({
|
|
189
165
|
messageId: 'hardCodedColor',
|
|
190
166
|
node: node,
|
|
@@ -195,18 +171,14 @@ var rule = {
|
|
|
195
171
|
if (!node.value) {
|
|
196
172
|
return;
|
|
197
173
|
}
|
|
198
|
-
|
|
199
174
|
if (['alt', 'src', 'label'].includes(node.name.name)) {
|
|
200
175
|
return;
|
|
201
176
|
}
|
|
202
|
-
|
|
203
177
|
if (node.value.type === 'Literal') {
|
|
204
178
|
if (isException(node)) {
|
|
205
179
|
return;
|
|
206
180
|
}
|
|
207
|
-
|
|
208
181
|
var literalValue = node.value.value;
|
|
209
|
-
|
|
210
182
|
if ((0, _isColor.isHardCodedColor)(literalValue) || (0, _isColor.includesHardCodedColor)(literalValue)) {
|
|
211
183
|
context.report({
|
|
212
184
|
messageId: 'hardCodedColor',
|
|
@@ -1,28 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
-
|
|
12
9
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
13
|
-
|
|
14
10
|
var _eslintCodemodUtils = require("eslint-codemod-utils");
|
|
15
|
-
|
|
16
11
|
var _tokensRaw = require("@atlaskit/tokens/tokens-raw");
|
|
17
|
-
|
|
18
12
|
var _isNode = require("../utils/is-node");
|
|
19
|
-
|
|
20
13
|
var _utils = require("./utils");
|
|
21
|
-
|
|
22
14
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
23
|
-
|
|
24
15
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
25
|
-
|
|
26
16
|
/**
|
|
27
17
|
* Currently we have a wide range of experimental spacing tokens that we are testing.
|
|
28
18
|
* We only want transforms to apply to the stable scale values, not the rest.
|
|
@@ -31,7 +21,6 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
31
21
|
var onlyScaleTokens = _tokensRaw.spacing.filter(function (token) {
|
|
32
22
|
return token.name.startsWith('space.');
|
|
33
23
|
});
|
|
34
|
-
|
|
35
24
|
var spacingValueToToken = Object.fromEntries(onlyScaleTokens.map(function (token) {
|
|
36
25
|
return [token.attributes['pixelValue'], token.name];
|
|
37
26
|
}));
|
|
@@ -46,17 +35,17 @@ var typographyValueToToken = Object.fromEntries(_tokensRaw.typography.map(functi
|
|
|
46
35
|
return token.length;
|
|
47
36
|
}))];
|
|
48
37
|
}));
|
|
38
|
+
|
|
49
39
|
/**
|
|
50
40
|
* Returns a token node for a given value including fallbacks.
|
|
51
41
|
* @param propertyName camelCase CSS property
|
|
52
42
|
* @param value string representing pixel value, or font family, or number representing font weight
|
|
53
43
|
* @example
|
|
54
44
|
* ```
|
|
55
|
-
* propertyName: padding, value: '8px' => token('
|
|
45
|
+
* propertyName: padding, value: '8px' => token('space.100', '8px')
|
|
56
46
|
* propertyName: fontWeight, value: 400 => token('font.weight.regular', '400')
|
|
57
47
|
* ```
|
|
58
48
|
*/
|
|
59
|
-
|
|
60
49
|
function getTokenNodeForValue(propertyName, value) {
|
|
61
50
|
var token = (0, _utils.isTypographyProperty)(propertyName) ? typographyValueToToken[propertyName][value] : spacingValueToToken[value];
|
|
62
51
|
var fallbackValue = propertyName === 'fontFamily' ? "\"".concat(value, "\"") : "'".concat(value, "'");
|
|
@@ -71,6 +60,25 @@ function getTokenNodeForValue(propertyName, value) {
|
|
|
71
60
|
});
|
|
72
61
|
}
|
|
73
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Returns a boolean that signals wether the current property is revelant under the current configuration
|
|
65
|
+
* @param propertyName camelCase CSS property
|
|
66
|
+
* @param targetOptions Array containing the types of properties that should be included in the rule
|
|
67
|
+
* @example
|
|
68
|
+
* ```
|
|
69
|
+
* propertyName: padding, targetOptions: ['spacing']
|
|
70
|
+
* propertyName: fontWeight, targetOptions: ['spacing', 'typography']
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
function shouldAnalyzeProperty(propertyName, targetOptions) {
|
|
74
|
+
if ((0, _utils.isSpacingProperty)(propertyName) && targetOptions.includes('spacing')) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
if ((0, _utils.isTypographyProperty)(propertyName) && targetOptions.includes('typography')) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
74
82
|
var rule = {
|
|
75
83
|
meta: {
|
|
76
84
|
type: 'problem',
|
|
@@ -85,6 +93,12 @@ var rule = {
|
|
|
85
93
|
}
|
|
86
94
|
},
|
|
87
95
|
create: function create(context) {
|
|
96
|
+
var _context$options$;
|
|
97
|
+
var targetCategories = ['spacing'];
|
|
98
|
+
var configCategories = (_context$options$ = context.options[0]) === null || _context$options$ === void 0 ? void 0 : _context$options$.addons;
|
|
99
|
+
if (Array.isArray(configCategories) && configCategories.includes('typography')) {
|
|
100
|
+
targetCategories.push('typography');
|
|
101
|
+
}
|
|
88
102
|
return {
|
|
89
103
|
// CSSObjectExpression
|
|
90
104
|
// const styles = css({ color: 'red', margin: '4px' })
|
|
@@ -92,51 +106,42 @@ var rule = {
|
|
|
92
106
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(parentNode, 'ObjectExpression')) {
|
|
93
107
|
return;
|
|
94
108
|
}
|
|
109
|
+
|
|
95
110
|
/**
|
|
96
111
|
* We do this in case the fontSize for a style object is declared alongside the `em` or `lineHeight` declaration
|
|
97
112
|
*/
|
|
98
|
-
|
|
99
|
-
|
|
100
113
|
var fontSizeNode = parentNode.properties.find(function (node) {
|
|
101
114
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'Property')) {
|
|
102
115
|
return;
|
|
103
116
|
}
|
|
104
|
-
|
|
105
117
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.key, 'Identifier')) {
|
|
106
118
|
return;
|
|
107
119
|
}
|
|
108
|
-
|
|
109
120
|
return node.key.name === 'fontSize';
|
|
110
121
|
});
|
|
111
|
-
var fontSizeValue = (0, _utils.getValue)(
|
|
122
|
+
var fontSizeValue = (0, _utils.getValue)(
|
|
123
|
+
// @ts-expect-error
|
|
112
124
|
(0, _eslintCodemodUtils.isNodeOfType)(fontSizeNode, 'Property') && fontSizeNode.value, context);
|
|
113
125
|
var fontSize = Array.isArray(fontSizeValue) ? fontSizeValue[0] : fontSizeValue;
|
|
114
|
-
|
|
115
126
|
function findObjectStyles(node) {
|
|
116
127
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(node, 'Property')) {
|
|
117
128
|
return;
|
|
118
129
|
}
|
|
119
|
-
|
|
120
130
|
if ((0, _eslintCodemodUtils.isNodeOfType)(node.value, 'ObjectExpression')) {
|
|
121
131
|
return node.value.properties.forEach(findObjectStyles);
|
|
122
132
|
}
|
|
123
|
-
|
|
124
133
|
if (!(0, _eslintCodemodUtils.isNodeOfType)(node.key, 'Identifier')) {
|
|
125
134
|
return;
|
|
126
135
|
}
|
|
127
|
-
|
|
128
|
-
if (!(0, _utils.isSpacingProperty)(node.key.name)) {
|
|
136
|
+
if (!shouldAnalyzeProperty(node.key.name, targetCategories)) {
|
|
129
137
|
return;
|
|
130
138
|
}
|
|
131
|
-
|
|
132
139
|
if ((0, _isNode.isDecendantOfGlobalToken)(node.value)) {
|
|
133
140
|
return;
|
|
134
141
|
}
|
|
135
|
-
|
|
136
142
|
if ((0, _eslintCodemodUtils.isNodeOfType)(node.value, 'TemplateLiteral') && node.value.expressions.some(_isNode.isDecendantOfGlobalToken)) {
|
|
137
143
|
return;
|
|
138
144
|
}
|
|
139
|
-
|
|
140
145
|
if (node.value.type === 'Literal' && !(0, _utils.isValidSpacingValue)(node.value.value, fontSize)) {
|
|
141
146
|
context.report({
|
|
142
147
|
node: node,
|
|
@@ -147,11 +152,11 @@ var rule = {
|
|
|
147
152
|
});
|
|
148
153
|
return;
|
|
149
154
|
}
|
|
150
|
-
|
|
151
155
|
var propertyName = node.key.name;
|
|
152
156
|
var isFontFamily = /fontFamily/.test(propertyName);
|
|
153
|
-
var value = (0, _utils.getValue)(node.value, context);
|
|
157
|
+
var value = (0, _utils.getValue)(node.value, context);
|
|
154
158
|
|
|
159
|
+
// value is either NaN or it can't be resolved eg, em, 100% etc...
|
|
155
160
|
if (!(value && (0, _utils.isValidSpacingValue)(value, fontSize))) {
|
|
156
161
|
return context.report({
|
|
157
162
|
node: node,
|
|
@@ -161,14 +166,13 @@ var rule = {
|
|
|
161
166
|
}
|
|
162
167
|
});
|
|
163
168
|
}
|
|
169
|
+
var values = Array.isArray(value) ? value : [value];
|
|
164
170
|
|
|
165
|
-
|
|
171
|
+
// value is a single value so we can apply a more robust approach to our fix
|
|
166
172
|
// treat fontFamily as having one value
|
|
167
|
-
|
|
168
173
|
if (values.length === 1 || isFontFamily) {
|
|
169
174
|
var _values = (0, _slicedToArray2.default)(values, 1),
|
|
170
|
-
|
|
171
|
-
|
|
175
|
+
_value = _values[0];
|
|
172
176
|
var pixelValue = isFontFamily ? _value : (0, _utils.emToPixels)(_value, fontSize);
|
|
173
177
|
return context.report({
|
|
174
178
|
node: node,
|
|
@@ -178,19 +182,15 @@ var rule = {
|
|
|
178
182
|
},
|
|
179
183
|
fix: function fix(fixer) {
|
|
180
184
|
var _node$loc;
|
|
181
|
-
|
|
182
|
-
if (!(0, _utils.isSpacingProperty)(propertyName)) {
|
|
185
|
+
if (!shouldAnalyzeProperty(propertyName, targetCategories)) {
|
|
183
186
|
return null;
|
|
184
187
|
}
|
|
185
|
-
|
|
186
188
|
var pixelValueString = "".concat(pixelValue, "px");
|
|
187
189
|
var lookupValue = /fontWeight|fontFamily/.test(propertyName) ? pixelValue : pixelValueString;
|
|
188
190
|
var tokenName = (0, _utils.isTypographyProperty)(propertyName) ? typographyValueToToken[propertyName][lookupValue] : spacingValueToToken[lookupValue];
|
|
189
|
-
|
|
190
191
|
if (!tokenName) {
|
|
191
192
|
return null;
|
|
192
193
|
}
|
|
193
|
-
|
|
194
194
|
var replacementValue = getTokenNodeForValue(propertyName, lookupValue);
|
|
195
195
|
return [fixer.insertTextBefore(node, "// TODO Delete this comment after verifying spacing token -> previous value `".concat((0, _eslintCodemodUtils.node)(node.value), "`\n").concat(' '.padStart(((_node$loc = node.loc) === null || _node$loc === void 0 ? void 0 : _node$loc.start.column) || 0))), fixer.replaceText(node, (0, _eslintCodemodUtils.property)(_objectSpread(_objectSpread({}, node), {}, {
|
|
196
196
|
value: replacementValue
|
|
@@ -198,6 +198,7 @@ var rule = {
|
|
|
198
198
|
}
|
|
199
199
|
});
|
|
200
200
|
}
|
|
201
|
+
|
|
201
202
|
/**
|
|
202
203
|
* Compound values are hard to deal with / replace because we need to find/replace strings inside an
|
|
203
204
|
* estree node.
|
|
@@ -205,8 +206,6 @@ var rule = {
|
|
|
205
206
|
* @example
|
|
206
207
|
* { padding: '8px 0px' } // two values we don't try and apply the fixer
|
|
207
208
|
*/
|
|
208
|
-
|
|
209
|
-
|
|
210
209
|
values.forEach(function (val, index) {
|
|
211
210
|
var pixelValue = (0, _utils.emToPixels)(val, fontSize);
|
|
212
211
|
context.report({
|
|
@@ -219,11 +218,9 @@ var rule = {
|
|
|
219
218
|
var allResolvableValues = values.every(function (value) {
|
|
220
219
|
return !Number.isNaN((0, _utils.emToPixels)(value, fontSize));
|
|
221
220
|
});
|
|
222
|
-
|
|
223
221
|
if (!allResolvableValues) {
|
|
224
222
|
return null;
|
|
225
223
|
}
|
|
226
|
-
|
|
227
224
|
return fixer.replaceText(node.value, "`".concat(values.map(function (value) {
|
|
228
225
|
var pixelValue = (0, _utils.emToPixels)(value, fontSize);
|
|
229
226
|
var pixelValueString = "".concat(pixelValue, "px");
|
|
@@ -233,7 +230,6 @@ var rule = {
|
|
|
233
230
|
});
|
|
234
231
|
});
|
|
235
232
|
}
|
|
236
|
-
|
|
237
233
|
parentNode.properties.forEach(findObjectStyles);
|
|
238
234
|
},
|
|
239
235
|
// CSSTemplateLiteral and StyledTemplateLiteral
|
|
@@ -243,46 +239,43 @@ var rule = {
|
|
|
243
239
|
if (node.type !== 'TaggedTemplateExpression') {
|
|
244
240
|
return;
|
|
245
241
|
}
|
|
246
|
-
|
|
247
242
|
var parentNode = (0, _utils.findParentNodeForLine)(node);
|
|
248
243
|
var combinedString = node.quasi.quasis.map(function (q, i) {
|
|
249
244
|
return "".concat(q.value.raw).concat(node.quasi.expressions[i] ? (0, _utils.getValue)(node.quasi.expressions[i], context) : '');
|
|
250
245
|
}).join('');
|
|
246
|
+
|
|
251
247
|
/**
|
|
252
248
|
* Attempts to remove all non-essential words & characters from a style block.
|
|
253
249
|
* Including selectors and queries
|
|
254
250
|
* Adapted from ensure-design-token-usage
|
|
255
251
|
*/
|
|
256
|
-
|
|
257
252
|
var cssProperties = combinedString.split('\n').filter(function (line) {
|
|
258
253
|
return !line.trim().startsWith('@');
|
|
259
254
|
}).join('\n').replace(/\n/g, '').split(/;|(?<!\$){|(?<!\${.+?)}/) // don't split on template literal expressions i.e. `${...}`
|
|
260
255
|
.map(function (el) {
|
|
261
256
|
return el.trim() || '';
|
|
262
|
-
}).filter(Boolean);
|
|
257
|
+
}).filter(Boolean);
|
|
263
258
|
|
|
259
|
+
// Get font size
|
|
264
260
|
var fontSizeNode = cssProperties.find(function (style) {
|
|
265
261
|
var _style$split = style.split(':'),
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
262
|
+
_style$split2 = (0, _slicedToArray2.default)(_style$split, 2),
|
|
263
|
+
rawProperty = _style$split2[0],
|
|
264
|
+
value = _style$split2[1];
|
|
270
265
|
return /font-size/.test(rawProperty) ? value : null;
|
|
271
266
|
});
|
|
272
267
|
var fontSize = (0, _utils.getValueFromShorthand)(fontSizeNode)[0];
|
|
273
268
|
cssProperties.forEach(function (style) {
|
|
274
269
|
var _style$split3 = style.split(':'),
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
270
|
+
_style$split4 = (0, _slicedToArray2.default)(_style$split3, 2),
|
|
271
|
+
rawProperty = _style$split4[0],
|
|
272
|
+
value = _style$split4[1];
|
|
279
273
|
var propertyName = (0, _utils.convertHyphenatedNameToCamelCase)(rawProperty);
|
|
280
|
-
|
|
281
|
-
if (!(0, _utils.isSpacingProperty)(propertyName)) {
|
|
274
|
+
if (!shouldAnalyzeProperty(propertyName, targetCategories)) {
|
|
282
275
|
return;
|
|
283
|
-
}
|
|
284
|
-
|
|
276
|
+
}
|
|
285
277
|
|
|
278
|
+
// value is either NaN or it can't be resolved eg, em, 100% etc...
|
|
286
279
|
if (!(0, _utils.isValidSpacingValue)(value, fontSize)) {
|
|
287
280
|
return context.report({
|
|
288
281
|
node: node,
|
|
@@ -292,13 +285,11 @@ var rule = {
|
|
|
292
285
|
}
|
|
293
286
|
});
|
|
294
287
|
}
|
|
295
|
-
|
|
296
288
|
var values = (0, _utils.getValueFromShorthand)(value);
|
|
297
289
|
values.forEach(function (val, index) {
|
|
298
|
-
if (!val && val !== 0 || !(
|
|
290
|
+
if (!val && val !== 0 || !shouldAnalyzeProperty(propertyName, targetCategories)) {
|
|
299
291
|
return;
|
|
300
292
|
}
|
|
301
|
-
|
|
302
293
|
var isFontFamily = /fontFamily/.test(propertyName);
|
|
303
294
|
var pixelValue = isFontFamily ? val : (0, _utils.emToPixels)(val, fontSize);
|
|
304
295
|
context.report({
|
|
@@ -311,39 +302,36 @@ var rule = {
|
|
|
311
302
|
var allResolvableValues = values.every(function (value) {
|
|
312
303
|
return !Number.isNaN((0, _utils.emToPixels)(value, fontSize));
|
|
313
304
|
});
|
|
314
|
-
|
|
315
305
|
if (!allResolvableValues) {
|
|
316
306
|
return null;
|
|
317
307
|
}
|
|
318
|
-
|
|
319
308
|
var replacementValue = values.map(function (value) {
|
|
320
309
|
var propertyValue = typeof value === 'string' ? value.trim() : value;
|
|
321
310
|
var pixelValue = isFontFamily ? propertyValue : (0, _utils.emToPixels)(propertyValue, fontSize);
|
|
322
311
|
var pixelValueString = "".concat(pixelValue, "px");
|
|
323
312
|
var lookupValue = /fontWeight|fontFamily/.test(propertyName) ? pixelValue : pixelValueString;
|
|
324
313
|
var tokenName = (0, _utils.isTypographyProperty)(propertyName) ? typographyValueToToken[propertyName][lookupValue] : spacingValueToToken[lookupValue];
|
|
325
|
-
|
|
326
314
|
if (!tokenName) {
|
|
327
315
|
return pixelValueString;
|
|
328
316
|
}
|
|
317
|
+
var replacementTokenValue = getTokenNodeForValue(propertyName, lookupValue);
|
|
329
318
|
|
|
330
|
-
|
|
331
|
-
|
|
319
|
+
// ${token('...', '...')}
|
|
332
320
|
var replacementSubValue = '${' + replacementTokenValue.toString() + '}';
|
|
333
321
|
return replacementSubValue;
|
|
334
|
-
}).join(' ');
|
|
335
|
-
|
|
336
|
-
var textForSource = context.getSourceCode().getText(node.quasi); // find `<property>: ...;` in original
|
|
322
|
+
}).join(' ');
|
|
337
323
|
|
|
338
|
-
|
|
324
|
+
// get original source
|
|
325
|
+
var textForSource = context.getSourceCode().getText(node.quasi);
|
|
339
326
|
|
|
327
|
+
// find `<property>: ...;` in original
|
|
328
|
+
var searchRegExp = new RegExp(style, 'g');
|
|
329
|
+
// replace property:val with new property:val
|
|
340
330
|
var replacement = textForSource.replace(searchRegExp, // padding: ${gridSize()}px;
|
|
341
331
|
"".concat(rawProperty, ": ").concat(replacementValue));
|
|
342
|
-
|
|
343
332
|
if (!replacement) {
|
|
344
333
|
return [];
|
|
345
334
|
}
|
|
346
|
-
|
|
347
335
|
return [fixer.insertTextBefore(parentNode, "// TODO Delete this comment after verifying spacing token -> previous value `".concat(value.trim(), "`\n")), fixer.replaceText(node.quasi, replacement)];
|
|
348
336
|
} : undefined
|
|
349
337
|
});
|