Skip to content

Commit b4f7864

Browse files
committed
fix(alita): 修复<> 类型推断 下,ts文件报错的bug
1 parent ea3b37d commit b4f7864

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@babel/plugin-transform-runtime": "^7.5.0",
5454
"@babel/plugin-transform-typescript": "^7.6.3",
5555
"@babel/preset-flow": "^7.0.0",
56+
"@babel/preset-typescript": "^7.6.0",
5657
"@babel/traverse": "^7.1.6",
5758
"@babel/types": "^7.1.6",
5859
"babel-eslint": "^8.2.6",

src/util/uast.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ import * as t from "@babel/types"
1515

1616
export function parseCode(code, extname) {
1717
const plugins = [
18-
'jsx',
1918
'classProperties',
2019
'objectRestSpread',
2120
'optionalChaining',
2221
['decorators', {decoratorsBeforeExport: true}],
2322
]
2423

25-
if (extname === '.ts' || extname === '.tsx') {
24+
25+
if (extname === '.ts') {
26+
plugins.push('typescript')
27+
} else if (extname === '.tsx') {
2628
plugins.push('typescript')
29+
plugins.push('jsx')
2730
} else {
2831
plugins.push('flow')
32+
plugins.push('jsx')
2933
}
3034

3135
return parse(code, {
@@ -39,8 +43,8 @@ const babelTransformJSX = babel.createConfigItem(require("../misc/transformJSX")
3943

4044

4145
const babelFlow = babel.createConfigItem(require("@babel/preset-flow"), {type: 'presets'})
42-
const babelTSX = babel.createConfigItem([require("@babel/plugin-transform-typescript"), {isTSX: true}], {type: 'plugin'})
43-
const babelTS = babel.createConfigItem([require("@babel/plugin-transform-typescript"), {isTSX: false}], {type: 'plugin'})
46+
const babelTSX = babel.createConfigItem([require("@babel/preset-typescript"), {isTSX: true, allExtensions: true}], {type: 'plugin'})
47+
const babelTS = babel.createConfigItem([require("@babel/preset-typescript"), {isTSX: false, allExtensions: true}], {type: 'plugin'})
4448

4549
const babelRestSpread = babel.createConfigItem([require("@babel/plugin-proposal-object-rest-spread"), { "loose": true, "useBuiltIns": true }])
4650
const babelClassProperties = babel.createConfigItem([require("@babel/plugin-proposal-class-properties"), {"loose": true}])
@@ -82,13 +86,15 @@ export function geneReactCode(ast, extname) {
8286
babelTransformJSX,
8387
]
8488
if (extname === '.tsx') {
85-
plugins.push(babelTSX)
89+
presets.push(babelTSX)
8690
} else if (extname === '.ts') {
87-
plugins.push(babelTS)
91+
presets.push(babelTS)
8892
} else {
8993
presets.push(babelFlow)
9094
}
9195

96+
console.log('plugins:', plugins)
97+
9298
code = babel.transformSync(code, {
9399
babelrc: false,
94100
configFile: false,

0 commit comments

Comments
 (0)