@atlaskit/codemod-cli 0.27.1 → 0.27.3

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 (119) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/cjs/cli.js +3 -3
  3. package/dist/cjs/filepath.js +4 -2
  4. package/dist/cjs/main.js +90 -38
  5. package/dist/cjs/presets/index.js +2 -1
  6. package/dist/cjs/presets/migrate-to-link/codemods/migrate-to-link.js +2 -2
  7. package/dist/cjs/presets/migrate-to-new-buttons/codemods/next-split-imports.js +1 -1
  8. package/dist/cjs/presets/migrate-to-new-buttons/migrate-to-new-buttons.js +2 -2
  9. package/dist/cjs/presets/migrate-to-new-buttons/utils/add-comment-for-custom-theme-buttons.js +1 -1
  10. package/dist/cjs/presets/remove-token-fallbacks/remove-token-fallbacks.js +225 -0
  11. package/dist/cjs/presets/remove-token-fallbacks/types.js +5 -0
  12. package/dist/cjs/presets/remove-token-fallbacks/utils/all-tokens.js +44 -0
  13. package/dist/cjs/presets/remove-token-fallbacks/utils/color-utils.js +93 -0
  14. package/dist/cjs/presets/remove-token-fallbacks/utils/get-team-info.js +51 -0
  15. package/dist/cjs/presets/remove-token-fallbacks/utils/normalize-values.js +113 -0
  16. package/dist/cjs/presets/remove-token-fallbacks/utils/remove-unused-imports.js +61 -0
  17. package/dist/cjs/presets/remove-token-fallbacks/utils/remove-unused-variables.js +37 -0
  18. package/dist/cjs/presets/remove-token-fallbacks/utils/reporter.js +310 -0
  19. package/dist/cjs/presets/remove-token-fallbacks/utils/token-processor.js +632 -0
  20. package/dist/cjs/presets/remove-token-fallbacks/utils/update-comments.js +58 -0
  21. package/dist/cjs/presets/upgrade-pragmatic-drag-and-drop-to-stable/upgrade-pragmatic-drag-and-drop-to-stable.js +3 -3
  22. package/dist/cjs/sinceRef.js +2 -1
  23. package/dist/cjs/transforms.js +3 -1
  24. package/dist/cjs/types.js +2 -1
  25. package/dist/es2019/cli.js +3 -3
  26. package/dist/es2019/filepath.js +4 -2
  27. package/dist/es2019/main.js +30 -6
  28. package/dist/es2019/presets/index.js +2 -1
  29. package/dist/es2019/presets/migrate-to-link/codemods/migrate-to-link.js +2 -3
  30. package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-remove-unsafe-size.js +1 -1
  31. package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-split-imports.js +2 -2
  32. package/dist/es2019/presets/migrate-to-new-buttons/migrate-to-new-buttons.js +2 -2
  33. package/dist/es2019/presets/migrate-to-new-buttons/utils/add-comment-for-custom-theme-buttons.js +1 -1
  34. package/dist/es2019/presets/migrate-to-new-buttons/utils/migrate-fit-container-icon-button.js +1 -1
  35. package/dist/es2019/presets/remove-token-fallbacks/remove-token-fallbacks.js +130 -0
  36. package/dist/es2019/presets/remove-token-fallbacks/types.js +1 -0
  37. package/dist/es2019/presets/remove-token-fallbacks/utils/all-tokens.js +30 -0
  38. package/dist/es2019/presets/remove-token-fallbacks/utils/color-utils.js +84 -0
  39. package/dist/es2019/presets/remove-token-fallbacks/utils/get-team-info.js +22 -0
  40. package/dist/es2019/presets/remove-token-fallbacks/utils/normalize-values.js +104 -0
  41. package/dist/es2019/presets/remove-token-fallbacks/utils/remove-unused-imports.js +51 -0
  42. package/dist/es2019/presets/remove-token-fallbacks/utils/remove-unused-variables.js +31 -0
  43. package/dist/es2019/presets/remove-token-fallbacks/utils/reporter.js +118 -0
  44. package/dist/es2019/presets/remove-token-fallbacks/utils/token-processor.js +377 -0
  45. package/dist/es2019/presets/remove-token-fallbacks/utils/update-comments.js +46 -0
  46. package/dist/es2019/presets/theme-remove-deprecated-mixins/theme-remove-deprecated-mixins.js +1 -1
  47. package/dist/es2019/presets/upgrade-pragmatic-drag-and-drop-to-stable/upgrade-pragmatic-drag-and-drop-to-stable.js +3 -3
  48. package/dist/es2019/sinceRef.js +2 -1
  49. package/dist/es2019/transforms.js +3 -1
  50. package/dist/es2019/types.js +2 -1
  51. package/dist/esm/cli.js +3 -3
  52. package/dist/esm/filepath.js +4 -2
  53. package/dist/esm/main.js +91 -39
  54. package/dist/esm/presets/index.js +2 -1
  55. package/dist/esm/presets/migrate-to-link/codemods/migrate-to-link.js +2 -3
  56. package/dist/esm/presets/migrate-to-new-buttons/codemods/next-remove-unsafe-size.js +1 -1
  57. package/dist/esm/presets/migrate-to-new-buttons/codemods/next-split-imports.js +2 -2
  58. package/dist/esm/presets/migrate-to-new-buttons/migrate-to-new-buttons.js +2 -2
  59. package/dist/esm/presets/migrate-to-new-buttons/utils/add-comment-for-custom-theme-buttons.js +1 -1
  60. package/dist/esm/presets/migrate-to-new-buttons/utils/migrate-fit-container-icon-button.js +1 -1
  61. package/dist/esm/presets/remove-token-fallbacks/remove-token-fallbacks.js +215 -0
  62. package/dist/esm/presets/remove-token-fallbacks/types.js +1 -0
  63. package/dist/esm/presets/remove-token-fallbacks/utils/all-tokens.js +38 -0
  64. package/dist/esm/presets/remove-token-fallbacks/utils/color-utils.js +86 -0
  65. package/dist/esm/presets/remove-token-fallbacks/utils/get-team-info.js +44 -0
  66. package/dist/esm/presets/remove-token-fallbacks/utils/normalize-values.js +107 -0
  67. package/dist/esm/presets/remove-token-fallbacks/utils/remove-unused-imports.js +55 -0
  68. package/dist/esm/presets/remove-token-fallbacks/utils/remove-unused-variables.js +31 -0
  69. package/dist/esm/presets/remove-token-fallbacks/utils/reporter.js +302 -0
  70. package/dist/esm/presets/remove-token-fallbacks/utils/token-processor.js +625 -0
  71. package/dist/esm/presets/remove-token-fallbacks/utils/update-comments.js +51 -0
  72. package/dist/esm/presets/theme-remove-deprecated-mixins/theme-remove-deprecated-mixins.js +1 -1
  73. package/dist/esm/presets/upgrade-pragmatic-drag-and-drop-to-stable/upgrade-pragmatic-drag-and-drop-to-stable.js +3 -3
  74. package/dist/esm/sinceRef.js +2 -1
  75. package/dist/esm/transforms.js +3 -1
  76. package/dist/esm/types.js +2 -1
  77. package/dist/types/filepath.d.ts +3 -1
  78. package/dist/types/main.d.ts +1 -1
  79. package/dist/types/presets/index.d.ts +1 -0
  80. package/dist/types/presets/migrate-to-new-buttons/utils/import-types-from-new-entry-point.d.ts +1 -1
  81. package/dist/types/presets/migrate-to-new-buttons/utils/move-icon-value-from-link-button-to-link-children.d.ts +1 -1
  82. package/dist/types/presets/remove-token-fallbacks/remove-token-fallbacks.d.ts +29 -0
  83. package/dist/types/presets/remove-token-fallbacks/types.d.ts +39 -0
  84. package/dist/types/presets/remove-token-fallbacks/utils/all-tokens.d.ts +1 -0
  85. package/dist/types/presets/remove-token-fallbacks/utils/color-utils.d.ts +3 -0
  86. package/dist/types/presets/remove-token-fallbacks/utils/get-team-info.d.ts +8 -0
  87. package/dist/types/presets/remove-token-fallbacks/utils/normalize-values.d.ts +8 -0
  88. package/dist/types/presets/remove-token-fallbacks/utils/remove-unused-imports.d.ts +2 -0
  89. package/dist/types/presets/remove-token-fallbacks/utils/remove-unused-variables.d.ts +2 -0
  90. package/dist/types/presets/remove-token-fallbacks/utils/reporter.d.ts +4 -0
  91. package/dist/types/presets/remove-token-fallbacks/utils/token-processor.d.ts +30 -0
  92. package/dist/types/presets/remove-token-fallbacks/utils/update-comments.d.ts +2 -0
  93. package/dist/types/presets/styled-to-emotion/styled-to-emotion.d.ts +1 -1
  94. package/dist/types/presets/upgrade-pragmatic-drag-and-drop-to-stable/upgrade-pragmatic-drag-and-drop-to-stable.d.ts +1 -1
  95. package/dist/types/sinceRef.d.ts +2 -1
  96. package/dist/types/transforms.d.ts +3 -1
  97. package/dist/types/types.d.ts +3 -2
  98. package/dist/types-ts4.5/filepath.d.ts +3 -1
  99. package/dist/types-ts4.5/main.d.ts +1 -1
  100. package/dist/types-ts4.5/presets/index.d.ts +1 -0
  101. package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/import-types-from-new-entry-point.d.ts +1 -1
  102. package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/move-icon-value-from-link-button-to-link-children.d.ts +1 -1
  103. package/dist/types-ts4.5/presets/remove-token-fallbacks/remove-token-fallbacks.d.ts +29 -0
  104. package/dist/types-ts4.5/presets/remove-token-fallbacks/types.d.ts +39 -0
  105. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/all-tokens.d.ts +1 -0
  106. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/color-utils.d.ts +3 -0
  107. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/get-team-info.d.ts +8 -0
  108. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/normalize-values.d.ts +8 -0
  109. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/remove-unused-imports.d.ts +2 -0
  110. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/remove-unused-variables.d.ts +2 -0
  111. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/reporter.d.ts +4 -0
  112. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/token-processor.d.ts +30 -0
  113. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/update-comments.d.ts +2 -0
  114. package/dist/types-ts4.5/presets/styled-to-emotion/styled-to-emotion.d.ts +1 -1
  115. package/dist/types-ts4.5/presets/upgrade-pragmatic-drag-and-drop-to-stable/upgrade-pragmatic-drag-and-drop-to-stable.d.ts +1 -1
  116. package/dist/types-ts4.5/sinceRef.d.ts +2 -1
  117. package/dist/types-ts4.5/transforms.d.ts +3 -1
  118. package/dist/types-ts4.5/types.d.ts +3 -2
  119. package/package.json +8 -3
