@oceanbase/codemod 0.2.7 → 0.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. package/README.md +14 -5
  2. package/bin/cli.js +3 -3
  3. package/package.json +4 -4
  4. package/transforms/__testfixtures__/less-to-token/antd-v4-less-to-token.input.less +2 -1
  5. package/transforms/__testfixtures__/less-to-token/antd-v4-less-to-token.output.less +1 -0
  6. package/transforms/__testfixtures__/less-to-token/case-insensitive.input.less +13 -0
  7. package/transforms/__testfixtures__/less-to-token/case-insensitive.output.less +14 -0
  8. package/transforms/__testfixtures__/style-to-token/{static.input.js → block-statement.input.js} +0 -8
  9. package/transforms/__testfixtures__/style-to-token/{static.output.js → block-statement.output.js} +0 -8
  10. package/transforms/__testfixtures__/style-to-token/case-insensitive.input.js +10 -0
  11. package/transforms/__testfixtures__/style-to-token/case-insensitive.output.js +11 -0
  12. package/transforms/__testfixtures__/style-to-token/existed-useToken.input.js +15 -0
  13. package/transforms/__testfixtures__/style-to-token/existed-useToken.output.js +15 -0
  14. package/transforms/__testfixtures__/style-to-token/function-component.input.js +2 -1
  15. package/transforms/__testfixtures__/style-to-token/function-component.output.js +2 -1
  16. package/transforms/__testfixtures__/style-to-token/nested-block-statement.input.js +18 -0
  17. package/transforms/__testfixtures__/style-to-token/nested-block-statement.output.js +19 -0
  18. package/transforms/__testfixtures__/style-to-token/top-identifier.input.js +10 -0
  19. package/transforms/__testfixtures__/style-to-token/top-identifier.output.js +11 -0
  20. package/transforms/__tests__/less-to-token.test.ts +1 -1
  21. package/transforms/__tests__/style-to-token.test.ts +9 -1
  22. package/transforms/__tests__/token.test.ts +11 -0
  23. package/transforms/less-to-token.js +3 -3
  24. package/transforms/style-to-token.js +90 -42
  25. package/transforms/utils/token.js +60 -8
package/README.md CHANGED
@@ -14,7 +14,10 @@ Before run codemod scripts, you'd better make sure to commit your local git chan
14
14
 
15
15
  ```shell
16
16
  # Run directly through npx
17
+ # `src` is the target directory or file that you want to transform.
17
18
  npx -p @oceanbase/codemod codemod src
19
+ # options
20
+ # --disablePrettier // disable prettier
18
21
  ```
19
22
 
20
23
  ## Codemod scripts introduction
