@atlaskit/codemod-cli 0.13.3 → 0.13.4
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 +6 -0
- package/README.md +117 -3
- package/dist/cjs/cli.js +70 -75
- package/dist/cjs/filepath.js +65 -29
- package/dist/cjs/index.js +3 -3
- package/dist/cjs/main.js +329 -167
- package/dist/cjs/presets/css-to-design-tokens/css-to-design-tokens.js +72 -31
- 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 +19 -6
- package/dist/cjs/presets/index.js +3 -1
- package/dist/cjs/presets/styled-to-emotion/styled-to-emotion.js +19 -12
- package/dist/cjs/presets/theme-remove-deprecated-mixins/theme-remove-deprecated-mixins.js +51 -36
- package/dist/cjs/presets/theme-remove-deprecated-mixins/utils/replacements.js +25 -25
- package/dist/cjs/presets/theme-to-design-tokens/theme-to-design-tokens.js +66 -46
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast-meta.js +33 -18
- package/dist/cjs/presets/theme-to-design-tokens/utils/color.js +11 -7
- package/dist/cjs/presets/theme-to-design-tokens/utils/fuzzy-search.js +10 -6
- 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 +16 -2
- package/dist/cjs/sinceRef.js +69 -35
- package/dist/cjs/transforms.js +44 -26
- package/dist/cjs/types.js +27 -3
- package/dist/cjs/utils.js +6 -6
- package/dist/es2019/cli.js +4 -0
- package/dist/es2019/main.js +2 -0
- package/dist/es2019/presets/css-to-design-tokens/css-to-design-tokens.js +1 -0
- package/dist/es2019/sinceRef.js +1 -0
- package/dist/esm/cli.js +4 -0
- package/dist/esm/main.js +3 -1
- package/dist/esm/presets/css-to-design-tokens/css-to-design-tokens.js +1 -0
- package/dist/esm/sinceRef.js +1 -0
- package/package.json +2 -2
- package/dist/cjs/version.json +0 -4
- package/dist/es2019/version.json +0 -4
- package/dist/esm/version.json +0 -4
|
@@ -5,6 +5,10 @@ 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"));
|
|
8
12
|
var _postcss = _interopRequireDefault(require("postcss"));
|
|
9
13
|
var _postcssLess = _interopRequireDefault(require("postcss-less"));
|
|
10
14
|
var _tokensRaw = require("@atlaskit/tokens/tokens-raw");
|
|
@@ -14,8 +18,14 @@ var _legacyColors = require("./utils/legacy-colors");
|
|
|
14
18
|
var _meta = require("./utils/meta");
|
|
15
19
|
// @ts-ignore
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
var tokens = _tokensRaw.light.filter(function (t) {
|
|
22
|
+
return t.attributes.state === 'active';
|
|
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);
|
|
19
29
|
function isRule(node) {
|
|
20
30
|
return node.type === 'rule';
|
|
21
31
|
}
|
|
@@ -54,17 +64,22 @@ function getDeclarationMeta(decl) {
|
|
|
54
64
|
return '';
|
|
55
65
|
}
|
|
56
66
|
function isDesignToken(tokenName) {
|
|
57
|
-
return Boolean(Object.entries(_tokenNames.default).find((
|
|
67
|
+
return Boolean(Object.entries(_tokenNames.default).find(function (_ref) {
|
|
68
|
+
var _ref2 = (0, _slicedToArray2.default)(_ref, 2),
|
|
69
|
+
_ = _ref2[0],
|
|
70
|
+
value = _ref2[1];
|
|
71
|
+
return tokenName === value;
|
|
72
|
+
}));
|
|
58
73
|
}
|
|
59
74
|
function getMetaFromCssVar(tokenName) {
|
|
60
|
-
|
|
75
|
+
var meta = _legacyColors.knownVariables[tokenName];
|
|
61
76
|
if (!meta || meta.length === 0) {
|
|
62
77
|
return tokenName.split('-');
|
|
63
78
|
}
|
|
64
79
|
return meta;
|
|
65
80
|
}
|
|
66
81
|
function getMetaFromRawColor(rawColor) {
|
|
67
|
-
|
|
82
|
+
var cleanColor = rawColor.toLowerCase();
|
|
68
83
|
if (cleanColor.length === 4) {
|
|
69
84
|
cleanColor = cleanColor + cleanColor.substring(cleanColor.indexOf('#') + 1);
|
|
70
85
|
}
|
|
@@ -73,11 +88,11 @@ function getMetaFromRawColor(rawColor) {
|
|
|
73
88
|
|
|
74
89
|
// https://github.com/postcss/postcss/blob/main/docs/writing-a-plugin.md
|
|
75
90
|
// https://astexplorer.net/#/2uBU1BLuJ1
|
|
76
|
-
|
|
77
|
-
|
|
91
|
+
var plugin = function plugin() {
|
|
92
|
+
var processed = Symbol('processed');
|
|
78
93
|
return {
|
|
79
94
|
postcssPlugin: 'UsingTokens',
|
|
80
|
-
Declaration: decl
|
|
95
|
+
Declaration: function Declaration(decl) {
|
|
81
96
|
// @ts-expect-error
|
|
82
97
|
if (decl[processed]) {
|
|
83
98
|
return;
|
|
@@ -85,53 +100,62 @@ const plugin = () => {
|
|
|
85
100
|
if (!isColorProperty(decl.prop)) {
|
|
86
101
|
return;
|
|
87
102
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
103
|
+
var searchTerms = [getDeclarationMeta(decl)].concat((0, _toConsumableArray2.default)(getParentSelectors(decl).split(/\-|\.|\,|\ |\:|\&/).filter(function (el) {
|
|
104
|
+
return !!el;
|
|
105
|
+
})));
|
|
106
|
+
var match;
|
|
107
|
+
var cssVarRe = /var\([^\)]+\)/g;
|
|
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;
|
|
93
110
|
|
|
94
111
|
// CSS variables
|
|
95
|
-
|
|
112
|
+
var cssVarMatch = decl.value.match(cssVarRe);
|
|
96
113
|
if (cssVarMatch) {
|
|
114
|
+
var _getMetaFromCssVar;
|
|
97
115
|
match = cssVarMatch[0];
|
|
98
116
|
if (isDesignToken(stripVar(match))) {
|
|
99
117
|
return;
|
|
100
118
|
}
|
|
101
|
-
searchTerms.push(
|
|
119
|
+
searchTerms.push.apply(searchTerms, (0, _toConsumableArray2.default)((_getMetaFromCssVar = getMetaFromCssVar(stripVar(match))) !== null && _getMetaFromCssVar !== void 0 ? _getMetaFromCssVar : []));
|
|
102
120
|
}
|
|
103
121
|
|
|
104
122
|
// Less variables
|
|
105
|
-
|
|
123
|
+
var lassVarMatch = decl.value.match(lessVarRe);
|
|
106
124
|
if (lassVarMatch) {
|
|
125
|
+
var _getMetaFromCssVar2;
|
|
107
126
|
match = lassVarMatch[0];
|
|
108
|
-
searchTerms.push(
|
|
127
|
+
searchTerms.push.apply(searchTerms, (0, _toConsumableArray2.default)((_getMetaFromCssVar2 = getMetaFromCssVar("--".concat(stripLessVar(match)))) !== null && _getMetaFromCssVar2 !== void 0 ? _getMetaFromCssVar2 : []));
|
|
109
128
|
}
|
|
110
129
|
|
|
111
130
|
// Raw colors
|
|
112
|
-
|
|
131
|
+
var rawColorMatch = decl.value.match(rawColorRe);
|
|
113
132
|
if (rawColorMatch) {
|
|
133
|
+
var _getMetaFromRawColor;
|
|
114
134
|
match = rawColorMatch[0];
|
|
115
|
-
searchTerms.push(
|
|
135
|
+
searchTerms.push.apply(searchTerms, (0, _toConsumableArray2.default)((_getMetaFromRawColor = getMetaFromRawColor(match)) !== null && _getMetaFromRawColor !== void 0 ? _getMetaFromRawColor : []));
|
|
116
136
|
}
|
|
117
137
|
|
|
118
138
|
// Named colors
|
|
119
139
|
if (_legacyColors.knownColors.hasOwnProperty(decl.value)) {
|
|
140
|
+
var _knownColors$decl$val;
|
|
120
141
|
match = decl.value;
|
|
121
|
-
searchTerms.push(
|
|
142
|
+
searchTerms.push.apply(searchTerms, (0, _toConsumableArray2.default)((_knownColors$decl$val = _legacyColors.knownColors[decl.value.toLowerCase()]) !== null && _knownColors$decl$val !== void 0 ? _knownColors$decl$val : []));
|
|
122
143
|
}
|
|
123
144
|
if (!match) {
|
|
124
|
-
|
|
145
|
+
// eslint-disable-next-line no-console
|
|
146
|
+
console.warn("Unable to find match for declaration: ".concat(decl.prop, ": ").concat(decl.value));
|
|
125
147
|
return;
|
|
126
148
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
149
|
+
var cleanSearchTerms = (0, _meta.cleanMeta)(searchTerms).join(' ');
|
|
150
|
+
var results = search.get(cleanSearchTerms);
|
|
151
|
+
var candidates = results.map(function (result) {
|
|
152
|
+
return result[1];
|
|
153
|
+
});
|
|
154
|
+
var replacement = candidates.length ? _tokenNames.default[candidates[0]] : 'MISSING_TOKEN';
|
|
131
155
|
if (decl.prop === 'box-shadow') {
|
|
132
|
-
decl.value =
|
|
156
|
+
decl.value = "var(".concat(replacement, ", ").concat(decl.value, ")");
|
|
133
157
|
} else {
|
|
134
|
-
decl.value = decl.value.split(match).join(
|
|
158
|
+
decl.value = decl.value.split(match).join("var(".concat(replacement, ", ").concat(match, ")"));
|
|
135
159
|
}
|
|
136
160
|
|
|
137
161
|
// @ts-expect-error
|
|
@@ -139,8 +163,25 @@ const plugin = () => {
|
|
|
139
163
|
}
|
|
140
164
|
};
|
|
141
165
|
};
|
|
142
|
-
|
|
143
|
-
return
|
|
144
|
-
|
|
145
|
-
|
|
166
|
+
function transformer(_x) {
|
|
167
|
+
return _transformer.apply(this, arguments);
|
|
168
|
+
}
|
|
169
|
+
function _transformer() {
|
|
170
|
+
_transformer = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(file) {
|
|
171
|
+
return _regenerator.default.wrap(function _callee$(_context) {
|
|
172
|
+
while (1) 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
|
+
}, _callee);
|
|
185
|
+
}));
|
|
186
|
+
return _transformer.apply(this, arguments);
|
|
146
187
|
}
|
|
@@ -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
|
+
var knownVariables = {
|
|
8
8
|
'--adg3-color-R50': ['danger'],
|
|
9
9
|
'--adg3-color-R75': ['danger'],
|
|
10
10
|
'--adg3-color-R100': ['danger'],
|
|
@@ -170,7 +170,7 @@ const knownVariables = {
|
|
|
170
170
|
'--jpo-border-secondary-color': ['border', 'subtle']
|
|
171
171
|
};
|
|
172
172
|
exports.knownVariables = knownVariables;
|
|
173
|
-
|
|
173
|
+
var knownColors = {
|
|
174
174
|
aliceblue: ['blue'],
|
|
175
175
|
antiquewhite: [],
|
|
176
176
|
aqua: ['teal'],
|
|
@@ -320,7 +320,7 @@ const knownColors = {
|
|
|
320
320
|
yellowgreen: ['yellow']
|
|
321
321
|
};
|
|
322
322
|
exports.knownColors = knownColors;
|
|
323
|
-
|
|
323
|
+
var knownRawColors = {
|
|
324
324
|
'#000000': ['text'],
|
|
325
325
|
'#cccccc': ['border'],
|
|
326
326
|
'#aaaaaa': ['border'],
|
|
@@ -5,17 +5,24 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
value: true
|
|
6
6
|
});
|
|
7
7
|
exports.cleanMeta = cleanMeta;
|
|
8
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
8
9
|
var _tokenNames = _interopRequireDefault(require("@atlaskit/tokens/token-names"));
|
|
9
|
-
|
|
10
|
+
var getUniqueWordsFromTokens = Object.keys(_tokenNames.default).reduce(function (accum, val) {
|
|
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) {
|
|
10
17
|
if (!accum.includes(val)) {
|
|
11
18
|
accum.push(val);
|
|
12
19
|
}
|
|
13
20
|
return accum;
|
|
14
21
|
}, []);
|
|
15
22
|
function filterDuplciateFoundations(meta) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return meta.filter(val
|
|
23
|
+
var foundations = ['text', 'background', 'shadow', 'border'];
|
|
24
|
+
var hasFoundation = false;
|
|
25
|
+
return meta.filter(function (val) {
|
|
19
26
|
if (!hasFoundation && foundations.includes(val)) {
|
|
20
27
|
hasFoundation = true;
|
|
21
28
|
return true;
|
|
@@ -27,10 +34,16 @@ function filterDuplciateFoundations(meta) {
|
|
|
27
34
|
});
|
|
28
35
|
}
|
|
29
36
|
function cleanMeta(meta) {
|
|
30
|
-
|
|
37
|
+
var cleanMeta = meta.reduce(function (accum, val) {
|
|
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) {
|
|
31
42
|
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'));
|
|
32
43
|
return accum;
|
|
33
|
-
}, []).filter(
|
|
44
|
+
}, []).filter(function (val) {
|
|
45
|
+
return getUniqueWordsFromTokens.includes(val);
|
|
46
|
+
}).reduce(function (accum, val) {
|
|
34
47
|
if (!accum.includes(val)) {
|
|
35
48
|
accum.push(val);
|
|
36
49
|
}
|
|
@@ -15,6 +15,8 @@ require("./css-to-design-tokens/css-to-design-tokens");
|
|
|
15
15
|
* in the final bundle
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
var presets = ['styled-to-emotion', 'theme-to-design-tokens', 'theme-remove-deprecated-mixins', 'css-to-design-tokens'].map(function (preset) {
|
|
19
|
+
return _path.default.join(__dirname, preset, "".concat(preset, ".@(ts|js|tsx)"));
|
|
20
|
+
});
|
|
19
21
|
var _default = presets;
|
|
20
22
|
exports.default = _default;
|
|
@@ -6,21 +6,27 @@ 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(
|
|
9
|
+
var coreExports = ['css', 'keyframes'];
|
|
10
|
+
var specifiers = [];
|
|
11
|
+
j(path).find(j.ImportSpecifier).filter(function (specifier) {
|
|
12
|
+
return coreExports.includes(specifier.value.imported.name);
|
|
13
|
+
}).forEach(function (specifier) {
|
|
12
14
|
specifiers.push(j.importSpecifier(j.identifier(specifier.value.imported.name)));
|
|
13
15
|
});
|
|
14
16
|
return specifiers.length ? j.importDeclaration(specifiers, j.literal('@emotion/core')) : null;
|
|
15
17
|
}
|
|
16
18
|
function buildStyledImportDeclaration(j, path) {
|
|
17
|
-
|
|
19
|
+
var specifier = j(path).find(j.ImportDefaultSpecifier).filter(function (specifier) {
|
|
20
|
+
return specifier.value.local.name === 'styled';
|
|
21
|
+
});
|
|
18
22
|
return specifier && specifier.length ? j.importDeclaration([j.importDefaultSpecifier(j.identifier(specifier.get(0).node.local.name))], j.literal('@emotion/styled')) : null;
|
|
19
23
|
}
|
|
20
24
|
function buildThemingImportDeclaration(j, path) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
j(path).find(j.ImportSpecifier).filter(
|
|
25
|
+
var themingExports = ['ThemeProvider', 'withTheme'];
|
|
26
|
+
var specifiers = [];
|
|
27
|
+
j(path).find(j.ImportSpecifier).filter(function (specifier) {
|
|
28
|
+
return themingExports.includes(specifier.value.imported.name);
|
|
29
|
+
}).forEach(function (specifier) {
|
|
24
30
|
specifiers.push(j.importSpecifier(j.identifier(specifier.value.imported.name)));
|
|
25
31
|
});
|
|
26
32
|
return specifiers && specifiers.length ? j.importDeclaration(specifiers, j.literal('emotion-theming')) : null;
|
|
@@ -29,15 +35,16 @@ function buildThemingImportDeclaration(j, path) {
|
|
|
29
35
|
/**
|
|
30
36
|
* Converts all imports of `styled-components` to `@emotion/styled`
|
|
31
37
|
*/
|
|
32
|
-
function transformer(fileInfo, {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
function transformer(fileInfo, _ref, options) {
|
|
39
|
+
var j = _ref.jscodeshift;
|
|
40
|
+
var source = j(fileInfo.source).find(j.ImportDeclaration).filter(function (path) {
|
|
41
|
+
return path.node.source.value === 'styled-components';
|
|
42
|
+
}).forEach(function (path) {
|
|
36
43
|
j(path).replaceWith([buildCoreImportDeclaration(j, path), buildStyledImportDeclaration(j, path), buildThemingImportDeclaration(j, path)]);
|
|
37
44
|
}).toSource(options.printOptions || {
|
|
38
45
|
quote: 'single'
|
|
39
46
|
});
|
|
40
47
|
return source;
|
|
41
48
|
}
|
|
42
|
-
|
|
49
|
+
var parser = 'tsx';
|
|
43
50
|
exports.parser = parser;
|
|
@@ -13,18 +13,18 @@ function insertTokenImport(j, source) {
|
|
|
13
13
|
if ((0, _utils.hasImportDeclaration)(j, source, '@atlaskit/tokens')) {
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
var newImport = j.importDeclaration([j.importSpecifier(j.identifier('token'))], j.stringLiteral('@atlaskit/tokens'));
|
|
17
17
|
source.get().node.program.body.unshift(newImport);
|
|
18
18
|
}
|
|
19
19
|
function insertThemedImport(j, source) {
|
|
20
20
|
if ((0, _utils.hasImportSpecifier)(j, source, 'themed', '@atlaskit/theme/components')) {
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
-
|
|
23
|
+
var newImport = j.importDeclaration([j.importSpecifier(j.identifier('themed'))], j.stringLiteral('@atlaskit/theme/components'));
|
|
24
24
|
source.get().node.program.body.unshift(newImport);
|
|
25
25
|
}
|
|
26
26
|
function buildToken(j, tokenId, node) {
|
|
27
|
-
|
|
27
|
+
var callExpr = j.callExpression(j.identifier('token'), [j.stringLiteral(tokenId), node].filter(Boolean));
|
|
28
28
|
return callExpr;
|
|
29
29
|
}
|
|
30
30
|
function isDecendantOfTokenMethod(j, path) {
|
|
@@ -34,36 +34,47 @@ function isDecendantOfTokenMethod(j, path) {
|
|
|
34
34
|
}
|
|
35
35
|
}).length > 0;
|
|
36
36
|
}
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
var isDeprecatedApi = function isDeprecatedApi(identifierName) {
|
|
38
|
+
return Object.keys(_replacements.colorReplacements).includes(identifierName);
|
|
39
|
+
};
|
|
40
|
+
function buildThemedNode(j) {
|
|
41
|
+
var tokenId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
42
|
+
var fallbackLight = arguments.length > 2 ? arguments[2] : undefined;
|
|
43
|
+
var fallbackDark = arguments.length > 3 ? arguments[3] : undefined;
|
|
39
44
|
return j.callExpression(j.identifier('themed'), [j.objectExpression([j.objectProperty(j.identifier('light'), tokenId ? buildToken(j, tokenId, fallbackLight) : fallbackLight), j.objectProperty(j.identifier('dark'), tokenId ? buildToken(j, tokenId, fallbackDark) : fallbackDark)])]);
|
|
40
45
|
}
|
|
41
46
|
function replaceIdentifiers(j, source, specifier) {
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
var importSpecifierLocal = specifier.value.local.name;
|
|
48
|
+
var importSpecifierImported = specifier.value.imported.name;
|
|
44
49
|
source.find(j.Identifier, {
|
|
45
50
|
name: importSpecifierLocal
|
|
46
|
-
}).filter(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
}).filter(function (identifier) {
|
|
52
|
+
return !isDecendantOfType(j, identifier, j.ImportDeclaration);
|
|
53
|
+
}).filter(function (identifier) {
|
|
54
|
+
return identifier.parent.value.type !== 'MemberExpression';
|
|
55
|
+
}).filter(function (identifier) {
|
|
56
|
+
return !['ArrowFunctionExpression', 'FunctionDeclaration', 'JSXAttribute', 'JSXOpeningElement', 'LogicalExpression', 'ObjectProperty', 'TSFunctionType', 'TSPropertySignature', 'VariableDeclarator'].includes(identifier.parent.value.type);
|
|
57
|
+
}).forEach(function (identifier) {
|
|
58
|
+
var replacement = _replacements.colorReplacements[importSpecifierImported];
|
|
59
|
+
var isDecendantOfToken = isDecendantOfTokenMethod(j, identifier);
|
|
60
|
+
var isDecendantOfCallExpression = identifier.parent.value.type === 'CallExpression';
|
|
61
|
+
var isDynamicReplacement = !isDecendantOfCallExpression || isDecendantOfCallExpression && identifier.parent.value.arguments.length > 0;
|
|
62
|
+
var tokenParentNode = isDecendantOfToken && j(identifier).closest(j.CallExpression, {
|
|
52
63
|
callee: {
|
|
53
64
|
name: 'token'
|
|
54
65
|
}
|
|
55
66
|
}).get();
|
|
56
|
-
|
|
67
|
+
var tokenId = isDecendantOfToken ? tokenParentNode.value.arguments[0].value : replacement.tokenId;
|
|
57
68
|
insertTokenImport(j, source);
|
|
58
69
|
(0, _utils.insertImportSpecifier)(j, source, j.importSpecifier(j.identifier(replacement.importSpecifiers[0])), '@atlaskit/theme/colors');
|
|
59
70
|
|
|
60
71
|
// Requires themed function
|
|
61
72
|
if (isDynamicReplacement) {
|
|
62
|
-
|
|
73
|
+
var themedNode = buildThemedNode(j, tokenId, j.identifier(replacement.importSpecifiers[0]), j.identifier(replacement.importSpecifiers[1]));
|
|
63
74
|
insertThemedImport(j, source);
|
|
64
75
|
(0, _utils.insertImportSpecifier)(j, source, j.importSpecifier(j.identifier(replacement.importSpecifiers[1])), '@atlaskit/theme/colors');
|
|
65
76
|
if (isDecendantOfToken && isDecendantOfCallExpression) {
|
|
66
|
-
|
|
77
|
+
var callExpression = identifier.parent;
|
|
67
78
|
tokenParentNode.replace(j.callExpression(themedNode, callExpression.value.arguments));
|
|
68
79
|
return;
|
|
69
80
|
}
|
|
@@ -89,26 +100,28 @@ function replaceMemberExpressions(j, source, specifier) {
|
|
|
89
100
|
object: {
|
|
90
101
|
name: specifier.value.local.name
|
|
91
102
|
}
|
|
92
|
-
}).find(j.Identifier).filter(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
103
|
+
}).find(j.Identifier).filter(function (identifier) {
|
|
104
|
+
return isDeprecatedApi(identifier.value.name);
|
|
105
|
+
}).forEach(function (identifier) {
|
|
106
|
+
var memberExpression = identifier.parent;
|
|
107
|
+
var replacement = _replacements.colorReplacements[identifier.value.name];
|
|
108
|
+
var isDecendantOfToken = isDecendantOfTokenMethod(j, identifier);
|
|
109
|
+
var isDecendantOfCallExpression = identifier.parent.parent.value.type === 'CallExpression';
|
|
110
|
+
var isDynamicReplacement = !isDecendantOfCallExpression || isDecendantOfCallExpression && identifier.parent.parent.value.arguments.length > 0;
|
|
111
|
+
var tokenParentNode = isDecendantOfToken && j(identifier).closest(j.CallExpression, {
|
|
99
112
|
callee: {
|
|
100
113
|
name: 'token'
|
|
101
114
|
}
|
|
102
115
|
}).get();
|
|
103
|
-
|
|
116
|
+
var tokenId = isDecendantOfToken ? tokenParentNode.value.arguments[0].value : replacement.tokenId;
|
|
104
117
|
insertTokenImport(j, source);
|
|
105
118
|
|
|
106
119
|
// Requires themed function
|
|
107
120
|
if (isDynamicReplacement) {
|
|
108
|
-
|
|
121
|
+
var themedNode = buildThemedNode(j, tokenId, j.memberExpression(j.identifier(memberExpression.value.object.name), j.identifier(replacement.importSpecifiers[0])), j.memberExpression(j.identifier(memberExpression.value.object.name), j.identifier(replacement.importSpecifiers[1])));
|
|
109
122
|
insertThemedImport(j, source);
|
|
110
123
|
if (isDecendantOfToken && isDecendantOfCallExpression) {
|
|
111
|
-
|
|
124
|
+
var callExpression = identifier.parent.parent;
|
|
112
125
|
tokenParentNode.replace(j.callExpression(themedNode, callExpression.value.arguments));
|
|
113
126
|
return;
|
|
114
127
|
}
|
|
@@ -130,32 +143,34 @@ function replaceMemberExpressions(j, source, specifier) {
|
|
|
130
143
|
});
|
|
131
144
|
}
|
|
132
145
|
function transformer(file, api) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
146
|
+
var j = api.jscodeshift;
|
|
147
|
+
var source = j(file.source);
|
|
148
|
+
var hasThemeImport = (0, _utils.hasImportDeclaration)(j, source, '@atlaskit/theme');
|
|
149
|
+
var hasThemeColorsImport = (0, _utils.hasImportDeclaration)(j, source, '@atlaskit/theme/colors');
|
|
137
150
|
if (!hasThemeImport && !hasThemeColorsImport) {
|
|
138
151
|
return file.source;
|
|
139
152
|
}
|
|
140
|
-
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme').find(j.ImportNamespaceSpecifier).forEach(specifier
|
|
153
|
+
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme').find(j.ImportNamespaceSpecifier).forEach(function (specifier) {
|
|
141
154
|
replaceMemberExpressions(j, source, specifier);
|
|
142
155
|
});
|
|
143
|
-
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme').find(j.ImportSpecifier).forEach(specifier
|
|
156
|
+
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme').find(j.ImportSpecifier).forEach(function (specifier) {
|
|
144
157
|
replaceMemberExpressions(j, source, specifier);
|
|
145
158
|
});
|
|
146
|
-
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme/colors').find(j.ImportNamespaceSpecifier).forEach(specifier
|
|
159
|
+
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme/colors').find(j.ImportNamespaceSpecifier).forEach(function (specifier) {
|
|
147
160
|
replaceMemberExpressions(j, source, specifier);
|
|
148
161
|
});
|
|
149
|
-
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme/colors').find(j.ImportSpecifier).filter(
|
|
162
|
+
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme/colors').find(j.ImportSpecifier).filter(function (specifier) {
|
|
163
|
+
return isDeprecatedApi(specifier.value.imported.name);
|
|
164
|
+
}).forEach(function (specifier) {
|
|
150
165
|
replaceIdentifiers(j, source, specifier);
|
|
151
166
|
}).remove();
|
|
152
167
|
|
|
153
168
|
// Clean-up empty imports
|
|
154
|
-
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme/colors').filter(importDec
|
|
169
|
+
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme/colors').filter(function (importDec) {
|
|
155
170
|
var _importDec$value$spec;
|
|
156
171
|
return !((_importDec$value$spec = importDec.value.specifiers) !== null && _importDec$value$spec !== void 0 && _importDec$value$spec.length);
|
|
157
172
|
}).remove();
|
|
158
|
-
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme').filter(importDec
|
|
173
|
+
(0, _utils.getImportDeclaration)(j, source, '@atlaskit/theme').filter(function (importDec) {
|
|
159
174
|
var _importDec$value$spec2;
|
|
160
175
|
return !((_importDec$value$spec2 = importDec.value.specifiers) !== null && _importDec$value$spec2 !== void 0 && _importDec$value$spec2.length);
|
|
161
176
|
}).remove();
|