@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,58 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.addOrUpdateEslintIgnoreComment = addOrUpdateEslintIgnoreComment;
8
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
9
+ function addOrUpdateEslintIgnoreComment(j, tokenValue, fallbackValue, callPath) {
10
+ var commentText = "eslint-disable-next-line @atlaskit/design-system/no-unsafe-design-token-usage -- The token value \"".concat(tokenValue, "\" and fallback \"").concat(fallbackValue, "\" do not match and can't be replaced automatically.");
11
+ // first see if we can add the comment to the parent object property
12
+ var updatedCommentInObjectExpression = addOrUpdateEslintIgnoreCommentForParentInObjectExpression(j, commentText, callPath);
13
+ // if the comment was not added to the parent object property, add it to the node itself
14
+ if (!updatedCommentInObjectExpression) {
15
+ addOrUpdateEslintIgnoreCommentForNodeItself(j, commentText, callPath);
16
+ }
17
+ }
18
+ function addOrUpdateEslintIgnoreCommentForNodeItself(j, commentText, callPath) {
19
+ var leadingComments = callPath.node.leadingComments || [];
20
+ var existingCommentIndex = leadingComments.findIndex(function (comment) {
21
+ return comment.value.includes('eslint-disable-next-line @atlaskit/design-system/no-unsafe-design-token-usage');
22
+ });
23
+ var commentLine = j.commentLine(commentText, true);
24
+ if (existingCommentIndex !== -1) {
25
+ // Replace the existing comment
26
+ // Note: in order to modify the comment, it's fine to update it in the `leadingComments` array
27
+ leadingComments[existingCommentIndex] = commentLine;
28
+ } else {
29
+ // Add a new comment if none exists
30
+ // Note: Adding new comment to 'leadingComments' doesn't affect anything, we need to add it to 'comments' property
31
+ callPath.node.comments = [commentLine];
32
+ }
33
+ }
34
+ function addOrUpdateEslintIgnoreCommentForParentInObjectExpression(j, commentText, callPath) {
35
+ var parent = callPath.parentPath;
36
+ // Check if the parent node is an ObjectProperty
37
+ if (parent && parent.node.type === 'ObjectProperty') {
38
+ var grandparent = parent.parentPath;
39
+ // Check if the grandparent is an ObjectExpression
40
+ if (grandparent && grandparent.node.type === 'ObjectExpression') {
41
+ // Check for existing leading comments
42
+ var leadingComments = parent.node.leadingComments || [];
43
+ var existingCommentIndex = leadingComments.findIndex(function (comment) {
44
+ return comment.value.includes('eslint-disable-next-line @atlaskit/design-system/no-unsafe-design-token-usage');
45
+ });
46
+ var commentLine = j.commentLine(commentText, true);
47
+ if (existingCommentIndex !== -1) {
48
+ // Replace the existing comment
49
+ leadingComments[existingCommentIndex] = commentLine;
50
+ } else {
51
+ // Add a new comment if none exists
52
+ parent.node.comments = [commentLine].concat((0, _toConsumableArray2.default)(parent.node.comments || []));
53
+ }
54
+ return true;
55
+ }
56
+ }
57
+ return false;
58
+ }
@@ -6,13 +6,13 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.default = transformer;
8
8
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
- var _updateSafeImports = require("./transformers/update-safe-imports");
9
+ var _autoScroll = require("./transformers/auto-scroll");
10
+ var _elementAdapterTypes = require("./transformers/element-adapter-types");
10
11
  var _externalAdapter = require("./transformers/external-adapter");
11
12
  var _moveToReactDropIndicator = require("./transformers/move-to-react-drop-indicator");
12
- var _autoScroll = require("./transformers/auto-scroll");
13
13
  var _pointerOutsideOfPreview = require("./transformers/pointer-outside-of-preview");
14
14
  var _preventUnhandled = require("./transformers/prevent-unhandled");
15
- var _elementAdapterTypes = require("./transformers/element-adapter-types");
15
+ var _updateSafeImports = require("./transformers/update-safe-imports");
16
16
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
17
17
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
18
18
  function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
@@ -23,7 +23,8 @@ var parseDiffLine = function parseDiffLine(line) {
23
23
  };
