@atlaskit/codemod-cli 0.13.4 → 0.15.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.
- package/CHANGELOG.md +71 -52
- package/dist/cjs/main.js +3 -3
- package/dist/cjs/presets/css-to-design-tokens/css-to-design-tokens.js +65 -135
- package/dist/cjs/presets/css-to-design-tokens/lib/colors.js +49 -0
- package/dist/cjs/presets/css-to-design-tokens/lib/declaration.js +29 -0
- package/dist/cjs/presets/css-to-design-tokens/{utils → lib}/legacy-colors.js +13 -17
- package/dist/cjs/presets/css-to-design-tokens/lib/logger.js +18 -0
- package/dist/cjs/presets/css-to-design-tokens/lib/meta.js +157 -0
- package/dist/cjs/presets/css-to-design-tokens/lib/tokens.js +47 -0
- package/dist/cjs/presets/css-to-design-tokens/lib/value.js +80 -0
- package/dist/cjs/presets/index.js +1 -2
- package/dist/cjs/presets/styled-to-emotion/styled-to-emotion.js +1 -2
- package/dist/cjs/presets/theme-remove-deprecated-mixins/utils/replacements.js +2 -3
- package/dist/cjs/presets/theme-to-design-tokens/theme-to-design-tokens.js +348 -104
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast.js +1 -1
- package/dist/cjs/presets/theme-to-design-tokens/utils/color.js +17 -17
- package/dist/cjs/presets/theme-to-design-tokens/utils/css-utils.js +38 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/fuzzy-search.js +1 -2
- package/dist/cjs/presets/theme-to-design-tokens/utils/legacy-colors.js +4 -7
- package/dist/cjs/presets/theme-to-design-tokens/utils/named-colors.js +1 -2
- package/dist/cjs/presets/theme-to-design-tokens/utils/string-utils.js +25 -0
- package/dist/cjs/presets/theme-to-design-tokens/utils/tokens.js +3 -5
- package/dist/cjs/sinceRef.js +1 -2
- package/dist/cjs/transforms.js +6 -12
- package/dist/cjs/types.js +3 -5
- package/dist/cjs/utils.js +2 -3
- package/dist/es2019/presets/css-to-design-tokens/css-to-design-tokens.js +61 -115
- package/dist/es2019/presets/css-to-design-tokens/lib/colors.js +34 -0
- package/dist/es2019/presets/css-to-design-tokens/lib/declaration.js +17 -0
- package/dist/es2019/presets/css-to-design-tokens/{utils → lib}/legacy-colors.js +9 -10
- package/dist/es2019/presets/css-to-design-tokens/lib/logger.js +11 -0
- package/dist/es2019/presets/css-to-design-tokens/lib/meta.js +133 -0
- package/dist/es2019/presets/css-to-design-tokens/lib/tokens.js +24 -0
- package/dist/es2019/presets/css-to-design-tokens/lib/value.js +68 -0
- package/dist/es2019/presets/theme-to-design-tokens/theme-to-design-tokens.js +191 -32
- package/dist/es2019/presets/theme-to-design-tokens/utils/ast.js +1 -1
- package/dist/es2019/presets/theme-to-design-tokens/utils/color.js +12 -10
- package/dist/es2019/presets/theme-to-design-tokens/utils/css-utils.js +31 -0
- package/dist/es2019/presets/theme-to-design-tokens/utils/string-utils.js +13 -0
- package/dist/esm/main.js +3 -3
- package/dist/esm/presets/css-to-design-tokens/css-to-design-tokens.js +65 -135
- package/dist/esm/presets/css-to-design-tokens/lib/colors.js +38 -0
- package/dist/esm/presets/css-to-design-tokens/lib/declaration.js +19 -0
- package/dist/esm/presets/css-to-design-tokens/{utils → lib}/legacy-colors.js +9 -10
- package/dist/esm/presets/css-to-design-tokens/lib/logger.js +11 -0
- package/dist/esm/presets/css-to-design-tokens/lib/meta.js +146 -0
- package/dist/esm/presets/css-to-design-tokens/lib/tokens.js +40 -0
- package/dist/esm/presets/css-to-design-tokens/lib/value.js +73 -0
- package/dist/esm/presets/theme-to-design-tokens/theme-to-design-tokens.js +346 -100
- package/dist/esm/presets/theme-to-design-tokens/utils/ast.js +1 -1
- package/dist/esm/presets/theme-to-design-tokens/utils/color.js +12 -10
- package/dist/esm/presets/theme-to-design-tokens/utils/css-utils.js +31 -0
- package/dist/esm/presets/theme-to-design-tokens/utils/string-utils.js +17 -0
- package/dist/types/main.d.ts +0 -1
- package/dist/types/presets/css-to-design-tokens/css-to-design-tokens.d.ts +1 -1
- package/dist/types/presets/css-to-design-tokens/lib/colors.d.ts +6 -0
- package/dist/types/presets/css-to-design-tokens/lib/declaration.d.ts +5 -0
- package/dist/types/presets/css-to-design-tokens/{utils → lib}/legacy-colors.d.ts +1 -1
- package/dist/types/presets/css-to-design-tokens/lib/logger.d.ts +4 -0
- package/dist/types/presets/css-to-design-tokens/lib/meta.d.ts +6 -0
- package/dist/types/presets/css-to-design-tokens/lib/tokens.d.ts +7 -0
- package/dist/types/presets/css-to-design-tokens/lib/value.d.ts +6 -0
- package/dist/types/presets/theme-to-design-tokens/theme-to-design-tokens.d.ts +1 -1
- package/dist/types/presets/theme-to-design-tokens/utils/ast.d.ts +1 -1
- package/dist/types/presets/theme-to-design-tokens/utils/color.d.ts +2 -1
- package/dist/types/presets/theme-to-design-tokens/utils/css-utils.d.ts +2 -0
- package/dist/types/presets/theme-to-design-tokens/utils/string-utils.d.ts +3 -0
- package/dist/types-ts4.5/main.d.ts +0 -1
- package/dist/types-ts4.5/presets/css-to-design-tokens/css-to-design-tokens.d.ts +1 -1
- package/dist/types-ts4.5/presets/css-to-design-tokens/lib/colors.d.ts +6 -0
- package/dist/types-ts4.5/presets/css-to-design-tokens/lib/declaration.d.ts +5 -0
- package/dist/types-ts4.5/presets/css-to-design-tokens/{utils → lib}/legacy-colors.d.ts +1 -1
- package/dist/types-ts4.5/presets/css-to-design-tokens/lib/logger.d.ts +4 -0
- package/dist/types-ts4.5/presets/css-to-design-tokens/lib/meta.d.ts +6 -0
- package/dist/types-ts4.5/presets/css-to-design-tokens/lib/tokens.d.ts +7 -0
- package/dist/types-ts4.5/presets/css-to-design-tokens/lib/value.d.ts +6 -0
- package/dist/types-ts4.5/presets/theme-to-design-tokens/theme-to-design-tokens.d.ts +1 -1
- package/dist/types-ts4.5/presets/theme-to-design-tokens/utils/ast.d.ts +1 -1
- package/dist/types-ts4.5/presets/theme-to-design-tokens/utils/color.d.ts +2 -1
- package/dist/types-ts4.5/presets/theme-to-design-tokens/utils/css-utils.d.ts +2 -0
- package/dist/types-ts4.5/presets/theme-to-design-tokens/utils/string-utils.d.ts +6 -0
- package/package.json +2 -2
- package/report.api.md +0 -2
- package/tmp/api-report-tmp.d.ts +0 -2
- package/dist/cjs/presets/css-to-design-tokens/utils/meta.js +0 -53
- package/dist/es2019/presets/css-to-design-tokens/utils/meta.js +0 -33
- package/dist/esm/presets/css-to-design-tokens/utils/meta.js +0 -46
- package/dist/types/presets/css-to-design-tokens/utils/meta.d.ts +0 -1
- package/dist/types-ts4.5/presets/css-to-design-tokens/utils/meta.d.ts +0 -1
|
@@ -1,82 +1,17 @@
|
|
|
1
1
|
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
|
|
2
2
|
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
3
|
-
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
4
3
|
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
5
4
|
import postcss from 'postcss';
|
|
6
5
|
// @ts-ignore
|
|
7
6
|
import lessSyntax from 'postcss-less';
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
return t.name.replace(/\.\[default\]/g, '');
|
|
17
|
-
}).filter(function (t) {
|
|
18
|
-
return !t.includes('UNSAFE') && !t.includes('interaction');
|
|
19
|
-
});
|
|
20
|
-
var search = Search(tokens, false);
|
|
21
|
-
function isRule(node) {
|
|
22
|
-
return node.type === 'rule';
|
|
23
|
-
}
|
|
24
|
-
function getParentSelectors(node) {
|
|
25
|
-
if (isRule(node)) {
|
|
26
|
-
// @ts-expect-error
|
|
27
|
-
return getParentSelectors(node.parent) + ' ' + node.selector;
|
|
28
|
-
}
|
|
29
|
-
if (node.parent) {
|
|
30
|
-
return getParentSelectors(node.parent);
|
|
31
|
-
}
|
|
32
|
-
return '';
|
|
33
|
-
}
|
|
34
|
-
function stripVar(prop) {
|
|
35
|
-
return prop.substring(prop.indexOf('(') + 1).split(/\,|\)/)[0];
|
|
36
|
-
}
|
|
37
|
-
function stripLessVar(prop) {
|
|
38
|
-
return prop.substring(1);
|
|
39
|
-
}
|
|
40
|
-
function isColorProperty(prop) {
|
|
41
|
-
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';
|
|
42
|
-
}
|
|
43
|
-
function getDeclarationMeta(decl) {
|
|
44
|
-
if (decl.prop === 'color') {
|
|
45
|
-
return 'text';
|
|
46
|
-
}
|
|
47
|
-
if (decl.prop.startsWith('background')) {
|
|
48
|
-
return 'background';
|
|
49
|
-
}
|
|
50
|
-
if (decl.prop.includes('shadow')) {
|
|
51
|
-
return 'shadow';
|
|
52
|
-
}
|
|
53
|
-
if (decl.prop.includes('border')) {
|
|
54
|
-
return 'border';
|
|
55
|
-
}
|
|
56
|
-
return '';
|
|
57
|
-
}
|
|
58
|
-
function isDesignToken(tokenName) {
|
|
59
|
-
return Boolean(Object.entries(designTokens).find(function (_ref) {
|
|
60
|
-
var _ref2 = _slicedToArray(_ref, 2),
|
|
61
|
-
_ = _ref2[0],
|
|
62
|
-
value = _ref2[1];
|
|
63
|
-
return tokenName === value;
|
|
64
|
-
}));
|
|
65
|
-
}
|
|
66
|
-
function getMetaFromCssVar(tokenName) {
|
|
67
|
-
var meta = knownVariables[tokenName];
|
|
68
|
-
if (!meta || meta.length === 0) {
|
|
69
|
-
return tokenName.split('-');
|
|
70
|
-
}
|
|
71
|
-
return meta;
|
|
72
|
-
}
|
|
73
|
-
function getMetaFromRawColor(rawColor) {
|
|
74
|
-
var cleanColor = rawColor.toLowerCase();
|
|
75
|
-
if (cleanColor.length === 4) {
|
|
76
|
-
cleanColor = cleanColor + cleanColor.substring(cleanColor.indexOf('#') + 1);
|
|
77
|
-
}
|
|
78
|
-
return knownRawColors[cleanColor];
|
|
79
|
-
}
|
|
7
|
+
import { isColorRelatedProperty, isCssDeclaration, splitCssValue } from './lib/declaration';
|
|
8
|
+
import { getBaseDeclarationMeta } from './lib/meta';
|
|
9
|
+
import findToken from './lib/tokens';
|
|
10
|
+
import parseValue from './lib/value';
|
|
11
|
+
var POSTCSS_OPTIONS = {
|
|
12
|
+
syntax: lessSyntax,
|
|
13
|
+
from: undefined
|
|
14
|
+
};
|
|
80
15
|
|
|
81
16
|
// https://github.com/postcss/postcss/blob/main/docs/writing-a-plugin.md
|
|
82
17
|
// https://astexplorer.net/#/2uBU1BLuJ1
|
|
@@ -84,70 +19,62 @@ var plugin = function plugin() {
|
|
|
84
19
|
var processed = Symbol('processed');
|
|
85
20
|
return {
|
|
86
21
|
postcssPlugin: 'UsingTokens',
|
|
87
|
-
|
|
22
|
+
AtRule: function AtRule(atRule, helper) {
|
|
88
23
|
// @ts-expect-error
|
|
89
|
-
if (
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
if (!isColorProperty(decl.prop)) {
|
|
24
|
+
if (atRule[processed]) {
|
|
93
25
|
return;
|
|
94
26
|
}
|
|
95
|
-
var searchTerms = [getDeclarationMeta(decl)].concat(_toConsumableArray(getParentSelectors(decl).split(/\-|\.|\,|\ |\:|\&/).filter(function (el) {
|
|
96
|
-
return !!el;
|
|
97
|
-
})));
|
|
98
|
-
var match;
|
|
99
|
-
var cssVarRe = /var\([^\)]+\)/g;
|
|
100
|
-
var lessVarRe = /@[a-zA-Z0-9-]+/g;
|
|
101
|
-
var 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;
|
|
102
27
|
|
|
103
|
-
//
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (isDesignToken(stripVar(match))) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
searchTerms.push.apply(searchTerms, _toConsumableArray((_getMetaFromCssVar = getMetaFromCssVar(stripVar(match))) !== null && _getMetaFromCssVar !== void 0 ? _getMetaFromCssVar : []));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Less variables
|
|
115
|
-
var lassVarMatch = decl.value.match(lessVarRe);
|
|
116
|
-
if (lassVarMatch) {
|
|
117
|
-
var _getMetaFromCssVar2;
|
|
118
|
-
match = lassVarMatch[0];
|
|
119
|
-
searchTerms.push.apply(searchTerms, _toConsumableArray((_getMetaFromCssVar2 = getMetaFromCssVar("--".concat(stripLessVar(match)))) !== null && _getMetaFromCssVar2 !== void 0 ? _getMetaFromCssVar2 : []));
|
|
28
|
+
// @ts-expect-error: The 'variable' property does not exist on 'AtRule' according to the TypeScript definitions.
|
|
29
|
+
// However, the 'postcss-less' library adds a 'variable' property to 'AtRule' when parsing LESS variables.
|
|
30
|
+
// This property indicates whether the 'AtRule' is a LESS variable.
|
|
31
|
+
if (atRule.variable) {
|
|
32
|
+
// TODO https://hello.atlassian.net/browse/DCA11Y-637
|
|
120
33
|
}
|
|
121
34
|
|
|
122
|
-
//
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
// Named colors
|
|
131
|
-
if (knownColors.hasOwnProperty(decl.value)) {
|
|
132
|
-
var _knownColors$decl$val;
|
|
133
|
-
match = decl.value;
|
|
134
|
-
searchTerms.push.apply(searchTerms, _toConsumableArray((_knownColors$decl$val = knownColors[decl.value.toLowerCase()]) !== null && _knownColors$decl$val !== void 0 ? _knownColors$decl$val : []));
|
|
35
|
+
// @ts-expect-error
|
|
36
|
+
atRule[processed] = true;
|
|
37
|
+
},
|
|
38
|
+
Declaration: function Declaration(decl) {
|
|
39
|
+
// @ts-expect-error
|
|
40
|
+
if (decl[processed]) {
|
|
41
|
+
return;
|
|
135
42
|
}
|
|
136
|
-
if (
|
|
137
|
-
// eslint-disable-next-line no-console
|
|
138
|
-
console.warn("Unable to find match for declaration: ".concat(decl.prop, ": ").concat(decl.value));
|
|
43
|
+
if (decl.value === 'none') {
|
|
139
44
|
return;
|
|
140
45
|
}
|
|
141
|
-
var
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
46
|
+
var baseMeta = getBaseDeclarationMeta(decl);
|
|
47
|
+
if (isCssDeclaration(decl.prop)) {
|
|
48
|
+
// TODO https://hello.atlassian.net/browse/DCA11Y-637
|
|
49
|
+
}
|
|
50
|
+
if (isColorRelatedProperty(decl.prop)) {
|
|
51
|
+
var values = splitCssValue(decl.value);
|
|
52
|
+
if (!values) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
switch (decl.prop) {
|
|
56
|
+
case 'box-shadow':
|
|
57
|
+
var meta = values.reduce(function (acc, curr) {
|
|
58
|
+
var parsedValue = parseValue(curr);
|
|
59
|
+
if (!parsedValue) {
|
|
60
|
+
return acc;
|
|
61
|
+
}
|
|
62
|
+
return [].concat(_toConsumableArray(acc), _toConsumableArray(parsedValue.getMeta()));
|
|
63
|
+
}, baseMeta);
|
|
64
|
+
var token = findToken(meta);
|
|
65
|
+
decl.value = "var(".concat(token, ", ").concat(decl.value, ")");
|
|
66
|
+
break;
|
|
67
|
+
default:
|
|
68
|
+
var replacedValues = values.map(function (value) {
|
|
69
|
+
var parsedValue = parseValue(value);
|
|
70
|
+
if (!parsedValue) {
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
return parsedValue.getReplacement(baseMeta);
|
|
74
|
+
});
|
|
75
|
+
decl.value = replacedValues.join(' ');
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
151
78
|
}
|
|
152
79
|
|
|
153
80
|
// @ts-expect-error
|
|
@@ -160,16 +87,19 @@ export default function transformer(_x) {
|
|
|
160
87
|
}
|
|
161
88
|
function _transformer() {
|
|
162
89
|
_transformer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(file) {
|
|
90
|
+
var processor, src, _yield$processor$proc, css;
|
|
163
91
|
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
164
92
|
while (1) switch (_context.prev = _context.next) {
|
|
165
93
|
case 0:
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
case
|
|
171
|
-
|
|
172
|
-
|
|
94
|
+
processor = postcss([plugin()]);
|
|
95
|
+
src = typeof file === 'string' ? file : file.source;
|
|
96
|
+
_context.next = 4;
|
|
97
|
+
return processor.process(src, POSTCSS_OPTIONS);
|
|
98
|
+
case 4:
|
|
99
|
+
_yield$processor$proc = _context.sent;
|
|
100
|
+
css = _yield$processor$proc.css;
|
|
101
|
+
return _context.abrupt("return", css);
|
|
102
|
+
case 7:
|
|
173
103
|
case "end":
|
|
174
104
|
return _context.stop();
|
|
175
105
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { knownNamedColors, knownVariables } from './legacy-colors';
|
|
2
|
+
var NAMED_COLORS = Object.keys(knownNamedColors);
|
|
3
|
+
var GRADIENT_TYPES = ['linear', 'radial', 'conic'];
|
|
4
|
+
var 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
|
+
var 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(function (gradient) {
|
|
27
|
+
return value.startsWith("".concat(gradient, "-gradient("));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export function extractBetweenParentheses(value) {
|
|
31
|
+
var match = value.match(/\((.*?)\)/);
|
|
32
|
+
return match ? match[1] : '';
|
|
33
|
+
}
|
|
34
|
+
export function isLessFunction(value) {
|
|
35
|
+
return LESS_COLOR_FUNCTIONS.some(function (func) {
|
|
36
|
+
return value.startsWith("".concat(func, "("));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var 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(function (property) {
|
|
4
|
+
return property === prop;
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
export function isCssDeclaration(prop) {
|
|
8
|
+
return prop.startsWith('--');
|
|
9
|
+
}
|
|
10
|
+
export function extractCssVarName(prop) {
|
|
11
|
+
return prop.substring(prop.indexOf('(') + 1).split(/\,|\)/)[0];
|
|
12
|
+
}
|
|
13
|
+
export function extractLessVarName(prop) {
|
|
14
|
+
return prop.substring(1);
|
|
15
|
+
}
|
|
16
|
+
export function splitCssValue(value) {
|
|
17
|
+
var regex = /(?:[^\s()]+|\((?:[^()]+|\([^()]*\))*\))+/g;
|
|
18
|
+
return value.match(regex);
|
|
19
|
+
}
|
|
@@ -163,7 +163,7 @@ export var knownVariables = {
|
|
|
163
163
|
'--jpo-border-default-color': ['border'],
|
|
164
164
|
'--jpo-border-secondary-color': ['border', 'subtle']
|
|
165
165
|
};
|
|
166
|
-
export var
|
|
166
|
+
export var knownNamedColors = {
|
|
167
167
|
aliceblue: ['blue'],
|
|
168
168
|
antiquewhite: [],
|
|
169
169
|
aqua: ['teal'],
|
|
@@ -183,7 +183,7 @@ export var knownColors = {
|
|
|
183
183
|
coral: [],
|
|
184
184
|
cornflowerblue: ['blue'],
|
|
185
185
|
cornsilk: [],
|
|
186
|
-
crimson: ['
|
|
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 var 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 var knownColors = {
|
|
|
283
283
|
plum: [],
|
|
284
284
|
powderblue: ['blue', 'subtle'],
|
|
285
285
|
purple: ['purple'],
|
|
286
|
-
|
|
286
|
+
rebeccapurple: ['purple', 'accent'],
|
|
287
|
+
red: ['red', 'accent'],
|
|
287
288
|
rosybrown: [],
|
|
288
289
|
royalblue: [],
|
|
289
290
|
saddlebrown: [],
|
|
@@ -313,10 +314,9 @@ export var knownColors = {
|
|
|
313
314
|
yellowgreen: ['yellow']
|
|
314
315
|
};
|
|
315
316
|
export var knownRawColors = {
|
|
316
|
-
'#
|
|
317
|
-
'#
|
|
318
|
-
'#
|
|
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 var 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 var logger = {
|
|
3
|
+
warn: function warn(message) {
|
|
4
|
+
// eslint-disable-next-line no-console
|
|
5
|
+
console.warn(chalk.yellow('WARNING'), message);
|
|
6
|
+
},
|
|
7
|
+
error: function error(message) {
|
|
8
|
+
// eslint-disable-next-line no-console
|
|
9
|
+
console.error(chalk.red('ERROR'), message);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
+
import designTokens from '@atlaskit/tokens/token-names';
|
|
3
|
+
import { extractCssVarName } from './declaration';
|
|
4
|
+
import { knownVariables, knownRawColors, knownNamedColors } from './legacy-colors';
|
|
5
|
+
var getUniqueWordsFromTokens = Object.keys(designTokens).reduce(function (accum, val) {
|
|
6
|
+
return [].concat(_toConsumableArray(accum), _toConsumableArray(val.split('.')));
|
|
7
|
+
}, []).reduce(function (accum, val) {
|
|
8
|
+
return [].concat(_toConsumableArray(accum), _toConsumableArray(val.split(/(?=[A-Z])/g).map(function (e) {
|
|
9
|
+
return e.toLowerCase();
|
|
10
|
+
})));
|
|
11
|
+
}, []).reduce(function (accum, val) {
|
|
12
|
+
if (!accum.includes(val)) {
|
|
13
|
+
accum.push(val);
|
|
14
|
+
}
|
|
15
|
+
return accum;
|
|
16
|
+
}, []);
|
|
17
|
+
function filterDuplciateFoundations(meta) {
|
|
18
|
+
var foundations = ['text', 'background', 'shadow', 'border'];
|
|
19
|
+
var hasFoundation = false;
|
|
20
|
+
return meta.filter(function (val) {
|
|
21
|
+
if (!hasFoundation && foundations.includes(val)) {
|
|
22
|
+
hasFoundation = true;
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
if (hasFoundation && foundations.includes(val)) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
var REPLACEMENTS = {
|
|
32
|
+
':': '',
|
|
33
|
+
',': '',
|
|
34
|
+
texts: 'text',
|
|
35
|
+
error: 'danger',
|
|
36
|
+
invalid: 'danger',
|
|
37
|
+
removed: 'danger',
|
|
38
|
+
removal: 'danger',
|
|
39
|
+
remove: 'danger',
|
|
40
|
+
focus: 'focused',
|
|
41
|
+
valid: 'success',
|
|
42
|
+
successful: 'success',
|
|
43
|
+
risk: 'warning',
|
|
44
|
+
caution: 'warning',
|
|
45
|
+
warn: 'warning',
|
|
46
|
+
primary: 'bold',
|
|
47
|
+
info: 'bold',
|
|
48
|
+
secondary: 'subtle',
|
|
49
|
+
hyperlink: 'link',
|
|
50
|
+
anchor: 'link',
|
|
51
|
+
active: 'pressed',
|
|
52
|
+
hover: 'hovered',
|
|
53
|
+
dragged: 'overlay',
|
|
54
|
+
dragging: 'overlay',
|
|
55
|
+
drag: 'overlay',
|
|
56
|
+
'background-color': 'background',
|
|
57
|
+
color: 'text',
|
|
58
|
+
icons: 'icon',
|
|
59
|
+
arrow: 'icon',
|
|
60
|
+
glyph: 'icon',
|
|
61
|
+
stroke: 'border',
|
|
62
|
+
'border-left': 'border',
|
|
63
|
+
'border-right': 'border',
|
|
64
|
+
'border-top': 'border',
|
|
65
|
+
'border-bottom': 'border',
|
|
66
|
+
'box-shadow': 'shadow'
|
|
67
|
+
};
|
|
68
|
+
var ADDITIONAL_META = {
|
|
69
|
+
grey: 'neutral',
|
|
70
|
+
red: 'danger'
|
|
71
|
+
};
|
|
72
|
+
export function cleanMeta(meta) {
|
|
73
|
+
var cleanMeta = meta.reduce(function (accum, val) {
|
|
74
|
+
return [].concat(_toConsumableArray(accum), _toConsumableArray(typeof val === 'string' ? val.split(/(?=[A-Z])/g).map(function (e) {
|
|
75
|
+
return e.toLowerCase();
|
|
76
|
+
}) : []));
|
|
77
|
+
}, []).reduce(function (accum, val) {
|
|
78
|
+
if (val in ADDITIONAL_META) {
|
|
79
|
+
accum.push(val, ADDITIONAL_META[val]);
|
|
80
|
+
} else {
|
|
81
|
+
accum.push(val);
|
|
82
|
+
}
|
|
83
|
+
accum.push(val in REPLACEMENTS ? REPLACEMENTS[val] : val);
|
|
84
|
+
return accum;
|
|
85
|
+
}, []).filter(function (val) {
|
|
86
|
+
return getUniqueWordsFromTokens.includes(val);
|
|
87
|
+
}).reduce(function (accum, val) {
|
|
88
|
+
if (!accum.includes(val)) {
|
|
89
|
+
accum.push(val);
|
|
90
|
+
}
|
|
91
|
+
return accum;
|
|
92
|
+
}, []);
|
|
93
|
+
return filterDuplciateFoundations(cleanMeta);
|
|
94
|
+
}
|
|
95
|
+
export function getBaseDeclarationMeta(decl) {
|
|
96
|
+
var parentSelectors = getParentSelectors(decl).split(/\-|\.|\,|\ |\:|\&/).filter(Boolean);
|
|
97
|
+
return [getPropertyMeta(decl.prop)].concat(_toConsumableArray(parentSelectors));
|
|
98
|
+
}
|
|
99
|
+
function getPropertyMeta(prop) {
|
|
100
|
+
if (prop === 'color') {
|
|
101
|
+
return 'text';
|
|
102
|
+
}
|
|
103
|
+
if (prop.startsWith('background')) {
|
|
104
|
+
return 'background';
|
|
105
|
+
}
|
|
106
|
+
if (prop.includes('shadow')) {
|
|
107
|
+
return 'shadow';
|
|
108
|
+
}
|
|
109
|
+
if (prop.includes('border')) {
|
|
110
|
+
return 'border';
|
|
111
|
+
}
|
|
112
|
+
return '';
|
|
113
|
+
}
|
|
114
|
+
function isRule(node) {
|
|
115
|
+
return node.type === 'rule';
|
|
116
|
+
}
|
|
117
|
+
function getParentSelectors(node) {
|
|
118
|
+
if (isRule(node)) {
|
|
119
|
+
// @ts-expect-error
|
|
120
|
+
return getParentSelectors(node.parent) + ' ' + node.selector;
|
|
121
|
+
}
|
|
122
|
+
if (node.parent) {
|
|
123
|
+
return getParentSelectors(node.parent);
|
|
124
|
+
}
|
|
125
|
+
return '';
|
|
126
|
+
}
|
|
127
|
+
export function getCssVarMeta(cssVariable) {
|
|
128
|
+
var tokenName = extractCssVarName(cssVariable);
|
|
129
|
+
var meta = knownVariables[tokenName];
|
|
130
|
+
if (!meta || meta.length === 0) {
|
|
131
|
+
return tokenName.split('-');
|
|
132
|
+
}
|
|
133
|
+
return meta;
|
|
134
|
+
}
|
|
135
|
+
export function getRawColorMeta(rawColor) {
|
|
136
|
+
var _knownRawColors$clean;
|
|
137
|
+
var cleanColor = rawColor.toLowerCase();
|
|
138
|
+
if (cleanColor.length === 4) {
|
|
139
|
+
cleanColor = cleanColor + cleanColor.substring(cleanColor.indexOf('#') + 1);
|
|
140
|
+
}
|
|
141
|
+
return (_knownRawColors$clean = knownRawColors[cleanColor]) !== null && _knownRawColors$clean !== void 0 ? _knownRawColors$clean : [];
|
|
142
|
+
}
|
|
143
|
+
export function getNamedColorMeta(namedColor) {
|
|
144
|
+
var _knownNamedColors$nam;
|
|
145
|
+
return (_knownNamedColors$nam = knownNamedColors[namedColor]) !== null && _knownNamedColors$nam !== void 0 ? _knownNamedColors$nam : [];
|
|
146
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
var MISSING_TOKEN_NAME = 'MISSING_TOKEN';
|
|
6
|
+
var ACTIVE_TOKENS = rawTokens.filter(function (token) {
|
|
7
|
+
return token.attributes.state === 'active';
|
|
8
|
+
}).map(function (token) {
|
|
9
|
+
return token.name.replace(/\.\[default\]/g, '');
|
|
10
|
+
}).filter(function (token) {
|
|
11
|
+
return !token.includes('UNSAFE') && !token.includes('interaction');
|
|
12
|
+
});
|
|
13
|
+
var COLOR_TOKEN_SEARCH_TYPES = ['text', 'link', 'icon', 'border', 'background', 'blanket', 'skeleton', 'chart', 'surface', 'shadow'];
|
|
14
|
+
var createFuzzySearch = function createFuzzySearch(tokens) {
|
|
15
|
+
return Search(tokens, false);
|
|
16
|
+
};
|
|
17
|
+
function filterTokens(meta) {
|
|
18
|
+
var commonSearchTypes = COLOR_TOKEN_SEARCH_TYPES.filter(function (type) {
|
|
19
|
+
return meta.includes(type);
|
|
20
|
+
});
|
|
21
|
+
return commonSearchTypes.length !== 0 ? ACTIVE_TOKENS.filter(function (token) {
|
|
22
|
+
return commonSearchTypes.some(function (type) {
|
|
23
|
+
return token.startsWith("color.".concat(type)) || token.startsWith("elevation.".concat(type));
|
|
24
|
+
});
|
|
25
|
+
}) : ACTIVE_TOKENS;
|
|
26
|
+
}
|
|
27
|
+
export default function findToken(meta) {
|
|
28
|
+
var filteredTokens = filterTokens(meta);
|
|
29
|
+
var tokenFuzzySearch = createFuzzySearch(filteredTokens);
|
|
30
|
+
var cleanSearchTerms = cleanMeta(meta).join(' ');
|
|
31
|
+
var results = tokenFuzzySearch.get(cleanSearchTerms);
|
|
32
|
+
if (!results) {
|
|
33
|
+
return MISSING_TOKEN_NAME;
|
|
34
|
+
}
|
|
35
|
+
var candidates = results.map(function (result) {
|
|
36
|
+
return result[1];
|
|
37
|
+
});
|
|
38
|
+
var bestCandidate = designTokens[candidates[0]];
|
|
39
|
+
return bestCandidate;
|
|
40
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
+
import { extractBetweenParentheses, isGradient, isKnownCssVariable, isLessFunction, isNamedColor, isRawColor } from './colors';
|
|
3
|
+
import { extractCssVarName } from './declaration';
|
|
4
|
+
import { getCssVarMeta, getNamedColorMeta, getRawColorMeta } from './meta';
|
|
5
|
+
import findToken from './tokens';
|
|
6
|
+
import { logger } from './logger';
|
|
7
|
+
export default function parseValue(value) {
|
|
8
|
+
if (isLessFunction(value)) {
|
|
9
|
+
logger.warn('Cannot parse - less function detected, would cause compilation error');
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
if (isGradient(value)) {
|
|
13
|
+
return {
|
|
14
|
+
getReplacement: function getReplacement() {
|
|
15
|
+
var additionalMeta = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
16
|
+
var gradientContent = extractBetweenParentheses(value);
|
|
17
|
+
var values = gradientContent.split(',');
|
|
18
|
+
var replacedValues = values.map(function (val) {
|
|
19
|
+
var parsedValue = parseValue(val.trim());
|
|
20
|
+
return parsedValue ? parsedValue.getReplacement(additionalMeta) : val;
|
|
21
|
+
});
|
|
22
|
+
return value.replace(gradientContent, replacedValues.join(', '));
|
|
23
|
+
},
|
|
24
|
+
getMeta: function getMeta() {
|
|
25
|
+
var BASE_META = [];
|
|
26
|
+
var gradientContent = extractBetweenParentheses(value);
|
|
27
|
+
var values = gradientContent.split(',');
|
|
28
|
+
var metas = values.reduce(function (acc, val) {
|
|
29
|
+
var parsedValue = parseValue(val.trim());
|
|
30
|
+
return parsedValue ? [].concat(_toConsumableArray(acc), _toConsumableArray(parsedValue.getMeta())) : acc;
|
|
31
|
+
}, BASE_META);
|
|
32
|
+
return metas;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if (isKnownCssVariable(extractCssVarName(value))) {
|
|
37
|
+
return {
|
|
38
|
+
getReplacement: function getReplacement() {
|
|
39
|
+
var additionalMeta = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
40
|
+
var token = findToken([].concat(_toConsumableArray(additionalMeta), _toConsumableArray(this.getMeta())));
|
|
41
|
+
return "var(".concat(token, ", ").concat(value, ")");
|
|
42
|
+
},
|
|
43
|
+
getMeta: function getMeta() {
|
|
44
|
+
return getCssVarMeta(value);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (isRawColor(value)) {
|
|
49
|
+
return {
|
|
50
|
+
getReplacement: function getReplacement() {
|
|
51
|
+
var additionalMeta = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
52
|
+
var token = findToken([].concat(_toConsumableArray(additionalMeta), _toConsumableArray(this.getMeta())));
|
|
53
|
+
return "var(".concat(token, ", ").concat(value, ")");
|
|
54
|
+
},
|
|
55
|
+
getMeta: function getMeta() {
|
|
56
|
+
return getRawColorMeta(value);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (isNamedColor(value)) {
|
|
61
|
+
return {
|
|
62
|
+
getReplacement: function getReplacement() {
|
|
63
|
+
var additionalMeta = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
64
|
+
var token = findToken([].concat(_toConsumableArray(additionalMeta), _toConsumableArray(this.getMeta())));
|
|
65
|
+
return "var(".concat(token, ", ").concat(value, ")");
|
|
66
|
+
},
|
|
67
|
+
getMeta: function getMeta() {
|
|
68
|
+
return getNamedColorMeta(value);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|