@atlaskit/eslint-plugin-design-system 13.14.2 → 13.16.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 (66) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +2 -0
  3. package/dist/cjs/presets/all-flat.codegen.js +3 -1
  4. package/dist/cjs/presets/all.codegen.js +3 -1
  5. package/dist/cjs/presets/recommended-flat.codegen.js +2 -1
  6. package/dist/cjs/presets/recommended.codegen.js +2 -1
  7. package/dist/cjs/rules/ensure-proper-xcss-usage/index.js +157 -0
  8. package/dist/cjs/rules/index.codegen.js +5 -1
  9. package/dist/cjs/rules/no-legacy-icons/checks.js +22 -5
  10. package/dist/cjs/rules/no-legacy-icons/helpers.js +122 -46
  11. package/dist/cjs/rules/no-utility-icons/checks.js +233 -0
  12. package/dist/cjs/rules/no-utility-icons/index.js +40 -0
  13. package/dist/cjs/rules/utils/get-deprecated-config.js +1 -1
  14. package/dist/es2019/presets/all-flat.codegen.js +3 -1
  15. package/dist/es2019/presets/all.codegen.js +3 -1
  16. package/dist/es2019/presets/recommended-flat.codegen.js +2 -1
  17. package/dist/es2019/presets/recommended.codegen.js +2 -1
  18. package/dist/es2019/rules/ensure-proper-xcss-usage/index.js +149 -0
  19. package/dist/es2019/rules/index.codegen.js +5 -1
  20. package/dist/es2019/rules/no-legacy-icons/checks.js +22 -6
  21. package/dist/es2019/rules/no-legacy-icons/helpers.js +125 -44
  22. package/dist/es2019/rules/no-utility-icons/checks.js +164 -0
  23. package/dist/es2019/rules/no-utility-icons/index.js +35 -0
  24. package/dist/es2019/rules/utils/get-deprecated-config.js +3 -2
  25. package/dist/esm/presets/all-flat.codegen.js +3 -1
  26. package/dist/esm/presets/all.codegen.js +3 -1
  27. package/dist/esm/presets/recommended-flat.codegen.js +2 -1
  28. package/dist/esm/presets/recommended.codegen.js +2 -1
  29. package/dist/esm/rules/ensure-proper-xcss-usage/index.js +151 -0
  30. package/dist/esm/rules/index.codegen.js +5 -1
  31. package/dist/esm/rules/no-legacy-icons/checks.js +22 -5
  32. package/dist/esm/rules/no-legacy-icons/helpers.js +121 -45
  33. package/dist/esm/rules/no-utility-icons/checks.js +226 -0
  34. package/dist/esm/rules/no-utility-icons/index.js +34 -0
  35. package/dist/esm/rules/utils/get-deprecated-config.js +2 -2
  36. package/dist/types/index.codegen.d.ts +14 -0
  37. package/dist/types/presets/all-flat.codegen.d.ts +2 -0
  38. package/dist/types/presets/all.codegen.d.ts +2 -0
  39. package/dist/types/presets/recommended-flat.codegen.d.ts +1 -0
  40. package/dist/types/presets/recommended.codegen.d.ts +1 -0
  41. package/dist/types/rules/ensure-proper-xcss-usage/index.d.ts +3 -0
  42. package/dist/types/rules/index.codegen.d.ts +2 -0
  43. package/dist/types/rules/no-legacy-icons/helpers.d.ts +12 -4
  44. package/dist/types/rules/no-utility-icons/checks.d.ts +10 -0
  45. package/dist/types/rules/no-utility-icons/index.d.ts +3 -0
  46. package/dist/types/rules/use-tokens-typography/transformers/banned-properties.d.ts +1 -1
  47. package/dist/types/rules/use-tokens-typography/transformers/font-family.d.ts +1 -1
  48. package/dist/types/rules/use-tokens-typography/transformers/font-weight.d.ts +1 -1
  49. package/dist/types/rules/use-tokens-typography/transformers/restricted-capitalisation.d.ts +1 -1
  50. package/dist/types/rules/use-tokens-typography/transformers/untokenized-properties.d.ts +1 -1
  51. package/dist/types-ts4.5/index.codegen.d.ts +14 -0
  52. package/dist/types-ts4.5/presets/all-flat.codegen.d.ts +2 -0
  53. package/dist/types-ts4.5/presets/all.codegen.d.ts +2 -0
  54. package/dist/types-ts4.5/presets/recommended-flat.codegen.d.ts +1 -0
  55. package/dist/types-ts4.5/presets/recommended.codegen.d.ts +1 -0
  56. package/dist/types-ts4.5/rules/ensure-proper-xcss-usage/index.d.ts +3 -0
  57. package/dist/types-ts4.5/rules/index.codegen.d.ts +2 -0
  58. package/dist/types-ts4.5/rules/no-legacy-icons/helpers.d.ts +12 -4
  59. package/dist/types-ts4.5/rules/no-utility-icons/checks.d.ts +10 -0
  60. package/dist/types-ts4.5/rules/no-utility-icons/index.d.ts +3 -0
  61. package/dist/types-ts4.5/rules/use-tokens-typography/transformers/banned-properties.d.ts +1 -1
  62. package/dist/types-ts4.5/rules/use-tokens-typography/transformers/font-family.d.ts +1 -1
  63. package/dist/types-ts4.5/rules/use-tokens-typography/transformers/font-weight.d.ts +1 -1
  64. package/dist/types-ts4.5/rules/use-tokens-typography/transformers/restricted-capitalisation.d.ts +1 -1
  65. package/dist/types-ts4.5/rules/use-tokens-typography/transformers/untokenized-properties.d.ts +1 -1
  66. package/package.json +4 -4
