From ff609b906711a58e7532d90d97bad2be8ac272fa Mon Sep 17 00:00:00 2001 From: "lili.21" Date: Tue, 10 Oct 2023 10:59:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Upload,=20Input,=20InputNumber,=20Radio?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- bin/transformer.js | 6 +++++- package.json | 1 + pnpm-lock.yaml | 2 ++ test.jsx | 8 ++------ transforms/Input.js | 9 +++++++++ transforms/InputNumber.js | 9 +++++++++ transforms/Radio.js | 9 +++++++++ transforms/Upload.js | 42 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 83 insertions(+), 11 deletions(-) create mode 100644 transforms/Input.js create mode 100644 transforms/InputNumber.js create mode 100644 transforms/Radio.js create mode 100644 transforms/Upload.js diff --git a/README.md b/README.md index dd83c00..0363d4c 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,12 @@ Replacing `` with appropriate values. - [ ] SelectProps - [ ] DatePicker - [ ] TimePicker -- [ ] Input -- [ ] InputNumber -- [ ] Radio +- [x] Input +- [x] InputNumber +- [x] Radio - [ ] Switch - [ ] Checkbox -- [ ] Upload +- [x] Upload - [ ] AutoComplete - [ ] Dropdown - [ ] Form diff --git a/bin/transformer.js b/bin/transformer.js index 537d6f8..d6b80ce 100644 --- a/bin/transformer.js +++ b/bin/transformer.js @@ -65,7 +65,11 @@ const TRANSFORMER_INQUIRER_CHOICES = [ 'Select', 'Popconfirm', 'Space', - 'Button' + 'Button', + 'InputNumber', + 'Radio', + 'Upload', + 'Input' ] .sort((a, b) => a.localeCompare(b)) .map((v) => ({ diff --git a/package.json b/package.json index 45bcb5f..0008fdd 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ ], "dependencies": { "@swc/core": "^1.3.82", + "chalk": "4", "execa": "4.0.3", "globby": "11.0.1", "inquirer": "7.3.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bd4f142..b46325a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3,6 +3,7 @@ lockfileVersion: 5.4 specifiers: '@release-it/conventional-changelog': ^7.0.0 '@swc/core': ^1.3.82 + chalk: '4' eslint: ^8.0.1 eslint-config-prettier: ^8.8.0 eslint-config-standard: ^17.1.0 @@ -22,6 +23,7 @@ specifiers: dependencies: '@swc/core': 1.3.82 + chalk: 4.1.2 execa: 4.0.3 globby: 11.0.1 inquirer: 7.3.3 diff --git a/test.jsx b/test.jsx index f6c5133..4639fb7 100644 --- a/test.jsx +++ b/test.jsx @@ -1,9 +1,5 @@ -import { Table, Space, Button } from 'antd' +import { Table, Space, Button, Upload } from 'antd' export const test = () => { - return ( - - ) + return } diff --git a/transforms/Input.js b/transforms/Input.js new file mode 100644 index 0000000..2861ecf --- /dev/null +++ b/transforms/Input.js @@ -0,0 +1,9 @@ +const { removeAntdImportAndAddSemiImport } = require('./utils') +module.exports = function transformer(file, api) { + const j = api.jscodeshift + const root = j(file.source) + + removeAntdImportAndAddSemiImport(j, root, 'Input', 'Input') + + return root.toSource() +} diff --git a/transforms/InputNumber.js b/transforms/InputNumber.js new file mode 100644 index 0000000..941456c --- /dev/null +++ b/transforms/InputNumber.js @@ -0,0 +1,9 @@ +const { removeAntdImportAndAddSemiImport } = require('./utils') +module.exports = function transformer(file, api) { + const j = api.jscodeshift + const root = j(file.source) + + removeAntdImportAndAddSemiImport(j, root, 'InputNumber', 'InputNumber') + + return root.toSource() +} diff --git a/transforms/Radio.js b/transforms/Radio.js new file mode 100644 index 0000000..e61d955 --- /dev/null +++ b/transforms/Radio.js @@ -0,0 +1,9 @@ +const { removeAntdImportAndAddSemiImport } = require('./utils') +module.exports = function transformer(file, api) { + const j = api.jscodeshift + const root = j(file.source) + + removeAntdImportAndAddSemiImport(j, root, 'Radio', 'Radio') + + return root.toSource() +} diff --git a/transforms/Upload.js b/transforms/Upload.js new file mode 100644 index 0000000..c42e51e --- /dev/null +++ b/transforms/Upload.js @@ -0,0 +1,42 @@ +const chalk = require('chalk') + +const { removeAntdImportAndAddSemiImport } = require('./utils') +module.exports = function transformer(file, api) { + const j = api.jscodeshift + const root = j(file.source) + + removeAntdImportAndAddSemiImport(j, root, 'Upload', 'Upload') + + // todo - 暂时没想好怎么自动处理 customRequest 的差异 + console.log( + chalk.yellow(` + Upload组件 customRequest函数的参数有差异,如果有用到,记得手动更改一下 + Antd + ---- + {})} /> + + Semi + ---- + {}} /> + + Semi中fileInstance参数和Antd中的file等价 + `) + ) + + // Find the Upload component and update its props + root.findJSXElements('Upload').forEach((path) => { + const { openingElement } = path.value + + const actionAttribute = openingElement.attributes.find( + (attr) => attr.name.name === 'action' + ) + // Semi组件 action是必须 + if (!actionAttribute) { + openingElement.attributes.push( + j.jsxAttribute(j.jsxIdentifier('action'), j.literal('')) + ) + } + }) + + return root.toSource() +}