@@ -154,7 +157,7 @@ transform fixed style to antd v5 design token.
154
157
  return (
155
158
  - <div>
156
159
  - <Alert style={{ color: 'rgba(0, 0, 0, 0.85)', background: 'rgba(0, 0, 0,0.65)', backgroundColor: 'rgba(0,0,0,0.45)', border: '1px solid #d9d9d9' }} />
157
- - <Button style={{ color: '#1890ff', background: '#52c41a', backgroundColor: '#faad14', borderColor: '#ff4D4F' }}></Button>
160
+ - <Button style={{ color: '#1890ff', background: '#52c41a', backgroundColor: '#faad14', borderColor: '#ff4d4f' }}></Button>
158
161
  - </div>
159
162
  + (<div>
160
163
  + <Alert style={{ color: token.colorText, background: token.colorTextSecondary, backgroundColor: token.colorTextTertiary, border: `1px solid ${token.colorBorder}` }} />
@@ -183,11 +186,13 @@ export default Demo;
183
186
  return (
184
187
  - <div>
185
188
  - <Alert style={{ color: 'rgba(0, 0, 0, 0.85)', background: 'rgba(0, 0, 0,0.65)', backgroundColor: 'rgba(0,0,0,0.45)', border: '#d9d9d9' }} />
186
- - <Button style={{ color: '#1890ff', background: '#52c41a', backgroundColor: '#faad14', borderColor: '#ff4D4F' }}></Button>
189
+ - <Button style={{ color: '#1890ff', background: '#52c41a', backgroundColor: '#faad14', borderColor: '#ff4d4f' }}></Button>
190
+ - <div color="#fafafa" border="1px solid #fafafa" />
187
191
  - </div>
188
192
  + (<div>
189
193
  + <Alert style={{ color: token.colorText, background: token.colorTextSecondary, backgroundColor: token.colorTextTertiary, border: `1px solid ${token.colorBgLayout}` }} />
190
194
  + <Button style={{ color: token.colorInfo, background: token.colorSuccess, backgroundColor: token.colorWarning, borderColor: token.colorError }}></Button>
195
+ + <div color={token.colorBgLayout} border={`1px solid ${token.colorBgLayout}`} />
191
196
  + </div>)
192
197
  );
193
198
  }
@@ -200,11 +205,15 @@ export default Demo;
200
205
 
201
206
  ```diff
202
207
  + import { token } from '@oceanbase/design';
208
+ - const color = '#fafafa';
209
+ - const border = '1px solid #fafafa';
210
+ + const color = token.colorBgLayout;
211
+ + const border = `1px solid ${token.colorBgLayout}`;
203
212
  const colorMap = {
204
213
  - info: '#1890ff',
205
214
  - success: '#52c41a',
206
215
  - warning: '#faad14',
207
- - error: '#ff4D4F',
216
+ - error: '#ff4d4f',
208
217
  - border: '1px solid #d9d9d9',
209
218
  + info: token.colorInfo,
210
219
  + success: token.colorSuccess,
@@ -232,7 +241,7 @@ export default Demo;
232
241
  },
233
242
  {
234
243
  type: 'error',
235
- - color: '#ff4D4F',
244
+ - color: '#ff4d4f',
236
245
  + color: token.colorError,
237
246
  },
238
247
  {
@@ -254,7 +263,7 @@ transform fixed less style to antd v5 design token.
254
263
  - color: #1890ff;
255
264
  - background: #52c41a;
256
265
  - background-color: #faad14;
257
- - border-color: #ff4D4F;
266
+ - border-color: #ff4d4f;
258
267
  + color: @colorInfo;
259
268
  + background: @colorSuccess;
260
269
  + background-color: @colorWarning;
package/bin/cli.js CHANGED
@@ -91,7 +91,7 @@ function getRunnerArgs(transformerPath, parser = 'babylon', options = {}) {
91
91
  // override default babylon parser config to enable `decorator-legacy`
92
92
  // https://github.com/facebook/jscodeshift/blob/master/parser/babylon.js
93
93
  parserConfig: require('./babylon.config.json'),
94
- extensions: ['js', 'jsx', 'ts', 'tsx', 'd.ts'].join(','),
94
+ extensions: ['js', 'jsx', 'ts', 'tsx'].join(','),
95
95
  transform: transformerPath,
96
96
  ignorePattern: '**/node_modules',
97
97
  ignoreConfig,
@@ -291,9 +291,9 @@ async function bootstrap() {
291
291
  console.log('[Prettier] format files running...');
292
292
  try {
293
293
  const isDir = isDirectory.sync(dir);
294
- const path = isDir ? path.join(dir, '**/*.{js,jsx,ts,tsx,d.ts}') : dir;
294
+ const targetPath = isDir ? path.join(dir, '**/*.{js,jsx,ts,tsx}') : dir;
295
295
  const npxCommand = commandExistsSync('tnpx') ? 'tnpx' : 'npx';
296
- await execa(npxCommand, ['prettier', '--write', path], { stdio: 'inherit' });
296
+ await execa(npxCommand, ['prettier', '--write', targetPath], { stdio: 'inherit' });
297
297
  console.log('\n[Prettier] format files completed!\n');
298
298
  } catch (err) {
299
299
  console.log('\n[Prettier] format files failed, please format it by yourself.\n', err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oceanbase/codemod",
3
- "version": "0.2.7",
3
+ "version": "0.2.8",
4
4
  "description": "Codemod for OceanBase Design upgrade",
5
5
  "keywords": [
6
6
  "oceanbase",
@@ -26,7 +26,7 @@
26
26
  "chalk": "^3.0.0",
27
27
  "command-exists": "^1.2.9",
28
28
  "execa": "^5.1.1",
29
- "find-up": "^4.1.0",
29
+ "find-up": "^6.3.0",
30
30
  "glob": "^10.3.10",
31
31
  "is-directory": "^0.3.1",
32
32
  "is-git-clean": "^1.1.0",
@@ -41,10 +41,10 @@
41
41
  "yargs-parser": "^21.1.1"
42
42
  },
43
43
  "devDependencies": {
44
- "@types/jest": "^29.5.5",
44
+ "@types/jest": "^29.5.6",
45
45
  "@types/jscodeshift": "^0.11.7",
46
46
  "enzyme": "^3.11.0",
47
47
  "enzyme-to-json": "^3.6.2"
48
48
  },
49
- "gitHead": "641619d79cb7980279179b21905141950c604c54"
49
+ "gitHead": "98cd4d8a2af9b6e5dcde6210fc61f9efb74c7e01"
50
50
  }
@@ -2,7 +2,8 @@
2
2
  color: #1890ff;
3
3
  background: #52c41a;
4
4
  background-color: #faad14;
5
- border-color: #ff4D4F;
5
+ border-color: #ff4d4f;
6
+ scrollbar-color: #ffffff;
6
7
  .content {
7
8
  color: rgba(0, 0, 0, 0.85);
8
9
  background: rgba(0, 0, 0,0.65);
@@ -4,6 +4,7 @@
4
4
  background: @colorSuccess;
5
5
  background-color: @colorWarning;
6
6
  border-color: @colorError;
7
+ scrollbar-color: @colorBgContainer;
7
8
  .content {
8
9
  color: @colorText;
9
10
  background: @colorTextSecondary;
@@ -0,0 +1,13 @@
1
+ .container {
2
+ color: #1890FF;
3
+ background: #52C41A;
4
+ background-color: #FAAD14;
5
+ border-color: #FF4D4F;
6
+ scrollbar-color: #FFFFFF;
7
+ .content {
8
+ color: rgba(0, 0, 0, 0.85);
9
+ background: rgba(0, 0, 0,0.65);
10
+ background-color: rgba(0,0,0,0.45);
11
+ border: 1px solid #D9D9D9;
12
+ }
13
+ }
@@ -0,0 +1,14 @@
1
+ @import '~@oceanbase/design/es/theme/index.less';
2
+ .container {
3
+ color: @colorInfo;
4
+ background: @colorSuccess;
5
+ background-color: @colorWarning;
6
+ border-color: @colorError;
7
+ scrollbar-color: @colorBgContainer;
8
+ .content {
9
+ color: @colorText;
10
+ background: @colorTextSecondary;
11
+ background-color: @colorTextTertiary;
12
+ border: 1px solid @colorBorder;
13
+ }
14
+ }
@@ -1,11 +1,3 @@
1
- const colorMap = {
2
- info: '#1890ff',
3
- success: '#52c41a',
4
- warning: '#faad14',
5
- error: '#ff4D4F',
6
- border: '1px solid #d9d9d9',
7
- };
8
-
9
1
  function getColorList() {
10
2
  return [
11
3
  {
@@ -1,12 +1,4 @@
1
1
  import { token } from '@oceanbase/design';
2
- const colorMap = {
3
- info: token.colorInfo,
4
- success: token.colorSuccess,
5
- warning: token.colorWarning,
6
- error: token.colorError,
7
- border: `1px solid ${token.colorBorder}`,
8
- };
9
-
10
2
  function getColorList() {
11
3
  return [
12
4
  {
@@ -0,0 +1,10 @@
1
+ const color = '#FAFAFA';
2
+ const border = '1px solid #FAFAFA';
3
+
4
+ const colorMap = {
5
+ info: '#1890FF',
6
+ success: '#52C41A',
7
+ warning: '#FAAD14',
8
+ error: '#FF4D4F',
9
+ border: '1px solid #D9D9D9',
10
+ };
@@ -0,0 +1,11 @@
1
+ import { token } from '@oceanbase/design';
2
+ const color = token.colorBgLayout;
3
+ const border = `1px solid ${token.colorBgLayout}`;
4
+
5
+ const colorMap = {
6
+ info: token.colorInfo,
7
+ success: token.colorSuccess,
8
+ warning: token.colorWarning,
9
+ error: token.colorError,
10
+ border: `1px solid ${token.colorBorder}`,
11
+ };
@@ -0,0 +1,15 @@
1
+ import React from 'react';
2
+ import { Alert, Button, theme, Tooltip } from '@oceanbase/design';
3
+
4
+ const Demo = () => {
5
+ const { token } = theme.useToken();
6
+ return (
7
+ <div>
8
+ <Alert style={{ color: 'rgba(0, 0, 0, 0.85)', background: 'rgba(0, 0, 0,0.65)', backgroundColor: 'rgba(0,0,0,0.45)', border: '1px solid #d9d9d9' }} />
9
+ <Button style={{ color: '#1890ff', background: '#52c41a', backgroundColor: '#faad14', borderColor: '#ff4D4F' }}></Button>
10
+ <Tooltip color="#ffffff" backgroundColor="#fff1f0" borderColor="#fafafa" border="1px solid #fafafa" />
11
+ </div>
12
+ );
13
+ };
14
+
15
+ export default Demo;
@@ -0,0 +1,15 @@
1
+ import React from 'react';
2
+ import { Alert, Button, theme, Tooltip } from '@oceanbase/design';
3
+
4
+ const Demo = () => {
5
+ const { token } = theme.useToken();
6
+ return (
7
+ (<div>
8
+ <Alert style={{ color: token.colorText, background: token.colorTextSecondary, backgroundColor: token.colorTextTertiary, border: `1px solid ${token.colorBorder}` }} />
9
+ <Button style={{ color: token.colorInfo, background: token.colorSuccess, backgroundColor: token.colorWarning, borderColor: token.colorError }}></Button>
10
+ <Tooltip color={token.colorBgContainer} backgroundColor={token.colorErrorBg} borderColor={token.colorBgLayout} border={`1px solid ${token.colorBgLayout}`} />
11
+ </div>)
12
+ );
13
+ };
14
+
15
+ export default Demo;
@@ -1,11 +1,12 @@
1
1
  import React from 'react';
2
- import { Alert, Button } from '@oceanbase/design';
2
+ import { Alert, Button, Tooltip } from '@oceanbase/design';
3
3
 
4
4
  const Demo = () => {
5
5
  return (
6
6
  <div>
7
7
  <Alert style={{ color: 'rgba(0, 0, 0, 0.85)', background: 'rgba(0, 0, 0,0.65)', backgroundColor: 'rgba(0,0,0,0.45)', border: '1px solid #d9d9d9' }} />
8
8
  <Button style={{ color: '#1890ff', background: '#52c41a', backgroundColor: '#faad14', borderColor: '#ff4D4F' }}></Button>
9
+ <Tooltip color="#ffffff" backgroundColor="#fff1f0" borderColor="#fafafa" border="1px solid #fafafa" />
9
10
  </div>
10
11
  );
11
12
  };
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { Alert, Button, theme } from '@oceanbase/design';
2
+ import { Alert, Button, theme, Tooltip } from '@oceanbase/design';
3
3
 
4
4
  const Demo = () => {
5
5
  const { token } = theme.useToken();
@@ -7,6 +7,7 @@ const Demo = () => {
7
7
  (<div>
8
8
  <Alert style={{ color: token.colorText, background: token.colorTextSecondary, backgroundColor: token.colorTextTertiary, border: `1px solid ${token.colorBorder}` }} />
9
9
  <Button style={{ color: token.colorInfo, background: token.colorSuccess, backgroundColor: token.colorWarning, borderColor: token.colorError }}></Button>
10
+ <Tooltip color={token.colorBgContainer} backgroundColor={token.colorErrorBg} borderColor={token.colorBgLayout} border={`1px solid ${token.colorBgLayout}`} />
10
11
  </div>)
11
12
  );
12
13
  };
@@ -0,0 +1,18 @@
1
+ import React from 'react';
2
+ import { Alert, Button, Tooltip } from '@oceanbase/design';
3
+
4
+ const Demo = () => {
5
+ const columns = [{
6
+ render: () => {
7
+ return <Tooltip color="#ffffff" backgroundColor="#fff1f0" borderColor="#fafafa" border="1px solid #fafafa" />;
8
+ },
9
+ }];
10
+ return (
11
+ <div>
12
+ <Alert style={{ color: 'rgba(0, 0, 0, 0.85)', background: 'rgba(0, 0, 0,0.65)', backgroundColor: 'rgba(0,0,0,0.45)', border: '1px solid #d9d9d9' }} />
13
+ <Button style={{ color: '#1890ff', background: '#52c41a', backgroundColor: '#faad14', borderColor: '#ff4D4F' }}></Button>
14
+ </div>
15
+ );
16
+ };
17
+
18
+ export default Demo;
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import { Alert, Button, theme, Tooltip } from '@oceanbase/design';
3
+
4
+ const Demo = () => {
5
+ const { token } = theme.useToken();
6
+ const columns = [{
7
+ render: () => {
8
+ return <Tooltip color={token.colorBgContainer} backgroundColor={token.colorErrorBg} borderColor={token.colorBgLayout} border={`1px solid ${token.colorBgLayout}`} />;
9
+ },
10
+ }];
11
+ return (
12
+ (<div>
13
+ <Alert style={{ color: token.colorText, background: token.colorTextSecondary, backgroundColor: token.colorTextTertiary, border: `1px solid ${token.colorBorder}` }} />
14
+ <Button style={{ color: token.colorInfo, background: token.colorSuccess, backgroundColor: token.colorWarning, borderColor: token.colorError }}></Button>
15
+ </div>)
16
+ );
17
+ };
18
+
19
+ export default Demo;
@@ -0,0 +1,10 @@
1
+ const color = '#fafafa';
2
+ const border = '1px solid #fafafa';
3
+
4
+ const colorMap = {
5
+ info: '#1890ff',
6
+ success: '#52c41a',
7
+ warning: '#faad14',
8
+ error: '#ff4d4f',
9
+ border: '1px solid #d9d9d9',
10
+ };
@@ -0,0 +1,11 @@
1
+ import { token } from '@oceanbase/design';
2
+ const color = token.colorBgLayout;
3
+ const border = `1px solid ${token.colorBgLayout}`;
4
+
5
+ const colorMap = {
6
+ info: token.colorInfo,
7
+ success: token.colorSuccess,
8
+ warning: token.colorWarning,
9
+ error: token.colorError,
10
+ border: `1px solid ${token.colorBorder}`,
11
+ };
@@ -3,7 +3,7 @@ import path from 'path';
3
3
  import { transform } from '../less-to-token';
4
4
 
5
5
  const testUnit = 'less-to-token';
6
- const tests = ['antd-v4-less-to-token', 'obui-less-to-token'];
6
+ const tests = ['antd-v4-less-to-token', 'obui-less-to-token', 'case-insensitive'];
7
7
 
8
8
  describe(testUnit, () => {
9
9
  tests.forEach(test => {
@@ -1,7 +1,15 @@
1
1
  import { defineTest } from 'jscodeshift/src/testUtils';
2
2
 
3
3
  const testUnit = 'style-to-token';
4
- const tests = ['function-component', 'class-component', 'static'];
4
+ const tests = [
5
+ 'function-component',
6
+ 'class-component',
7
+ 'block-statement',
8
+ 'nested-block-statement',
9
+ 'existed-useToken',
10
+ 'top-identifier',
11
+ 'case-insensitive',
12
+ ];
5
13
 
6
14
  describe(testUnit, () => {
7
15
  tests.forEach(test =>
@@ -0,0 +1,11 @@
1
+ import { TOKEN_MAP_KEYS, isLower } from '../utils/token';
2
+
3
+ describe('token', () => {
4
+ it('TOKEN_MAP_KEYS should be lower case', async () => {
5
+ expect(TOKEN_MAP_KEYS.every(key => isLower(key))).toEqual(true);
6
+ });
7
+
8
+ it('TOKEN_MAP_KEYS should not include blank space', async () => {
9
+ expect(TOKEN_MAP_KEYS.every(key => !key.includes(' '))).toEqual(true);
10
+ });
11
+ });
@@ -18,7 +18,7 @@ const findAllLessFiles = dir => {
18
18
  files.forEach(file => {
19
19
  const filePath = path.join(dir, file);
20
20
  if (isDirectory.sync(filePath)) {
21
- if (filePath.includes('.umi')) {
21
+ if (filePath.includes('.umi') || filePath.includes('.umi-production')) {
22
22
  return;
23
23
  }
24
24
  lessFiles.push(...findAllLessFiles(filePath));
@@ -26,7 +26,7 @@ const findAllLessFiles = dir => {
26
26
  lessFiles.push(filePath);
27
27
  }
28
28
  });
29
- } else {
29
+ } else if (dir.endsWith('.less')) {
30
30
  lessFiles.push(dir);
31
31
  }
32
32
  return lessFiles;
@@ -77,7 +77,7 @@ async function lessToToken(file) {
77
77
  const allLessFiles = findAllLessFiles(file);
78
78
  for await (const item of allLessFiles) {
79
79
  const content = await transform(item);
80
- fs.writeFileSync(file, content);
80
+ fs.writeFileSync(item, content);
81
81
  }
82
82
  }
83
83
 
@@ -2,6 +2,35 @@ const { addSubmoduleImport } = require('./utils');
2
2
  const { tokenParse } = require('./utils/token');
3
3
  const { printOptions } = require('./utils/config');
4
4
 
5
+ function isTopBlockStatement(path) {
6
+ const isBlockStatement = path.value.type === 'BlockStatement';
7
+ let isTop = isBlockStatement && true;
8
+ path = path.parentPath;
9
+ while (isTop && path.value.type !== 'Program') {
10
+ // isTopBlockStatement => not wrapped by BlockStatement
11
+ if (path.value.type === 'BlockStatement') {
12
+ isTop = false;
13
+ break;
14
+ }
15
+ path = path.parentPath;
16
+ }
17
+ return isTop;
18
+ }
19
+
20
+ function isTopIdentifier(path) {
21
+ let isTop = true;
22
+ path = path.parentPath;
23
+ while (isTop && path.value.type !== 'Program') {
24
+ // isTopIdentifier => not wrapped by BlockStatement
25
+ if (path.value.type === 'BlockStatement') {
26
+ isTop = false;
27
+ break;
28
+ }
29
+ path = path.parentPath;
30
+ }
31
+ return isTop;
32
+ }
33
+
5
34
  function importComponent(j, root, options) {
6
35
  let hasChanged = false;
7
36
 
@@ -16,51 +45,70 @@ function importComponent(j, root, options) {
16
45
  stringList.replaceWith(path => {
17
46
  hasChanged = true;
18
47
  const { key, token, formattedValue } = tokenParse(path.value.value);
19
- return formattedValue === key
20
- ? j.identifier(`token.${token}`)
21
- : j.templateLiteral(
22
- [
23
- j.templateElement(
24
- {
25
- raw: formattedValue.replace(key, `\${token.${token}}`),
26
- cooked: formattedValue.replace(key, `\${token.${token}}`),
27
- },
28
- true
29
- ),
30
- ],
31
- []
32
- );
48
+ const isJSXAttribute = path.parentPath.value.type === 'JSXAttribute';
49
+ let stringValue = `token.${token}`;
50
+ let templateStringValue = `\`${formattedValue.replace(key, `\${token.${token}}`)}\``;
51
+ // add {} wrapper for JSXAttribute
52
+ if (isJSXAttribute) {
53
+ stringValue = `{${stringValue}}`;
54
+ templateStringValue = `{${templateStringValue}}`;
55
+ }
56
+ return formattedValue === key ? j.identifier(stringValue) : j.identifier(templateStringValue);
33
57
  });