24
24
  };
25
25
 
26
- /** Returns packages that have been upgraded in package.json since ref. The version refers to their previous
26
+ /**
27
+ * Returns packages that have been upgraded in package.json since ref. The version refers to their previous
27
28
  * version
28
29
  */
29
30
  var getPackagesSinceRef = exports.getPackagesSinceRef = /*#__PURE__*/function () {
@@ -20,7 +20,9 @@ var hasTransform = exports.hasTransform = function hasTransform(transformPath) {
20
20
  return (0, _glob.globSync)(transformPath).length > 0;
21
21
  };
22
22
 
23
- /** Retrieves transforms for `packages` if provided, otherwise all transforms including presets */
23
+ /**
24
+ * Retrieves transforms for `packages` if provided, otherwise all transforms including presets
25
+ */
24
26
  var getTransforms = exports.getTransforms = function getTransforms(packages) {
25
27
  var transforms = [_path.default.join(basePath(packages), '*.@(ts|tsx|js)'), _path.default.join(basePath(packages), '*', 'index.@(ts|tsx|js)')];
26
28
  if (!packages) {
package/dist/cjs/types.js CHANGED
@@ -13,7 +13,8 @@ var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits
13
13
  var _wrapNativeSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapNativeSuper"));
14
14
  function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
15
15
  function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
16
- /** Converts required args to optional if they have a default
16
+ /**
17
+ * Converts required args to optional if they have a default
17
18
  * Example: export type UserFlags = Default<Flags, keyof typeof defaultFlags>;
18
19
  */
19
20
  var ValidationError = exports.ValidationError = /*#__PURE__*/function (_Error) {
@@ -1,7 +1,7 @@
1
+ import chalk from 'chalk';
1
2
  import meow from 'meow';
2
- import { ValidationError, NoTransformsExistError } from './types';
3
3
  import main from './main';
4
- import chalk from 'chalk';
4
+ import { NoTransformsExistError, ValidationError } from './types';
5
5
  export async function run() {
6
6
  const cli = meow(`
7
7
  Usage
@@ -17,7 +17,7 @@ Options
17
17
  --ignore-pattern, ignore files that match a provided glob expression
18
18
  --fail-on-error, return a 1 exit code when errors were found during execution of codemods
19
19
  --version, -v version number
20
- --no-filter-paths disables dependant package file path filtering logic
20
+ --no-filter-paths disables dependant package file path filtering logic
21
21
  --help Help me 😱
22
22
 
23
23
  Examples
@@ -1,10 +1,12 @@
1
- import { globSync } from 'glob';
2
1
  import { promises } from 'fs';
2
+ import { globSync } from 'glob';
3
3
  const {
4
4
  readFile
5
5
  } = promises;
6
6
 
7
- /** Return products packages filePaths for running codemods from specified dependent package */
7
+ /**
8
+ * Return products packages filePaths for running codemods from specified dependent package
9
+ */
8
10
  export async function findDependentPackagePaths(crawlPaths, dependencyPackage) {
9
11
  // Get file paths leading to package.jsons
10
12
  const searchStrings = crawlPaths.map(crawlPath => {
@@ -1,14 +1,14 @@
1
- import chalk from 'chalk';
2
1
  import fs from 'fs';
3
- import spawn from 'projector-spawn';
2
+ import chalk from 'chalk';
4
3
  import { AutoComplete } from 'enquirer';
4
+ import spawn from 'projector-spawn';
5
5
  import semver from 'semver';
6
6
  const jscodeshift = require.resolve('.bin/jscodeshift');
7
- import { fixLineEnding } from './utils';
8
- import { getTransforms, getTransformPath, hasTransform, parseTransformPath, getTransformModule } from './transforms';
9
- import { ValidationError, NoTransformsExistError } from './types';
10
- import { getPackagesSinceRef } from './sinceRef';
11
7
  import { findDependentPackagePaths } from './filepath';
8
+ import { getPackagesSinceRef } from './sinceRef';
9
+ import { getTransformModule, getTransformPath, getTransforms, hasTransform, parseTransformPath } from './transforms';
10
+ import { NoTransformsExistError, ValidationError } from './types';
11
+ import { fixLineEnding } from './utils';
12
12
  const applyTransformMeta = transforms => transforms.map(transform => {
13
13
  const moduleMatch = transform.dir.match(/\/@atlaskit\/[^\/]+\//);
14
14
  if (moduleMatch) {
@@ -106,9 +106,22 @@ const runTransform = async (filePaths, transform, flags) => {
106
106
  const jscodeshiftContent = fs.readFileSync(jscodeshift, 'utf8');
107
107
  const jscodeshiftContentNew = fixLineEnding(jscodeshiftContent, 'LF');
108
108
  fs.writeFileSync(jscodeshift, jscodeshiftContentNew);
109
+ let transformModule;
110
+ try {
111
+ transformModule = require(transformPath);
112
+ } catch (error) {
113
+ // eslint-disable-next-line no-console
114
+ console.warn(`Error loading transform module: ${transformPath}. Skipping lifecycle hooks.`);
115
+ }
116
+ if (transformModule) {
117
+ await processLifecycleHook('beforeAll', transformModule, logger, transform, flags);
118
+ }
109
119
  await spawn(jscodeshift, args, {
110
120
  stdio: 'inherit'
111
121
  });
122
+ if (transformModule) {
123
+ await processLifecycleHook('afterAll', transformModule, logger, transform, flags);
124
+ }
112
125
  };
113
126
  const parsePkg = pkg => {
114
127
  if (!pkg.startsWith('@')) {
@@ -179,6 +192,17 @@ const defaultFlags = {
179
192
  ignorePattern: 'node_modules',
180
193
  logger: console
181
194
  };
195
+ async function processLifecycleHook(hookName, transformModule, logger, transform, flags) {
196
+ if (typeof transformModule[hookName] === 'function') {
197
+ try {
198
+ logger.log(chalk.green(`Executing ${hookName} for transform '${transform.name}'...`));
199
+ await transformModule[hookName](flags);
200
+ } catch (error) {
201
+ logger.log(chalk.red(`Error in ${hookName} for transform '${transform.name}': ${error}`));
202
+ throw error; // Re-throw error for beforeAll to halt execution
203
+ }
204
+ }
205
+ }
182
206
  export default async function main(input, userFlags) {
183
207
  const flags = {
184
208
  ...defaultFlags,
@@ -10,5 +10,6 @@ import './migrate-to-link/migrate-to-link';
10
10
  import './migrate-to-new-buttons/migrate-to-new-buttons';
11
11
  import './upgrade-pragmatic-drag-and-drop-to-stable/upgrade-pragmatic-drag-and-drop-to-stable';
12
12
  import './remove-dark-theme-vr-options/remove-dark-theme-vr-options';
13
- const presets = ['styled-to-emotion', 'theme-remove-deprecated-mixins', 'migrate-to-link', 'migrate-to-new-buttons', 'upgrade-pragmatic-drag-and-drop-to-stable', 'remove-dark-theme-vr-options'].map(preset => path.join(__dirname, preset, `${preset}.@(ts|js|tsx)`));
13
+ import './remove-token-fallbacks/remove-token-fallbacks';
14
+ const presets = ['styled-to-emotion', 'theme-remove-deprecated-mixins', 'migrate-to-link', 'migrate-to-new-buttons', 'upgrade-pragmatic-drag-and-drop-to-stable', 'remove-dark-theme-vr-options', 'remove-token-fallbacks'].map(preset => path.join(__dirname, preset, `${preset}.@(ts|js|tsx)`));
14
15
  export default presets;
@@ -1,9 +1,8 @@
1
1
  /* eslint-disable @repo/internal/fs/filename-pattern-match */
2
-
3
- import { addCommentBefore } from '@atlaskit/codemod-utils';
4
2
  import { getImportDeclaration } from '@hypermod/utils';
5
- import { LINK_ENTRY_POINT, LINK_EXPORT_NAME, PRINT_SETTINGS, UNSUPPORTED_STYLE_PROPS } from '../utils/constants';
3
+ import { addCommentBefore } from '@atlaskit/codemod-utils';
6
4
  import { genericUnsupportedMigrationComment, spreadPropsComment, stylePropComment } from '../utils/comments';
5
+ import { LINK_ENTRY_POINT, LINK_EXPORT_NAME, PRINT_SETTINGS, UNSUPPORTED_STYLE_PROPS } from '../utils/constants';
7
6
  import { findJSXAttributeWithValue } from '../utils/find-attribute-with-value';
8
7
  export default function transformer(file, api) {
9
8
  const j = api.jscodeshift;
@@ -1,5 +1,5 @@
1
1
  import { getImportDeclaration, hasJSXAttributes } from '@hypermod/utils';
2
- import { PRINT_SETTINGS, NEW_BUTTON_ENTRY_POINT, UNSAFE_SIZE_PROPS_MAP } from '../utils/constants';
2
+ import { NEW_BUTTON_ENTRY_POINT, PRINT_SETTINGS, UNSAFE_SIZE_PROPS_MAP } from '../utils/constants';
3
3
  function transformer(file, api) {
4
4
  const j = api.jscodeshift;
5
5
  const source = j(file.source);
@@ -1,6 +1,6 @@
1
- import { PRINT_SETTINGS, entryPointsMapping, BUTTON_TYPES, NEW_BUTTON_ENTRY_POINT } from '../utils/constants';
2
- import renameElements from '../utils/rename-elements';
3
1
  import { getDefaultImportSpecifierName } from '@hypermod/utils';
2
+ import { BUTTON_TYPES, entryPointsMapping, NEW_BUTTON_ENTRY_POINT, PRINT_SETTINGS } from '../utils/constants';
3
+ import renameElements from '../utils/rename-elements';
4
4
  const transformer = (file, api) => {
5
5
  const j = api.jscodeshift;
6
6
  const fileSource = j(file.source);
@@ -1,7 +1,7 @@
1
- import splitImportsTransformer from './codemods/next-split-imports';
1
+ import migrateLinkButtonToLinkTransformer from './codemods/next-migrate-link-button-to-link';
2
2
  import migrateToNewButtonVariantsTransformer from './codemods/next-migrate-to-new-button-variants';
3
3
  import removeUnsafeSizeTransformer from './codemods/next-remove-unsafe-size';
4
- import migrateLinkButtonToLinkTransformer from './codemods/next-migrate-link-button-to-link';
4
+ import splitImportsTransformer from './codemods/next-split-imports';
5
5
  export default async function transformer(file, api) {
6
6
  const transformers = [splitImportsTransformer, migrateToNewButtonVariantsTransformer, removeUnsafeSizeTransformer, migrateLinkButtonToLinkTransformer];
7
7
  let src = file.source;
@@ -1,6 +1,6 @@
1
1
  import { addCommentBefore } from '@atlaskit/codemod-utils';
2
- import { entryPointsMapping, customThemeButtonComment } from './constants';
3
2
  import { addCommentForOverlayProp } from './add-comment-for-overlay-prop';
3
+ import { customThemeButtonComment, entryPointsMapping } from './constants';
4
4
  export const addCommentForCustomThemeButtons = (fileSource, j) => {
5
5
  var _fileSource$find$filt;
6
6
  let customThemeButtonImportName;
@@ -1,6 +1,6 @@
1
1
  import { addCommentBefore } from '@atlaskit/codemod-utils';
2
2
  import { getIconAttributes, getIconElement } from '../utils/generate-new-button-element';
3
- import { NEW_BUTTON_VARIANTS, migrateFitContainerButtonToDefaultButtonComment, migrateFitContainerButtonToIconButtonComment } from './constants';
3
+ import { migrateFitContainerButtonToDefaultButtonComment, migrateFitContainerButtonToIconButtonComment, NEW_BUTTON_VARIANTS } from './constants';
4
4
  export const migrateFitContainerIconButton = (element, j) => {
5
5
  var _iconElement$openingE, _labelAttribute$value;
6
6
  const {
@@ -0,0 +1,130 @@
1
+ /* eslint-disable no-console */
2
+ import { exec } from 'child_process';
3
+ import fs from 'fs/promises';
4
+ import path from 'path';
5
+ import { promisify } from 'util';
6
+ import { hasImportDeclaration } from '@hypermod/utils';
7
+ import { findRoot } from '@manypkg/find-root';
8
+ import chalk from 'chalk';
9
+ import { getTokenMap } from './utils/all-tokens';
10
+ import { getTeamInfo } from './utils/get-team-info';
11
+ import { removeUnusedImports } from './utils/remove-unused-imports';
12
+ import { removeUnusedVariables } from './utils/remove-unused-variables';
13
+ import { clearFolder, combineReports, writeReports } from './utils/reporter';
14
+ import { TokenProcessor } from './utils/token-processor';
15
+ const execAsync = promisify(exec);
16
+
17
+ /**
18
+ * Transforms the source code of a file by removing fallback values from the @atlaskit/tokens/token functions.
19
+ * By default removes only the fallbacks that have the same values as the tokens.
20
+ *
21
+ * @param {FileInfo} fileInfo - Information about the file to be transformed.
22
+ * @param {API} api - The jscodeshift API, providing utilities for AST transformations.
23
+ * @param {RemoveTokenFallbackOptions} options - Options for the transformation, including:
24
+ * @param {boolean} [options.verbose] - If true, enables verbose logging.
25
+ * @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.
26
+ * @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.
27
+ * @param {boolean} [options.useLegacyColorTheme] - If true, uses the legacy theme for color token mapping.
28
+ * @param {string} [options.reportFolder] - Directory path to output transformation reports. Reports will be generated only if this option is provided.
29
+ * @param {boolean} [options.dry] - If true, performs a dry run without modifying the files.
30
+ *
31
+ * @returns {Promise<string>} A promise that resolves to the transformed source code as a string.
32
+ */
33
+ export default async function transformer(fileInfo, {
34
+ jscodeshift: j
35
+ }, options) {
36
+ var _options$useLegacyCol;
37
+ const rootDir = await findRoot(path.dirname(fileInfo.path));
38
+ const source = j(fileInfo.source);
39
+ if (!hasImportDeclaration(j, source, '@atlaskit/tokens')) {
40
+ return fileInfo.source;
41
+ }
42
+ const details = {
43
+ replaced: [],
44
+ notReplaced: []
45
+ };
46
+ if (options.verbose) {
47
+ console.log(chalk.yellow(`Using ${options.useLegacyColorTheme ? 'legacy light' : 'light'} theme.`));
48
+ }
49
+ const tokenMap = getTokenMap((_options$useLegacyCol = options.useLegacyColorTheme) !== null && _options$useLegacyCol !== void 0 ? _options$useLegacyCol : false);
50
+ const teamInfo = await getTeamInfo(fileInfo.path);
51
+ const transformPromises = source.find(j.CallExpression, {
52
+ callee: {
53
+ type: 'Identifier',
54
+ name: 'token'
55
+ }
56
+ }).paths().map(callPath => {
57
+ const processor = new TokenProcessor(j, options, fileInfo, source, rootDir, details, tokenMap, teamInfo);
58
+ return processor.processAndLogSingleToken(callPath);
59
+ });
60
+ const results = await Promise.all(transformPromises);
61
+ const unusedVars = [];
62
+ if (results.some(result => result.fallbackRemoved)) {
63
+ const allImports = results.flatMap(result => {
64
+ var _result$resolvedImpor;
65
+ return (_result$resolvedImpor = result.resolvedImportDeclaration) !== null && _result$resolvedImpor !== void 0 ? _result$resolvedImpor : [];
66
+ });
67
+ const allVars = results.flatMap(result => {
68
+ var _result$resolvedLocal;
69
+ return (_result$resolvedLocal = result.resolvedLocalVarDeclaration) !== null && _result$resolvedLocal !== void 0 ? _result$resolvedLocal : [];
70
+ });
71
+ unusedVars.push(...allVars);
72
+ if (allImports.length) {
73
+ if (options.verbose) {
74
+ console.log(chalk.green(`${fileInfo.path}: Some fallbacks were removed. Cleaning up ${allImports.length} imports.`));
75
+ }
76
+ removeUnusedImports(allImports, j);
77
+ }
78
+ }
79
+ removeUnusedVariables(unusedVars, j);
80
+ if (options.reportFolder) {
81
+ await writeReports(details, options.reportFolder);
82
+ }
83
+ if (options.dry) {
84
+ if (options.verbose) {
85
+ console.log(chalk.cyan(`${fileInfo.path}: dry run mode active. Source was not modified.`));
86
+ }
87
+ // Return the unmodified source if dryRun is true
88
+ return fileInfo.source;
89
+ } else {
90
+ // Return the transformed source
91
+ return source.toSource();
92
+ }
93
+ }
94
+ export const parser = 'tsx';
95
+
96
+ /**
97
+ * Function executed before all transformations to prepare the environment by clearing the report folder.
98
+ */
99
+ export async function beforeAll(options) {
100
+ if (options.reportFolder) {
101
+ await clearFolder(options.reportFolder);
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Function executed after all transformations to combine individual file reports into a comprehensive transformation report.
107
+ * It also applies prettier to the affected files.
108
+ */
109
+ export async function afterAll(options) {
110
+ if (options.reportFolder) {
111
+ await combineReports(options.reportFolder);
112
+ }
113
+ if (options.reportFolder && !options.dry) {
114
+ try {
115
+ const filesTxtPath = path.join(options.reportFolder, 'files.txt');
116
+ const fileContent = await fs.readFile(filesTxtPath, 'utf-8');
117
+ if (fileContent.length > 0) {
118
+ console.log(`Running prettier on files: ${chalk.magenta(fileContent)}`);
119
+ await execAsync(`yarn prettier --write ${fileContent}`);
120
+ console.log(chalk.green(`Prettier was run successfully`));
121
+ }
122
+ } catch (error) {
123
+ if (error instanceof Error) {
124
+ console.error(chalk.red(`Unexpected error running Prettier: ${error.message}`));
125
+ } else {
126
+ console.error(chalk.red('An unknown error occurred while running Prettier.'));
127
+ }
128
+ }
129
+ }
130
+ }
@@ -0,0 +1,30 @@
1
+ import { legacyLightTokens as legacyLightTheme, light as lightTheme, shape as shapeTheme, spacing as spacingTheme, typographyAdg3 as typographyAdg3Theme } from '@atlaskit/tokens/tokens-raw';
2
+ const typographyGroups = ['typography', 'fontWeight', 'fontFamily'];
3
+ function buildCombinedMap(...arrays) {
4
+ const combinedMap = {};
5
+ arrays.forEach(array => {
6
+ array.forEach(token => {
7
+ combinedMap[token.cleanName] = token.value.toString();
8
+ });
9
+ });
10
+ return combinedMap;
11
+ }
12
+
13
+ // Filter the typography tokens based on predefined groups and exclusions
14
+ const typographyAdg3ThemeFiltered = typographyAdg3Theme.filter(token => typographyGroups.includes(token.attributes.group)).filter(token => token.cleanName !== 'font.body.UNSAFE_small');
15
+
16
+ // Cache array: [0] for light theme, [1] for legacy light theme
17
+ const tokenMapCache = [null, null];
18
+
19
+ // Function to get the token map with the desired theme.
20
+ // This should be used with the same value as the @atlaskit/tokens/babel-plugin is cofigured with for the given path.
21
+ // 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.
22
+ // 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).
23
+ export function getTokenMap(useLegacyTheme = false) {
24
+ const themeIndex = useLegacyTheme ? 1 : 0;
25
+ if (!tokenMapCache[themeIndex]) {
26
+ const selectedTheme = useLegacyTheme ? legacyLightTheme : lightTheme;
27
+ tokenMapCache[themeIndex] = buildCombinedMap(selectedTheme, spacingTheme, shapeTheme, typographyAdg3ThemeFiltered);
28
+ }
29
+ return tokenMapCache[themeIndex];
30
+ }
@@ -0,0 +1,84 @@
1
+ import { diff, rgb_to_lab } from 'color-diff';
2
+
3
+ // Compare hex values using a CIEDE2000 color difference algorithm
4
+ export const compareHex = (hex, hex2) => diff(rgb_to_lab(hexToRgbA(hex)), rgb_to_lab(hexToRgbA(hex2)));
5
+ function hexToRgbA(hex) {
6
+ // Remove the leading '#' if present
7
+ hex = hex.replace(/^#/, '');
8
+ // Parse the hex string
9
+ const r = parseInt(hex.substring(0, 2), 16);
10
+ const g = parseInt(hex.substring(2, 4), 16);
11
+ const b = parseInt(hex.substring(4, 6), 16);
12
+ const a = parseInt(hex.substring(6, 8), 16) / 255;
13
+ return {
14
+ R: r,
15
+ G: g,
16
+ B: b,
17
+ A: a
18
+ };
19
+ }
20
+ const namedColors = {
21
+ black: '#000000',
22
+ silver: '#C0C0C0',
23
+ gray: '#808080',
24
+ grey: '#808080',
25
+ pink: '#FFC0CB',
26
+ white: '#FFFFFF',
27
+ maroon: '#800000',
28
+ red: '#FF0000',
29
+ purple: '#800080',
30
+ fuchsia: '#FF00FF',
31
+ green: '#008000',
32
+ lime: '#00FF00',
33
+ olive: '#808000',
34
+ yellow: '#FFFF00',
35
+ navy: '#000080',
36
+ blue: '#0000FF',
37
+ teal: '#008080',
38
+ aqua: '#00FFFF'
39
+ };
40
+ export function isValidColor(color) {
41
+ // Check if it's a named color
42
+ if (namedColors[color.toLowerCase()]) {
43
+ return true;
44
+ }
45
+ // Check for hex colors (including those with alpha)
46
+ if (/^#([0-9A-F]{3}){1,2}([0-9A-F]{2})?$/i.test(color)) {
47
+ return true;
48
+ }
49
+ // Check for rgba() values
50
+ if (/^rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(?:,\s*(?:0?\.)?\d+\s*)?\)$/i.test(color)) {
51
+ return true;
52
+ }
53
+ return false;
54
+ }
55
+ export function colorToHex(color) {
56
+ // Handle named colors
57
+ if (namedColors[color.toLowerCase()]) {
58
+ return namedColors[color.toLowerCase()].toUpperCase() + 'FF'; // Add full opacity
59
+ }
60
+ if (color.startsWith('#')) {
61
+ // If it's already a hex color
62
+ if (color.length === 7) {
63
+ // #RRGGBB format, add full opacity
64
+ return (color + 'FF').toUpperCase();
65
+ } else if (color.length === 9) {
66
+ // #RRGGBBAA format, return as is
67
+ return color.toUpperCase();
68
+ } else if (color.length === 4) {
69
+ // #RGB format, expand to #RRGGBBFF
70
+ return ('#' + color[1] + color[1] + color[2] + color[2] + color[3] + color[3] + 'FF').toUpperCase();
71
+ }
72
+ }
73
+ // For rgb() and rgba(), convert to hex
74
+ const match = color.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d*\.?\d+)\s*)?\)$/i);
75
+ if (match) {
76
+ const r = parseInt(match[1], 10);
77
+ const g = parseInt(match[2], 10);
78
+ const b = parseInt(match[3], 10);
79
+ const a = match[4] !== undefined ? Math.round(parseFloat(match[4]) * 255) : 255;
80
+ return `#${((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).padStart(6, '0')}${a.toString(16).padStart(2, '0')}`.toUpperCase();
81
+ }
82
+ // If conversion is not possible, return the original color
83
+ return color;
84
+ }
@@ -0,0 +1,22 @@
1
+ import readPkgUp from 'read-pkg-up';
2
+ /**
3
+ * Looks for the closest package.json and gets team and package information from it.
4
+ *
5
+ * @param {string} filePath
6
+ * @returns {Promise<AtlassianTeamInfo>}
7
+ */
8
+ export const getTeamInfo = async filePath => {
9
+ var _pkgJson$packageJson$;
10
+ const pkgJson = await readPkgUp({
11
+ cwd: filePath
12
+ });
13
+ if (!pkgJson || !pkgJson.packageJson) {
14
+ throw new Error(`Closest package.json file could not be found or is invalid for ${filePath}.`);
15
+ }
16
+ const packageName = pkgJson.packageJson.name || '';
17
+ const teamName = ((_pkgJson$packageJson$ = pkgJson.packageJson.atlassian) === null || _pkgJson$packageJson$ === void 0 ? void 0 : _pkgJson$packageJson$.team) || '';
18
+ return {
19
+ packageName,
20
+ teamName
21
+ };
22
+ };