@atlaskit/codemod-cli 0.13.4 → 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 +6 -0
- package/dist/cjs/main.js +1 -1
- package/dist/cjs/presets/css-to-design-tokens/css-to-design-tokens.js +22 -11
- package/dist/cjs/presets/theme-to-design-tokens/theme-to-design-tokens.js +348 -104
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast.js +1 -1
- package/dist/cjs/presets/theme-to-design-tokens/utils/color.js +15 -11
- package/dist/cjs/presets/theme-to-design-tokens/utils/css-utils.js +38 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/string-utils.js +26 -0
- package/dist/es2019/presets/css-to-design-tokens/css-to-design-tokens.js +18 -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/esm/main.js +1 -1
- package/dist/esm/presets/css-to-design-tokens/css-to-design-tokens.js +22 -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/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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @atlaskit/codemod-cli
|
|
2
2
|
|
|
3
|
+
## 0.14.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`26d4e25c2b3`](https://bitbucket.org/atlassian/atlassian-frontend/commits/26d4e25c2b3) - replace strings that contain only the color name; replace hard-coded colors in template literals that have CSS declarations; better color names for literals that have multiple expressions; replace full value of box-shadow in template literals
|
|
8
|
+
|
|
3
9
|
## 0.13.4
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/dist/cjs/main.js
CHANGED
|
@@ -298,7 +298,7 @@ function _main() {
|
|
|
298
298
|
case 4:
|
|
299
299
|
_yield$parseArgs = _context5.sent;
|
|
300
300
|
packages = _yield$parseArgs.packages;
|
|
301
|
-
_process$env$_PACKAGE = "0.
|
|
301
|
+
_process$env$_PACKAGE = "0.14.0", _PACKAGE_VERSION_ = _process$env$_PACKAGE === void 0 ? '0.0.0-dev' : _process$env$_PACKAGE;
|
|
302
302
|
logger.log(_chalk.default.bgBlue(_chalk.default.black("\uD83D\uDCDA Atlassian-Frontend codemod library @ ".concat(_PACKAGE_VERSION_, " \uD83D\uDCDA"))));
|
|
303
303
|
if (packages && packages.length > 0) {
|
|
304
304
|
logger.log(_chalk.default.gray("Searching for codemods for newer versions of the following packages: ".concat(packages.map(function (pkg) {
|
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.default = transformer;
|
|
8
|
+
exports.isColorProperty = isColorProperty;
|
|
8
9
|
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
9
10
|
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
10
11
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
@@ -18,6 +19,10 @@ var _legacyColors = require("./utils/legacy-colors");
|
|
|
18
19
|
var _meta = require("./utils/meta");
|
|
19
20
|
// @ts-ignore
|
|
20
21
|
|
|
22
|
+
var options = {
|
|
23
|
+
syntax: _postcssLess.default,
|
|
24
|
+
from: undefined
|
|
25
|
+
};
|
|
21
26
|
var tokens = _tokensRaw.light.filter(function (t) {
|
|
22
27
|
return t.attributes.state === 'active';
|
|
23
28
|
}).map(function (t) {
|
|
@@ -46,7 +51,7 @@ function stripLessVar(prop) {
|
|
|
46
51
|
return prop.substring(1);
|
|
47
52
|
}
|
|
48
53
|
function isColorProperty(prop) {
|
|
49
|
-
return prop === 'color' || prop === 'background' || prop === 'background-color' || prop === 'box-shadow' || prop === 'border' || prop === 'border-left' || prop === 'border-right' || prop === 'border-top' || prop === 'border-bottom' || prop === 'border-color';
|
|
54
|
+
return prop === 'color' || prop === 'background' || prop === 'background-color' || prop === 'box-shadow' || prop === 'border' || prop === 'border-left' || prop === 'border-right' || prop === 'border-top' || prop === 'border-bottom' || prop === 'border-color' || prop === 'outline';
|
|
50
55
|
}
|
|
51
56
|
function getDeclarationMeta(decl) {
|
|
52
57
|
if (decl.prop === 'color') {
|
|
@@ -100,6 +105,9 @@ var plugin = function plugin() {
|
|
|
100
105
|
if (!isColorProperty(decl.prop)) {
|
|
101
106
|
return;
|
|
102
107
|
}
|
|
108
|
+
if (decl.value === 'none') {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
103
111
|
var searchTerms = [getDeclarationMeta(decl)].concat((0, _toConsumableArray2.default)(getParentSelectors(decl).split(/\-|\.|\,|\ |\:|\&/).filter(function (el) {
|
|
104
112
|
return !!el;
|
|
105
113
|
})));
|
|
@@ -120,10 +128,10 @@ var plugin = function plugin() {
|
|
|
120
128
|
}
|
|
121
129
|
|
|
122
130
|
// Less variables
|
|
123
|
-
var
|
|
124
|
-
if (
|
|
131
|
+
var lessVarMatch = decl.value.match(lessVarRe);
|
|
132
|
+
if (lessVarMatch) {
|
|
125
133
|
var _getMetaFromCssVar2;
|
|
126
|
-
match =
|
|
134
|
+
match = lessVarMatch[0];
|
|
127
135
|
searchTerms.push.apply(searchTerms, (0, _toConsumableArray2.default)((_getMetaFromCssVar2 = getMetaFromCssVar("--".concat(stripLessVar(match)))) !== null && _getMetaFromCssVar2 !== void 0 ? _getMetaFromCssVar2 : []));
|
|
128
136
|
}
|
|
129
137
|
|
|
@@ -168,16 +176,19 @@ function transformer(_x) {
|
|
|
168
176
|
}
|
|
169
177
|
function _transformer() {
|
|
170
178
|
_transformer = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(file) {
|
|
179
|
+
var processor, src, _yield$processor$proc, css;
|
|
171
180
|
return _regenerator.default.wrap(function _callee$(_context) {
|
|
172
181
|
while (1) switch (_context.prev = _context.next) {
|
|
173
182
|
case 0:
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
case
|
|
179
|
-
|
|
180
|
-
|
|
183
|
+
processor = (0, _postcss.default)([plugin()]);
|
|
184
|
+
src = typeof file === 'string' ? file : file.source;
|
|
185
|
+
_context.next = 4;
|
|
186
|
+
return processor.process(src, options);
|
|
187
|
+
case 4:
|
|
188
|
+
_yield$processor$proc = _context.sent;
|
|
189
|
+
css = _yield$processor$proc.css;
|
|
190
|
+
return _context.abrupt("return", css);
|
|
191
|
+
case 7:
|
|
181
192
|
case "end":
|
|
182
193
|
return _context.stop();
|
|
183
194
|
}
|
|
@@ -5,6 +5,9 @@ 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"));
|
|
8
11
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
12
|
var _utils = require("@codeshift/utils");
|
|
10
13
|
var _ast = require("./utils/ast");
|
|
@@ -13,17 +16,12 @@ var _color = require("./utils/color");
|
|
|
13
16
|
var _fuzzySearch = _interopRequireDefault(require("./utils/fuzzy-search"));
|
|
14
17
|
var _legacyColors = require("./utils/legacy-colors");
|
|
15
18
|
var _tokens = require("./utils/tokens");
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
};
|
|
23
|
-
function isBoldColor(color) {
|
|
24
|
-
var number = parseInt(color.replace(/^./, ''), 10);
|
|
25
|
-
return number > 300;
|
|
26
|
-
}
|
|
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 */
|
|
27
25
|
function insertTokenImport(j, source) {
|
|
28
26
|
if ((0, _utils.hasImportDeclaration)(j, source, '@atlaskit/tokens')) {
|
|
29
27
|
return;
|
|
@@ -35,6 +33,24 @@ function buildToken(j, tokenId, node) {
|
|
|
35
33
|
var callExpr = j.callExpression(j.identifier('token'), [j.stringLiteral(tokenId), node].filter(Boolean));
|
|
36
34
|
return callExpr;
|
|
37
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
|
+
}
|
|
38
54
|
function getColorFromIdentifier(expression) {
|
|
39
55
|
var value = '';
|
|
40
56
|
if (expression.type === 'Identifier') {
|
|
@@ -50,8 +66,8 @@ function getColorFromIdentifier(expression) {
|
|
|
50
66
|
}
|
|
51
67
|
function getTokenFromNode(j, path, value, propertyName) {
|
|
52
68
|
var valueMeta = (0, _astMeta.cleanMeta)(_legacyColors.legacyColorMetaMap[value] || []);
|
|
53
|
-
var ancestorMeta = (0, _astMeta.cleanMeta)([].concat((0, _toConsumableArray2.default)((0, _astMeta.getMetaFromAncestors)(j, path)), (0, _toConsumableArray2.default)(kebabize(propertyName).split('-'))) || []);
|
|
54
|
-
var property = (0, _astMeta.cleanMeta)([kebabize(propertyName)])[0];
|
|
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];
|
|
55
71
|
|
|
56
72
|
// Attempt to find a property from ancestors if one is not found
|
|
57
73
|
if (!property || !['border', 'icon', 'background', 'text'].includes(property)) {
|
|
@@ -100,7 +116,7 @@ function getTokenFromNode(j, path, value, propertyName) {
|
|
|
100
116
|
} else if (value.includes('N0')) {
|
|
101
117
|
// default surface
|
|
102
118
|
meta.push('elevation', 'surface');
|
|
103
|
-
} else if (valueMeta.includes('neutral') && isBoldColor(value)) {
|
|
119
|
+
} else if (valueMeta.includes('neutral') && (0, _color.isBoldColor)(value)) {
|
|
104
120
|
// bold netural backgrounds
|
|
105
121
|
meta.push('background', 'neutral', 'bold');
|
|
106
122
|
} else if (valueMeta.includes('neutral')) {
|
|
@@ -146,100 +162,328 @@ function getTokenFromNode(j, path, value, propertyName) {
|
|
|
146
162
|
}
|
|
147
163
|
function parseCSSPropertyName(cssString) {
|
|
148
164
|
var lastColonIndex = cssString.lastIndexOf(':');
|
|
149
|
-
|
|
150
|
-
|
|
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);
|
|
151
180
|
}
|
|
152
|
-
function
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
+
}
|
|
157
308
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
+
}
|
|
163
327
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
var key;
|
|
176
|
-
if (path.value.key.type === 'NumericLiteral' || path.value.key.type === 'StringLiteral') {
|
|
177
|
-
key = path.value.key.value.toString();
|
|
178
|
-
}
|
|
179
|
-
if (path.value.key.type === 'Identifier') {
|
|
180
|
-
key = path.value.key.name;
|
|
181
|
-
}
|
|
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
|
+
});
|
|
182
337
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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
|
+
});
|
|
192
360
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
+
}
|
|
221
408
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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);
|
|
245
489
|
}
|
|
@@ -8,7 +8,7 @@ exports.isDecendantOfToken = isDecendantOfToken;
|
|
|
8
8
|
exports.isParentOfToken = isParentOfToken;
|
|
9
9
|
var _utils = require("@codeshift/utils");
|
|
10
10
|
function isDecendantOfToken(j, path) {
|
|
11
|
-
if (path.type === 'CallExpression' && path.callee.type === 'Identifier' && path.callee.name === 'token') {
|
|
11
|
+
if ('type' in path && path.type === 'CallExpression' && path.callee.type === 'Identifier' && path.callee.name === 'token') {
|
|
12
12
|
return true;
|
|
13
13
|
}
|
|
14
14
|
return j(path).closest(j.CallExpression, {
|