@atlaskit/codemod-cli 0.14.0 → 0.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 (95) hide show
  1. package/CHANGELOG.md +72 -53
  2. package/dist/cjs/main.js +3 -3
  3. package/dist/cjs/presets/css-to-design-tokens/css-to-design-tokens.js +52 -133
  4. package/dist/cjs/presets/css-to-design-tokens/lib/colors.js +49 -0
  5. package/dist/cjs/presets/css-to-design-tokens/lib/declaration.js +29 -0
  6. package/dist/cjs/presets/css-to-design-tokens/{utils → lib}/legacy-colors.js +13 -17
  7. package/dist/cjs/presets/css-to-design-tokens/lib/logger.js +18 -0
  8. package/dist/cjs/presets/css-to-design-tokens/lib/meta.js +157 -0
  9. package/dist/cjs/presets/css-to-design-tokens/lib/tokens.js +47 -0
  10. package/dist/cjs/presets/css-to-design-tokens/lib/value.js +80 -0
  11. package/dist/cjs/presets/index.js +3 -3
  12. package/dist/cjs/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js +118 -0
  13. package/dist/cjs/presets/migrate-to-new-buttons/codemods/next-split-imports.js +58 -0
  14. package/dist/cjs/presets/migrate-to-new-buttons/migrate-to-new-buttons.js +45 -0
  15. package/dist/cjs/presets/migrate-to-new-buttons/utils/constants.js +35 -0
  16. package/dist/cjs/presets/styled-to-emotion/styled-to-emotion.js +1 -2
  17. package/dist/cjs/presets/theme-remove-deprecated-mixins/utils/replacements.js +2 -3
  18. package/dist/cjs/presets/theme-to-design-tokens/theme-to-design-tokens.js +2 -2
  19. package/dist/cjs/presets/theme-to-design-tokens/utils/color.js +4 -8
  20. package/dist/cjs/presets/theme-to-design-tokens/utils/css-utils.js +2 -2
  21. package/dist/cjs/presets/theme-to-design-tokens/utils/fuzzy-search.js +1 -2
  22. package/dist/cjs/presets/theme-to-design-tokens/utils/legacy-colors.js +4 -7
  23. package/dist/cjs/presets/theme-to-design-tokens/utils/named-colors.js +1 -2
  24. package/dist/cjs/presets/theme-to-design-tokens/utils/string-utils.js +1 -2
  25. package/dist/cjs/presets/theme-to-design-tokens/utils/tokens.js +3 -5
  26. package/dist/cjs/sinceRef.js +1 -2
  27. package/dist/cjs/transforms.js +6 -12
  28. package/dist/cjs/types.js +3 -5
  29. package/dist/cjs/utils.js +2 -3
  30. package/dist/es2019/presets/css-to-design-tokens/css-to-design-tokens.js +52 -116
  31. package/dist/es2019/presets/css-to-design-tokens/lib/colors.js +34 -0
  32. package/dist/es2019/presets/css-to-design-tokens/lib/declaration.js +17 -0
  33. package/dist/es2019/presets/css-to-design-tokens/{utils → lib}/legacy-colors.js +9 -10
  34. package/dist/es2019/presets/css-to-design-tokens/lib/logger.js +11 -0
  35. package/dist/es2019/presets/css-to-design-tokens/lib/meta.js +133 -0
  36. package/dist/es2019/presets/css-to-design-tokens/lib/tokens.js +24 -0
  37. package/dist/es2019/presets/css-to-design-tokens/lib/value.js +68 -0
  38. package/dist/es2019/presets/index.js +2 -1
  39. package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js +86 -0
  40. package/dist/es2019/presets/migrate-to-new-buttons/codemods/next-split-imports.js +44 -0
  41. package/dist/es2019/presets/migrate-to-new-buttons/migrate-to-new-buttons.js +19 -0
  42. package/dist/es2019/presets/migrate-to-new-buttons/utils/constants.js +29 -0
  43. package/dist/es2019/presets/theme-to-design-tokens/utils/css-utils.js +2 -2
  44. package/dist/esm/main.js +3 -3
  45. package/dist/esm/presets/css-to-design-tokens/css-to-design-tokens.js +52 -132
  46. package/dist/esm/presets/css-to-design-tokens/lib/colors.js +38 -0
  47. package/dist/esm/presets/css-to-design-tokens/lib/declaration.js +19 -0
  48. package/dist/esm/presets/css-to-design-tokens/{utils → lib}/legacy-colors.js +9 -10
  49. package/dist/esm/presets/css-to-design-tokens/lib/logger.js +11 -0
  50. package/dist/esm/presets/css-to-design-tokens/lib/meta.js +146 -0
  51. package/dist/esm/presets/css-to-design-tokens/lib/tokens.js +40 -0
  52. package/dist/esm/presets/css-to-design-tokens/lib/value.js +73 -0
  53. package/dist/esm/presets/index.js +2 -1
  54. package/dist/esm/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.js +112 -0
  55. package/dist/esm/presets/migrate-to-new-buttons/codemods/next-split-imports.js +52 -0
  56. package/dist/esm/presets/migrate-to-new-buttons/migrate-to-new-buttons.js +38 -0
  57. package/dist/esm/presets/migrate-to-new-buttons/utils/constants.js +29 -0
  58. package/dist/esm/presets/theme-to-design-tokens/theme-to-design-tokens.js +2 -2
  59. package/dist/esm/presets/theme-to-design-tokens/utils/css-utils.js +2 -2
  60. package/dist/types/main.d.ts +0 -1
  61. package/dist/types/presets/css-to-design-tokens/css-to-design-tokens.d.ts +0 -1
  62. package/dist/types/presets/css-to-design-tokens/lib/colors.d.ts +6 -0
  63. package/dist/types/presets/css-to-design-tokens/lib/declaration.d.ts +5 -0
  64. package/dist/types/presets/css-to-design-tokens/{utils → lib}/legacy-colors.d.ts +1 -1
  65. package/dist/types/presets/css-to-design-tokens/lib/logger.d.ts +4 -0
  66. package/dist/types/presets/css-to-design-tokens/lib/meta.d.ts +6 -0
  67. package/dist/types/presets/css-to-design-tokens/lib/tokens.d.ts +7 -0
  68. package/dist/types/presets/css-to-design-tokens/lib/value.d.ts +6 -0
  69. package/dist/types/presets/index.d.ts +1 -0
  70. package/dist/types/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.d.ts +3 -0
  71. package/dist/types/presets/migrate-to-new-buttons/codemods/next-split-imports.d.ts +3 -0
  72. package/dist/types/presets/migrate-to-new-buttons/migrate-to-new-buttons.d.ts +2 -0
  73. package/dist/types/presets/migrate-to-new-buttons/utils/constants.d.ts +14 -0
  74. package/dist/types-ts4.5/main.d.ts +0 -1
  75. package/dist/types-ts4.5/presets/css-to-design-tokens/css-to-design-tokens.d.ts +0 -1
  76. package/dist/types-ts4.5/presets/css-to-design-tokens/lib/colors.d.ts +6 -0
  77. package/dist/types-ts4.5/presets/css-to-design-tokens/lib/declaration.d.ts +5 -0
  78. package/dist/types-ts4.5/presets/css-to-design-tokens/{utils → lib}/legacy-colors.d.ts +1 -1
  79. package/dist/types-ts4.5/presets/css-to-design-tokens/lib/logger.d.ts +4 -0
  80. package/dist/types-ts4.5/presets/css-to-design-tokens/lib/meta.d.ts +6 -0
  81. package/dist/types-ts4.5/presets/css-to-design-tokens/lib/tokens.d.ts +7 -0
  82. package/dist/types-ts4.5/presets/css-to-design-tokens/lib/value.d.ts +6 -0
  83. package/dist/types-ts4.5/presets/index.d.ts +1 -0
  84. package/dist/types-ts4.5/presets/migrate-to-new-buttons/codemods/next-migrate-to-new-button-variants.d.ts +3 -0
  85. package/dist/types-ts4.5/presets/migrate-to-new-buttons/codemods/next-split-imports.d.ts +3 -0
  86. package/dist/types-ts4.5/presets/migrate-to-new-buttons/migrate-to-new-buttons.d.ts +2 -0
  87. package/dist/types-ts4.5/presets/migrate-to-new-buttons/utils/constants.d.ts +14 -0
  88. package/package.json +3 -2
  89. package/report.api.md +0 -2
  90. package/tmp/api-report-tmp.d.ts +0 -2
  91. package/dist/cjs/presets/css-to-design-tokens/utils/meta.js +0 -53
  92. package/dist/es2019/presets/css-to-design-tokens/utils/meta.js +0 -33
  93. package/dist/esm/presets/css-to-design-tokens/utils/meta.js +0 -46
  94. package/dist/types/presets/css-to-design-tokens/utils/meta.d.ts +0 -1
  95. package/dist/types-ts4.5/presets/css-to-design-tokens/utils/meta.d.ts +0 -1
