Skip to content

Commit 9658206

Browse files
committed
fix(alita): 修改直接导出函数式组件转化报错的bug
re #6
1 parent 10eeb0f commit 9658206

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react'
2+
import PropTypes from 'prop-types'
3+
import {View, Text, StyleSheet} from 'react-native'
4+
import styles from './styles';
5+
6+
export default function MyFunComp({name, age}, {color}) {
7+
8+
return <View style={styles.item}>
9+
<Text style={styles.itemText}>{name}{age}{color}</Text>
10+
</View>
11+
}
12+
13+
MyFunComp.contextTypes = {
14+
color: PropTypes.string,
15+
}

src/basetran/handleMisc.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const historyPrefixSet = new Set([
1717
'replace'
1818
])
1919

20-
export default function (ast) {
20+
export default function (ast, {isFuncComp}) {
2121
let leftIden = null
2222
traverse(ast, {
2323
exit: path => {
@@ -35,10 +35,12 @@ export default function (ast) {
3535
* const x = 1
3636
* export default x
3737
*
38+
* 函数式组件在后续会被处理为class组件,这里不需要处理
3839
*
3940
*/
4041

41-
if (path.type === 'ExportDefaultDeclaration'
42+
if (!isFuncComp
43+
&& path.type === 'ExportDefaultDeclaration'
4244
&& path.node.declaration
4345
&& path.node.declaration.type === 'AssignmentExpression'
4446
&& path.node.declaration.left.type === 'Identifier'

src/basetran/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import handleMisc from './handleMisc'
1212
import geneJS from './geneJS'
1313
import {geneCode} from "../util/uast";
1414

15-
export default async function (ast, filepath, justTran) {
15+
export default async function (ast, filepath, justTran, isFuncComp) {
1616
const info = {
17-
filepath
17+
filepath,
18+
isFuncComp
1819
}
1920

2021
ast = handleMisc(ast, info)

src/struc/handleRF.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ export default async function (ast, filepath, isFuncComp, entryFilePath, isPageC
1515
const dirname = path.dirname(filepath)
1616
await fse.mkdirs(dirname)
1717

18-
await baseTran(ast, filepath, true)
18+
await baseTran(ast, filepath, true, isFuncComp)
1919
await componentTran(ast, filepath, isFuncComp, entryFilePath, isPageComp, isStatelessComp)
2020
}

0 commit comments

Comments
 (0)