From ff22641d4ea5aa2381ccb9868b8c4262d48e5b5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E5=8D=9A=E6=96=87?= Date: Tue, 18 May 2021 21:03:38 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20typescript=20?= =?UTF-8?q?=E7=9A=84=20as=20=E8=A1=A8=E8=BE=BE=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Plugin.js | 16 ++++++++++++++++ src/index.js | 1 + 2 files changed, 17 insertions(+) diff --git a/src/Plugin.js b/src/Plugin.js index 61cb7d81..40d87bdd 100644 --- a/src/Plugin.js +++ b/src/Plugin.js @@ -291,4 +291,20 @@ export default class Plugin { const { node } = path; this.buildExpressionHandler(node, ['superClass'], path, state); } + + TSAsExpression(path, state) { + const { types } = this; + const file = (path && path.hub && path.hub.file) || (state && state.file); + const pluginState = this.getPluginState(state); + + let { + node + } = path; + + do { + node = node.expression; + } while (types.isTSAsExpression(node)); + + path.replaceWith(this.importMethod(node.name, file, pluginState)); + } } diff --git a/src/index.js b/src/index.js index f6de1a58..a9cbce35 100644 --- a/src/index.js +++ b/src/index.js @@ -101,6 +101,7 @@ export default function ({ types }) { 'ClassDeclaration', 'SwitchStatement', 'SwitchCase', + 'TSAsExpression', ]; const ret = { From e462b96fa48f6a2bf8ae772a33be4a77f790f47f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E5=8D=9A=E6=96=87?= Date: Tue, 18 May 2021 21:44:04 +0800 Subject: [PATCH 2/2] chore: add test --- package.json | 1 + test/fixtures/typescript-as-expression/actual.js | 3 +++ test/fixtures/typescript-as-expression/expected.js | 2 ++ test/index.test.js | 6 ++++++ 4 files changed, 12 insertions(+) create mode 100644 test/fixtures/typescript-as-expression/actual.js create mode 100644 test/fixtures/typescript-as-expression/expected.js diff --git a/package.json b/package.json index 920b7d36..f53ef205 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "devDependencies": { "@babel/core": "^7.0.0", "@babel/preset-react": "^7.0.0", + "@babel/preset-typescript": "^7.13.0", "@umijs/test": "^3.2.28", "babel-core": "^7.0.0-0", "babel-preset-umi": "^1.0.0", diff --git a/test/fixtures/typescript-as-expression/actual.js b/test/fixtures/typescript-as-expression/actual.js new file mode 100644 index 00000000..7a4b7289 --- /dev/null +++ b/test/fixtures/typescript-as-expression/actual.js @@ -0,0 +1,3 @@ +import { Button } from 'antd'; + +const foo = (Button as any).foo; diff --git a/test/fixtures/typescript-as-expression/expected.js b/test/fixtures/typescript-as-expression/expected.js new file mode 100644 index 00000000..5f494d3d --- /dev/null +++ b/test/fixtures/typescript-as-expression/expected.js @@ -0,0 +1,2 @@ +import _Button from "antd/lib/button"; +const foo = _Button.foo; diff --git a/test/index.test.js b/test/index.test.js index 7f8e54dc..6ce88128 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -146,6 +146,12 @@ describe('index', () => { plugins: [[plugin, { libraryName: 'antd' }]], babelrc: false, }).code; + } else if (caseName === 'typescript-as-expression') { + return transformFileSync(actualFile, { + presets: [['@babel/preset-typescript', { allExtensions: true }]], + plugins: [[plugin, { libraryName: 'antd' }]], + babelrc: false, + }).code; } else { return transformFileSync(actualFile, { presets: ['@babel/preset-react'],