package/dist/cjs/types.js CHANGED
@@ -16,7 +16,7 @@ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Re
16
16
  /** Converts required args to optional if they have a default
17
17
  * Example: export type UserFlags = Default<Flags, keyof typeof defaultFlags>;
18
18
  */
19
- var ValidationError = /*#__PURE__*/function (_Error) {
19
+ var ValidationError = exports.ValidationError = /*#__PURE__*/function (_Error) {
20
20
  (0, _inherits2.default)(ValidationError, _Error);
21
21
  var _super = _createSuper(ValidationError);
22
22
  function ValidationError() {
@@ -25,8 +25,7 @@ var ValidationError = /*#__PURE__*/function (_Error) {
25
25
  }
26
26
  return (0, _createClass2.default)(ValidationError);
27
27
  }( /*#__PURE__*/(0, _wrapNativeSuper2.default)(Error));
28
- exports.ValidationError = ValidationError;
29
- var NoTransformsExistError = /*#__PURE__*/function (_Error2) {
28
+ var NoTransformsExistError = exports.NoTransformsExistError = /*#__PURE__*/function (_Error2) {
30
29
  (0, _inherits2.default)(NoTransformsExistError, _Error2);
31
30
  var _super2 = _createSuper(NoTransformsExistError);
32
31
  function NoTransformsExistError() {
@@ -34,5 +33,4 @@ var NoTransformsExistError = /*#__PURE__*/function (_Error2) {
34
33
  return _super2.apply(this, arguments);
35
34
  }
36
35
  return (0, _createClass2.default)(NoTransformsExistError);
37
- }( /*#__PURE__*/(0, _wrapNativeSuper2.default)(Error));
38
- exports.NoTransformsExistError = NoTransformsExistError;
36
+ }( /*#__PURE__*/(0, _wrapNativeSuper2.default)(Error));
package/dist/cjs/utils.js CHANGED
@@ -36,7 +36,7 @@ var getLineEndingRegex = function getLineEndingRegex(type) {
36
36
  return '\r\n';
37
37
  }
38
38
  };
39
- var fixLineEnding = function fixLineEnding(source, lineEnding) {
39
+ var fixLineEnding = exports.fixLineEnding = function fixLineEnding(source, lineEnding) {
40
40
  var current = returnLineEnding(source);
41
41
  if (current === lineEnding) {
42
42
  return source;
@@ -47,5 +47,4 @@ var fixLineEnding = function fixLineEnding(source, lineEnding) {
47
47
  return source.replace(new RegExp(regexCurrentLineEnding, 'g'), regexLineEnding);
48
48
  }
49
49
  return source;
50
- };
51
- exports.fixLineEnding = fixLineEnding;
50
+ };
@@ -1,71 +1,14 @@
1
1
  import postcss from 'postcss';
2
2
  // @ts-ignore
3
3
  import lessSyntax from 'postcss-less';
4
- import { light as rawTokens } from '@atlaskit/tokens/tokens-raw';
5
- import designTokens from '@atlaskit/tokens/token-names';
6
- import Search from '../theme-to-design-tokens/utils/fuzzy-search';
7
- import { knownVariables, knownColors, knownRawColors } from './utils/legacy-colors';
8
- import { cleanMeta } from './utils/meta';
9
- const options = {
4
+ import { isColorRelatedProperty, isCssDeclaration, splitCssValue } from './lib/declaration';
5
+ import { getBaseDeclarationMeta } from './lib/meta';
6
+ import findToken from './lib/tokens';
7
+ import parseValue from './lib/value';
8
+ const POSTCSS_OPTIONS = {
10
9
  syntax: lessSyntax,
11
10
  from: undefined
12
11
  };
13
- const tokens = rawTokens.filter(t => t.attributes.state === 'active').map(t => t.name.replace(/\.\[default\]/g, '')).filter(t => !t.includes('UNSAFE') && !t.includes('interaction'));
14
- const search = Search(tokens, false);
15
- function isRule(node) {
16
- return node.type === 'rule';
17
- }
18
- function getParentSelectors(node) {
19
- if (isRule(node)) {
20
- // @ts-expect-error
21
- return getParentSelectors(node.parent) + ' ' + node.selector;
22
- }
23
- if (node.parent) {
24
- return getParentSelectors(node.parent);
25
- }
26
- return '';
27
- }
28
- function stripVar(prop) {
29
- return prop.substring(prop.indexOf('(') + 1).split(/\,|\)/)[0];
30
- }
31
- function stripLessVar(prop) {
32
- return prop.substring(1);
33
- }
34
- export function isColorProperty(prop) {
35
- return prop === 'color' || prop === 'background' || prop === 'background-color' || prop === 'box-shadow' || prop === 'border' || prop === 'border-left' || prop === 'border-right' || prop === 'border-top' || prop === 'border-bottom' || prop === 'border-color' || prop === 'outline';
36
- }
37
- function getDeclarationMeta(decl) {
38
- if (decl.prop === 'color') {
39
- return 'text';
40
- }
41
- if (decl.prop.startsWith('background')) {
42
- return 'background';
43
- }
44
- if (decl.prop.includes('shadow')) {
45
- return 'shadow';
46
- }
47
- if (decl.prop.includes('border')) {
48
- return 'border';
49
- }
50
- return '';
51
- }
52
- function isDesignToken(tokenName) {
53
- return Boolean(Object.entries(designTokens).find(([_, value]) => tokenName === value));
54
- }
55
- function getMetaFromCssVar(tokenName) {
56
- const meta = knownVariables[tokenName];
57
- if (!meta || meta.length === 0) {
58
- return tokenName.split('-');
59
- }
60
- return meta;
61
- }
62
- function getMetaFromRawColor(rawColor) {
63
- let cleanColor = rawColor.toLowerCase();
64
- if (cleanColor.length === 4) {
65
- cleanColor = cleanColor + cleanColor.substring(cleanColor.indexOf('#') + 1);
66
- }
67
- return knownRawColors[cleanColor];
68
- }
69
12
 
70
13
  // https://github.com/postcss/postcss/blob/main/docs/writing-a-plugin.md
71
14
  // https://astexplorer.net/#/2uBU1BLuJ1
@@ -73,69 +16,62 @@ const plugin = () => {
73
16
  const processed = Symbol('processed');
74
17
  return {
75
18
  postcssPlugin: 'UsingTokens',
76
- Declaration: decl => {
19
+ AtRule(atRule, helper) {
77
20
  // @ts-expect-error
78
- if (decl[processed]) {
21
+ if (atRule[processed]) {
79
22
  return;
80
23
  }
81
- if (!isColorProperty(decl.prop)) {
24
+
25
+ // @ts-expect-error: The 'variable' property does not exist on 'AtRule' according to the TypeScript definitions.
26
+ // However, the 'postcss-less' library adds a 'variable' property to 'AtRule' when parsing LESS variables.
27
+ // This property indicates whether the 'AtRule' is a LESS variable.
28
+ if (atRule.variable) {
29
+ // TODO https://hello.atlassian.net/browse/DCA11Y-637
30
+ }
31
+
32
+ // @ts-expect-error
33
+ atRule[processed] = true;
34
+ },
35
+ Declaration: decl => {
36
+ // @ts-expect-error
37
+ if (decl[processed]) {
82
38
  return;
83
39
  }
84
40
  if (decl.value === 'none') {
85
41
  return;
86
42
  }
87
- const searchTerms = [getDeclarationMeta(decl), ...getParentSelectors(decl).split(/\-|\.|\,|\ |\:|\&/).filter(el => !!el)];
88
- let match;
89
- const cssVarRe = /var\([^\)]+\)/g;
90
- const lessVarRe = /@[a-zA-Z0-9-]+/g;
91
- const rawColorRe = /(#([0-9a-f]{3}){1,2}|(rgba|hsla)\(\d{1,3}%?(,\s?\d{1,3}%?){2},\s?(1|0|0?\.\d+)\)|(rgb|hsl)\(\d{1,3}%?(,\s?\d{1,3}%?){2}\))/i;
92
-
93
- // CSS variables
94
- const cssVarMatch = decl.value.match(cssVarRe);
95
- if (cssVarMatch) {
96
- var _getMetaFromCssVar;
97
- match = cssVarMatch[0];
98
- if (isDesignToken(stripVar(match))) {
43
+ const baseMeta = getBaseDeclarationMeta(decl);
44
+ if (isCssDeclaration(decl.prop)) {
45
+ // TODO https://hello.atlassian.net/browse/DCA11Y-637
46
+ }
47
+ if (isColorRelatedProperty(decl.prop)) {
48
+ const values = splitCssValue(decl.value);
49
+ if (!values) {
99
50
  return;
100
51
  }
101
- searchTerms.push(...((_getMetaFromCssVar = getMetaFromCssVar(stripVar(match))) !== null && _getMetaFromCssVar !== void 0 ? _getMetaFromCssVar : []));
102
- }
103
-
104
- // Less variables
105
- const lessVarMatch = decl.value.match(lessVarRe);
106
- if (lessVarMatch) {
107
- var _getMetaFromCssVar2;
108
- match = lessVarMatch[0];
109
- searchTerms.push(...((_getMetaFromCssVar2 = getMetaFromCssVar(`--${stripLessVar(match)}`)) !== null && _getMetaFromCssVar2 !== void 0 ? _getMetaFromCssVar2 : []));
110
- }
111
-
112
- // Raw colors
113
- const rawColorMatch = decl.value.match(rawColorRe);
114
- if (rawColorMatch) {
115
- var _getMetaFromRawColor;
116
- match = rawColorMatch[0];
117
- searchTerms.push(...((_getMetaFromRawColor = getMetaFromRawColor(match)) !== null && _getMetaFromRawColor !== void 0 ? _getMetaFromRawColor : []));
118
- }
119
-
120
- // Named colors
121
- if (knownColors.hasOwnProperty(decl.value)) {
122
- var _knownColors$decl$val;
123
- match = decl.value;
124
- searchTerms.push(...((_knownColors$decl$val = knownColors[decl.value.toLowerCase()]) !== null && _knownColors$decl$val !== void 0 ? _knownColors$decl$val : []));
125
- }
126
- if (!match) {
127
- // eslint-disable-next-line no-console
128
- console.warn(`Unable to find match for declaration: ${decl.prop}: ${decl.value}`);
129
- return;
130
- }
131
- const cleanSearchTerms = cleanMeta(searchTerms).join(' ');
132
- const results = search.get(cleanSearchTerms);
133
- const candidates = results.map(result => result[1]);
134
- const replacement = candidates.length ? designTokens[candidates[0]] : 'MISSING_TOKEN';
135
- if (decl.prop === 'box-shadow') {
136
- decl.value = `var(${replacement}, ${decl.value})`;
137
- } else {
138
- decl.value = decl.value.split(match).join(`var(${replacement}, ${match})`);
52
+ switch (decl.prop) {
53
+ case 'box-shadow':
54
+ const meta = values.reduce((acc, curr) => {
55
+ const parsedValue = parseValue(curr);
56
+ if (!parsedValue) {
57
+ return acc;
58
+ }
59
+ return [...acc, ...parsedValue.getMeta()];
60
+ }, baseMeta);
61
+ const token = findToken(meta);
62
+ decl.value = `var(${token}, ${decl.value})`;
63
+ break;
64
+ default:
65
+ const replacedValues = values.map(value => {
66
+ const parsedValue = parseValue(value);
67
+ if (!parsedValue) {
68
+ return value;
69
+ }
70
+ return parsedValue.getReplacement(baseMeta);
71
+ });
72
+ decl.value = replacedValues.join(' ');
73
+ break;
74
+ }
139
75
  }
140
76
 
141
77
  // @ts-expect-error
@@ -148,6 +84,6 @@ export default async function transformer(file) {
148
84
  const src = typeof file === 'string' ? file : file.source;
149
85
  const {
150
86
  css
151
- } = await processor.process(src, options);
87
+ } = await processor.process(src, POSTCSS_OPTIONS);
152
88
  return css;
153
89
  }
@@ -0,0 +1,34 @@
1
+ import { knownNamedColors, knownVariables } from './legacy-colors';
2
+ const NAMED_COLORS = Object.keys(knownNamedColors);
3
+ const GRADIENT_TYPES = ['linear', 'radial', 'conic'];
4
+ const LESS_COLOR_FUNCTIONS = ['lighten', 'darken', 'saturate', 'desaturate', 'fadein', 'fadeout', 'fade', 'spin', 'mix', 'greyscale', 'contrast', 'multiply', 'screen', 'overlay', 'softlight', 'hardlight', 'difference', 'exclusion', 'average', 'negation', 'tint', 'shade', 'luma', 'hue', 'saturation', 'lightness', 'alpha', 'red', 'green', 'blue'];
5
+ const REGEXES = {
6
+ // The CSS regular expression matches CSS variable declarations.
7
+ // It looks for the string "var(" followed by any characters except a closing parenthesis, and ending with a closing parenthesis.
8
+ CSS: /var\([^\)]+\)/g,
9
+ // The RAW_COLOR regular expression matches various CSS color formats including hexadecimal, RGB(A), HSL(A), LAB, LCH, and HWB.
10
+ // It allows for optional leading and trailing white spaces.
11
+ // For RGBA and HSLA, it allows any number (including negative numbers and numbers greater than 1) for the alpha channel.
12
+ // For RGB, HSL, LAB, LCH, and HWB, it expects three comma-separated values.
13
+ // It also allows optional white spaces around the commas and the values.
14
+ RAW_COLOR: /^\s*(#([0-9a-f]{3}){1,2}|(rgba|hsla)\(\s*\d{1,3}%?\s*(,\s*\d{1,3}%?\s*){2},\s*-?\d*\.?\d+\s*\)|(rgb|hsl)\(\s*\d{1,3}%?\s*(,\s*\d{1,3}%?\s*){2}\)\s*|(lab|lch)\(\s*\d{1,3}%?\s+\d{1,3}%?\s+\d{1,3}%?\s*\)|hwb\(\s*\d{1,3}\s+\d{1,3}%?\s+\d{1,3}%?\s*\))\s*$/i
15
+ };
16
+ export function isKnownCssVariable(value) {
17
+ return value in knownVariables;
18
+ }
19
+ export function isRawColor(value) {
20
+ return REGEXES.RAW_COLOR.test(value);
21
+ }
22
+ export function isNamedColor(value) {
23
+ return NAMED_COLORS.includes(value);
24
+ }
25
+ export function isGradient(value) {
26
+ return GRADIENT_TYPES.some(gradient => value.startsWith(`${gradient}-gradient(`));
27
+ }
28
+ export function extractBetweenParentheses(value) {
29
+ const match = value.match(/\((.*?)\)/);
30
+ return match ? match[1] : '';
31
+ }
32
+ export function isLessFunction(value) {
33
+ return LESS_COLOR_FUNCTIONS.some(func => value.startsWith(`${func}(`));
34
+ }
@@ -0,0 +1,17 @@
1
+ const COLOR_PROPERTIES = ['color', 'background', 'background-color', 'box-shadow', 'border', 'border-left', 'border-right', 'border-top', 'border-bottom', 'border-color', 'border-left-color', 'border-right-color', 'border-top-color', 'border-bottom-color', 'outline', 'outline-color', 'accent-color', 'caret-color', 'scrollbar-color', 'text-stroke'];
2
+ export function isColorRelatedProperty(prop) {
3
+ return COLOR_PROPERTIES.some(property => property === prop);
4
+ }
5
+ export function isCssDeclaration(prop) {
6
+ return prop.startsWith('--');
7
+ }
8
+ export function extractCssVarName(prop) {
9
+ return prop.substring(prop.indexOf('(') + 1).split(/\,|\)/)[0];
10
+ }
11
+ export function extractLessVarName(prop) {
12
+ return prop.substring(1);
13
+ }
14
+ export function splitCssValue(value) {
15
+ const regex = /(?:[^\s()]+|\((?:[^()]+|\([^()]*\))*\))+/g;
16
+ return value.match(regex);
17
+ }
@@ -163,7 +163,7 @@ export const knownVariables = {
163
163
  '--jpo-border-default-color': ['border'],
164
164
  '--jpo-border-secondary-color': ['border', 'subtle']
165
165
  };
166
- export const knownColors = {
166
+ export const knownNamedColors = {
167
167
  aliceblue: ['blue'],
168
168
  antiquewhite: [],
169
169
  aqua: ['teal'],
@@ -183,7 +183,7 @@ export const knownColors = {
183
183
  coral: [],
184
184
  cornflowerblue: ['blue'],
185
185
  cornsilk: [],
186
- crimson: ['accent', 'red'],
186
+ crimson: ['red'],
187
187
  cyan: ['accent', 'teal', 'subtle'],
188
188
  darkblue: ['accent', 'blue', 'bold'],
189
189
  darkcyan: ['accent', 'teal', 'bold'],
@@ -237,11 +237,11 @@ export const knownColors = {
237
237
  lightgoldenrodyellow: [],
238
238
  lightgray: [],
239
239
  lightgrey: [],
240
- lightgreen: ['green'],
240
+ lightgreen: ['green', 'accent'],
241
241
  lightpink: ['magenta'],
242
242
  lightsalmon: ['pink', 'subtler'],
243
243
  lightseagreen: ['green'],
244
- lightskyblue: ['blue'],
244
+ lightskyblue: ['blue', 'accent'],
245
245
  lightslategray: [],
246
246
  lightslategrey: [],
247
247
  lightsteelblue: ['blue'],
@@ -283,7 +283,8 @@ export const knownColors = {
283
283
  plum: [],
284
284
  powderblue: ['blue', 'subtle'],
285
285
  purple: ['purple'],
286
- red: ['accent', 'red', 'subtle'],
286
+ rebeccapurple: ['purple', 'accent'],
287
+ red: ['red', 'accent'],
287
288
  rosybrown: [],
288
289
  royalblue: [],
289
290
  saddlebrown: [],
@@ -313,10 +314,9 @@ export const knownColors = {
313
314
  yellowgreen: ['yellow']
314
315
  };
315
316
  export const knownRawColors = {
316
- '#000000': ['text'],
317
- '#cccccc': ['border'],
318
- '#aaaaaa': ['border'],
319
- '#bbbbbb': ['border'],
317
+ '#cccccc': ['gray'],
318
+ '#aaaaaa': ['gray', 'subtlest'],
319
+ '#bbbbbb': ['gray', 'subtle'],
320
320
  '#ffffff': ['elevation', 'surface'],
321
321
  '#f0f0f0': ['elevation', 'surface'],
322
322
  '#eeeeee': ['elevation', 'surface', 'sunken'],
@@ -325,7 +325,6 @@ export const knownRawColors = {
325
325
  '#c00c00': ['danger'],
326
326
  '#5243aa': ['discovery'],
327
327
  '#ffc712': ['warning'],
328
- '#292929': ['text'],
329
328
  '#00f00f': ['brand'],
330
329
  '#3b73af': ['brand'],
331
330
  '#326ca6': ['brand'],
@@ -0,0 +1,11 @@
1
+ import chalk from 'chalk';
2
+ export const logger = {
3
+ warn(message) {
4
+ // eslint-disable-next-line no-console
5
+ console.warn(chalk.yellow('WARNING'), message);
6
+ },
7
+ error(message) {
8
+ // eslint-disable-next-line no-console
9
+ console.error(chalk.red('ERROR'), message);
10
+ }
11
+ };
@@ -0,0 +1,133 @@
1
+ import designTokens from '@atlaskit/tokens/token-names';
2
+ import { extractCssVarName } from './declaration';
3
+ import { knownVariables, knownRawColors, knownNamedColors } from './legacy-colors';
4
+ const getUniqueWordsFromTokens = Object.keys(designTokens).reduce((accum, val) => [...accum, ...val.split('.')], []).reduce((accum, val) => [...accum, ...val.split(/(?=[A-Z])/g).map(e => e.toLowerCase())], []).reduce((accum, val) => {
5
+ if (!accum.includes(val)) {
6
+ accum.push(val);
7
+ }
8
+ return accum;
9
+ }, []);
10
+ function filterDuplciateFoundations(meta) {
11
+ const foundations = ['text', 'background', 'shadow', 'border'];
12
+ let hasFoundation = false;
13
+ return meta.filter(val => {
14
+ if (!hasFoundation && foundations.includes(val)) {
15
+ hasFoundation = true;
16
+ return true;
17
+ }
18
+ if (hasFoundation && foundations.includes(val)) {
19
+ return false;
20
+ }
21
+ return true;
22
+ });
23
+ }
24
+ const REPLACEMENTS = {
25
+ ':': '',
26
+ ',': '',
27
+ texts: 'text',
28
+ error: 'danger',
29
+ invalid: 'danger',
30
+ removed: 'danger',
31
+ removal: 'danger',
32
+ remove: 'danger',
33
+ focus: 'focused',
34
+ valid: 'success',
35
+ successful: 'success',
36
+ risk: 'warning',
37
+ caution: 'warning',
38
+ warn: 'warning',
39
+ primary: 'bold',
40
+ info: 'bold',
41
+ secondary: 'subtle',
42
+ hyperlink: 'link',
43
+ anchor: 'link',
44
+ active: 'pressed',
45
+ hover: 'hovered',
46
+ dragged: 'overlay',
47
+ dragging: 'overlay',
48
+ drag: 'overlay',
49
+ 'background-color': 'background',
50
+ color: 'text',
51
+ icons: 'icon',
52
+ arrow: 'icon',
53
+ glyph: 'icon',
54
+ stroke: 'border',
55
+ 'border-left': 'border',
56
+ 'border-right': 'border',
57
+ 'border-top': 'border',
58
+ 'border-bottom': 'border',
59
+ 'box-shadow': 'shadow'
60
+ };
61
+ const ADDITIONAL_META = {
62
+ grey: 'neutral',
63
+ red: 'danger'
64
+ };
65
+ export function cleanMeta(meta) {
66
+ const cleanMeta = meta.reduce((accum, val) => [...accum, ...(typeof val === 'string' ? val.split(/(?=[A-Z])/g).map(e => e.toLowerCase()) : [])], []).reduce((accum, val) => {
67
+ if (val in ADDITIONAL_META) {
68
+ accum.push(val, ADDITIONAL_META[val]);
69
+ } else {
70
+ accum.push(val);
71
+ }
72
+ accum.push(val in REPLACEMENTS ? REPLACEMENTS[val] : val);
73
+ return accum;
74
+ }, []).filter(val => getUniqueWordsFromTokens.includes(val)).reduce((accum, val) => {
75
+ if (!accum.includes(val)) {
76
+ accum.push(val);
77
+ }
78
+ return accum;
79
+ }, []);
80
+ return filterDuplciateFoundations(cleanMeta);
81
+ }
82
+ export function getBaseDeclarationMeta(decl) {
83
+ const parentSelectors = getParentSelectors(decl).split(/\-|\.|\,|\ |\:|\&/).filter(Boolean);
84
+ return [getPropertyMeta(decl.prop), ...parentSelectors];
85
+ }
86
+ function getPropertyMeta(prop) {
87
+ if (prop === 'color') {
88
+ return 'text';
89
+ }
90
+ if (prop.startsWith('background')) {
91
+ return 'background';
92
+ }
93
+ if (prop.includes('shadow')) {
94
+ return 'shadow';
95
+ }
96
+ if (prop.includes('border')) {
97
+ return 'border';
98
+ }
99
+ return '';
100
+ }
101
+ function isRule(node) {
102
+ return node.type === 'rule';
103
+ }
104
+ function getParentSelectors(node) {
105
+ if (isRule(node)) {
106
+ // @ts-expect-error
107
+ return getParentSelectors(node.parent) + ' ' + node.selector;
108
+ }
109
+ if (node.parent) {
110
+ return getParentSelectors(node.parent);
111
+ }
112
+ return '';
113
+ }
114
+ export function getCssVarMeta(cssVariable) {
115
+ const tokenName = extractCssVarName(cssVariable);
116
+ const meta = knownVariables[tokenName];
117
+ if (!meta || meta.length === 0) {
118
+ return tokenName.split('-');
119
+ }
120
+ return meta;
121
+ }
122
+ export function getRawColorMeta(rawColor) {
123
+ var _knownRawColors$clean;
124
+ let cleanColor = rawColor.toLowerCase();
125
+ if (cleanColor.length === 4) {
126
+ cleanColor = cleanColor + cleanColor.substring(cleanColor.indexOf('#') + 1);
127
+ }
128
+ return (_knownRawColors$clean = knownRawColors[cleanColor]) !== null && _knownRawColors$clean !== void 0 ? _knownRawColors$clean : [];
129
+ }
130
+ export function getNamedColorMeta(namedColor) {
131
+ var _knownNamedColors$nam;
132
+ return (_knownNamedColors$nam = knownNamedColors[namedColor]) !== null && _knownNamedColors$nam !== void 0 ? _knownNamedColors$nam : [];
133
+ }
@@ -0,0 +1,24 @@
1
+ import designTokens from '@atlaskit/tokens/token-names';
2
+ import { light as rawTokens } from '@atlaskit/tokens/tokens-raw';
3
+ import Search from '../../theme-to-design-tokens/utils/fuzzy-search';
4
+ import { cleanMeta } from './meta';
5
+ const MISSING_TOKEN_NAME = 'MISSING_TOKEN';
6
+ const ACTIVE_TOKENS = rawTokens.filter(token => token.attributes.state === 'active').map(token => token.name.replace(/\.\[default\]/g, '')).filter(token => !token.includes('UNSAFE') && !token.includes('interaction'));
7
+ const COLOR_TOKEN_SEARCH_TYPES = ['text', 'link', 'icon', 'border', 'background', 'blanket', 'skeleton', 'chart', 'surface', 'shadow'];
8
+ const createFuzzySearch = tokens => Search(tokens, false);
9
+ function filterTokens(meta) {
10
+ const commonSearchTypes = COLOR_TOKEN_SEARCH_TYPES.filter(type => meta.includes(type));
11
+ return commonSearchTypes.length !== 0 ? ACTIVE_TOKENS.filter(token => commonSearchTypes.some(type => token.startsWith(`color.${type}`) || token.startsWith(`elevation.${type}`))) : ACTIVE_TOKENS;
12
+ }
13
+ export default function findToken(meta) {
14
+ const filteredTokens = filterTokens(meta);
15
+ const tokenFuzzySearch = createFuzzySearch(filteredTokens);
16
+ const cleanSearchTerms = cleanMeta(meta).join(' ');
17
+ const results = tokenFuzzySearch.get(cleanSearchTerms);
18
+ if (!results) {
19
+ return MISSING_TOKEN_NAME;
20
+ }
21
+ const candidates = results.map(result => result[1]);
22
+ const bestCandidate = designTokens[candidates[0]];
23
+ return bestCandidate;
24
+ }
@@ -0,0 +1,68 @@
1
+ import { extractBetweenParentheses, isGradient, isKnownCssVariable, isLessFunction, isNamedColor, isRawColor } from './colors';
2
+ import { extractCssVarName } from './declaration';
3
+ import { getCssVarMeta, getNamedColorMeta, getRawColorMeta } from './meta';
4
+ import findToken from './tokens';
5
+ import { logger } from './logger';
6
+ export default function parseValue(value) {
7
+ if (isLessFunction(value)) {
8
+ logger.warn('Cannot parse - less function detected, would cause compilation error');
9
+ return null;
10
+ }
11
+ if (isGradient(value)) {
12
+ return {
13
+ getReplacement(additionalMeta = []) {
14
+ const gradientContent = extractBetweenParentheses(value);
15
+ const values = gradientContent.split(',');
16
+ const replacedValues = values.map(val => {
17
+ const parsedValue = parseValue(val.trim());
18
+ return parsedValue ? parsedValue.getReplacement(additionalMeta) : val;
19
+ });
20
+ return value.replace(gradientContent, replacedValues.join(', '));
21
+ },
22
+ getMeta() {
23
+ const BASE_META = [];
24
+ const gradientContent = extractBetweenParentheses(value);
25
+ const values = gradientContent.split(',');
26
+ const metas = values.reduce((acc, val) => {
27
+ const parsedValue = parseValue(val.trim());
28
+ return parsedValue ? [...acc, ...parsedValue.getMeta()] : acc;
29
+ }, BASE_META);
30
+ return metas;
31
+ }
32
+ };
33
+ }
34
+ if (isKnownCssVariable(extractCssVarName(value))) {
35
+ return {
36
+ getReplacement(additionalMeta = []) {
37
+ const token = findToken([...additionalMeta, ...this.getMeta()]);
38
+ return `var(${token}, ${value})`;
39
+ },
40
+ getMeta() {
41
+ return getCssVarMeta(value);
42
+ }
43
+ };
44
+ }
45
+ if (isRawColor(value)) {
46
+ return {
47
+ getReplacement(additionalMeta = []) {
48
+ const token = findToken([...additionalMeta, ...this.getMeta()]);
49
+ return `var(${token}, ${value})`;
50
+ },
51
+ getMeta() {
52
+ return getRawColorMeta(value);
53
+ }
54
+ };
55
+ }
56
+ if (isNamedColor(value)) {
57
+ return {
58
+ getReplacement(additionalMeta = []) {
59
+ const token = findToken([...additionalMeta, ...this.getMeta()]);
60
+ return `var(${token}, ${value})`;
61
+ },
62
+ getMeta() {
63
+ return getNamedColorMeta(value);
64
+ }
65
+ };
66
+ }
67
+ return null;
68
+ }
@@ -8,5 +8,6 @@ import './styled-to-emotion/styled-to-emotion';
8
8
  import './theme-to-design-tokens/theme-to-design-tokens';
9
9
  import './theme-remove-deprecated-mixins/theme-remove-deprecated-mixins';
10
10
  import './css-to-design-tokens/css-to-design-tokens';
11
- const presets = ['styled-to-emotion', 'theme-to-design-tokens', 'theme-remove-deprecated-mixins', 'css-to-design-tokens'].map(preset => path.join(__dirname, preset, `${preset}.@(ts|js|tsx)`));
11
+ import './migrate-to-new-buttons/migrate-to-new-buttons';
12
+ const presets = ['styled-to-emotion', 'theme-to-design-tokens', 'theme-remove-deprecated-mixins', 'css-to-design-tokens', 'migrate-to-new-buttons'].map(preset => path.join(__dirname, preset, `${preset}.@(ts|js|tsx)`));
12
13
  export default presets;