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