@atlaskit/codemod-cli 0.13.3 → 0.14.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 +12 -0
- package/README.md +117 -3
- package/dist/cjs/cli.js +70 -75
- package/dist/cjs/filepath.js +65 -29
- package/dist/cjs/index.js +3 -3
- package/dist/cjs/main.js +329 -167
- package/dist/cjs/presets/css-to-design-tokens/css-to-design-tokens.js +86 -34
- package/dist/cjs/presets/css-to-design-tokens/utils/legacy-colors.js +3 -3
- package/dist/cjs/presets/css-to-design-tokens/utils/meta.js +19 -6
- package/dist/cjs/presets/index.js +3 -1
- package/dist/cjs/presets/styled-to-emotion/styled-to-emotion.js +19 -12
- package/dist/cjs/presets/theme-remove-deprecated-mixins/theme-remove-deprecated-mixins.js +51 -36
- package/dist/cjs/presets/theme-remove-deprecated-mixins/utils/replacements.js +25 -25
- package/dist/cjs/presets/theme-to-design-tokens/theme-to-design-tokens.js +378 -114
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast-meta.js +33 -18
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast.js +1 -1
- package/dist/cjs/presets/theme-to-design-tokens/utils/color.js +25 -17
- package/dist/cjs/presets/theme-to-design-tokens/utils/css-utils.js +38 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/fuzzy-search.js +10 -6
- package/dist/cjs/presets/theme-to-design-tokens/utils/legacy-colors.js +3 -3
- package/dist/cjs/presets/theme-to-design-tokens/utils/named-colors.js +1 -1
- package/dist/cjs/presets/theme-to-design-tokens/utils/string-utils.js +26 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/tokens.js +16 -2
- package/dist/cjs/sinceRef.js +69 -35
- package/dist/cjs/transforms.js +44 -26
- package/dist/cjs/types.js +27 -3
- package/dist/cjs/utils.js +6 -6
- package/dist/es2019/cli.js +4 -0
- package/dist/es2019/main.js +2 -0
- package/dist/es2019/presets/css-to-design-tokens/css-to-design-tokens.js +19 -8
- package/dist/es2019/presets/theme-to-design-tokens/theme-to-design-tokens.js +191 -32
- package/dist/es2019/presets/theme-to-design-tokens/utils/ast.js +1 -1
- package/dist/es2019/presets/theme-to-design-tokens/utils/color.js +12 -10
- package/dist/es2019/presets/theme-to-design-tokens/utils/css-utils.js +31 -0
- package/dist/es2019/presets/theme-to-design-tokens/utils/string-utils.js +13 -0
- package/dist/es2019/sinceRef.js +1 -0
- package/dist/esm/cli.js +4 -0
- package/dist/esm/main.js +3 -1
- package/dist/esm/presets/css-to-design-tokens/css-to-design-tokens.js +23 -12
- package/dist/esm/presets/theme-to-design-tokens/theme-to-design-tokens.js +346 -100
- package/dist/esm/presets/theme-to-design-tokens/utils/ast.js +1 -1
- package/dist/esm/presets/theme-to-design-tokens/utils/color.js +12 -10
- package/dist/esm/presets/theme-to-design-tokens/utils/css-utils.js +31 -0
- package/dist/esm/presets/theme-to-design-tokens/utils/string-utils.js +17 -0
- package/dist/esm/sinceRef.js +1 -0
- package/dist/types/presets/css-to-design-tokens/css-to-design-tokens.d.ts +2 -1
- package/dist/types/presets/theme-to-design-tokens/theme-to-design-tokens.d.ts +1 -1
- package/dist/types/presets/theme-to-design-tokens/utils/ast.d.ts +1 -1
- package/dist/types/presets/theme-to-design-tokens/utils/color.d.ts +2 -1
- package/dist/types/presets/theme-to-design-tokens/utils/css-utils.d.ts +2 -0
- package/dist/types/presets/theme-to-design-tokens/utils/string-utils.d.ts +3 -0
- package/dist/types-ts4.5/presets/css-to-design-tokens/css-to-design-tokens.d.ts +2 -1
- package/dist/types-ts4.5/presets/theme-to-design-tokens/theme-to-design-tokens.d.ts +1 -1
- package/dist/types-ts4.5/presets/theme-to-design-tokens/utils/ast.d.ts +1 -1
- package/dist/types-ts4.5/presets/theme-to-design-tokens/utils/color.d.ts +2 -1
- package/dist/types-ts4.5/presets/theme-to-design-tokens/utils/css-utils.d.ts +2 -0
- package/dist/types-ts4.5/presets/theme-to-design-tokens/utils/string-utils.d.ts +6 -0
- package/package.json +2 -2
- package/dist/cjs/version.json +0 -4
- package/dist/es2019/version.json +0 -4
- package/dist/esm/version.json +0 -4
|
@@ -5,6 +5,10 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.default = transformer;
|
|
8
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
9
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
10
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
11
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
8
12
|
var _utils = require("@codeshift/utils");
|
|
9
13
|
var _ast = require("./utils/ast");
|
|
10
14
|
var _astMeta = require("./utils/ast-meta");
|
|
@@ -12,26 +16,43 @@ var _color = require("./utils/color");
|
|
|
12
16
|
var _fuzzySearch = _interopRequireDefault(require("./utils/fuzzy-search"));
|
|
13
17
|
var _legacyColors = require("./utils/legacy-colors");
|
|
14
18
|
var _tokens = require("./utils/tokens");
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
19
|
+
var _stringUtils = require("./utils/string-utils");
|
|
20
|
+
var _cssUtils = require("./utils/css-utils");
|
|
21
|
+
var _cssToDesignTokens = _interopRequireDefault(require("../css-to-design-tokens/css-to-design-tokens"));
|
|
22
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
23
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
24
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } /* eslint-disable no-console */
|
|
22
25
|
function insertTokenImport(j, source) {
|
|
23
26
|
if ((0, _utils.hasImportDeclaration)(j, source, '@atlaskit/tokens')) {
|
|
24
27
|
return;
|
|
25
28
|
}
|
|
26
|
-
|
|
29
|
+
var newImport = j.importDeclaration([j.importSpecifier(j.identifier('token'))], j.stringLiteral('@atlaskit/tokens'));
|
|
27
30
|
source.get().node.program.body.unshift(newImport);
|
|
28
31
|
}
|
|
29
32
|
function buildToken(j, tokenId, node) {
|
|
30
|
-
|
|
33
|
+
var callExpr = j.callExpression(j.identifier('token'), [j.stringLiteral(tokenId), node].filter(Boolean));
|
|
31
34
|
return callExpr;
|
|
32
35
|
}
|
|
36
|
+
|
|
37
|
+
// Wrap over the j.templateElement builder to provide a more convenient API.
|
|
38
|
+
function buildTemplateElement(j, text) {
|
|
39
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
|
|
40
|
+
tail: false,
|
|
41
|
+
fromNode: null
|
|
42
|
+
};
|
|
43
|
+
var tail;
|
|
44
|
+
if (options.fromNode) {
|
|
45
|
+
tail = options.fromNode.tail;
|
|
46
|
+
} else {
|
|
47
|
+
tail = !!options.tail;
|
|
48
|
+
}
|
|
49
|
+
return j.templateElement({
|
|
50
|
+
raw: text,
|
|
51
|
+
cooked: null
|
|
52
|
+
}, tail);
|
|
53
|
+
}
|
|
33
54
|
function getColorFromIdentifier(expression) {
|
|
34
|
-
|
|
55
|
+
var value = '';
|
|
35
56
|
if (expression.type === 'Identifier') {
|
|
36
57
|
value = expression.name;
|
|
37
58
|
}
|
|
@@ -44,9 +65,9 @@ function getColorFromIdentifier(expression) {
|
|
|
44
65
|
return value;
|
|
45
66
|
}
|
|
46
67
|
function getTokenFromNode(j, path, value, propertyName) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
68
|
+
var valueMeta = (0, _astMeta.cleanMeta)(_legacyColors.legacyColorMetaMap[value] || []);
|
|
69
|
+
var ancestorMeta = (0, _astMeta.cleanMeta)([].concat((0, _toConsumableArray2.default)((0, _astMeta.getMetaFromAncestors)(j, path)), (0, _toConsumableArray2.default)((0, _stringUtils.kebabize)(propertyName).split('-'))) || []);
|
|
70
|
+
var property = (0, _astMeta.cleanMeta)([(0, _stringUtils.kebabize)(propertyName)])[0];
|
|
50
71
|
|
|
51
72
|
// Attempt to find a property from ancestors if one is not found
|
|
52
73
|
if (!property || !['border', 'icon', 'background', 'text'].includes(property)) {
|
|
@@ -63,10 +84,12 @@ function getTokenFromNode(j, path, value, propertyName) {
|
|
|
63
84
|
property = 'text';
|
|
64
85
|
}
|
|
65
86
|
}
|
|
66
|
-
|
|
67
|
-
|
|
87
|
+
var meta = [];
|
|
88
|
+
var possibleTokens = _tokens.tokens;
|
|
68
89
|
if (property === 'text') {
|
|
69
|
-
possibleTokens = _tokens.tokens.filter(
|
|
90
|
+
possibleTokens = _tokens.tokens.filter(function (token) {
|
|
91
|
+
return token.includes('.text');
|
|
92
|
+
});
|
|
70
93
|
if (valueMeta.includes('neutral')) {
|
|
71
94
|
meta.push('color', 'text');
|
|
72
95
|
}
|
|
@@ -79,21 +102,21 @@ function getTokenFromNode(j, path, value, propertyName) {
|
|
|
79
102
|
|
|
80
103
|
// handle non-neutrals
|
|
81
104
|
if (!valueMeta.includes('neutral')) {
|
|
82
|
-
meta.push('color',
|
|
105
|
+
meta.push.apply(meta, ['color'].concat((0, _toConsumableArray2.default)(ancestorMeta), (0, _toConsumableArray2.default)(valueMeta)));
|
|
83
106
|
}
|
|
84
107
|
}
|
|
85
108
|
if (property === 'background' || property === 'background-color') {
|
|
86
109
|
if (ancestorMeta.includes('disabled')) {
|
|
87
110
|
// disabled backgrounds
|
|
88
|
-
meta.push(property,
|
|
111
|
+
meta.push.apply(meta, [property].concat((0, _toConsumableArray2.default)(ancestorMeta)));
|
|
89
112
|
} else if (
|
|
90
113
|
// Surfaces
|
|
91
114
|
valueMeta.includes('neutral') && value !== 'N100' && value !== 'N200' && value !== 'N300' && value !== 'N400' && value !== 'N500' && value !== 'N600' && value !== 'N700' && value !== 'N800') {
|
|
92
|
-
meta.push('surface',
|
|
115
|
+
meta.push.apply(meta, ['surface'].concat((0, _toConsumableArray2.default)(ancestorMeta)));
|
|
93
116
|
} else if (value.includes('N0')) {
|
|
94
117
|
// default surface
|
|
95
118
|
meta.push('elevation', 'surface');
|
|
96
|
-
} else if (valueMeta.includes('neutral') && isBoldColor(value)) {
|
|
119
|
+
} else if (valueMeta.includes('neutral') && (0, _color.isBoldColor)(value)) {
|
|
97
120
|
// bold netural backgrounds
|
|
98
121
|
meta.push('background', 'neutral', 'bold');
|
|
99
122
|
} else if (valueMeta.includes('neutral')) {
|
|
@@ -102,124 +125,365 @@ function getTokenFromNode(j, path, value, propertyName) {
|
|
|
102
125
|
}
|
|
103
126
|
}
|
|
104
127
|
if (property === 'border' || property === 'border-color' || property === 'border-left' || property === 'border-right' || property === 'border-top' || property === 'border-bottom' || property === 'outline' || property === 'outline-color') {
|
|
105
|
-
possibleTokens = _tokens.tokens.filter(
|
|
128
|
+
possibleTokens = _tokens.tokens.filter(function (token) {
|
|
129
|
+
return token.includes('.border') || token.includes('.focus');
|
|
130
|
+
});
|
|
106
131
|
if (valueMeta.includes('neutral')) {
|
|
107
132
|
// standard netural boarder
|
|
108
|
-
meta.push('color', 'border',
|
|
133
|
+
meta.push.apply(meta, ['color', 'border'].concat((0, _toConsumableArray2.default)(ancestorMeta)));
|
|
109
134
|
} else {
|
|
110
|
-
meta.push('border',
|
|
135
|
+
meta.push.apply(meta, ['border'].concat((0, _toConsumableArray2.default)(valueMeta), (0, _toConsumableArray2.default)(ancestorMeta)));
|
|
111
136
|
}
|
|
112
137
|
}
|
|
113
138
|
if (ancestorMeta.includes('icon')) {
|
|
114
|
-
possibleTokens = _tokens.tokens.filter(
|
|
139
|
+
possibleTokens = _tokens.tokens.filter(function (token) {
|
|
140
|
+
return token.includes('.icon');
|
|
141
|
+
});
|
|
115
142
|
if (ancestorMeta.includes('disabled')) {
|
|
116
143
|
// disabled backgrounds
|
|
117
144
|
meta.push('disabled');
|
|
118
145
|
}
|
|
119
|
-
meta.push('color', 'icon',
|
|
146
|
+
meta.push.apply(meta, ['color', 'icon'].concat((0, _toConsumableArray2.default)(valueMeta)));
|
|
120
147
|
}
|
|
121
148
|
|
|
122
149
|
// Fallback if guided behavior yields nothing
|
|
123
150
|
if (meta.length === 0) {
|
|
124
|
-
meta.push(property,
|
|
151
|
+
meta.push.apply(meta, [property].concat((0, _toConsumableArray2.default)(valueMeta), (0, _toConsumableArray2.default)(ancestorMeta)));
|
|
125
152
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
153
|
+
var search = (0, _fuzzySearch.default)(possibleTokens, false);
|
|
154
|
+
var results = search.get(meta.join(' '));
|
|
155
|
+
var tokenId = ['MISSING_TOKEN'];
|
|
129
156
|
if (results) {
|
|
130
|
-
tokenId = results.map(
|
|
157
|
+
tokenId = results.map(function (result) {
|
|
158
|
+
return result[1];
|
|
159
|
+
});
|
|
131
160
|
}
|
|
132
161
|
return tokenId[0];
|
|
133
162
|
}
|
|
134
163
|
function parseCSSPropertyName(cssString) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
164
|
+
var lastColonIndex = cssString.lastIndexOf(':');
|
|
165
|
+
if (lastColonIndex === -1) {
|
|
166
|
+
return {
|
|
167
|
+
colonIndex: null,
|
|
168
|
+
cssPropertyName: null
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
var propertyNameEndIndex = Math.max(cssString.lastIndexOf(';', lastColonIndex), cssString.lastIndexOf(' ', lastColonIndex), -1);
|
|
172
|
+
var startIndex = propertyNameEndIndex + 1;
|
|
173
|
+
return {
|
|
174
|
+
cssPropertyName: cssString.slice(startIndex, lastColonIndex).trim(),
|
|
175
|
+
colonIndex: lastColonIndex
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function transformer(_x, _x2) {
|
|
179
|
+
return _transformer.apply(this, arguments);
|
|
138
180
|
}
|
|
139
|
-
function
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
181
|
+
function _transformer() {
|
|
182
|
+
_transformer = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(file, api) {
|
|
183
|
+
var debug,
|
|
184
|
+
j,
|
|
185
|
+
source,
|
|
186
|
+
transformed,
|
|
187
|
+
templateLiteralPaths,
|
|
188
|
+
_iterator,
|
|
189
|
+
_step,
|
|
190
|
+
_loop,
|
|
191
|
+
replaceStringLiteralIfItConsistsOnlyOfColor,
|
|
192
|
+
replaceTemplateLiteralExpression,
|
|
193
|
+
_args2 = arguments;
|
|
194
|
+
return _regenerator.default.wrap(function _callee$(_context2) {
|
|
195
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
196
|
+
case 0:
|
|
197
|
+
replaceTemplateLiteralExpression = function _replaceTemplateLiter(j, mainPath, expressionPath, expressionIndex) {
|
|
198
|
+
var expression = expressionPath.value;
|
|
199
|
+
if (!(expression.type === 'MemberExpression' || expression.type === 'Identifier')) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
if ((0, _ast.isDecendantOfToken)(j, expressionPath)) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
var value = getColorFromIdentifier(expression);
|
|
206
|
+
if (!value || !(0, _color.includesHardCodedColor)(value) && !(0, _color.isHardCodedColor)(value) && !(0, _color.isLegacyColor)(value) && !(0, _color.isLegacyNamedColor)(value)) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
var precedingQuasi = mainPath.value.quasis[expressionIndex];
|
|
210
|
+
var precedingQuasiText = precedingQuasi.value.raw;
|
|
211
|
+
var _parseCSSPropertyName = parseCSSPropertyName(precedingQuasiText),
|
|
212
|
+
cssPropertyName = _parseCSSPropertyName.cssPropertyName,
|
|
213
|
+
colonIndex = _parseCSSPropertyName.colonIndex;
|
|
214
|
+
if (!cssPropertyName) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
var tokenId = getTokenFromNode(j, expressionPath, value, cssPropertyName);
|
|
218
|
+
insertTokenImport(j, source);
|
|
219
|
+
var newQuasis = (0, _toConsumableArray2.default)(mainPath.value.quasis);
|
|
220
|
+
var newExpressions = (0, _toConsumableArray2.default)(mainPath.value.expressions);
|
|
221
|
+
if (cssPropertyName !== 'box-shadow') {
|
|
222
|
+
var tokenExpression = buildToken(j, tokenId, expressionPath.value);
|
|
223
|
+
newExpressions[expressionIndex] = tokenExpression;
|
|
224
|
+
} else {
|
|
225
|
+
// box-shadow is a multi-part property where the color can appear at any
|
|
226
|
+
// part position (even though the standard suggests that color comes
|
|
227
|
+
// last, browsers' CSS parsers are more lax). If we get here, then the
|
|
228
|
+
// color part is replaceable. Textually, it's something like:
|
|
229
|
+
//
|
|
230
|
+
// <rules before>; box-shadow: 0 1px ${colors.N50} 2rem; <rules after>
|
|
231
|
+
//
|
|
232
|
+
// the fallback value will be multipart, i.e. the token call is
|
|
233
|
+
//
|
|
234
|
+
// token(<replacedValue>, `0 1px ${colors.N50} 2rem`)
|
|
235
|
+
//
|
|
236
|
+
// and it's wrapped in a substitution like this:
|
|
237
|
+
//
|
|
238
|
+
// <rules before>; box-shadow: ${token(<...>)}; <rules after>
|
|
239
|
+
//
|
|
240
|
+
// We stich the fallback from the last part of preceding quasi (after
|
|
241
|
+
// colon) and the first part of the following quasi (before ';' or '}').
|
|
242
|
+
//
|
|
243
|
+
// If multiple box-shadows are comma-separated but only one of them has a
|
|
244
|
+
// replaceable color and others are hard-coded, this logic would still
|
|
245
|
+
// work. When multiple shadows have expressions, it's unfortunately not
|
|
246
|
+
// possible to proceed because we cannot find where the value ends from a
|
|
247
|
+
// single following quasi.
|
|
248
|
+
var valueStartIndex = (0, _stringUtils.findFirstNonspaceIndexAfter)(precedingQuasiText, colonIndex);
|
|
249
|
+
var _splitAtIndex = (0, _stringUtils.splitAtIndex)(precedingQuasiText, valueStartIndex),
|
|
250
|
+
_splitAtIndex2 = (0, _slicedToArray2.default)(_splitAtIndex, 2),
|
|
251
|
+
newPrecedingQuasiText = _splitAtIndex2[0],
|
|
252
|
+
partialValueBeginning = _splitAtIndex2[1];
|
|
253
|
+
var followingQuasi = mainPath.value.quasis[expressionIndex + 1];
|
|
254
|
+
var followingQuasiText = followingQuasi.value.raw;
|
|
255
|
+
var valueEndIndex = (0, _cssUtils.findEndIndexOfCSSExpression)(followingQuasiText, followingQuasi.tail);
|
|
256
|
+
if (!valueEndIndex) {
|
|
257
|
+
console.warn('cannot find end of box-shadow value, please check manually');
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
var _splitAtIndex3 = (0, _stringUtils.splitAtIndex)(followingQuasiText, valueEndIndex + 1),
|
|
261
|
+
_splitAtIndex4 = (0, _slicedToArray2.default)(_splitAtIndex3, 2),
|
|
262
|
+
partialValueEnd = _splitAtIndex4[0],
|
|
263
|
+
newFollowingQuasiText = _splitAtIndex4[1];
|
|
264
|
+
var internalQuasis = [buildTemplateElement(j, partialValueBeginning, {
|
|
265
|
+
tail: false
|
|
266
|
+
}), buildTemplateElement(j, partialValueEnd, {
|
|
267
|
+
tail: true
|
|
268
|
+
})];
|
|
269
|
+
var internalExpressions = [expressionPath.value];
|
|
270
|
+
var newFallback = j.templateLiteral(internalQuasis, internalExpressions);
|
|
271
|
+
var newExpression = buildToken(j, tokenId, newFallback);
|
|
272
|
+
newQuasis[expressionIndex] = buildTemplateElement(j, newPrecedingQuasiText, {
|
|
273
|
+
fromNode: newQuasis[expressionIndex]
|
|
274
|
+
});
|
|
275
|
+
newExpressions[expressionIndex] = newExpression;
|
|
276
|
+
newQuasis[expressionIndex + 1] = buildTemplateElement(j, newFollowingQuasiText, {
|
|
277
|
+
fromNode: newQuasis[expressionIndex]
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
mainPath.replace(j.templateLiteral(newQuasis, newExpressions));
|
|
281
|
+
return true;
|
|
282
|
+
};
|
|
283
|
+
replaceStringLiteralIfItConsistsOnlyOfColor = function _replaceStringLiteral(j, path, value) {
|
|
284
|
+
if ((0, _ast.isDecendantOfToken)(j, path)) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
if ((0, _color.isHardCodedColor)(value) && !(0, _color.isLegacyColor)(value) && !(0, _color.isLegacyNamedColor)(value)) {
|
|
288
|
+
var parent = path.parent.value;
|
|
289
|
+
var key = '';
|
|
290
|
+
if (parent.type === 'VariableDeclarator') {
|
|
291
|
+
key = parent.id.name;
|
|
292
|
+
}
|
|
293
|
+
var tokenId = getTokenFromNode(j, path, value, key);
|
|
294
|
+
insertTokenImport(j, source);
|
|
295
|
+
j(path).replaceWith(buildToken(j, tokenId, path.value));
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
return false;
|
|
299
|
+
};
|
|
300
|
+
debug = _args2.length > 2 && _args2[2] !== undefined ? _args2[2] : false;
|
|
301
|
+
j = api.jscodeshift;
|
|
302
|
+
source = j(file.source);
|
|
303
|
+
transformed = false; // Objects
|
|
304
|
+
source.find(j.ObjectProperty).forEach(function (path) {
|
|
305
|
+
if (path.value.value.type === 'ObjectExpression') {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
143
308
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
309
|
+
// Avoid transforming objects that are default arguments
|
|
310
|
+
if (path.parent.parent.value.type === 'ArrowFunctionExpression' || path.parent.parent.value.type === 'FunctionDeclaration') {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if ((0, _ast.isParentOfToken)(j, path.value.value)) {
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
var value = getColorFromIdentifier(path.value.value);
|
|
317
|
+
if (!value || !(0, _color.includesHardCodedColor)(value) && !(0, _color.isHardCodedColor)(value) && !(0, _color.isLegacyColor)(value) && !(0, _color.isLegacyNamedColor)(value)) {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
var key;
|
|
321
|
+
if (path.value.key.type === 'NumericLiteral' || path.value.key.type === 'StringLiteral') {
|
|
322
|
+
key = path.value.key.value.toString();
|
|
323
|
+
}
|
|
324
|
+
if (path.value.key.type === 'Identifier') {
|
|
325
|
+
key = path.value.key.name;
|
|
326
|
+
}
|
|
149
327
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
let key;
|
|
162
|
-
if (path.value.key.type === 'NumericLiteral' || path.value.key.type === 'StringLiteral') {
|
|
163
|
-
key = path.value.key.value.toString();
|
|
164
|
-
}
|
|
165
|
-
if (path.value.key.type === 'Identifier') {
|
|
166
|
-
key = path.value.key.name;
|
|
167
|
-
}
|
|
328
|
+
// Key is a node type we do not support
|
|
329
|
+
if (!key) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
var tokenId = getTokenFromNode(j, path, value, key);
|
|
333
|
+
insertTokenImport(j, source);
|
|
334
|
+
j(path).replaceWith(j.objectProperty(path.value.key, buildToken(j, tokenId, path.value.value)));
|
|
335
|
+
transformed = true;
|
|
336
|
+
});
|
|
168
337
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
338
|
+
// JSX props
|
|
339
|
+
source.find(j.JSXAttribute).forEach(function (path) {
|
|
340
|
+
var _path$value, _path$value$value;
|
|
341
|
+
if (((_path$value = path.value) === null || _path$value === void 0 ? void 0 : (_path$value$value = _path$value.value) === null || _path$value$value === void 0 ? void 0 : _path$value$value.type) !== 'JSXExpressionContainer') {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
if ((0, _ast.isParentOfToken)(j, path)) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
var expression = path.value.value.expression;
|
|
348
|
+
var value = getColorFromIdentifier(expression);
|
|
349
|
+
if (!value || !(0, _color.includesHardCodedColor)(value) && !(0, _color.isHardCodedColor)(value) && !(0, _color.isLegacyColor)(value) && !(0, _color.isLegacyNamedColor)(value)) {
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
var tokenId = getTokenFromNode(j, path, value, path.value.name.name);
|
|
353
|
+
insertTokenImport(j, source);
|
|
354
|
+
j(path).find(j.JSXExpressionContainer).forEach(function (path) {
|
|
355
|
+
var tokenNode = buildToken(j, tokenId, path.value.expression);
|
|
356
|
+
j(path).replaceWith(j.jsxExpressionContainer(tokenNode));
|
|
357
|
+
});
|
|
358
|
+
transformed = true;
|
|
359
|
+
});
|
|
178
360
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
361
|
+
// Strings
|
|
362
|
+
source.find(j.StringLiteral).forEach(function (path) {
|
|
363
|
+
j(path).filter(function (expression) {
|
|
364
|
+
return !(0, _utils.isDecendantOfType)(j, expression, j.ObjectExpression);
|
|
365
|
+
}).forEach(function (path) {
|
|
366
|
+
var value = path.value.value;
|
|
367
|
+
if (replaceStringLiteralIfItConsistsOnlyOfColor(j, path, value)) {
|
|
368
|
+
transformed = true;
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
});
|
|
372
|
+
templateLiteralPaths = source.find(j.TemplateLiteral).paths();
|
|
373
|
+
_iterator = _createForOfIteratorHelper(templateLiteralPaths);
|
|
374
|
+
_context2.prev = 11;
|
|
375
|
+
_loop = /*#__PURE__*/_regenerator.default.mark(function _loop() {
|
|
376
|
+
var path, text, quasiPaths, _iterator2, _step2, quasiPath, _text, newCSS;
|
|
377
|
+
return _regenerator.default.wrap(function _loop$(_context) {
|
|
378
|
+
while (1) switch (_context.prev = _context.next) {
|
|
379
|
+
case 0:
|
|
380
|
+
path = _step.value;
|
|
381
|
+
// Background: a 'type: TemplateLiteral' Node has quasis and expressions
|
|
382
|
+
// (see ast-types/src/gen/namedTypes.ts), and invariant holds that
|
|
383
|
+
// quasis.length === expression.length + 1.
|
|
384
|
+
//
|
|
385
|
+
// eg `${foo}bar` has quasis [Node(''), Node('bar')] and expressions
|
|
386
|
+
// [Node('foo')]. Each quasi has type: 'TemplateElement'; expressions are
|
|
387
|
+
// probably safe to treat as subtypes of Expression, though ast-types
|
|
388
|
+
// codebase has a more involved definition.
|
|
389
|
+
if (path.value.expressions.length === 0) {
|
|
390
|
+
// A single-quasi (equivalently, no-expression) template literal is
|
|
391
|
+
// basically just a string literal, possibly multi-line. We handle the
|
|
392
|
+
// simple `#ababab` case here, and the multi-line case after.
|
|
393
|
+
text = path.value.quasis[0].value.raw;
|
|
394
|
+
if (replaceStringLiteralIfItConsistsOnlyOfColor(j, path, text)) {
|
|
395
|
+
transformed = true;
|
|
396
|
+
}
|
|
397
|
+
} else {
|
|
398
|
+
j(path).find(j.Expression).filter(function (expressionPath) {
|
|
399
|
+
// jscodeshift walks over the whole tree; we are interested only in
|
|
400
|
+
// the direct children: i.e. top-level expressions appearing in ${}.
|
|
401
|
+
return expressionPath.parent === path;
|
|
402
|
+
}).forEach(function (expressionPath, expressionIndex) {
|
|
403
|
+
if (replaceTemplateLiteralExpression(j, path, expressionPath, expressionIndex)) {
|
|
404
|
+
transformed = true;
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
}
|
|
201
408
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
409
|
+
// No matter if we have one big quasi or many small chunks between
|
|
410
|
+
// expressions (which potentially have been transformed), try to pass them
|
|
411
|
+
// through the CSS transformer; it's robust enough to understand malformed
|
|
412
|
+
// CSS that would result if we split e.g. this template:
|
|
413
|
+
//
|
|
414
|
+
// `${gridSize}px; color: red;`, giving `px; color: red;` as input; or
|
|
415
|
+
// `@media ${mobile} { color: red }`, giving `{ color: red }`.
|
|
416
|
+
quasiPaths = j(path).find(j.TemplateElement).filter(function (quasiPath) {
|
|
417
|
+
return quasiPath.parent === path;
|
|
418
|
+
}).paths();
|
|
419
|
+
_iterator2 = _createForOfIteratorHelper(quasiPaths);
|
|
420
|
+
_context.prev = 4;
|
|
421
|
+
_iterator2.s();
|
|
422
|
+
case 6:
|
|
423
|
+
if ((_step2 = _iterator2.n()).done) {
|
|
424
|
+
_context.next = 17;
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
quasiPath = _step2.value;
|
|
428
|
+
_text = quasiPath.value.value.raw;
|
|
429
|
+
if (!((0, _color.includesHardCodedColor)(_text) && (0, _cssUtils.containsReplaceableCSSDeclarations)(_text))) {
|
|
430
|
+
_context.next = 15;
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
_context.next = 12;
|
|
434
|
+
return (0, _cssToDesignTokens.default)(_text);
|
|
435
|
+
case 12:
|
|
436
|
+
newCSS = _context.sent;
|
|
437
|
+
j(quasiPath).replaceWith(buildTemplateElement(j, newCSS));
|
|
438
|
+
transformed = true;
|
|
439
|
+
case 15:
|
|
440
|
+
_context.next = 6;
|
|
441
|
+
break;
|
|
442
|
+
case 17:
|
|
443
|
+
_context.next = 22;
|
|
444
|
+
break;
|
|
445
|
+
case 19:
|
|
446
|
+
_context.prev = 19;
|
|
447
|
+
_context.t0 = _context["catch"](4);
|
|
448
|
+
_iterator2.e(_context.t0);
|
|
449
|
+
case 22:
|
|
450
|
+
_context.prev = 22;
|
|
451
|
+
_iterator2.f();
|
|
452
|
+
return _context.finish(22);
|
|
453
|
+
case 25:
|
|
454
|
+
case "end":
|
|
455
|
+
return _context.stop();
|
|
456
|
+
}
|
|
457
|
+
}, _loop, null, [[4, 19, 22, 25]]);
|
|
458
|
+
});
|
|
459
|
+
_iterator.s();
|
|
460
|
+
case 14:
|
|
461
|
+
if ((_step = _iterator.n()).done) {
|
|
462
|
+
_context2.next = 18;
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
return _context2.delegateYield(_loop(), "t0", 16);
|
|
466
|
+
case 16:
|
|
467
|
+
_context2.next = 14;
|
|
468
|
+
break;
|
|
469
|
+
case 18:
|
|
470
|
+
_context2.next = 23;
|
|
471
|
+
break;
|
|
472
|
+
case 20:
|
|
473
|
+
_context2.prev = 20;
|
|
474
|
+
_context2.t1 = _context2["catch"](11);
|
|
475
|
+
_iterator.e(_context2.t1);
|
|
476
|
+
case 23:
|
|
477
|
+
_context2.prev = 23;
|
|
478
|
+
_iterator.f();
|
|
479
|
+
return _context2.finish(23);
|
|
480
|
+
case 26:
|
|
481
|
+
return _context2.abrupt("return", transformed ? source.toSource() : file.source);
|
|
482
|
+
case 27:
|
|
483
|
+
case "end":
|
|
484
|
+
return _context2.stop();
|
|
485
|
+
}
|
|
486
|
+
}, _callee, null, [[11, 20, 23, 26]]);
|
|
487
|
+
}));
|
|
488
|
+
return _transformer.apply(this, arguments);
|
|
225
489
|
}
|