@@ -0,0 +1,215 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
4
+ /* eslint-disable no-console */
5
+ import { exec } from 'child_process';
6
+ import fs from 'fs/promises';
7
+ import path from 'path';
8
+ import { promisify } from 'util';
9
+ import { hasImportDeclaration } from '@hypermod/utils';
10
+ import { findRoot } from '@manypkg/find-root';
11
+ import chalk from 'chalk';
12
+ import { getTokenMap } from './utils/all-tokens';
13
+ import { getTeamInfo } from './utils/get-team-info';
14
+ import { removeUnusedImports } from './utils/remove-unused-imports';
15
+ import { removeUnusedVariables } from './utils/remove-unused-variables';
16
+ import { clearFolder, combineReports, writeReports } from './utils/reporter';
17
+ import { TokenProcessor } from './utils/token-processor';
18
+ var execAsync = promisify(exec);
19
+
20
+ /**
21
+ * Transforms the source code of a file by removing fallback values from the @atlaskit/tokens/token functions.
22
+ * By default removes only the fallbacks that have the same values as the tokens.
23
+ *
24
+ * @param {FileInfo} fileInfo - Information about the file to be transformed.
25
+ * @param {API} api - The jscodeshift API, providing utilities for AST transformations.
26
+ * @param {RemoveTokenFallbackOptions} options - Options for the transformation, including:
27
+ * @param {boolean} [options.verbose] - If true, enables verbose logging.
28
+ * @param {boolean} [options.forceUpdate] - If true, removes the fallbacks regardless of the difference between token and fallback. Otherwise removes only the fallbacks that are equal to the token values.
29
+ * @param {boolean} [options.addEslintComments] - If true, adds the eslint ignore comment for the rule @atlaskit/design-system/no-unsafe-design-token-usage for the fallbacks that weren't removed.
30
+ * @param {boolean} [options.useLegacyColorTheme] - If true, uses the legacy theme for color token mapping.
31
+ * @param {string} [options.reportFolder] - Directory path to output transformation reports. Reports will be generated only if this option is provided.
32
+ * @param {boolean} [options.dry] - If true, performs a dry run without modifying the files.
33
+ *
34
+ * @returns {Promise<string>} A promise that resolves to the transformed source code as a string.
35
+ */
36
+ export default function transformer(_x, _x2, _x3) {
37
+ return _transformer.apply(this, arguments);
38
+ }
39
+ function _transformer() {
40
+ _transformer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(fileInfo, _ref, options) {
41
+ var _options$useLegacyCol;
42
+ var j, rootDir, source, details, tokenMap, teamInfo, transformPromises, results, unusedVars, allImports, allVars;
43
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
44
+ while (1) switch (_context.prev = _context.next) {
45
+ case 0:
46
+ j = _ref.jscodeshift;
47
+ _context.next = 3;
48
+ return findRoot(path.dirname(fileInfo.path));
49
+ case 3:
50
+ rootDir = _context.sent;
51
+ source = j(fileInfo.source);
52
+ if (hasImportDeclaration(j, source, '@atlaskit/tokens')) {
53
+ _context.next = 7;
54
+ break;
55
+ }
56
+ return _context.abrupt("return", fileInfo.source);
57
+ case 7:
58
+ details = {
59
+ replaced: [],
60
+ notReplaced: []
61
+ };
62
+ if (options.verbose) {
63
+ console.log(chalk.yellow("Using ".concat(options.useLegacyColorTheme ? 'legacy light' : 'light', " theme.")));
64
+ }
65
+ tokenMap = getTokenMap((_options$useLegacyCol = options.useLegacyColorTheme) !== null && _options$useLegacyCol !== void 0 ? _options$useLegacyCol : false);
66
+ _context.next = 12;
67
+ return getTeamInfo(fileInfo.path);
68
+ case 12:
69
+ teamInfo = _context.sent;
70
+ transformPromises = source.find(j.CallExpression, {
71
+ callee: {
72
+ type: 'Identifier',
73
+ name: 'token'
74
+ }
75
+ }).paths().map(function (callPath) {
76
+ var processor = new TokenProcessor(j, options, fileInfo, source, rootDir, details, tokenMap, teamInfo);
77
+ return processor.processAndLogSingleToken(callPath);
78
+ });
79
+ _context.next = 16;
80
+ return Promise.all(transformPromises);
81
+ case 16:
82
+ results = _context.sent;
83
+ unusedVars = [];
84
+ if (results.some(function (result) {
85
+ return result.fallbackRemoved;
86
+ })) {
87
+ allImports = results.flatMap(function (result) {
88
+ var _result$resolvedImpor;
89
+ return (_result$resolvedImpor = result.resolvedImportDeclaration) !== null && _result$resolvedImpor !== void 0 ? _result$resolvedImpor : [];
90
+ });
91
+ allVars = results.flatMap(function (result) {
92
+ var _result$resolvedLocal;
93
+ return (_result$resolvedLocal = result.resolvedLocalVarDeclaration) !== null && _result$resolvedLocal !== void 0 ? _result$resolvedLocal : [];
94
+ });
95
+ unusedVars.push.apply(unusedVars, _toConsumableArray(allVars));
96
+ if (allImports.length) {
97
+ if (options.verbose) {
98
+ console.log(chalk.green("".concat(fileInfo.path, ": Some fallbacks were removed. Cleaning up ").concat(allImports.length, " imports.")));
99
+ }
100
+ removeUnusedImports(allImports, j);
101
+ }
102
+ }
103
+ removeUnusedVariables(unusedVars, j);
104
+ if (!options.reportFolder) {
105
+ _context.next = 23;
106
+ break;
107
+ }
108
+ _context.next = 23;
109
+ return writeReports(details, options.reportFolder);
110
+ case 23:
111
+ if (!options.dry) {
112
+ _context.next = 28;
113
+ break;
114
+ }
115
+ if (options.verbose) {
116
+ console.log(chalk.cyan("".concat(fileInfo.path, ": dry run mode active. Source was not modified.")));
117
+ }
118
+ // Return the unmodified source if dryRun is true
119
+ return _context.abrupt("return", fileInfo.source);
120
+ case 28:
121
+ return _context.abrupt("return", source.toSource());
122
+ case 29:
123
+ case "end":
124
+ return _context.stop();
125
+ }
126
+ }, _callee);
127
+ }));
128
+ return _transformer.apply(this, arguments);
129
+ }
130
+ export var parser = 'tsx';
131
+
132
+ /**
133
+ * Function executed before all transformations to prepare the environment by clearing the report folder.
134
+ */
135
+ export function beforeAll(_x4) {
136
+ return _beforeAll.apply(this, arguments);
137
+ }
138
+
139
+ /**
140
+ * Function executed after all transformations to combine individual file reports into a comprehensive transformation report.
141
+ * It also applies prettier to the affected files.
142
+ */
143
+ function _beforeAll() {
144
+ _beforeAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(options) {
145
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
146
+ while (1) switch (_context2.prev = _context2.next) {
147
+ case 0:
148
+ if (!options.reportFolder) {
149
+ _context2.next = 3;
150
+ break;
151
+ }
152
+ _context2.next = 3;
153
+ return clearFolder(options.reportFolder);
154
+ case 3:
155
+ case "end":
156
+ return _context2.stop();
157
+ }
158
+ }, _callee2);
159
+ }));
160
+ return _beforeAll.apply(this, arguments);
161
+ }
162
+ export function afterAll(_x5) {
163
+ return _afterAll.apply(this, arguments);
164
+ }
165
+ function _afterAll() {
166
+ _afterAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(options) {
167
+ var filesTxtPath, fileContent;
168
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
169
+ while (1) switch (_context3.prev = _context3.next) {
170
+ case 0:
171
+ if (!options.reportFolder) {
172
+ _context3.next = 3;
173
+ break;
174
+ }
175
+ _context3.next = 3;
176
+ return combineReports(options.reportFolder);
177
+ case 3:
178
+ if (!(options.reportFolder && !options.dry)) {
179
+ _context3.next = 19;
180
+ break;
181
+ }
182
+ _context3.prev = 4;
183
+ filesTxtPath = path.join(options.reportFolder, 'files.txt');
184
+ _context3.next = 8;
185
+ return fs.readFile(filesTxtPath, 'utf-8');
186
+ case 8:
187
+ fileContent = _context3.sent;
188
+ if (!(fileContent.length > 0)) {
189
+ _context3.next = 14;
190
+ break;
191
+ }
192
+ console.log("Running prettier on files: ".concat(chalk.magenta(fileContent)));
193
+ _context3.next = 13;
194
+ return execAsync("yarn prettier --write ".concat(fileContent));
195
+ case 13:
196
+ console.log(chalk.green("Prettier was run successfully"));
197
+ case 14:
198
+ _context3.next = 19;
199
+ break;
200
+ case 16:
201
+ _context3.prev = 16;
202
+ _context3.t0 = _context3["catch"](4);
203
+ if (_context3.t0 instanceof Error) {
204
+ console.error(chalk.red("Unexpected error running Prettier: ".concat(_context3.t0.message)));
205
+ } else {
206
+ console.error(chalk.red('An unknown error occurred while running Prettier.'));
207
+ }
208
+ case 19:
209
+ case "end":
210
+ return _context3.stop();
211
+ }
212
+ }, _callee3, null, [[4, 16]]);
213
+ }));
214
+ return _afterAll.apply(this, arguments);
215
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,38 @@
1
+ import { legacyLightTokens as legacyLightTheme, light as lightTheme, shape as shapeTheme, spacing as spacingTheme, typographyAdg3 as typographyAdg3Theme } from '@atlaskit/tokens/tokens-raw';
2
+ var typographyGroups = ['typography', 'fontWeight', 'fontFamily'];
3
+ function buildCombinedMap() {
4
+ var combinedMap = {};
5
+ for (var _len = arguments.length, arrays = new Array(_len), _key = 0; _key < _len; _key++) {
6
+ arrays[_key] = arguments[_key];
7
+ }
8
+ arrays.forEach(function (array) {
9
+ array.forEach(function (token) {
10
+ combinedMap[token.cleanName] = token.value.toString();
11
+ });
12
+ });
13
+ return combinedMap;
14
+ }
15
+
16
+ // Filter the typography tokens based on predefined groups and exclusions
17
+ var typographyAdg3ThemeFiltered = typographyAdg3Theme.filter(function (token) {
18
+ return typographyGroups.includes(token.attributes.group);
19
+ }).filter(function (token) {
20
+ return token.cleanName !== 'font.body.UNSAFE_small';
21
+ });
22
+
23
+ // Cache array: [0] for light theme, [1] for legacy light theme
24
+ var tokenMapCache = [null, null];
25
+
26
+ // Function to get the token map with the desired theme.
27
+ // This should be used with the same value as the @atlaskit/tokens/babel-plugin is cofigured with for the given path.
28
+ // When there is no theme, the babel plugin would use the given theme to provide default token fallbacks. Therefore it's safe to remove fallbacks only when they match the fallbacks that would be generated by the babel plugin with the given theme.
29
+ // The default value is set to use the Light theme to match the behavior of the babel plugin (packages/design-system/tokens/src/babel-plugin/plugin.tsx).
30
+ export function getTokenMap() {
31
+ var useLegacyTheme = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
32
+ var themeIndex = useLegacyTheme ? 1 : 0;
33
+ if (!tokenMapCache[themeIndex]) {
34
+ var selectedTheme = useLegacyTheme ? legacyLightTheme : lightTheme;
35
+ tokenMapCache[themeIndex] = buildCombinedMap(selectedTheme, spacingTheme, shapeTheme, typographyAdg3ThemeFiltered);
36
+ }
37
+ return tokenMapCache[themeIndex];
38
+ }
@@ -0,0 +1,86 @@
1
+ import { diff, rgb_to_lab } from 'color-diff';
2
+
3
+ // Compare hex values using a CIEDE2000 color difference algorithm
4
+ export var compareHex = function compareHex(hex, hex2) {
5
+ return diff(rgb_to_lab(hexToRgbA(hex)), rgb_to_lab(hexToRgbA(hex2)));
6
+ };
7
+ function hexToRgbA(hex) {
8
+ // Remove the leading '#' if present
9
+ hex = hex.replace(/^#/, '');
10
+ // Parse the hex string
11
+ var r = parseInt(hex.substring(0, 2), 16);
12
+ var g = parseInt(hex.substring(2, 4), 16);
13
+ var b = parseInt(hex.substring(4, 6), 16);
14
+ var a = parseInt(hex.substring(6, 8), 16) / 255;
15
+ return {
16
+ R: r,
17
+ G: g,
18
+ B: b,
19
+ A: a
20
+ };
21
+ }
22
+ var namedColors = {
23
+ black: '#000000',
24
+ silver: '#C0C0C0',
25
+ gray: '#808080',
26
+ grey: '#808080',
27
+ pink: '#FFC0CB',
28
+ white: '#FFFFFF',
29
+ maroon: '#800000',
30
+ red: '#FF0000',
31
+ purple: '#800080',
32
+ fuchsia: '#FF00FF',
33
+ green: '#008000',
34
+ lime: '#00FF00',
35
+ olive: '#808000',
36
+ yellow: '#FFFF00',
37
+ navy: '#000080',
38
+ blue: '#0000FF',
39
+ teal: '#008080',
40
+ aqua: '#00FFFF'
41
+ };
42
+ export function isValidColor(color) {
43
+ // Check if it's a named color
44
+ if (namedColors[color.toLowerCase()]) {
45
+ return true;
46
+ }
47
+ // Check for hex colors (including those with alpha)
48
+ if (/^#([0-9A-F]{3}){1,2}([0-9A-F]{2})?$/i.test(color)) {
49
+ return true;
50
+ }
51
+ // Check for rgba() values
52
+ if (/^rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(?:,\s*(?:0?\.)?\d+\s*)?\)$/i.test(color)) {
53
+ return true;
54
+ }
55
+ return false;
56
+ }
57
+ export function colorToHex(color) {
58
+ // Handle named colors
59
+ if (namedColors[color.toLowerCase()]) {
60
+ return namedColors[color.toLowerCase()].toUpperCase() + 'FF'; // Add full opacity
61
+ }
62
+ if (color.startsWith('#')) {
63
+ // If it's already a hex color
64
+ if (color.length === 7) {
65
+ // #RRGGBB format, add full opacity
66
+ return (color + 'FF').toUpperCase();
67
+ } else if (color.length === 9) {
68
+ // #RRGGBBAA format, return as is
69
+ return color.toUpperCase();
70
+ } else if (color.length === 4) {
71
+ // #RGB format, expand to #RRGGBBFF
72
+ return ('#' + color[1] + color[1] + color[2] + color[2] + color[3] + color[3] + 'FF').toUpperCase();
73
+ }
74
+ }
75
+ // For rgb() and rgba(), convert to hex
76
+ var match = color.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d*\.?\d+)\s*)?\)$/i);
77
+ if (match) {
78
+ var r = parseInt(match[1], 10);
79
+ var g = parseInt(match[2], 10);
80
+ var b = parseInt(match[3], 10);
81
+ var a = match[4] !== undefined ? Math.round(parseFloat(match[4]) * 255) : 255;
82
+ return "#".concat(((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).padStart(6, '0')).concat(a.toString(16).padStart(2, '0')).toUpperCase();
83
+ }
84
+ // If conversion is not possible, return the original color
85
+ return color;
86
+ }
@@ -0,0 +1,44 @@
1
+ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
2
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
3
+ import readPkgUp from 'read-pkg-up';
4
+ /**
5
+ * Looks for the closest package.json and gets team and package information from it.
6
+ *
7
+ * @param {string} filePath
8
+ * @returns {Promise<AtlassianTeamInfo>}
9
+ */
10
+ export var getTeamInfo = /*#__PURE__*/function () {
11
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(filePath) {
12
+ var _pkgJson$packageJson$;
13
+ var pkgJson, packageName, teamName;
14
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
15
+ while (1) switch (_context.prev = _context.next) {
16
+ case 0:
17
+ _context.next = 2;
18
+ return readPkgUp({
19
+ cwd: filePath
20
+ });
21
+ case 2:
22
+ pkgJson = _context.sent;
23
+ if (!(!pkgJson || !pkgJson.packageJson)) {
24
+ _context.next = 5;
25
+ break;
26
+ }
27
+ throw new Error("Closest package.json file could not be found or is invalid for ".concat(filePath, "."));
28
+ case 5:
29
+ packageName = pkgJson.packageJson.name || '';
30
+ teamName = ((_pkgJson$packageJson$ = pkgJson.packageJson.atlassian) === null || _pkgJson$packageJson$ === void 0 ? void 0 : _pkgJson$packageJson$.team) || '';
31
+ return _context.abrupt("return", {
32
+ packageName: packageName,
33
+ teamName: teamName
34
+ });
35
+ case 8:
36
+ case "end":
37
+ return _context.stop();
38
+ }
39
+ }, _callee);
40
+ }));
41
+ return function getTeamInfo(_x) {
42
+ return _ref.apply(this, arguments);
43
+ };
44
+ }();
@@ -0,0 +1,107 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import chalk from 'chalk';
3
+ import { colorToHex, compareHex, isValidColor } from './color-utils';
4
+
5
+ // so far allowing to remove only exact matches in values. Can be increased to auto-remove fallbacks with similar values
6
+ var ACCEPTABLE_COLOR_DIFFERENCE = 0;
7
+ var ACCEPTABLE_SPACE_DIFFERENCE = 0;
8
+ var ACCEPTABLE_NUMERIC_DIFFERENCE = 0;
9
+ export function normalizeValues(tokenKey, tokenValue, fallbackValue) {
10
+ var tokenLogValue;
11
+ var fallbackLogValue;
12
+ var normalizedTokenValue = tokenValue;
13
+ var normalizedFallbackValue = fallbackValue;
14
+ var lowerCaseTokenKey = tokenKey === null || tokenKey === void 0 ? void 0 : tokenKey.toLowerCase();
15
+ var difference;
16
+ var isAcceptableDifference;
17
+ if (lowerCaseTokenKey.startsWith('color') || lowerCaseTokenKey.startsWith('elevation')) {
18
+ if (tokenValue && isValidColor(tokenValue)) {
19
+ var normalizedHex = colorToHex(tokenValue);
20
+ tokenLogValue = chalk.bgHex(normalizedHex)(tokenValue);
21
+ normalizedTokenValue = normalizedHex;
22
+ }
23
+ if (fallbackValue && isValidColor(fallbackValue)) {
24
+ var _normalizedHex = colorToHex(fallbackValue);
25
+ fallbackLogValue = chalk.bgHex(_normalizedHex)(fallbackValue);
26
+ normalizedFallbackValue = _normalizedHex;
27
+ }
28
+ if (normalizedTokenValue && normalizedFallbackValue) {
29
+ difference = compareHex(normalizedTokenValue, normalizedFallbackValue);
30
+ isAcceptableDifference = difference <= ACCEPTABLE_COLOR_DIFFERENCE;
31
+ }
32
+ } else if (lowerCaseTokenKey.startsWith('space')) {
33
+ var tokenValueInPx = tokenValue ? convertToPx(tokenValue) : undefined;
34
+ var fallbackValueInPx = fallbackValue ? convertToPx(fallbackValue) : undefined;
35
+ if (tokenValueInPx !== undefined && fallbackValueInPx !== undefined) {
36
+ var maxVal = Math.max(tokenValueInPx, fallbackValueInPx);
37
+ difference = Math.abs(tokenValueInPx - fallbackValueInPx) / maxVal * 100;
38
+ isAcceptableDifference = difference <= ACCEPTABLE_SPACE_DIFFERENCE;
39
+ }
40
+ // Log the normalized values
41
+ normalizedTokenValue = tokenValue;
42
+ normalizedFallbackValue = fallbackValue;
43
+ tokenLogValue = tokenValue;
44
+ fallbackLogValue = fallbackValue;
45
+ } else {
46
+ // Handle other numeric comparisons
47
+ var tokenValueNumber = parseFloat(tokenValue !== null && tokenValue !== void 0 ? tokenValue : '');
48
+ var fallbackValueNumber = parseFloat(fallbackValue !== null && fallbackValue !== void 0 ? fallbackValue : '');
49
+ if (!isNaN(tokenValueNumber) && !isNaN(fallbackValueNumber)) {
50
+ var _maxVal = Math.max(tokenValueNumber, fallbackValueNumber);
51
+ difference = Math.abs(tokenValueNumber - fallbackValueNumber) / _maxVal * 100;
52
+ isAcceptableDifference = difference <= ACCEPTABLE_NUMERIC_DIFFERENCE;
53
+ }
54
+ // Log the normalized values
55
+ normalizedTokenValue = tokenValue;
56
+ normalizedFallbackValue = fallbackValue;
57
+ tokenLogValue = tokenValue;
58
+ fallbackLogValue = fallbackValue;
59
+ }
60
+ if (tokenLogValue === undefined) {
61
+ tokenLogValue = chalk.magenta(tokenValue || '');
62
+ }
63
+ if (fallbackLogValue === undefined) {
64
+ fallbackLogValue = chalk.yellow(fallbackValue || '');
65
+ }
66
+ return {
67
+ difference: difference,
68
+ isAcceptableDifference: isAcceptableDifference,
69
+ tokenLogValue: tokenLogValue,
70
+ fallbackLogValue: fallbackLogValue,
71
+ normalizedTokenValue: normalizedTokenValue,
72
+ normalizedFallbackValue: normalizedFallbackValue
73
+ };
74
+ }
75
+ function convertToPx(value) {
76
+ // If the value is a number, return it directly
77
+ if (typeof value === 'number') {
78
+ return value;
79
+ }
80
+ // Check if the string is a plain number (without units)
81
+ var plainNumberRegex = /^-?\d+(\.\d+)?$/;
82
+ if (plainNumberRegex.test(value)) {
83
+ return parseFloat(value);
84
+ }
85
+ // Regular expression to match CSS units
86
+ var unitRegex = /^(-?\d+(\.\d+)?)(px|rem|em|%)$/;
87
+ var match = value.match(unitRegex);
88
+ if (!match) {
89
+ return undefined;
90
+ }
91
+ var _match = _slicedToArray(match, 4),
92
+ num = _match[1],
93
+ unit = _match[3];
94
+ var numericValue = parseFloat(num);
95
+ switch (unit) {
96
+ case 'px':
97
+ return numericValue;
98
+ case 'rem':
99
+ return numericValue * 16;
100
+ // Assuming 1rem = 16px
101
+ case 'em':
102
+ return numericValue * 16;
103
+ // Assuming 1em = 16px
104
+ default:
105
+ return undefined;
106
+ }
107
+ }
@@ -0,0 +1,55 @@
1
+ export function removeUnusedImports(importDeclarations, j) {
2
+ var removeIfUnused = function removeIfUnused(importSpecifier, importDeclaration) {
3
+ var _importSpecifier$valu;
4
+ var varName = (_importSpecifier$valu = importSpecifier.value.local) === null || _importSpecifier$valu === void 0 ? void 0 : _importSpecifier$valu.name;
5
+ if (varName === 'React' || varName === 'jsx') {
6
+ return false;
7
+ }
8
+ var isUsedInScopes = function isUsedInScopes() {
9
+ return j(importDeclaration).closestScope().find(j.Identifier, {
10
+ name: varName
11
+ }).filter(function (p) {
12
+ var _importSpecifier$valu2;
13
+ if (p.value.start === ((_importSpecifier$valu2 = importSpecifier.value.local) === null || _importSpecifier$valu2 === void 0 ? void 0 : _importSpecifier$valu2.start)) {
14
+ return false;
15
+ }
16
+ if (p.parentPath.value.type === 'Property' && p.name === 'key') {
17
+ return false;
18
+ }
19
+ if (p.name === 'property') {
20
+ return false;
21
+ }
22
+ return true;
23
+ }).size() > 0;
24
+ };
25
+ if (!isUsedInScopes()) {
26
+ j(importSpecifier).remove();
27
+ return true;
28
+ }
29
+ return false;
30
+ };
31
+ var removeUnusedDefaultImport = function removeUnusedDefaultImport(importDeclaration) {
32
+ return j(importDeclaration).find(j.ImportDefaultSpecifier).filter(function (s) {
33
+ return removeIfUnused(s, importDeclaration);
34
+ }).size() > 0;
35
+ };
36
+ var removeUnusedNonDefaultImports = function removeUnusedNonDefaultImports(importDeclaration) {
37
+ return j(importDeclaration).find(j.ImportSpecifier).filter(function (s) {
38
+ return removeIfUnused(s, importDeclaration);
39
+ }).size() > 0;
40
+ };
41
+ var processImportDeclaration = function processImportDeclaration(importDeclaration) {
42
+ var _importDeclaration$va, _importDeclaration$va2;
43
+ if (((_importDeclaration$va = importDeclaration.value.specifiers) === null || _importDeclaration$va === void 0 ? void 0 : _importDeclaration$va.length) === 0) {
44
+ return false;
45
+ }
46
+ var hadUnusedDefaultImport = removeUnusedDefaultImport(importDeclaration);
47
+ var hadUnusedNonDefaultImports = removeUnusedNonDefaultImports(importDeclaration);
48
+ if (((_importDeclaration$va2 = importDeclaration.value.specifiers) === null || _importDeclaration$va2 === void 0 ? void 0 : _importDeclaration$va2.length) === 0) {
49
+ j(importDeclaration).remove();
50
+ return true;
51
+ }
52
+ return hadUnusedDefaultImport || hadUnusedNonDefaultImports;
53
+ };
54
+ importDeclarations.forEach(processImportDeclaration);
55
+ }
@@ -0,0 +1,31 @@
1
+ export function removeUnusedVariables(variableDeclarations, j) {
2
+ var removeIfUnused = function removeIfUnused(varDeclarator) {
3
+ var _varDeclarator$value;
4
+ if (((_varDeclarator$value = varDeclarator.value) === null || _varDeclarator$value === void 0 ? void 0 : _varDeclarator$value.id.type) !== 'Identifier') {
5
+ return false;
6
+ }
7
+ var varName = varDeclarator.value.id.name;
8
+ var isUsedInScopes = function isUsedInScopes() {
9
+ return j(varDeclarator).closestScope().find(j.Identifier, {
10
+ name: varName
11
+ }).filter(function (p) {
12
+ if (p.value.start === varDeclarator.value.id.start) {
13
+ return false;
14
+ }
15
+ if (p.parentPath.value.type === 'Property' && p.name === 'key') {
16
+ return false;
17
+ }
18
+ if (p.name === 'property') {
19
+ return false;
20
+ }
21
+ return true;
22
+ }).size() > 0;
23
+ };
24
+ if (!isUsedInScopes()) {
25
+ j(varDeclarator).remove();
26
+ return true;
27
+ }
28
+ return false;
29
+ };
30
+ variableDeclarations.forEach(removeIfUnused);
31
+ }