@atlaskit/codemod-cli 0.11.4 → 0.12.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 +17 -0
- package/dist/cjs/cli.js +72 -75
- package/dist/cjs/filepath.js +29 -83
- package/dist/cjs/index.js +3 -6
- package/dist/cjs/main.js +165 -441
- package/dist/cjs/presets/css-to-design-tokens/css-to-design-tokens.js +44 -135
- package/dist/cjs/presets/css-to-design-tokens/utils/legacy-colors.js +3 -4
- package/dist/cjs/presets/css-to-design-tokens/utils/meta.js +6 -29
- package/dist/cjs/presets/index.js +2 -9
- package/dist/cjs/presets/styled-to-emotion/styled-to-emotion.js +13 -25
- package/dist/cjs/presets/theme-to-design-tokens/theme-to-design-tokens.js +34 -82
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast-meta.js +14 -41
- package/dist/cjs/presets/theme-to-design-tokens/utils/ast.js +0 -2
- package/dist/cjs/presets/theme-to-design-tokens/utils/color.js +9 -28
- package/dist/cjs/presets/theme-to-design-tokens/utils/fuzzy-search.js +44 -106
- 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 -22
- package/dist/cjs/sinceRef.js +36 -92
- package/dist/cjs/transforms.js +27 -72
- package/dist/cjs/types.js +2 -45
- package/dist/cjs/utils.js +7 -20
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/filepath.js +7 -5
- package/dist/es2019/main.js +17 -51
- package/dist/es2019/presets/css-to-design-tokens/css-to-design-tokens.js +19 -51
- package/dist/es2019/presets/css-to-design-tokens/utils/legacy-colors.js +0 -1
- package/dist/es2019/presets/css-to-design-tokens/utils/meta.js +0 -6
- package/dist/es2019/presets/index.js +1 -1
- package/dist/es2019/presets/styled-to-emotion/styled-to-emotion.js +1 -4
- package/dist/es2019/presets/theme-to-design-tokens/theme-to-design-tokens.js +4 -14
- package/dist/es2019/presets/theme-to-design-tokens/utils/ast-meta.js +2 -11
- package/dist/es2019/presets/theme-to-design-tokens/utils/color.js +2 -7
- package/dist/es2019/presets/theme-to-design-tokens/utils/fuzzy-search.js +38 -95
- package/dist/es2019/presets/theme-to-design-tokens/utils/tokens.js +0 -1
- package/dist/es2019/sinceRef.js +2 -11
- package/dist/es2019/transforms.js +3 -13
- package/dist/es2019/types.js +1 -0
- package/dist/es2019/utils.js +1 -12
- package/dist/es2019/version.json +1 -1
- package/dist/esm/cli.js +53 -57
- package/dist/esm/filepath.js +51 -63
- package/dist/esm/main.js +221 -322
- package/dist/esm/presets/css-to-design-tokens/css-to-design-tokens.js +33 -71
- package/dist/esm/presets/css-to-design-tokens/utils/legacy-colors.js +0 -1
- package/dist/esm/presets/css-to-design-tokens/utils/meta.js +0 -6
- package/dist/esm/presets/index.js +1 -1
- package/dist/esm/presets/styled-to-emotion/styled-to-emotion.js +1 -4
- package/dist/esm/presets/theme-to-design-tokens/theme-to-design-tokens.js +4 -15
- package/dist/esm/presets/theme-to-design-tokens/utils/ast-meta.js +2 -11
- package/dist/esm/presets/theme-to-design-tokens/utils/color.js +2 -7
- package/dist/esm/presets/theme-to-design-tokens/utils/fuzzy-search.js +38 -95
- package/dist/esm/presets/theme-to-design-tokens/utils/tokens.js +0 -1
- package/dist/esm/sinceRef.js +49 -65
- package/dist/esm/transforms.js +4 -14
- package/dist/esm/types.js +1 -11
- package/dist/esm/utils.js +1 -12
- package/dist/esm/version.json +1 -1
- package/package.json +2 -2
- package/tmp/api-report-tmp.d.ts +0 -66
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import postcss from 'postcss';
|
|
2
|
-
|
|
1
|
+
import postcss from 'postcss';
|
|
2
|
+
// @ts-ignore
|
|
3
3
|
import lessSyntax from 'postcss-less';
|
|
4
4
|
import { light as rawTokens } from '@atlaskit/tokens/tokens-raw';
|
|
5
5
|
import designTokens from '@atlaskit/tokens/token-names';
|
|
@@ -8,82 +8,63 @@ import { knownVariables, knownColors, knownRawColors } from './utils/legacy-colo
|
|
|
8
8
|
import { cleanMeta } from './utils/meta';
|
|
9
9
|
const tokens = rawTokens.filter(t => t.attributes.state === 'active').map(t => t.name.replace(/\.\[default\]/g, '')).filter(t => !t.includes('UNSAFE') && !t.includes('interaction'));
|
|
10
10
|
const search = Search(tokens, false);
|
|
11
|
-
|
|
12
11
|
function isRule(node) {
|
|
13
12
|
return node.type === 'rule';
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
function getParentSelectors(node) {
|
|
17
15
|
if (isRule(node)) {
|
|
18
16
|
// @ts-expect-error
|
|
19
17
|
return getParentSelectors(node.parent) + ' ' + node.selector;
|
|
20
18
|
}
|
|
21
|
-
|
|
22
19
|
if (node.parent) {
|
|
23
20
|
return getParentSelectors(node.parent);
|
|
24
21
|
}
|
|
25
|
-
|
|
26
22
|
return '';
|
|
27
23
|
}
|
|
28
|
-
|
|
29
24
|
function stripVar(prop) {
|
|
30
25
|
return prop.substring(prop.indexOf('(') + 1).split(/\,|\)/)[0];
|
|
31
26
|
}
|
|
32
|
-
|
|
33
27
|
function stripLessVar(prop) {
|
|
34
28
|
return prop.substring(1);
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
function isColorProperty(prop) {
|
|
38
31
|
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';
|
|
39
32
|
}
|
|
40
|
-
|
|
41
33
|
function getDeclarationMeta(decl) {
|
|
42
34
|
if (decl.prop === 'color') {
|
|
43
35
|
return 'text';
|
|
44
36
|
}
|
|
45
|
-
|
|
46
37
|
if (decl.prop.startsWith('background')) {
|
|
47
38
|
return 'background';
|
|
48
39
|
}
|
|
49
|
-
|
|
50
40
|
if (decl.prop.includes('shadow')) {
|
|
51
41
|
return 'shadow';
|
|
52
42
|
}
|
|
53
|
-
|
|
54
43
|
if (decl.prop.includes('border')) {
|
|
55
44
|
return 'border';
|
|
56
45
|
}
|
|
57
|
-
|
|
58
46
|
return '';
|
|
59
47
|
}
|
|
60
|
-
|
|
61
48
|
function isDesignToken(tokenName) {
|
|
62
49
|
return Boolean(Object.entries(designTokens).find(([_, value]) => tokenName === value));
|
|
63
50
|
}
|
|
64
|
-
|
|
65
51
|
function getMetaFromCssVar(tokenName) {
|
|
66
52
|
const meta = knownVariables[tokenName];
|
|
67
|
-
|
|
68
53
|
if (!meta || meta.length === 0) {
|
|
69
54
|
return tokenName.split('-');
|
|
70
55
|
}
|
|
71
|
-
|
|
72
56
|
return meta;
|
|
73
57
|
}
|
|
74
|
-
|
|
75
58
|
function getMetaFromRawColor(rawColor) {
|
|
76
59
|
let cleanColor = rawColor.toLowerCase();
|
|
77
|
-
|
|
78
60
|
if (cleanColor.length === 4) {
|
|
79
61
|
cleanColor = cleanColor + cleanColor.substring(cleanColor.indexOf('#') + 1);
|
|
80
62
|
}
|
|
81
|
-
|
|
82
63
|
return knownRawColors[cleanColor];
|
|
83
|
-
}
|
|
84
|
-
// https://astexplorer.net/#/2uBU1BLuJ1
|
|
85
|
-
|
|
64
|
+
}
|
|
86
65
|
|
|
66
|
+
// https://github.com/postcss/postcss/blob/main/docs/writing-a-plugin.md
|
|
67
|
+
// https://astexplorer.net/#/2uBU1BLuJ1
|
|
87
68
|
const plugin = () => {
|
|
88
69
|
const processed = Symbol('processed');
|
|
89
70
|
return {
|
|
@@ -93,80 +74,67 @@ const plugin = () => {
|
|
|
93
74
|
if (decl[processed]) {
|
|
94
75
|
return;
|
|
95
76
|
}
|
|
96
|
-
|
|
97
77
|
if (!isColorProperty(decl.prop)) {
|
|
98
78
|
return;
|
|
99
79
|
}
|
|
100
|
-
|
|
101
80
|
const searchTerms = [getDeclarationMeta(decl), ...getParentSelectors(decl).split(/\-|\.|\,|\ |\:|\&/).filter(el => !!el)];
|
|
102
81
|
let match;
|
|
103
82
|
const cssVarRe = /var\([^\)]+\)/g;
|
|
104
83
|
const lessVarRe = /@[a-zA-Z0-9-]+/g;
|
|
105
|
-
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;
|
|
84
|
+
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;
|
|
106
85
|
|
|
86
|
+
// CSS variables
|
|
107
87
|
const cssVarMatch = decl.value.match(cssVarRe);
|
|
108
|
-
|
|
109
88
|
if (cssVarMatch) {
|
|
110
89
|
var _getMetaFromCssVar;
|
|
111
|
-
|
|
112
90
|
match = cssVarMatch[0];
|
|
113
|
-
|
|
114
91
|
if (isDesignToken(stripVar(match))) {
|
|
115
92
|
return;
|
|
116
93
|
}
|
|
117
|
-
|
|
118
94
|
searchTerms.push(...((_getMetaFromCssVar = getMetaFromCssVar(stripVar(match))) !== null && _getMetaFromCssVar !== void 0 ? _getMetaFromCssVar : []));
|
|
119
|
-
}
|
|
120
|
-
|
|
95
|
+
}
|
|
121
96
|
|
|
97
|
+
// Less variables
|
|
122
98
|
const lassVarMatch = decl.value.match(lessVarRe);
|
|
123
|
-
|
|
124
99
|
if (lassVarMatch) {
|
|
125
100
|
var _getMetaFromCssVar2;
|
|
126
|
-
|
|
127
101
|
match = lassVarMatch[0];
|
|
128
102
|
searchTerms.push(...((_getMetaFromCssVar2 = getMetaFromCssVar(`--${stripLessVar(match)}`)) !== null && _getMetaFromCssVar2 !== void 0 ? _getMetaFromCssVar2 : []));
|
|
129
|
-
}
|
|
130
|
-
|
|
103
|
+
}
|
|
131
104
|
|
|
105
|
+
// Raw colors
|
|
132
106
|
const rawColorMatch = decl.value.match(rawColorRe);
|
|
133
|
-
|
|
134
107
|
if (rawColorMatch) {
|
|
135
108
|
var _getMetaFromRawColor;
|
|
136
|
-
|
|
137
109
|
match = rawColorMatch[0];
|
|
138
110
|
searchTerms.push(...((_getMetaFromRawColor = getMetaFromRawColor(match)) !== null && _getMetaFromRawColor !== void 0 ? _getMetaFromRawColor : []));
|
|
139
|
-
}
|
|
140
|
-
|
|
111
|
+
}
|
|
141
112
|
|
|
113
|
+
// Named colors
|
|
142
114
|
if (knownColors.hasOwnProperty(decl.value)) {
|
|
143
115
|
var _knownColors$decl$val;
|
|
144
|
-
|
|
145
116
|
match = decl.value;
|
|
146
117
|
searchTerms.push(...((_knownColors$decl$val = knownColors[decl.value.toLowerCase()]) !== null && _knownColors$decl$val !== void 0 ? _knownColors$decl$val : []));
|
|
147
118
|
}
|
|
148
|
-
|
|
149
119
|
if (!match) {
|
|
150
120
|
console.warn(`Unable to find match for declaration: ${decl.prop}: ${decl.value}`);
|
|
151
121
|
return;
|
|
152
122
|
}
|
|
153
|
-
|
|
154
123
|
const cleanSearchTerms = cleanMeta(searchTerms).join(' ');
|
|
155
124
|
const results = search.get(cleanSearchTerms);
|
|
156
|
-
const
|
|
157
|
-
|
|
125
|
+
const candidates = results.map(result => result[1]);
|
|
126
|
+
const replacement = candidates.length ? designTokens[candidates[0]] : 'MISSING_TOKEN';
|
|
158
127
|
if (decl.prop === 'box-shadow') {
|
|
159
|
-
decl.value = `var(${
|
|
128
|
+
decl.value = `var(${replacement}, ${decl.value})`;
|
|
160
129
|
} else {
|
|
161
|
-
decl.value = decl.value.split(match).join(`var(${
|
|
162
|
-
}
|
|
163
|
-
|
|
130
|
+
decl.value = decl.value.split(match).join(`var(${replacement}, ${match})`);
|
|
131
|
+
}
|
|
164
132
|
|
|
133
|
+
// @ts-expect-error
|
|
165
134
|
decl[processed] = true;
|
|
166
135
|
}
|
|
167
136
|
};
|
|
168
137
|
};
|
|
169
|
-
|
|
170
138
|
export default async function transformer(file) {
|
|
171
139
|
return await postcss([plugin()]).process(file.source, {
|
|
172
140
|
syntax: lessSyntax
|
|
@@ -150,7 +150,6 @@ export const knownVariables = {
|
|
|
150
150
|
'--adg3-color-green-light': ['accent', 'subtler', 'green'],
|
|
151
151
|
'--adg3-color-green-dark': ['accent', 'bolder', 'green'],
|
|
152
152
|
'--adg3-color-N20-transparent': [],
|
|
153
|
-
|
|
154
153
|
/* Alias variables */
|
|
155
154
|
'--jpo-theme-color': ['brand'],
|
|
156
155
|
'--jpo-text-default-color': ['text'],
|
|
@@ -3,10 +3,8 @@ const getUniqueWordsFromTokens = Object.keys(designTokens).reduce((accum, val) =
|
|
|
3
3
|
if (!accum.includes(val)) {
|
|
4
4
|
accum.push(val);
|
|
5
5
|
}
|
|
6
|
-
|
|
7
6
|
return accum;
|
|
8
7
|
}, []);
|
|
9
|
-
|
|
10
8
|
function filterDuplciateFoundations(meta) {
|
|
11
9
|
const foundations = ['text', 'background', 'shadow', 'border'];
|
|
12
10
|
let hasFoundation = false;
|
|
@@ -15,15 +13,12 @@ function filterDuplciateFoundations(meta) {
|
|
|
15
13
|
hasFoundation = true;
|
|
16
14
|
return true;
|
|
17
15
|
}
|
|
18
|
-
|
|
19
16
|
if (hasFoundation && foundations.includes(val)) {
|
|
20
17
|
return false;
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
return true;
|
|
24
20
|
});
|
|
25
21
|
}
|
|
26
|
-
|
|
27
22
|
export function cleanMeta(meta) {
|
|
28
23
|
const cleanMeta = meta.reduce((accum, val) => [...accum, ...(typeof val === 'string' ? val.split(/(?=[A-Z])/g).map(e => e.toLowerCase()) : [])], []).reduce((accum, val) => {
|
|
29
24
|
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,7 +27,6 @@ export function cleanMeta(meta) {
|
|
|
32
27
|
if (!accum.includes(val)) {
|
|
33
28
|
accum.push(val);
|
|
34
29
|
}
|
|
35
|
-
|
|
36
30
|
return accum;
|
|
37
31
|
}, []);
|
|
38
32
|
return filterDuplciateFoundations(cleanMeta);
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* Manually import presets to make sure typescript includes them
|
|
4
5
|
* in the final bundle
|
|
5
6
|
*/
|
|
6
|
-
|
|
7
7
|
import './styled-to-emotion/styled-to-emotion';
|
|
8
8
|
import './theme-to-design-tokens/theme-to-design-tokens';
|
|
9
9
|
import './css-to-design-tokens/css-to-design-tokens';
|
|
@@ -6,12 +6,10 @@ function buildCoreImportDeclaration(j, path) {
|
|
|
6
6
|
});
|
|
7
7
|
return specifiers.length ? j.importDeclaration(specifiers, j.literal('@emotion/core')) : null;
|
|
8
8
|
}
|
|
9
|
-
|
|
10
9
|
function buildStyledImportDeclaration(j, path) {
|
|
11
10
|
const specifier = j(path).find(j.ImportDefaultSpecifier).filter(specifier => specifier.value.local.name === 'styled');
|
|
12
11
|
return specifier && specifier.length ? j.importDeclaration([j.importDefaultSpecifier(j.identifier(specifier.get(0).node.local.name))], j.literal('@emotion/styled')) : null;
|
|
13
12
|
}
|
|
14
|
-
|
|
15
13
|
function buildThemingImportDeclaration(j, path) {
|
|
16
14
|
const themingExports = ['ThemeProvider', 'withTheme'];
|
|
17
15
|
const specifiers = [];
|
|
@@ -20,11 +18,10 @@ function buildThemingImportDeclaration(j, path) {
|
|
|
20
18
|
});
|
|
21
19
|
return specifiers && specifiers.length ? j.importDeclaration(specifiers, j.literal('emotion-theming')) : null;
|
|
22
20
|
}
|
|
21
|
+
|
|
23
22
|
/**
|
|
24
23
|
* Converts all imports of `styled-components` to `@emotion/styled`
|
|
25
24
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
25
|
export default function transformer(fileInfo, {
|
|
29
26
|
jscodeshift: j
|
|
30
27
|
}, options) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
|
+
|
|
2
3
|
import { isDecendantOfToken, isDecendantOfType } from './utils/ast';
|
|
3
4
|
import { cleanMeta, getMetaFromAncestors } from './utils/ast-meta';
|
|
4
5
|
import { includesHardCodedColor, isHardCodedColor, isLegacyColor, isLegacyNamedColor } from './utils/color';
|
|
@@ -6,11 +7,9 @@ import Search from './utils/fuzzy-search';
|
|
|
6
7
|
import { legacyColorMetaMap } from './utils/legacy-colors';
|
|
7
8
|
import { tokens } from './utils/tokens';
|
|
8
9
|
const search = Search(tokens, false);
|
|
9
|
-
|
|
10
10
|
function hasImportDeclaration(j, source, sourcePath) {
|
|
11
11
|
return !!source.find(j.ImportDeclaration).filter(path => path.node.source.value === sourcePath).length;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
13
|
function hasImportSpecifier(j, source, specifier, sourcePath) {
|
|
15
14
|
return !!source.find(j.ImportDeclaration).filter(path => path.node.source.value === sourcePath).find(j.ImportSpecifier, {
|
|
16
15
|
local: {
|
|
@@ -18,39 +17,33 @@ function hasImportSpecifier(j, source, specifier, sourcePath) {
|
|
|
18
17
|
}
|
|
19
18
|
}).length;
|
|
20
19
|
}
|
|
21
|
-
|
|
22
20
|
function insertTokenImport(j, source) {
|
|
23
21
|
if (hasImportDeclaration(j, source, '@atlaskit/tokens')) {
|
|
24
22
|
return;
|
|
25
23
|
}
|
|
26
|
-
|
|
27
24
|
const newImport = j.importDeclaration([j.importSpecifier(j.identifier('token'))], j.stringLiteral('@atlaskit/tokens'));
|
|
28
25
|
source.get().node.program.body.unshift(newImport);
|
|
29
26
|
}
|
|
30
|
-
|
|
31
27
|
function buildToken(j, tokenId, node) {
|
|
32
28
|
const callExpr = j.callExpression(j.identifier('token'), [j.stringLiteral(tokenId), node].filter(Boolean));
|
|
33
29
|
return callExpr;
|
|
34
30
|
}
|
|
35
|
-
|
|
36
31
|
function getTokenFromNode(j, path, baseMeta = []) {
|
|
37
32
|
const foundMeta = getMetaFromAncestors(j, path);
|
|
38
33
|
const meta = cleanMeta([...foundMeta, ...baseMeta]);
|
|
39
34
|
const results = search.get(meta.join(' '));
|
|
40
|
-
let tokenId = ['
|
|
41
|
-
|
|
35
|
+
let tokenId = ['MISSING_TOKEN'];
|
|
42
36
|
if (results) {
|
|
43
37
|
tokenId = results.map(result => result[1]);
|
|
44
38
|
}
|
|
45
|
-
|
|
46
39
|
return tokenId[0];
|
|
47
40
|
}
|
|
48
|
-
|
|
49
41
|
export default function transformer(file, api, debug = false) {
|
|
50
42
|
const j = api.jscodeshift;
|
|
51
43
|
const source = j(file.source);
|
|
52
44
|
let transformed = false;
|
|
53
|
-
source
|
|
45
|
+
source
|
|
46
|
+
// Handle colors.N100
|
|
54
47
|
.find(j.MemberExpression).filter(path => {
|
|
55
48
|
return path.value.object.type === 'Identifier' && path.value.object.name === 'colors' && path.value.property.type === 'Identifier' && isLegacyColor(path.value.property.name);
|
|
56
49
|
}).filter(path => !isDecendantOfToken(j, path)).forEach(path => {
|
|
@@ -81,7 +74,6 @@ export default function transformer(file, api, debug = false) {
|
|
|
81
74
|
});
|
|
82
75
|
source.find(j.Literal).filter(path => typeof path.value.value === 'string' && (includesHardCodedColor(path.value.value) || isHardCodedColor(path.value.value))).filter(path => !isDecendantOfToken(j, path)).forEach(path => {
|
|
83
76
|
var _path$value, _path$value$value;
|
|
84
|
-
|
|
85
77
|
debug && console.log('file:', file.path);
|
|
86
78
|
insertTokenImport(j, source);
|
|
87
79
|
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();
|
|
@@ -91,10 +83,8 @@ export default function transformer(file, api, debug = false) {
|
|
|
91
83
|
j(path).replaceWith(isDecendantOfType(j, path, j.JSXAttribute) ? j.jsxExpressionContainer(tokenNode) : tokenNode);
|
|
92
84
|
transformed = true;
|
|
93
85
|
});
|
|
94
|
-
|
|
95
86
|
if (transformed) {
|
|
96
87
|
return source.toSource();
|
|
97
88
|
}
|
|
98
|
-
|
|
99
89
|
return file.source;
|
|
100
90
|
}
|
|
@@ -2,27 +2,23 @@ import { getUniqueWordsFromTokens } from './tokens';
|
|
|
2
2
|
export function getMetaFromAncestors(j, path, meta = []) {
|
|
3
3
|
const parent = path.parentPath;
|
|
4
4
|
const grandParent = parent && parent.parentPath;
|
|
5
|
-
|
|
6
5
|
if (parent && parent.value.type === 'ObjectProperty') {
|
|
7
6
|
let value = '';
|
|
8
|
-
|
|
9
7
|
if (parent.value.key.type === 'Literal' || parent.value.key.type === 'StringLiteral' || parent.value.key.type === 'NumericLiteral') {
|
|
10
8
|
value = parent.value.key.value.toString();
|
|
11
9
|
} else {
|
|
12
10
|
value = parent.value.key.name;
|
|
13
11
|
}
|
|
14
|
-
|
|
15
12
|
meta.push(value);
|
|
16
13
|
}
|
|
17
|
-
|
|
18
14
|
if (parent && grandParent && grandParent.value.type === 'TemplateLiteral') {
|
|
19
15
|
const expressionIndex = grandParent.value.expressions.findIndex(exp => exp.name === path.value.name);
|
|
20
16
|
const quasi = grandParent.value.quasis[expressionIndex];
|
|
21
17
|
const propertyName = (quasi.value.cooked || quasi.value.raw || '').replace(/\n/g, '').split(/;|{|}/).filter(el => !el.match(/\.|\@|\(|\)/)).pop().split(/:/g)[0].trim();
|
|
22
|
-
grandParent.value.quasis.slice(0, expressionIndex + 1).map(q => q.value.cooked)
|
|
18
|
+
grandParent.value.quasis.slice(0, expressionIndex + 1).map(q => q.value.cooked)
|
|
19
|
+
// We reverse so the most nested one is first which we're more likely than not interested in
|
|
23
20
|
.reverse().some(str => {
|
|
24
21
|
const result = /(hover|active|disabled|focus)/.exec(str.toLowerCase());
|
|
25
|
-
|
|
26
22
|
if (result) {
|
|
27
23
|
meta.push(result[0]);
|
|
28
24
|
return true;
|
|
@@ -30,21 +26,17 @@ export function getMetaFromAncestors(j, path, meta = []) {
|
|
|
30
26
|
});
|
|
31
27
|
meta.push(propertyName);
|
|
32
28
|
}
|
|
33
|
-
|
|
34
29
|
if (parent && parent.value.type === 'JSXAttribute') {
|
|
35
30
|
if (!['css', 'styles', 'style'].includes(parent.value.name.name)) {
|
|
36
31
|
meta.push(parent.value.name.name);
|
|
37
32
|
}
|
|
38
33
|
}
|
|
39
|
-
|
|
40
34
|
if (parent && parent.value.type === 'VariableDeclarator') {
|
|
41
35
|
meta.push(parent.value.id.name);
|
|
42
36
|
}
|
|
43
|
-
|
|
44
37
|
if (parent) {
|
|
45
38
|
return getMetaFromAncestors(j, parent, meta);
|
|
46
39
|
}
|
|
47
|
-
|
|
48
40
|
return meta;
|
|
49
41
|
}
|
|
50
42
|
export function cleanMeta(meta) {
|
|
@@ -57,7 +49,6 @@ export function cleanMeta(meta) {
|
|
|
57
49
|
if (!accum.includes(val)) {
|
|
58
50
|
accum.push(val);
|
|
59
51
|
}
|
|
60
|
-
|
|
61
52
|
return accum;
|
|
62
53
|
}, []);
|
|
63
54
|
}
|
|
@@ -4,32 +4,27 @@ export const isLegacyColor = value => legacyColors.includes(value);
|
|
|
4
4
|
export const isLegacyNamedColor = value => legacyColorMixins.includes(value);
|
|
5
5
|
export const includesHardCodedColor = raw => {
|
|
6
6
|
const value = raw.toLowerCase();
|
|
7
|
-
|
|
8
7
|
if (/#(?:[a-f0-9]{3}|[a-f0-9]{6}|[a-f0-9]{8})\b|(?:rgb|hsl)a?\([^\)]*\)/.exec(value.toLowerCase())) {
|
|
9
8
|
return true;
|
|
10
9
|
}
|
|
11
|
-
|
|
12
10
|
for (let i = 0; i < namedColors.length; i++) {
|
|
13
11
|
if (value.includes(`${namedColors[i]};`)) {
|
|
14
12
|
return true;
|
|
15
13
|
}
|
|
16
14
|
}
|
|
17
|
-
|
|
18
15
|
return false;
|
|
19
16
|
};
|
|
20
17
|
export const isHardCodedColor = value => {
|
|
21
18
|
if (namedColors.includes(value.toLowerCase())) {
|
|
22
19
|
return true;
|
|
23
20
|
}
|
|
24
|
-
|
|
25
21
|
if (value.startsWith('rgb(') || value.startsWith('rgba(') || value.startsWith('hsl(') || value.startsWith('hsla(') || value.startsWith('lch(') || value.startsWith('lab(') || value.startsWith('color(')) {
|
|
26
22
|
return true;
|
|
27
23
|
}
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
if (value.startsWith('#') && (
|
|
25
|
+
// short hex, hex, or hex with alpha
|
|
30
26
|
value.length === 4 || value.length === 7 || value.length === 9)) {
|
|
31
27
|
return true;
|
|
32
28
|
}
|
|
33
|
-
|
|
34
29
|
return false;
|
|
35
30
|
};
|