@atlaskit/codemod-cli 0.11.5 → 0.12.1
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 +16 -0
- package/dist/cjs/cli.js +75 -68
- package/dist/cjs/filepath.js +29 -69
- package/dist/cjs/index.js +3 -3
- package/dist/cjs/main.js +167 -337
- package/dist/cjs/presets/css-to-design-tokens/css-to-design-tokens.js +31 -73
- package/dist/cjs/presets/css-to-design-tokens/utils/legacy-colors.js +3 -3
- package/dist/cjs/presets/css-to-design-tokens/utils/meta.js +6 -19
- package/dist/cjs/presets/index.js +1 -3
- package/dist/cjs/presets/styled-to-emotion/styled-to-emotion.js +12 -19
- package/dist/cjs/presets/theme-to-design-tokens/theme-to-design-tokens.js +32 -61
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast-meta.js +13 -26
- package/dist/cjs/presets/theme-to-design-tokens/utils/color.js +7 -11
- package/dist/cjs/presets/theme-to-design-tokens/utils/fuzzy-search.js +6 -10
- package/dist/cjs/presets/theme-to-design-tokens/utils/legacy-colors.js +3 -3
- package/dist/cjs/presets/theme-to-design-tokens/utils/named-colors.js +1 -1
- package/dist/cjs/presets/theme-to-design-tokens/utils/tokens.js +2 -16
- package/dist/cjs/sinceRef.js +35 -70
- package/dist/cjs/transforms.js +26 -44
- package/dist/cjs/types.js +3 -27
- package/dist/cjs/utils.js +6 -6
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/version.json +1 -1
- package/dist/esm/cli.js +53 -55
- package/dist/esm/filepath.js +50 -54
- package/dist/esm/main.js +220 -230
- package/dist/esm/presets/css-to-design-tokens/css-to-design-tokens.js +11 -13
- package/dist/esm/sinceRef.js +48 -50
- package/dist/esm/version.json +1 -1
- package/dist/types-ts4.0/cli.d.ts +1 -0
- package/dist/types-ts4.0/filepath.d.ts +2 -0
- package/dist/types-ts4.0/index.d.ts +4 -0
- package/dist/types-ts4.0/main.d.ts +19 -0
- package/dist/types-ts4.0/presets/css-to-design-tokens/css-to-design-tokens.d.ts +2 -0
- package/dist/types-ts4.0/presets/css-to-design-tokens/utils/legacy-colors.d.ts +3 -0
- package/dist/types-ts4.0/presets/css-to-design-tokens/utils/meta.d.ts +1 -0
- package/dist/types-ts4.0/presets/index.d.ts +9 -0
- package/dist/types-ts4.0/presets/styled-to-emotion/styled-to-emotion.d.ts +6 -0
- package/dist/types-ts4.0/presets/theme-to-design-tokens/theme-to-design-tokens.d.ts +2 -0
- package/dist/types-ts4.0/presets/theme-to-design-tokens/utils/ast-meta.d.ts +3 -0
- package/dist/types-ts4.0/presets/theme-to-design-tokens/utils/ast.d.ts +3 -0
- package/dist/types-ts4.0/presets/theme-to-design-tokens/utils/color.d.ts +4 -0
- package/dist/types-ts4.0/presets/theme-to-design-tokens/utils/fuzzy-search.d.ts +5 -0
- package/dist/types-ts4.0/presets/theme-to-design-tokens/utils/legacy-colors.d.ts +3 -0
- package/dist/types-ts4.0/presets/theme-to-design-tokens/utils/named-colors.d.ts +1 -0
- package/dist/types-ts4.0/presets/theme-to-design-tokens/utils/tokens.d.ts +2 -0
- package/dist/types-ts4.0/sinceRef.d.ts +5 -0
- package/dist/types-ts4.0/transforms.d.ts +10 -0
- package/dist/types-ts4.0/types.d.ts +59 -0
- package/dist/types-ts4.0/utils.d.ts +1 -0
- package/package.json +2 -2
|
@@ -5,10 +5,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.default = transformer;
|
|
8
|
-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
9
|
-
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
10
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
11
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
12
8
|
var _postcss = _interopRequireDefault(require("postcss"));
|
|
13
9
|
var _postcssLess = _interopRequireDefault(require("postcss-less"));
|
|
14
10
|
var _tokensRaw = require("@atlaskit/tokens/tokens-raw");
|
|
@@ -18,14 +14,8 @@ var _legacyColors = require("./utils/legacy-colors");
|
|
|
18
14
|
var _meta = require("./utils/meta");
|
|
19
15
|
// @ts-ignore
|
|
20
16
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}).map(function (t) {
|
|
24
|
-
return t.name.replace(/\.\[default\]/g, '');
|
|
25
|
-
}).filter(function (t) {
|
|
26
|
-
return !t.includes('UNSAFE') && !t.includes('interaction');
|
|
27
|
-
});
|
|
28
|
-
var search = (0, _fuzzySearch.default)(tokens, false);
|
|
17
|
+
const tokens = _tokensRaw.light.filter(t => t.attributes.state === 'active').map(t => t.name.replace(/\.\[default\]/g, '')).filter(t => !t.includes('UNSAFE') && !t.includes('interaction'));
|
|
18
|
+
const search = (0, _fuzzySearch.default)(tokens, false);
|
|
29
19
|
function isRule(node) {
|
|
30
20
|
return node.type === 'rule';
|
|
31
21
|
}
|
|
@@ -64,22 +54,17 @@ function getDeclarationMeta(decl) {
|
|
|
64
54
|
return '';
|
|
65
55
|
}
|
|
66
56
|
function isDesignToken(tokenName) {
|
|
67
|
-
return Boolean(Object.entries(_tokenNames.default).find(
|
|
68
|
-
var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
|
|
69
|
-
_ = _ref2[0],
|
|
70
|
-
value = _ref2[1];
|
|
71
|
-
return tokenName === value;
|
|
72
|
-
}));
|
|
57
|
+
return Boolean(Object.entries(_tokenNames.default).find(([_, value]) => tokenName === value));
|
|
73
58
|
}
|
|
74
59
|
function getMetaFromCssVar(tokenName) {
|
|
75
|
-
|
|
60
|
+
const meta = _legacyColors.knownVariables[tokenName];
|
|
76
61
|
if (!meta || meta.length === 0) {
|
|
77
62
|
return tokenName.split('-');
|
|
78
63
|
}
|
|
79
64
|
return meta;
|
|
80
65
|
}
|
|
81
66
|
function getMetaFromRawColor(rawColor) {
|
|
82
|
-
|
|
67
|
+
let cleanColor = rawColor.toLowerCase();
|
|
83
68
|
if (cleanColor.length === 4) {
|
|
84
69
|
cleanColor = cleanColor + cleanColor.substring(cleanColor.indexOf('#') + 1);
|
|
85
70
|
}
|
|
@@ -88,11 +73,11 @@ function getMetaFromRawColor(rawColor) {
|
|
|
88
73
|
|
|
89
74
|
// https://github.com/postcss/postcss/blob/main/docs/writing-a-plugin.md
|
|
90
75
|
// https://astexplorer.net/#/2uBU1BLuJ1
|
|
91
|
-
|
|
92
|
-
|
|
76
|
+
const plugin = () => {
|
|
77
|
+
const processed = Symbol('processed');
|
|
93
78
|
return {
|
|
94
79
|
postcssPlugin: 'UsingTokens',
|
|
95
|
-
Declaration:
|
|
80
|
+
Declaration: decl => {
|
|
96
81
|
// @ts-expect-error
|
|
97
82
|
if (decl[processed]) {
|
|
98
83
|
return;
|
|
@@ -100,61 +85,53 @@ var plugin = function plugin() {
|
|
|
100
85
|
if (!isColorProperty(decl.prop)) {
|
|
101
86
|
return;
|
|
102
87
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
var lessVarRe = /@[a-zA-Z0-9-]+/g;
|
|
109
|
-
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;
|
|
88
|
+
const searchTerms = [getDeclarationMeta(decl), ...getParentSelectors(decl).split(/\-|\.|\,|\ |\:|\&/).filter(el => !!el)];
|
|
89
|
+
let match;
|
|
90
|
+
const cssVarRe = /var\([^\)]+\)/g;
|
|
91
|
+
const lessVarRe = /@[a-zA-Z0-9-]+/g;
|
|
92
|
+
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;
|
|
110
93
|
|
|
111
94
|
// CSS variables
|
|
112
|
-
|
|
95
|
+
const cssVarMatch = decl.value.match(cssVarRe);
|
|
113
96
|
if (cssVarMatch) {
|
|
114
|
-
var _getMetaFromCssVar;
|
|
115
97
|
match = cssVarMatch[0];
|
|
116
98
|
if (isDesignToken(stripVar(match))) {
|
|
117
99
|
return;
|
|
118
100
|
}
|
|
119
|
-
searchTerms.push
|
|
101
|
+
searchTerms.push(...(getMetaFromCssVar(stripVar(match)) ?? []));
|
|
120
102
|
}
|
|
121
103
|
|
|
122
104
|
// Less variables
|
|
123
|
-
|
|
105
|
+
const lassVarMatch = decl.value.match(lessVarRe);
|
|
124
106
|
if (lassVarMatch) {
|
|
125
|
-
var _getMetaFromCssVar2;
|
|
126
107
|
match = lassVarMatch[0];
|
|
127
|
-
searchTerms.push
|
|
108
|
+
searchTerms.push(...(getMetaFromCssVar(`--${stripLessVar(match)}`) ?? []));
|
|
128
109
|
}
|
|
129
110
|
|
|
130
111
|
// Raw colors
|
|
131
|
-
|
|
112
|
+
const rawColorMatch = decl.value.match(rawColorRe);
|
|
132
113
|
if (rawColorMatch) {
|
|
133
|
-
var _getMetaFromRawColor;
|
|
134
114
|
match = rawColorMatch[0];
|
|
135
|
-
searchTerms.push
|
|
115
|
+
searchTerms.push(...(getMetaFromRawColor(match) ?? []));
|
|
136
116
|
}
|
|
137
117
|
|
|
138
118
|
// Named colors
|
|
139
119
|
if (_legacyColors.knownColors.hasOwnProperty(decl.value)) {
|
|
140
|
-
var _knownColors$decl$val;
|
|
141
120
|
match = decl.value;
|
|
142
|
-
searchTerms.push
|
|
121
|
+
searchTerms.push(...(_legacyColors.knownColors[decl.value.toLowerCase()] ?? []));
|
|
143
122
|
}
|
|
144
123
|
if (!match) {
|
|
145
|
-
console.warn(
|
|
124
|
+
console.warn(`Unable to find match for declaration: ${decl.prop}: ${decl.value}`);
|
|
146
125
|
return;
|
|
147
126
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
});
|
|
153
|
-
var replacement = candidates.length ? _tokenNames.default[candidates[0]] : 'MISSING_TOKEN';
|
|
127
|
+
const cleanSearchTerms = (0, _meta.cleanMeta)(searchTerms).join(' ');
|
|
128
|
+
const results = search.get(cleanSearchTerms);
|
|
129
|
+
const candidates = results.map(result => result[1]);
|
|
130
|
+
const replacement = candidates.length ? _tokenNames.default[candidates[0]] : 'MISSING_TOKEN';
|
|
154
131
|
if (decl.prop === 'box-shadow') {
|
|
155
|
-
decl.value =
|
|
132
|
+
decl.value = `var(${replacement}, ${decl.value})`;
|
|
156
133
|
} else {
|
|
157
|
-
decl.value = decl.value.split(match).join(
|
|
134
|
+
decl.value = decl.value.split(match).join(`var(${replacement}, ${match})`);
|
|
158
135
|
}
|
|
159
136
|
|
|
160
137
|
// @ts-expect-error
|
|
@@ -162,27 +139,8 @@ var plugin = function plugin() {
|
|
|
162
139
|
}
|
|
163
140
|
};
|
|
164
141
|
};
|
|
165
|
-
function transformer(
|
|
166
|
-
return
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
_transformer = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(file) {
|
|
170
|
-
return _regenerator.default.wrap(function _callee$(_context) {
|
|
171
|
-
while (1) {
|
|
172
|
-
switch (_context.prev = _context.next) {
|
|
173
|
-
case 0:
|
|
174
|
-
_context.next = 2;
|
|
175
|
-
return (0, _postcss.default)([plugin()]).process(file.source, {
|
|
176
|
-
syntax: _postcssLess.default
|
|
177
|
-
}).css;
|
|
178
|
-
case 2:
|
|
179
|
-
return _context.abrupt("return", _context.sent);
|
|
180
|
-
case 3:
|
|
181
|
-
case "end":
|
|
182
|
-
return _context.stop();
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}, _callee);
|
|
186
|
-
}));
|
|
187
|
-
return _transformer.apply(this, arguments);
|
|
142
|
+
async function transformer(file) {
|
|
143
|
+
return await (0, _postcss.default)([plugin()]).process(file.source, {
|
|
144
|
+
syntax: _postcssLess.default
|
|
145
|
+
}).css;
|
|
188
146
|
}
|
|
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.knownVariables = exports.knownRawColors = exports.knownColors = void 0;
|
|
7
|
-
|
|
7
|
+
const knownVariables = {
|
|
8
8
|
'--adg3-color-R50': ['danger'],
|
|
9
9
|
'--adg3-color-R75': ['danger'],
|
|
10
10
|
'--adg3-color-R100': ['danger'],
|
|
@@ -170,7 +170,7 @@ var knownVariables = {
|
|
|
170
170
|
'--jpo-border-secondary-color': ['border', 'subtle']
|
|
171
171
|
};
|
|
172
172
|
exports.knownVariables = knownVariables;
|
|
173
|
-
|
|
173
|
+
const knownColors = {
|
|
174
174
|
aliceblue: ['blue'],
|
|
175
175
|
antiquewhite: [],
|
|
176
176
|
aqua: ['teal'],
|
|
@@ -320,7 +320,7 @@ var knownColors = {
|
|
|
320
320
|
yellowgreen: ['yellow']
|
|
321
321
|
};
|
|
322
322
|
exports.knownColors = knownColors;
|
|
323
|
-
|
|
323
|
+
const knownRawColors = {
|
|
324
324
|
'#000000': ['text'],
|
|
325
325
|
'#cccccc': ['border'],
|
|
326
326
|
'#aaaaaa': ['border'],
|
|
@@ -5,24 +5,17 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.cleanMeta = cleanMeta;
|
|
8
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
8
|
var _tokenNames = _interopRequireDefault(require("@atlaskit/tokens/token-names"));
|
|
10
|
-
|
|
11
|
-
return [].concat((0, _toConsumableArray2.default)(accum), (0, _toConsumableArray2.default)(val.split('.')));
|
|
12
|
-
}, []).reduce(function (accum, val) {
|
|
13
|
-
return [].concat((0, _toConsumableArray2.default)(accum), (0, _toConsumableArray2.default)(val.split(/(?=[A-Z])/g).map(function (e) {
|
|
14
|
-
return e.toLowerCase();
|
|
15
|
-
})));
|
|
16
|
-
}, []).reduce(function (accum, val) {
|
|
9
|
+
const getUniqueWordsFromTokens = Object.keys(_tokenNames.default).reduce((accum, val) => [...accum, ...val.split('.')], []).reduce((accum, val) => [...accum, ...val.split(/(?=[A-Z])/g).map(e => e.toLowerCase())], []).reduce((accum, val) => {
|
|
17
10
|
if (!accum.includes(val)) {
|
|
18
11
|
accum.push(val);
|
|
19
12
|
}
|
|
20
13
|
return accum;
|
|
21
14
|
}, []);
|
|
22
15
|
function filterDuplciateFoundations(meta) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return meta.filter(
|
|
16
|
+
const foundations = ['text', 'background', 'shadow', 'border'];
|
|
17
|
+
let hasFoundation = false;
|
|
18
|
+
return meta.filter(val => {
|
|
26
19
|
if (!hasFoundation && foundations.includes(val)) {
|
|
27
20
|
hasFoundation = true;
|
|
28
21
|
return true;
|
|
@@ -34,16 +27,10 @@ function filterDuplciateFoundations(meta) {
|
|
|
34
27
|
});
|
|
35
28
|
}
|
|
36
29
|
function cleanMeta(meta) {
|
|
37
|
-
|
|
38
|
-
return [].concat((0, _toConsumableArray2.default)(accum), (0, _toConsumableArray2.default)(typeof val === 'string' ? val.split(/(?=[A-Z])/g).map(function (e) {
|
|
39
|
-
return e.toLowerCase();
|
|
40
|
-
}) : []));
|
|
41
|
-
}, []).reduce(function (accum, val) {
|
|
30
|
+
const cleanMeta = meta.reduce((accum, val) => [...accum, ...(typeof val === 'string' ? val.split(/(?=[A-Z])/g).map(e => e.toLowerCase()) : [])], []).reduce((accum, val) => {
|
|
42
31
|
accum.push(val.replace(/:/g, '').replace(/,/g, '').replace('grey', 'neutral').replace('texts', 'text').replace('red', 'danger').replace('error', 'danger').replace('invalid', 'danger').replace('removed', 'danger').replace('removal', 'danger').replace('remove', 'danger').replace('focus', 'focused').replace('valid', 'success').replace('successful', 'success').replace('risk', 'warning').replace('caution', 'warning').replace('warn', 'warning').replace('primary', 'bold').replace('info', 'bold').replace('secondary', 'subtle').replace('hyperlink', 'link').replace('anchor', 'link').replace('active', 'pressed').replace('hover', 'hovered').replace('dragged', 'overlay').replace('dragging', 'overlay').replace('drag', 'overlay').replace('background-color', 'background').replace('color', 'text').replace('icons', 'icon').replace('arrow', 'icon').replace('glyph', 'icon').replace('stroke', 'border').replace('border-left', 'border').replace('border-right', 'border').replace('border-top', 'border').replace('border-bottom', 'border').replace('box-shadow', 'shadow'));
|
|
43
32
|
return accum;
|
|
44
|
-
}, []).filter(
|
|
45
|
-
return getUniqueWordsFromTokens.includes(val);
|
|
46
|
-
}).reduce(function (accum, val) {
|
|
33
|
+
}, []).filter(val => getUniqueWordsFromTokens.includes(val)).reduce((accum, val) => {
|
|
47
34
|
if (!accum.includes(val)) {
|
|
48
35
|
accum.push(val);
|
|
49
36
|
}
|
|
@@ -14,8 +14,6 @@ require("./css-to-design-tokens/css-to-design-tokens");
|
|
|
14
14
|
* in the final bundle
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
return _path.default.join(__dirname, preset, "".concat(preset, ".@(ts|js|tsx)"));
|
|
19
|
-
});
|
|
17
|
+
const presets = ['styled-to-emotion', 'theme-to-design-tokens', 'css-to-design-tokens'].map(preset => _path.default.join(__dirname, preset, `${preset}.@(ts|js|tsx)`));
|
|
20
18
|
var _default = presets;
|
|
21
19
|
exports.default = _default;
|
|
@@ -6,27 +6,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.default = transformer;
|
|
7
7
|
exports.parser = void 0;
|
|
8
8
|
function buildCoreImportDeclaration(j, path) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
j(path).find(j.ImportSpecifier).filter(
|
|
12
|
-
return coreExports.includes(specifier.value.imported.name);
|
|
13
|
-
}).forEach(function (specifier) {
|
|
9
|
+
const coreExports = ['css', 'keyframes'];
|
|
10
|
+
const specifiers = [];
|
|
11
|
+
j(path).find(j.ImportSpecifier).filter(specifier => coreExports.includes(specifier.value.imported.name)).forEach(specifier => {
|
|
14
12
|
specifiers.push(j.importSpecifier(j.identifier(specifier.value.imported.name)));
|
|
15
13
|
});
|
|
16
14
|
return specifiers.length ? j.importDeclaration(specifiers, j.literal('@emotion/core')) : null;
|
|
17
15
|
}
|
|
18
16
|
function buildStyledImportDeclaration(j, path) {
|
|
19
|
-
|
|
20
|
-
return specifier.value.local.name === 'styled';
|
|
21
|
-
});
|
|
17
|
+
const specifier = j(path).find(j.ImportDefaultSpecifier).filter(specifier => specifier.value.local.name === 'styled');
|
|
22
18
|
return specifier && specifier.length ? j.importDeclaration([j.importDefaultSpecifier(j.identifier(specifier.get(0).node.local.name))], j.literal('@emotion/styled')) : null;
|
|
23
19
|
}
|
|
24
20
|
function buildThemingImportDeclaration(j, path) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
j(path).find(j.ImportSpecifier).filter(
|
|
28
|
-
return themingExports.includes(specifier.value.imported.name);
|
|
29
|
-
}).forEach(function (specifier) {
|
|
21
|
+
const themingExports = ['ThemeProvider', 'withTheme'];
|
|
22
|
+
const specifiers = [];
|
|
23
|
+
j(path).find(j.ImportSpecifier).filter(specifier => themingExports.includes(specifier.value.imported.name)).forEach(specifier => {
|
|
30
24
|
specifiers.push(j.importSpecifier(j.identifier(specifier.value.imported.name)));
|
|
31
25
|
});
|
|
32
26
|
return specifiers && specifiers.length ? j.importDeclaration(specifiers, j.literal('emotion-theming')) : null;
|
|
@@ -35,16 +29,15 @@ function buildThemingImportDeclaration(j, path) {
|
|
|
35
29
|
/**
|
|
36
30
|
* Converts all imports of `styled-components` to `@emotion/styled`
|
|
37
31
|
*/
|
|
38
|
-
function transformer(fileInfo,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}).forEach(function (path) {
|
|
32
|
+
function transformer(fileInfo, {
|
|
33
|
+
jscodeshift: j
|
|
34
|
+
}, options) {
|
|
35
|
+
const source = j(fileInfo.source).find(j.ImportDeclaration).filter(path => path.node.source.value === 'styled-components').forEach(path => {
|
|
43
36
|
j(path).replaceWith([buildCoreImportDeclaration(j, path), buildStyledImportDeclaration(j, path), buildThemingImportDeclaration(j, path)]);
|
|
44
37
|
}).toSource(options.printOptions || {
|
|
45
38
|
quote: 'single'
|
|
46
39
|
});
|
|
47
40
|
return source;
|
|
48
41
|
}
|
|
49
|
-
|
|
42
|
+
const parser = 'tsx';
|
|
50
43
|
exports.parser = parser;
|
|
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.default = transformer;
|
|
8
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
8
|
var _ast = require("./utils/ast");
|
|
10
9
|
var _astMeta = require("./utils/ast-meta");
|
|
11
10
|
var _color = require("./utils/color");
|
|
@@ -14,16 +13,12 @@ var _legacyColors = require("./utils/legacy-colors");
|
|
|
14
13
|
var _tokens = require("./utils/tokens");
|
|
15
14
|
/* eslint-disable no-console */
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
const search = (0, _fuzzySearch.default)(_tokens.tokens, false);
|
|
18
17
|
function hasImportDeclaration(j, source, sourcePath) {
|
|
19
|
-
return !!source.find(j.ImportDeclaration).filter(
|
|
20
|
-
return path.node.source.value === sourcePath;
|
|
21
|
-
}).length;
|
|
18
|
+
return !!source.find(j.ImportDeclaration).filter(path => path.node.source.value === sourcePath).length;
|
|
22
19
|
}
|
|
23
20
|
function hasImportSpecifier(j, source, specifier, sourcePath) {
|
|
24
|
-
return !!source.find(j.ImportDeclaration).filter(
|
|
25
|
-
return path.node.source.value === sourcePath;
|
|
26
|
-
}).find(j.ImportSpecifier, {
|
|
21
|
+
return !!source.find(j.ImportDeclaration).filter(path => path.node.source.value === sourcePath).find(j.ImportSpecifier, {
|
|
27
22
|
local: {
|
|
28
23
|
name: specifier
|
|
29
24
|
}
|
|
@@ -33,89 +28,65 @@ function insertTokenImport(j, source) {
|
|
|
33
28
|
if (hasImportDeclaration(j, source, '@atlaskit/tokens')) {
|
|
34
29
|
return;
|
|
35
30
|
}
|
|
36
|
-
|
|
31
|
+
const newImport = j.importDeclaration([j.importSpecifier(j.identifier('token'))], j.stringLiteral('@atlaskit/tokens'));
|
|
37
32
|
source.get().node.program.body.unshift(newImport);
|
|
38
33
|
}
|
|
39
34
|
function buildToken(j, tokenId, node) {
|
|
40
|
-
|
|
35
|
+
const callExpr = j.callExpression(j.identifier('token'), [j.stringLiteral(tokenId), node].filter(Boolean));
|
|
41
36
|
return callExpr;
|
|
42
37
|
}
|
|
43
|
-
function getTokenFromNode(j, path) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
var tokenId = ['MISSING_TOKEN'];
|
|
38
|
+
function getTokenFromNode(j, path, baseMeta = []) {
|
|
39
|
+
const foundMeta = (0, _astMeta.getMetaFromAncestors)(j, path);
|
|
40
|
+
const meta = (0, _astMeta.cleanMeta)([...foundMeta, ...baseMeta]);
|
|
41
|
+
const results = search.get(meta.join(' '));
|
|
42
|
+
let tokenId = ['MISSING_TOKEN'];
|
|
49
43
|
if (results) {
|
|
50
|
-
tokenId = results.map(
|
|
51
|
-
return result[1];
|
|
52
|
-
});
|
|
44
|
+
tokenId = results.map(result => result[1]);
|
|
53
45
|
}
|
|
54
46
|
return tokenId[0];
|
|
55
47
|
}
|
|
56
|
-
function transformer(file, api) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
var transformed = false;
|
|
48
|
+
function transformer(file, api, debug = false) {
|
|
49
|
+
const j = api.jscodeshift;
|
|
50
|
+
const source = j(file.source);
|
|
51
|
+
let transformed = false;
|
|
61
52
|
source
|
|
62
53
|
// Handle colors.N100
|
|
63
|
-
.find(j.MemberExpression).filter(
|
|
54
|
+
.find(j.MemberExpression).filter(path => {
|
|
64
55
|
return path.value.object.type === 'Identifier' && path.value.object.name === 'colors' && path.value.property.type === 'Identifier' && (0, _color.isLegacyColor)(path.value.property.name);
|
|
65
|
-
}).filter(
|
|
66
|
-
return !(0, _ast.isDecendantOfToken)(j, path);
|
|
67
|
-
}).forEach(function (path) {
|
|
56
|
+
}).filter(path => !(0, _ast.isDecendantOfToken)(j, path)).forEach(path => {
|
|
68
57
|
debug && console.log('file:', file.path);
|
|
69
58
|
insertTokenImport(j, source);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
59
|
+
const key = path.value.property.type === 'Identifier' ? path.value.property.name : undefined;
|
|
60
|
+
const colorMeta = _legacyColors.legacyColorMetaMap[key] || [];
|
|
61
|
+
const tokenId = getTokenFromNode(j, path, colorMeta);
|
|
73
62
|
j(path).replaceWith(buildToken(j, tokenId, path.value));
|
|
74
63
|
transformed = true;
|
|
75
64
|
});
|
|
76
|
-
source.find(j.ObjectProperty).filter(
|
|
77
|
-
|
|
78
|
-
}).filter(function (path) {
|
|
79
|
-
return hasImportSpecifier(j, source, path.value.value.type === 'Identifier' ? path.value.value.name : '', '@atlaskit/theme') || hasImportSpecifier(j, source, path.value.value.type === 'Identifier' ? path.value.value.name : '', '@atlaskit/theme/colors');
|
|
80
|
-
}).filter(function (path) {
|
|
81
|
-
return !(0, _ast.isDecendantOfToken)(j, path.value.value);
|
|
82
|
-
}).forEach(function (path) {
|
|
83
|
-
var valuePath = path.get('value');
|
|
65
|
+
source.find(j.ObjectProperty).filter(path => path.value.value.type === 'Identifier' && ((0, _color.isLegacyColor)(path.value.value.name) || (0, _color.isLegacyNamedColor)(path.value.value.name))).filter(path => hasImportSpecifier(j, source, path.value.value.type === 'Identifier' ? path.value.value.name : '', '@atlaskit/theme') || hasImportSpecifier(j, source, path.value.value.type === 'Identifier' ? path.value.value.name : '', '@atlaskit/theme/colors')).filter(path => !(0, _ast.isDecendantOfToken)(j, path.value.value)).forEach(path => {
|
|
66
|
+
const valuePath = path.get('value');
|
|
84
67
|
debug && console.log('file:', file.path);
|
|
85
68
|
insertTokenImport(j, source);
|
|
86
|
-
|
|
87
|
-
|
|
69
|
+
const colorMeta = _legacyColors.legacyColorMetaMap[valuePath.name] || [];
|
|
70
|
+
const tokenId = getTokenFromNode(j, valuePath, colorMeta);
|
|
88
71
|
j(path).replaceWith(j.objectProperty(path.value.key, buildToken(j, tokenId, valuePath.value)));
|
|
89
72
|
transformed = true;
|
|
90
73
|
});
|
|
91
|
-
source.find(j.Identifier).filter(
|
|
92
|
-
return (0, _color.isLegacyColor)(path.value.name) || (0, _color.isLegacyNamedColor)(path.value.name);
|
|
93
|
-
}).filter(function (path) {
|
|
94
|
-
return hasImportSpecifier(j, source, path.value.name, '@atlaskit/theme') || hasImportSpecifier(j, source, path.value.name, '@atlaskit/theme/colors');
|
|
95
|
-
}).filter(function (path) {
|
|
96
|
-
return !['ImportSpecifier', 'MemberExpression', 'ObjectProperty'].includes(path.parentPath.value.type);
|
|
97
|
-
}).filter(function (path) {
|
|
98
|
-
return !(0, _ast.isDecendantOfToken)(j, path);
|
|
99
|
-
}).forEach(function (path) {
|
|
74
|
+
source.find(j.Identifier).filter(path => (0, _color.isLegacyColor)(path.value.name) || (0, _color.isLegacyNamedColor)(path.value.name)).filter(path => hasImportSpecifier(j, source, path.value.name, '@atlaskit/theme') || hasImportSpecifier(j, source, path.value.name, '@atlaskit/theme/colors')).filter(path => !['ImportSpecifier', 'MemberExpression', 'ObjectProperty'].includes(path.parentPath.value.type)).filter(path => !(0, _ast.isDecendantOfToken)(j, path)).forEach(path => {
|
|
100
75
|
debug && console.log('file:', file.path);
|
|
101
76
|
insertTokenImport(j, source);
|
|
102
|
-
|
|
103
|
-
|
|
77
|
+
const colorMeta = _legacyColors.legacyColorMetaMap[path.value.name] || [];
|
|
78
|
+
const tokenId = getTokenFromNode(j, path, colorMeta);
|
|
104
79
|
j(path).replaceWith(buildToken(j, tokenId, path.value));
|
|
105
80
|
transformed = true;
|
|
106
81
|
});
|
|
107
|
-
source.find(j.Literal).filter(
|
|
108
|
-
return typeof path.value.value === 'string' && ((0, _color.includesHardCodedColor)(path.value.value) || (0, _color.isHardCodedColor)(path.value.value));
|
|
109
|
-
}).filter(function (path) {
|
|
110
|
-
return !(0, _ast.isDecendantOfToken)(j, path);
|
|
111
|
-
}).forEach(function (path) {
|
|
82
|
+
source.find(j.Literal).filter(path => typeof path.value.value === 'string' && ((0, _color.includesHardCodedColor)(path.value.value) || (0, _color.isHardCodedColor)(path.value.value))).filter(path => !(0, _ast.isDecendantOfToken)(j, path)).forEach(path => {
|
|
112
83
|
var _path$value, _path$value$value;
|
|
113
84
|
debug && console.log('file:', file.path);
|
|
114
85
|
insertTokenImport(j, source);
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
86
|
+
const value = path === null || path === void 0 ? void 0 : (_path$value = path.value) === null || _path$value === void 0 ? void 0 : (_path$value$value = _path$value.value) === null || _path$value$value === void 0 ? void 0 : _path$value$value.toString();
|
|
87
|
+
const colorMeta = _legacyColors.legacyColorMetaMap[value] || [];
|
|
88
|
+
const tokenId = getTokenFromNode(j, path, colorMeta);
|
|
89
|
+
const tokenNode = buildToken(j, tokenId, path.value);
|
|
119
90
|
j(path).replaceWith((0, _ast.isDecendantOfType)(j, path, j.JSXAttribute) ? j.jsxExpressionContainer(tokenNode) : tokenNode);
|
|
120
91
|
transformed = true;
|
|
121
92
|
});
|
|
@@ -1,19 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
7
6
|
exports.cleanMeta = cleanMeta;
|
|
8
7
|
exports.getMetaFromAncestors = getMetaFromAncestors;
|
|
9
|
-
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
10
8
|
var _tokens = require("./tokens");
|
|
11
|
-
function getMetaFromAncestors(j, path) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
var grandParent = parent && parent.parentPath;
|
|
9
|
+
function getMetaFromAncestors(j, path, meta = []) {
|
|
10
|
+
const parent = path.parentPath;
|
|
11
|
+
const grandParent = parent && parent.parentPath;
|
|
15
12
|
if (parent && parent.value.type === 'ObjectProperty') {
|
|
16
|
-
|
|
13
|
+
let value = '';
|
|
17
14
|
if (parent.value.key.type === 'Literal' || parent.value.key.type === 'StringLiteral' || parent.value.key.type === 'NumericLiteral') {
|
|
18
15
|
value = parent.value.key.value.toString();
|
|
19
16
|
} else {
|
|
@@ -22,19 +19,13 @@ function getMetaFromAncestors(j, path) {
|
|
|
22
19
|
meta.push(value);
|
|
23
20
|
}
|
|
24
21
|
if (parent && grandParent && grandParent.value.type === 'TemplateLiteral') {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
var propertyName = (quasi.value.cooked || quasi.value.raw || '').replace(/\n/g, '').split(/;|{|}/).filter(function (el) {
|
|
30
|
-
return !el.match(/\.|\@|\(|\)/);
|
|
31
|
-
}).pop().split(/:/g)[0].trim();
|
|
32
|
-
grandParent.value.quasis.slice(0, expressionIndex + 1).map(function (q) {
|
|
33
|
-
return q.value.cooked;
|
|
34
|
-
})
|
|
22
|
+
const expressionIndex = grandParent.value.expressions.findIndex(exp => exp.name === path.value.name);
|
|
23
|
+
const quasi = grandParent.value.quasis[expressionIndex];
|
|
24
|
+
const propertyName = (quasi.value.cooked || quasi.value.raw || '').replace(/\n/g, '').split(/;|{|}/).filter(el => !el.match(/\.|\@|\(|\)/)).pop().split(/:/g)[0].trim();
|
|
25
|
+
grandParent.value.quasis.slice(0, expressionIndex + 1).map(q => q.value.cooked)
|
|
35
26
|
// We reverse so the most nested one is first which we're more likely than not interested in
|
|
36
|
-
.reverse().some(
|
|
37
|
-
|
|
27
|
+
.reverse().some(str => {
|
|
28
|
+
const result = /(hover|active|disabled|focus)/.exec(str.toLowerCase());
|
|
38
29
|
if (result) {
|
|
39
30
|
meta.push(result[0]);
|
|
40
31
|
return true;
|
|
@@ -56,16 +47,12 @@ function getMetaFromAncestors(j, path) {
|
|
|
56
47
|
return meta;
|
|
57
48
|
}
|
|
58
49
|
function cleanMeta(meta) {
|
|
59
|
-
return meta.reduce(
|
|
60
|
-
return [].concat((0, _toConsumableArray2.default)(accum), (0, _toConsumableArray2.default)(typeof val === 'string' ? val.split(/(?=[A-Z])/g).map(function (e) {
|
|
61
|
-
return e.toLowerCase();
|
|
62
|
-
}) : []));
|
|
63
|
-
}, []).reduce(function (accum, val) {
|
|
50
|
+
return meta.reduce((accum, val) => [...accum, ...(typeof val === 'string' ? val.split(/(?=[A-Z])/g).map(e => e.toLowerCase()) : [])], []).reduce((accum, val) => {
|
|
64
51
|
accum.push(val.replace(/:/g, '').replace(/,/g, '').replace('grey', 'neutral').replace('skeleton', 'neutral').replace('texts', 'text').replace('red', 'danger').replace('error', 'danger').replace('invalid', 'danger').replace('removed', 'danger').replace('removal', 'danger').replace('remove', 'danger').replace('focus', 'focused').replace('valid', 'success').replace('successful', 'success').replace('risk', 'warning').replace('caution', 'warning').replace('warn', 'warning').replace('primary', 'bold').replace('info', 'bold').replace('secondary', 'subtle').replace('hyperlink', 'link').replace('anchor', 'link').replace('active', 'pressed').replace('hover', 'hovered').replace('dragged', 'overlay').replace('dragging', 'overlay').replace('drag', 'overlay').replace('background-color', 'background').replace('color', 'text').replace('icons', 'icon').replace('glyph', 'icon').replace('stroke', 'border').replace('border-left', 'border').replace('border-right', 'border').replace('border-top', 'border').replace('border-bottom', 'border').replace('box-shadow', 'shadow'));
|
|
65
52
|
return accum;
|
|
66
|
-
}, []).filter(
|
|
53
|
+
}, []).filter(val => {
|
|
67
54
|
return _tokens.getUniqueWordsFromTokens.includes(val);
|
|
68
|
-
}).reduce(
|
|
55
|
+
}).reduce((accum, val) => {
|
|
69
56
|
if (!accum.includes(val)) {
|
|
70
57
|
accum.push(val);
|
|
71
58
|
}
|
|
@@ -6,28 +6,24 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.isLegacyNamedColor = exports.isLegacyColor = exports.isHardCodedColor = exports.includesHardCodedColor = void 0;
|
|
7
7
|
var _legacyColors = require("./legacy-colors");
|
|
8
8
|
var _namedColors = require("./named-colors");
|
|
9
|
-
|
|
10
|
-
return _legacyColors.legacyColors.includes(value);
|
|
11
|
-
};
|
|
9
|
+
const isLegacyColor = value => _legacyColors.legacyColors.includes(value);
|
|
12
10
|
exports.isLegacyColor = isLegacyColor;
|
|
13
|
-
|
|
14
|
-
return _legacyColors.legacyColorMixins.includes(value);
|
|
15
|
-
};
|
|
11
|
+
const isLegacyNamedColor = value => _legacyColors.legacyColorMixins.includes(value);
|
|
16
12
|
exports.isLegacyNamedColor = isLegacyNamedColor;
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
const includesHardCodedColor = raw => {
|
|
14
|
+
const value = raw.toLowerCase();
|
|
19
15
|
if (/#(?:[a-f0-9]{3}|[a-f0-9]{6}|[a-f0-9]{8})\b|(?:rgb|hsl)a?\([^\)]*\)/.exec(value.toLowerCase())) {
|
|
20
16
|
return true;
|
|
21
17
|
}
|
|
22
|
-
for (
|
|
23
|
-
if (value.includes(
|
|
18
|
+
for (let i = 0; i < _namedColors.namedColors.length; i++) {
|
|
19
|
+
if (value.includes(`${_namedColors.namedColors[i]};`)) {
|
|
24
20
|
return true;
|
|
25
21
|
}
|
|
26
22
|
}
|
|
27
23
|
return false;
|
|
28
24
|
};
|
|
29
25
|
exports.includesHardCodedColor = includesHardCodedColor;
|
|
30
|
-
|
|
26
|
+
const isHardCodedColor = value => {
|
|
31
27
|
if (_namedColors.namedColors.includes(value.toLowerCase())) {
|
|
32
28
|
return true;
|
|
33
29
|
}
|