34
58
 
35
- root.find(j.BlockStatement).forEach(path => {
36
- const includeToken =
37
- j(path).find(j.Identifier, {
38
- name: name => name?.includes('token.'),
39
- }).length > 0;
40
- if (includeToken) {
41
- const includeJsxElementList = j(path).find(j.JSXElement).length > 0;
42
- const parentType = path.parentPath.value?.type;
43
- // React function component
44
- if (includeJsxElementList && parentType !== 'ClassMethod') {
45
- const importString = `const { token } = theme.useToken()`;
46
- path.get('body').value.unshift(j.expressionStatement(j.identifier(importString)));
47
- // import theme from @oceanbase/design
48
- addSubmoduleImport(j, root, {
49
- moduleName: '@oceanbase/design',
50
- importedName: 'theme',
51
- importKind: 'value',
52
- });
53
- } else {
54
- // React class component and static file (not react component)
55
- // import token from @oceanbase/design
56
- addSubmoduleImport(j, root, {
57
- moduleName: '@oceanbase/design',
58
- importedName: 'token',
59
- importKind: 'value',
60
- });
59
+ root
60
+ .find(j.BlockStatement)
61
+ // avoid duplicate insert for nested block statement
62
+ .filter(path => isTopBlockStatement(path))
63
+ .forEach(path => {
64
+ const includeToken =
65
+ j(path).find(j.Identifier, {
66
+ name: name => name?.includes('token.'),
67
+ }).length > 0;
68
+ if (includeToken) {
69
+ const includeJSXElement = j(path).find(j.JSXElement).length > 0;
70
+ const includeUseTokenStatement =
71
+ j(path).find(j.Identifier, {
72
+ name: name => name.includes('useToken'),
73
+ }).length > 0;
74
+ const parentType = path.parentPath.value?.type;
75
+ // React function component
76
+ if (includeJSXElement && parentType !== 'ClassMethod') {
77
+ // avoid duplicate insert when it's existed
78
+ if (!includeUseTokenStatement) {
79
+ const insertString = 'const { token } = theme.useToken()';
80
+ // insert `const { token } = theme.useToken()`
81
+ path.get('body').value.unshift(j.expressionStatement(j.identifier(insertString)));
82
+ // import theme from @oceanbase/design
83
+ addSubmoduleImport(j, root, {
84
+ moduleName: '@oceanbase/design',
85
+ importedName: 'theme',
86
+ importKind: 'value',
87
+ });
88
+ }
89
+ } else {
90
+ // React class component and static file (not react component)
91
+ // import token from @oceanbase/design
92
+ addSubmoduleImport(j, root, {
93
+ moduleName: '@oceanbase/design',
94
+ importedName: 'token',
95
+ importKind: 'value',
96
+ });
97
+ }
61
98
  }
62
- }
63
- });
99
+ });
100
+
101
+ root
102
+ .find(j.Identifier)
103
+ .filter(path => isTopIdentifier(path) && path.value.name?.includes('token.'))
104
+ .forEach(() => {
105
+ // import token from @oceanbase/design
106
+ addSubmoduleImport(j, root, {
107
+ moduleName: '@oceanbase/design',
108
+ importedName: 'token',
109
+ importKind: 'value',
110
+ });
111
+ });
64
112
  }