@@ -56,7 +56,7 @@ var getIconKey = function getIconKey(iconPackage) {
56
56
  * Checks if a new icon can be auto-migrated based on guidance from the migration map
57
57
  */
58
58
  export var canAutoMigrateNewIconBasedOnSize = function canAutoMigrateNewIconBasedOnSize(guidance) {
59
- return ['swap', 'swap-slight-visual-change', 'swap-visual-change', 'swap-size-shift-utility'].includes(guidance || '');
59
+ return guidance ? ['swap', 'swap-slight-visual-change', 'swap-visual-change'].includes(guidance) : false;
60
60
  };
61
61
 
62
62
  /**
@@ -84,8 +84,10 @@ var getNewIconNameAndImportPath = function getNewIconNameAndImportPath(iconPacka
84
84
  export var createGuidance = function createGuidance(_ref) {
85
85
  var iconPackage = _ref.iconPackage,
86
86
  insideNewButton = _ref.insideNewButton,
87
- size = _ref.size,
88
- shouldUseMigrationPath = _ref.shouldUseMigrationPath;
87
+ initialSize = _ref.size,
88
+ shouldUseMigrationPath = _ref.shouldUseMigrationPath,
89
+ shouldForceSmallIcon = _ref.shouldForceSmallIcon;
90
+ var size = shouldForceSmallIcon ? 'small' : initialSize;
89
91
  var migrationMapObject = getMigrationMapObject(iconPackage);
90
92
  var upcomingIcon = getUpcomingIcons(iconPackage);
91
93
  if (upcomingIcon) {
@@ -145,8 +147,18 @@ export var createGuidance = function createGuidance(_ref) {
145
147
  }
146
148
  if (insideNewButton) {
147
149
  _guidance += buttonGuidanceStr;
148
- } else if (size && size === 'medium') {
149
- _guidance += "Setting the spacing property to 'spacious' will maintain the icon's box dimensions - but consider setting spacing='none' as it allows for easier control of spacing by parent elements.\n";
150
+ } else if (size === 'medium') {
151
+ _guidance += "Setting the spacing='spacious' will maintain the icon's box dimensions - but consider setting spacing='none' as it allows for easier control of spacing by parent elements.\n";
152
+ } else if (size === 'small') {
153
+ if (initialSize !== 'small' && shouldForceSmallIcon) {
154
+ _guidance += "For this icon, it's recommended to use a smaller size using size='small'. Alternatively, for special cases where a larger version is needed size='medium' can be used, but it is generally discouraged for this icon.\n";
155
+ } else if (initialSize === 'small') {
156
+ if (shouldForceSmallIcon) {
157
+ _guidance += "Setting spacing='compact' will maintain the icon's box dimensions - but consider setting spacing='none' as it allows for easier control of spacing by parent elements.\n";
158
+ } else {
159
+ _guidance += "It's recommended to upscale to a medium icon with no spacing. Alternatively for special cases where smaller icons are required, the original icon size and dimensions can be maintained by using size='small' and spacing='compact'.\n";
160
+ }
161
+ }
150
162
  } else if (size) {
151
163
  _guidance += "In the new icon, please use spacing='none'.\n";
152
164
  }
@@ -270,7 +282,8 @@ export var createAutoMigrationError = function createAutoMigrationError(_ref4) {
270
282
  iconName = _ref4.iconName,
271
283
  errors = _ref4.errors,
272
284
  spacing = _ref4.spacing,
273
- insideNewButton = _ref4.insideNewButton;
285
+ insideNewButton = _ref4.insideNewButton,
286
+ shouldForceSmallIcon = _ref4.shouldForceSmallIcon;
274
287
  var myError = {
275
288
  node: node,
276
289
  messageId: 'noLegacyIconsAutoMigration',
@@ -279,7 +292,8 @@ export var createAutoMigrationError = function createAutoMigrationError(_ref4) {
279
292
  iconName: iconName,
280
293
  spacing: spacing !== null && spacing !== void 0 ? spacing : '',
281
294
  // value type need to be a string in Rule.ReportDescriptor
282
- insideNewButton: String(insideNewButton)
295
+ insideNewButton: String(insideNewButton),
296
+ shouldForceSmallIcon: String(shouldForceSmallIcon)
283
297
  }
284
298
  };
285
299
  errors[locToString(node)] = myError;
@@ -434,7 +448,7 @@ var getNewIconNameForRenaming = function getNewIconNameForRenaming(isInManualArr
434
448
  var newIconName;
435
449
  if (isInManualArray) {
436
450
  newIconName = getNewIconNameAndImportPath(importSource).iconName;
437
- var keyToName = newIconName ? newIconName[0].toUpperCase() + newIconName.slice(1) + 'Icon' : undefined;
451
+ var keyToName = newIconName ? getComponentName(newIconName) : undefined;
438
452
  newIconName = keyToName;
439
453
  if (newIconName === undefined || importSpecifier === keyToName) {
440
454
  newIconName = "".concat(keyToName, "New");
@@ -442,6 +456,11 @@ var getNewIconNameForRenaming = function getNewIconNameForRenaming(isInManualArr
442
456
  }
443
457
  return newIconName;
444
458
  };
459
+ export var getComponentName = function getComponentName(name) {
460
+ return name.split(/\W/).map(function (part) {
461
+ return "".concat(part[0].toUpperCase()).concat(part.slice(1));
462
+ }).join('').concat('Icon');
463
+ };
445
464
 
446
465
  /**
447
466
  *
@@ -507,7 +526,8 @@ var createPropFixes = function createPropFixes(_ref7) {
507
526
  migrationImportNode = _ref7.migrationImportNode,
508
527
  newIconName = _ref7.newIconName;
509
528
  var fixes = [];
510
- var spacing = metadata.spacing;
529
+ var spacing = metadata.spacing,
530
+ size = metadata.size;
511
531
  if (shouldUseMigrationPath && !legacyImportNode) {
512
532
  return fixes;
513
533
  }
@@ -523,24 +543,31 @@ var createPropFixes = function createPropFixes(_ref7) {
523
543
  if (primaryColor && primaryColor.type === 'JSXAttribute') {
524
544
  fixes.push(fixer.replaceText(primaryColor.name, 'color'));
525
545
  }
526
-
527
- // rename or remove size prop based on shouldUseMigrationPath,
528
- // add spacing="spacious" if
529
- // 1. it's in error metadata, which means size is medium
530
- // 2. no existing spacing prop
531
- // 3. iconType is "core"
532
- // 4. icon is not imported from migration entrypoint
533
546
  var sizeProp = findProp(attributes, 'size');
534
547
  var spacingProp = findProp(attributes, 'spacing');
535
- if (spacing && !spacingProp && !migrationImportNode) {
536
- fixes.push(fixer.insertTextAfter(sizeProp || openingElement.name, " spacing=\"".concat(spacing, "\"")));
537
- }
538
548
  if (sizeProp && sizeProp.type === 'JSXAttribute') {
539
- fixes.push(shouldUseMigrationPath ?
540
- // replace size prop with LEGACY_size,
541
- fixer.replaceText(sizeProp.name, 'LEGACY_size') :
542
- // remove size prop if shouldUseMigrationPath is false
543
- fixer.remove(sizeProp));
549
+ if (shouldUseMigrationPath) {
550
+ // Rename existing size prop to LEGACY_size and add new size prop if applicable
551
+ fixes.push(fixer.replaceText(sizeProp.name, 'LEGACY_size'));
552
+ if (size) {
553
+ fixes.push(fixer.insertTextAfter(sizeProp, " size=\"".concat(size, "\"")));
554
+ }
555
+ } else {
556
+ if (size && sizeProp.value) {
557
+ // update size prop with new replacement size
558
+ fixes.push(fixer.replaceText(sizeProp.value, "\"".concat(size, "\"")));
559
+ } else {
560
+ // remove size prop if no new replacement size is specified
561
+ fixes.push(fixer.remove(sizeProp));
562
+ }
563
+ }
564
+ } else if (size) {
565
+ fixes.push(fixer.insertTextAfter(openingElement.name, " size=\"".concat(size, "\"")));
566
+ }
567
+
568
+ // Add spacing prop if no existing spacing prop and icon is not imported from migration entrypoint
569
+ if (spacing && spacing !== 'none' && !spacingProp && !migrationImportNode) {
570
+ fixes.push(fixer.insertTextAfter(sizeProp || openingElement.name, " spacing=\"".concat(spacing, "\"")));
544
571
  }
545
572
 
546
573
  // rename or remove secondaryColor prop based on shouldUseMigrationPath
@@ -637,6 +664,7 @@ export var throwManualErrors = function throwManualErrors(_ref9) {
637
664
  export var throwAutoErrors = function throwAutoErrors(_ref10) {
638
665
  var errorsManual = _ref10.errorsManual,
639
666
  errorsAuto = _ref10.errorsAuto,
667
+ iconSizesInfo = _ref10.iconSizesInfo,
640
668
  legacyIconImports = _ref10.legacyIconImports,
641
669
  guidance = _ref10.guidance,
642
670
  migrationIconImports = _ref10.migrationIconImports,
@@ -692,7 +720,7 @@ export var throwAutoErrors = function throwAutoErrors(_ref10) {
692
720
  _step2;
693
721
  try {
694
722
  var _loop2 = function _loop2() {
695
- var _legacyIconImports$er, _legacyIconImports$er2, _migrationIconImports;
723
+ var _iconSizesInfo$import, _iconSizesInfo$import2, _iconSizesInfo$import3, _legacyIconImports$er, _error$data2, _legacyIconImports$er2, _migrationIconImports;
696
724
  var _step2$value = _slicedToArray(_step2.value, 2),
697
725
  _ = _step2$value[0],
698
726
  error = _step2$value[1];
@@ -702,8 +730,20 @@ export var throwAutoErrors = function throwAutoErrors(_ref10) {
702
730
  // If that is the case we'll need to provide a suggestion instead of auto-fixing as the suggestion will
703
731
  // add another import without removing the old import and this needs to be validated
704
732
  var isInManualArray = allManualErrorSources.has(importSource);
733
+
734
+ // Check if the icon has size of small, if so it cannot be automatically migrated. Two suggestions will be provided
735
+ // 1. Use core icon with no spacing
736
+ // 2. Use utility icon with compact spacing
737
+ var isSizeSmall = (_iconSizesInfo$import = iconSizesInfo[importSource]) === null || _iconSizesInfo$import === void 0 ? void 0 : _iconSizesInfo$import.small.includes(key);
738
+ var isMixedSizeUsage = ((_iconSizesInfo$import2 = iconSizesInfo[importSource]) === null || _iconSizesInfo$import2 === void 0 ? void 0 : _iconSizesInfo$import2.small.length) > 0 && ((_iconSizesInfo$import3 = iconSizesInfo[importSource]) === null || _iconSizesInfo$import3 === void 0 ? void 0 : _iconSizesInfo$import3.small.length) < iconSizesInfo[importSource].usageCount;
739
+
740
+ // Icon should be renamed
741
+ // 1. If the icon is in the manual array OR
742
+ // 2. If there is mixed size usages of this icon with size small
743
+ var shouldRenameIcon = isInManualArray || isMixedSizeUsage;
744
+
705
745
  // New icon name for renaming if the icon is in the manual array
706
- var newIconName = getNewIconNameForRenaming(isInManualArray, importSource, errorList[0].data ? (_legacyIconImports$er = legacyIconImports[errorList[0].data.iconName]) === null || _legacyIconImports$er === void 0 ? void 0 : _legacyIconImports$er.importSpecifier : undefined);
746
+ var newIconName = getNewIconNameForRenaming(shouldRenameIcon, importSource, errorList[0].data ? (_legacyIconImports$er = legacyIconImports[errorList[0].data.iconName]) === null || _legacyIconImports$er === void 0 ? void 0 : _legacyIconImports$er.importSpecifier : undefined);
707
747
  if (!node) {
708
748
  return 0; // continue
709
749
  }
@@ -711,22 +751,43 @@ export var throwAutoErrors = function throwAutoErrors(_ref10) {
711
751
  if (Object.keys(error).includes('data') && error.data) {
712
752
  error.data.guidance = guidanceMessage;
713
753
  }
754
+ var shouldForceSmallIcon = ((_error$data2 = error.data) === null || _error$data2 === void 0 ? void 0 : _error$data2.shouldForceSmallIcon) === 'true';
714
755
  var fixArguments = error.data ? {
715
- metadata: error.data,
756
+ metadata: _objectSpread(_objectSpread({}, error.data), {}, {
757
+ spacing: error.data.isInNewButton ? 'none' : error.data.spacing,
758
+ size: shouldForceSmallIcon ? 'small' : error.data.size
759
+ }),
716
760
  legacyImportNode: (_legacyIconImports$er2 = legacyIconImports[error.data.iconName]) === null || _legacyIconImports$er2 === void 0 ? void 0 : _legacyIconImports$er2.importNode,
717
761
  migrationImportNode: (_migrationIconImports = migrationIconImports[error.data.iconName]) === null || _migrationIconImports === void 0 ? void 0 : _migrationIconImports.importNode,
718
762
  shouldUseMigrationPath: shouldUseMigrationPath,
719
- newIconName: isInManualArray ? newIconName : undefined
763
+ newIconName: shouldRenameIcon ? newIconName : undefined
720
764
  } : null;
721
765
  if (!error.data || shouldUseMigrationPath && !checkIfNewIconExist(error) || !fixArguments) {
722
766
  return 0; // continue
723
767
  }
724
- if (isInManualArray) {
725
- // provide suggestion if there is a manual error for the same import source and thus the legacy import can't be removed
768
+ var isInNewButton = fixArguments.metadata.insideNewButton === 'true';
769
+ if (isSizeSmall && !shouldForceSmallIcon) {
726
770
  error.suggest = [{
727
- desc: 'Rename icon import, import from the new package, and update props.',
771
+ desc: isInNewButton ? 'Replace with medium core icon (Recommended)' : 'Replace with medium core icon and no spacing (Recommended)',
772
+ fix: function fix(fixer) {
773
+ return [].concat(_toConsumableArray(createPropFixes(_objectSpread(_objectSpread({}, fixArguments), {}, {
774
+ metadata: _objectSpread(_objectSpread({}, fixArguments.metadata), {}, {
775
+ spacing: 'none'
776
+ }),
777
+ node: node,
778
+ fixer: fixer
779
+ }))), _toConsumableArray(createImportFix(_objectSpread(_objectSpread({}, fixArguments), {}, {
780
+ fixer: fixer
781
+ }))));
782
+ }
783
+ }, {
784
+ desc: isInNewButton ? 'Replace with small core icon' : 'Replace with small core icon and compact spacing',
728
785
  fix: function fix(fixer) {
729
786
  return [].concat(_toConsumableArray(createPropFixes(_objectSpread(_objectSpread({}, fixArguments), {}, {
787
+ metadata: _objectSpread(_objectSpread({}, fixArguments.metadata), {}, {
788
+ spacing: 'compact',
789
+ size: 'small'
790
+ }),
730
791
  node: node,
731
792
  fixer: fixer
732
793
  }))), _toConsumableArray(createImportFix(_objectSpread(_objectSpread({}, fixArguments), {}, {
@@ -735,25 +796,40 @@ export var throwAutoErrors = function throwAutoErrors(_ref10) {
735
796
  }
736
797
  }];
737
798
  } else {
738
- // Update Guidance message for auto-fixing
739
- if (error.data) {
740
- error.data.guidance = error.data.guidance + "\nTo automatically fix this icon, run the auto-fixer attached to the first use of ".concat(importSource, " in this file - either manually, or by saving this file.");
741
- }
742
- // There should only be 1 import fix for each import source and thus only add this at the start of the list
743
- if (autoFixers.length === 0) {
799
+ if (isInManualArray) {
800
+ // provide suggestion if there is a manual error for the same import source and thus the legacy import can't be removed
801
+ error.suggest = [{
802
+ desc: 'Rename icon import, import from the new package, and update props.',
803
+ fix: function fix(fixer) {
804
+ return [].concat(_toConsumableArray(createPropFixes(_objectSpread(_objectSpread({}, fixArguments), {}, {
805
+ node: node,
806
+ fixer: fixer
807
+ }))), _toConsumableArray(createImportFix(_objectSpread(_objectSpread({}, fixArguments), {}, {
808
+ fixer: fixer
809
+ }))));
810
+ }
811
+ }];
812
+ } else {
813
+ // Update Guidance message for auto-fixing
814
+ if (error.data) {
815
+ error.data.guidance = error.data.guidance + "\nTo automatically fix this icon, run the auto-fixer attached to the first use of ".concat(importSource, " in this file - either manually, or by saving this file.");
816
+ }
817
+ // There should only be 1 import fix for each import source and thus only add this at the start of the list
818
+ if (autoFixers.length === 0) {
819
+ autoFixers.push(function (fixer) {
820
+ return createImportFix(_objectSpread(_objectSpread({}, fixArguments), {}, {
821
+ fixer: fixer
822
+ }));
823
+ });
824
+ }
825
+ // Push the prop fix regardless
744
826
  autoFixers.push(function (fixer) {
745
- return createImportFix(_objectSpread(_objectSpread({}, fixArguments), {}, {
827
+ return createPropFixes(_objectSpread(_objectSpread({}, fixArguments), {}, {
828
+ node: node,
746
829
  fixer: fixer
747
830
  }));
748
831
  });
749
832
  }
750
- // Push the prop fix regardless
751
- autoFixers.push(function (fixer) {
752
- return createPropFixes(_objectSpread(_objectSpread({}, fixArguments), {}, {
753
- node: node,
754
- fixer: fixer
755
- }));
756
- });
757
833
  }
758
834
  // Add the error to the appliedErrorsForImport, ready to be thrown later
759
835
  appliedErrorsForImport.push(error);
@@ -0,0 +1,226 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ 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; } } }; }
3
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
4
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
5
+ import { isNodeOfType } from 'eslint-codemod-utils';
6
+ var specialCases = {
7
+ '@atlaskit/icon/utility/cross': '@atlaskit/icon/core/close',
8
+ '@atlaskit/icon/utility/migration/cross--editor-close': '@atlaskit/icon/core/migration/close--editor-close'
9
+ };
10
+ var iconPropsinNewButton = ['icon', 'iconBefore', 'iconAfter'];
11
+ export var createChecks = function createChecks(context) {
12
+ var importStatementsUtility = {};
13
+ var importStatementsCore = {};
14
+ var newButtonImports = new Set();
15
+ var errors = {};
16
+ var checkImportDeclarations = function checkImportDeclarations(node) {
17
+ var moduleSource = node.source.value;
18
+ if (typeof moduleSource !== 'string') {
19
+ return;
20
+ }
21
+ if (moduleSource.startsWith('@atlaskit/icon/utility/')) {
22
+ var _iterator = _createForOfIteratorHelper(node.specifiers),
23
+ _step;
24
+ try {
25
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
26
+ var specifier = _step.value;
27
+ if (specifier.type === 'ImportDefaultSpecifier') {
28
+ importStatementsUtility[specifier.local.name] = node;
29
+ }
30
+ }
31
+ } catch (err) {
32
+ _iterator.e(err);
33
+ } finally {
34
+ _iterator.f();
35
+ }
36
+ } else if (moduleSource.startsWith('@atlaskit/icon/core/')) {
37
+ var _iterator2 = _createForOfIteratorHelper(node.specifiers),
38
+ _step2;
39
+ try {
40
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
41
+ var _specifier = _step2.value;
42
+ if (_specifier.type === 'ImportDefaultSpecifier') {
43
+ importStatementsCore[moduleSource] = {
44
+ node: node,
45
+ localName: _specifier.local.name
46
+ };
47
+ }
48
+ }
49
+ } catch (err) {
50
+ _iterator2.e(err);
51
+ } finally {
52
+ _iterator2.f();
53
+ }
54
+ } else if (moduleSource.startsWith('@atlaskit/button/')) {
55
+ var _iterator3 = _createForOfIteratorHelper(node.specifiers),
56
+ _step3;
57
+ try {
58
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
59
+ var _specifier2 = _step3.value;
60
+ newButtonImports.add(_specifier2.local.name);
61
+ }
62
+ } catch (err) {
63
+ _iterator3.e(err);
64
+ } finally {
65
+ _iterator3.f();
66
+ }
67
+ }
68
+ };
69
+ var checkJSXElement = function checkJSXElement(node) {
70
+ if (!(isNodeOfType(node, 'JSXElement') && isNodeOfType(node.openingElement.name, 'JSXIdentifier') && importStatementsUtility.hasOwnProperty(node.openingElement.name.name) && typeof importStatementsUtility[node.openingElement.name.name].source.value === 'string')) {
71
+ return;
72
+ }
73
+ if (!errors.hasOwnProperty(node.openingElement.name.name)) {
74
+ errors[node.openingElement.name.name] = [];
75
+ }
76
+ errors[node.openingElement.name.name].push({
77
+ node: node,
78
+ messageId: 'noUtilityIconsJSXElement',
79
+ localName: node.openingElement.name.name,
80
+ fixable: true,
81
+ inNewButton: false
82
+ });
83
+ };
84
+
85
+ // Cases: As Props, In Function Calls, In Arrays, In Maps, In Exports
86
+ var checkIconReference = function checkIconReference(node) {
87
+ //if this is an import statement then exit early
88
+ if (node.parent && (isNodeOfType(node.parent, 'ImportSpecifier') || isNodeOfType(node.parent, 'ImportDefaultSpecifier'))) {
89
+ return;
90
+ }
91
+
92
+ //check the reference to see if it's a utility icon, if not exit early
93
+ if (!importStatementsUtility.hasOwnProperty(node.name)) {
94
+ return;
95
+ }
96
+
97
+ // if it is in new Button then we can fix it
98
+ if (node.parent && node.parent.parent && node.parent.parent.parent && isNodeOfType(node.parent, 'JSXExpressionContainer') && isNodeOfType(node.parent.parent, 'JSXAttribute') && isNodeOfType(node.parent.parent.name, 'JSXIdentifier') && iconPropsinNewButton.includes(node.parent.parent.name.name) && isNodeOfType(node.parent.parent.parent, 'JSXOpeningElement') && isNodeOfType(node.parent.parent.parent.name, 'JSXIdentifier') && newButtonImports.has(node.parent.parent.parent.name.name)) {
99
+ // if it is in new Button then we can fix it
100
+ if (!errors.hasOwnProperty(node.name)) {
101
+ errors[node.name] = [];
102
+ }
103
+ errors[node.name].push({
104
+ node: node,
105
+ messageId: 'noUtilityIconsReference',
106
+ localName: node.name,
107
+ fixable: true,
108
+ inNewButton: true
109
+ });
110
+ return;
111
+ }
112
+
113
+ // manually error
114
+ if (!errors.hasOwnProperty(node.name)) {
115
+ errors[node.name] = [];
116
+ }
117
+ errors[node.name].push({
118
+ node: node,
119
+ messageId: 'noUtilityIconsReference',
120
+ localName: node.name,
121
+ fixable: false,
122
+ inNewButton: false
123
+ });
124
+ };
125
+
126
+ /**
127
+ * Throws the relevant errors in the correct order.
128
+ */
129
+ var throwErrors = function throwErrors() {
130
+ var _loop = function _loop() {
131
+ var utilityIcon = _Object$keys[_i];
132
+ var allFixable = errors[utilityIcon].every(function (x) {
133
+ return x.fixable;
134
+ }); // Check if ALL errors for a giving import are fixable
135
+ var originalImportNode = importStatementsUtility[utilityIcon];
136
+ var oldImportName = importStatementsUtility[utilityIcon].source.value;
137
+ var newImportName = specialCases.hasOwnProperty(oldImportName) ? specialCases[oldImportName] : oldImportName.replace('@atlaskit/icon/utility/', '@atlaskit/icon/core/');
138
+ var existingImport = importStatementsCore.hasOwnProperty(newImportName) ? importStatementsCore[newImportName].localName : null;
139
+ var replacementImportName = existingImport ? existingImport : allFixable ? utilityIcon : "".concat(utilityIcon, "Core");
140
+ var importFixAdded = false;
141
+ var _iterator4 = _createForOfIteratorHelper(errors[utilityIcon].entries()),
142
+ _step4;
143
+ try {
144
+ var _loop2 = function _loop2() {
145
+ var _step4$value = _slicedToArray(_step4.value, 2),
146
+ index = _step4$value[0],
147
+ error = _step4$value[1];
148
+ if (error.fixable) {
149
+ context.report({
150
+ node: error.node,
151
+ messageId: error.messageId,
152
+ fix: function fix(fixer) {
153
+ var fixes = [];
154
+
155
+ // Add a new import statement if it doesn't already exist
156
+ if (!existingImport && !importFixAdded) {
157
+ importFixAdded = true;
158
+ fixes.push(fixer.insertTextBeforeRange([0, 0], "import ".concat(replacementImportName, " from '").concat(newImportName, "';\n")));
159
+ }
160
+
161
+ // Handle JSX elements differently if they are in a "new Button"
162
+ if (error.inNewButton) {
163
+ // Replace the icon with a function that wraps it with iconProps and size="small"
164
+ var wrappedIcon = "(iconProps) => <".concat(replacementImportName, " label=\"\" {...iconProps} size=\"small\" />");
165
+ fixes.push(fixer.replaceText(error.node, wrappedIcon));
166
+ } else if (isNodeOfType(error.node, 'JSXElement') && isNodeOfType(error.node.openingElement.name, 'JSXIdentifier')) {
167
+ // Replace the JSX element's closing tag with size="small"
168
+ var newOpeningElementText = context.sourceCode.getText(error.node.openingElement).replace(/\s*\/\s*>$/, " size=\"small\"/>").replace(new RegExp('<\s*' + error.node.openingElement.name.name), "<".concat(replacementImportName));
169
+ fixes.push(fixer.replaceText(error.node.openingElement, newOpeningElementText));
170
+ }
171
+
172
+ // Handle the first fixable error for import removal if all fixable for this import
173
+ if (index === 0 && allFixable) {
174
+ fixes.push(fixer.remove(originalImportNode));
175
+ }
176
+ return fixes;
177
+ }
178
+ });
179
+ } else {
180
+ // Report non-fixable errors
181
+ context.report({
182
+ node: error.node,
183
+ messageId: error.messageId
184
+ });
185
+ }
186
+ };
187
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
188
+ _loop2();
189
+ }
190
+ } catch (err) {
191
+ _iterator4.e(err);
192
+ } finally {
193
+ _iterator4.f();
194
+ }
195
+ };
196
+ for (var _i = 0, _Object$keys = Object.keys(errors); _i < _Object$keys.length; _i++) {
197
+ _loop();
198
+ }
199
+
200
+ // If other utility icons are imported but there were no errors for them - (this should only be unused imports but good to have as a backup), report them
201
+ var otherUtilityImportsWithoutErrors = Object.keys(importStatementsUtility).filter(function (utilityIcon) {
202
+ return !errors.hasOwnProperty(utilityIcon);
203
+ });
204
+ var _iterator5 = _createForOfIteratorHelper(otherUtilityImportsWithoutErrors),
205
+ _step5;
206
+ try {
207
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
208
+ var utilityIcon = _step5.value;
209
+ context.report({
210
+ node: importStatementsUtility[utilityIcon],
211
+ messageId: 'noUtilityIconsImport'
212
+ });
213
+ }
214
+ } catch (err) {
215
+ _iterator5.e(err);
216
+ } finally {
217
+ _iterator5.f();
218
+ }
219
+ };
220
+ return {
221
+ checkImportDeclarations: checkImportDeclarations,
222
+ checkJSXElement: checkJSXElement,
223
+ checkIconReference: checkIconReference,
224
+ throwErrors: throwErrors
225
+ };
226
+ };
@@ -0,0 +1,34 @@
1
+ import { createLintRule } from '../utils/create-rule';
2
+ import { createChecks } from './checks';
3
+ var rule = createLintRule({
4
+ meta: {
5
+ name: 'no-utility-icons',
6
+ fixable: 'code',
7
+ hasSuggestions: true,
8
+ type: 'problem',
9
+ docs: {
10
+ description: 'Disallow use of deprecated utility icons, in favor of core icons with `size="small"`.',
11
+ recommended: true,
12
+ severity: 'warn'
13
+ },
14
+ messages: {
15
+ noUtilityIconsJSXElement: "Utility icons are deprecated. Please use core icons instead with the size prop set to small.",
16
+ noUtilityIconsImport: "Utility icons are deprecated. Please do not import them, use core icons instead.",
17
+ noUtilityIconsReference: "Utility icons are deprecated. To replace them, please use core icons with the size prop set to small instead."
18
+ }
19
+ },
20
+ create: function create(context) {
21
+ var _createChecks = createChecks(context),
22
+ checkImportDeclarations = _createChecks.checkImportDeclarations,
23
+ checkJSXElement = _createChecks.checkJSXElement,
24
+ checkIconReference = _createChecks.checkIconReference,
25
+ throwErrors = _createChecks.throwErrors;
26
+ return {
27
+ ImportDeclaration: checkImportDeclarations,
28
+ JSXElement: checkJSXElement,
29
+ Identifier: checkIconReference,
30
+ 'Program:exit': throwErrors
31
+ };
32
+ }
33
+ });
34
+ export default rule;
@@ -3,14 +3,14 @@ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbol
3
3
  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) { _defineProperty(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; }
4
4
  import fs from 'fs';
5
5
  import path from 'path';
6
- import { deprecatedCore as deprecatedIconLabCore } from '@atlaskit/icon-lab/deprecated-map';
6
+ import { deprecatedCore as deprecatedIconLabCore, deprecatedUtility as deprecatedIconLabUtility } from '@atlaskit/icon-lab/deprecated-map';
7
7
  import { deprecatedCore as deprecatedIconCore, deprecatedUtility as deprecatedIconUtility } from '@atlaskit/icon/deprecated-map';
8
8
  export var getConfig = function getConfig(specifier) {
9
9
  var configPath = path.resolve(__dirname, '..', '..', '..', 'configs', 'deprecated.json');
10
10
  var source = fs.readFileSync(configPath, 'utf8');
11
11
  var parsedConfig = JSON.parse(source);
12
12
  var combinedConfig = _objectSpread(_objectSpread({}, parsedConfig), {}, {
13
- imports: _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, parsedConfig.imports), deprecatedIconCore), deprecatedIconUtility), deprecatedIconLabCore)
13
+ imports: _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, parsedConfig.imports), deprecatedIconCore), deprecatedIconUtility), deprecatedIconLabCore), deprecatedIconLabUtility)
14
14
  });
15
15
  return combinedConfig[specifier];
16
16
  };