65
113
 
66
114
  return hasChanged;
@@ -4,39 +4,84 @@ const TOKEN_MAP = {
4
4
  // antd style => token
5
5
  '#1677ff': 'colorInfo',
6
6
  '#1890ff': 'colorInfo',
7
+ '#40a9ff': 'colorInfo',
8
+ '#f7f9fb': 'colorInfoBg',
9
+ '#e6f7ff': 'colorInfoBgHover',
10
+ '#f3f9ff': 'colorInfoBgHover',
11
+ '#e6f7ff': 'colorInfoBgHover',
7
12
  '#73d13d': 'colorSuccess',
8
13
  '#52c41a': 'colorSuccess',
9
14
  '#faad14': 'colorWarning',
15
+ '#fef6e7': 'colorWarningBg',
10
16
  '#ff4d4f': 'colorError',
11
- '#F5222D': 'colorError',
12
- '#F8636B': 'colorError',
17
+ '#f5222d': 'colorError',
18
+ '#f8636b': 'colorError',
13
19
  '#d9d9d9': 'colorBorder',
14
20
  '#bfbfbf': 'colorBorder',
21
+ '#e8e8e8': 'colorBorder',
22
+ '#f0f0f0': 'colorBorder',
23
+ '#dadada': 'colorBorder',
24
+ 'rgba(217,217,217,1)': 'colorBorder',
25
+ 'rgba(217,217,217,1.0)': 'colorBorder',
26
+ 'rgba(217,217,217)': 'colorBorder',
15
27
  '#f0f2f5': 'colorBgLayout',
16
28
  '#fafafa': 'colorBgLayout',
17
- '#fff': 'colorBgContainer',
29
+ '#f7f8fc': 'colorBgLayout',
18
30
  '#ffffff': 'colorBgContainer',
31
+ '#fff': 'colorBgContainer',
19
32
  'rgba(0,0,0,0.85)': 'colorText',
20
33
  'rgba(0,0,0,0.65)': 'colorTextSecondary',
21
34
  'rgba(0,0,0,0.45)': 'colorTextTertiary',
35
+ '#5c6b8a': 'colorTextTertiary',
22
36
  'rgba(0,0,0,0.25)': 'colorTextQuaternary',
37
+ 'rgba(0,0,0,.85)': 'colorText',
38
+ 'rgba(0,0,0,.65)': 'colorTextSecondary',
39
+ 'rgba(0,0,0,.45)': 'colorTextTertiary',
40
+ 'rgba(0,0,0,.25)': 'colorTextQuaternary',
23
41
  'rgba(0,0,0,0.2)': 'colorFillQuaternary',
42
+ '#00000073': 'colorTextDescription',
43
+ '#f5f5f5': 'colorFillQuaternary',
44
+ 'rgba(0,0,0,0.02)': 'colorBgLayout',
24
45
  'rgba(0,0,0,0.04)': 'colorBgLayout',
46
+ '#f5f6fa': 'colorBgLayout',
47
+ '#edeff2': 'colorBgLayout',
25
48
  // obui style => token
26
49
  '#006aff': 'colorInfo',
50
+ '#086fff': 'colorInfo',
51
+ 'rgba(24,144,255,0.1)': 'colorInfoBg',
52
+ 'rgba(95,149,255,0.10)': 'colorInfoBg',
53
+ 'rgba(95,149,255,0.1)': 'colorInfoBg',
54
+ 'rgba(95,149,255,1)': 'colorInfoBorder',
27
55
  '#0ac185': 'colorSuccess',
56
+ '#4dcca2': 'colorSuccess',
57
+ 'rgba(10,193,133,1)': 'colorSuccess',
58
+ 'rgba(82,196,26,0.1)': 'colorSuccessBg',
28
59
  '#ffac33': 'colorWarning',
60
+ '#ff9700': 'colorWarning',
61
+ '#fbba3a': 'colorWarning',
62
+ 'rgb(243,164,60)': 'colorWarning',
63
+ 'rgba(250,140,22,0.1)': 'colorWarningBg',
29
64
  '#ff4b4b': 'colorError',
30
- '#CDD5E4': 'colorBorder',
31
- '#F5F8FE': 'colorBgLayout',
65
+ '#ff1a1a': 'colorError',
66
+ '#fff1f0': 'colorErrorBg',
67
+ 'rgba(245,34,45,0.1)': 'colorErrorBg',
68
+ '#ffa39e': 'colorErrorBorder',
69
+ '#cdd5e4': 'colorBorder',
70
+ '#f5f8fe': 'colorBgLayout',
71
+ '#f5f7fa': 'colorBgLayout',
72
+ 'rgba(140,140,140,0.1)': 'colorBgLayout',
32
73
  '#132039': 'colorText',
33
74
  '#364563': 'colorTextSecondary',
34
- '#8592AD': 'colorTextTertiary',
35
- '#F8FAFE': 'colorFillQuaternary',
75
+ '#8592ad': 'colorTextTertiary',
76
+ '#f8fafe': 'colorFillQuaternary',
36
77
  };
37
78
 
38
79
  const TOKEN_MAP_KEYS = Object.keys(TOKEN_MAP);
39
80
 
81
+ function isLower(str) {
82
+ return toLower(str) === str;
83
+ }
84
+
40
85
  function customTrim(str) {
41
86
  return str?.replace(/(\s)*([,\(\)])(\s)*/g, '$2');
42
87
  }
@@ -47,7 +92,13 @@ function formatValue(value) {
47
92
 
48
93
  function tokenParse(value) {
49
94
  const formattedValue = formatValue(value);
50
- const key = TOKEN_MAP_KEYS.find(item => formattedValue.includes(item));
95
+ const key = TOKEN_MAP_KEYS.find(
96
+ item =>
97
+ formattedValue.endsWith(item) ||
98
+ formattedValue.includes(`${item} `) ||
99
+ formattedValue.includes(`${item}, `) ||
100
+ formattedValue.includes(`${item},`)
101
+ );
51
102
  return {
52
103
  key,
53
104
  token: TOKEN_MAP[key],
@@ -59,4 +110,5 @@ module.exports = {
59
110
  TOKEN_MAP,
60
111
  TOKEN_MAP_KEYS,
61
112
  tokenParse,
113
+ isLower,
62
114
  };