Skip to content

Commit 4a7ff8b

Browse files
committed
feat(alita): 添加alita转化出错的 友好提示信息
1 parent 041c882 commit 4a7ff8b

13 files changed

+101
-29
lines changed

src/packByWebpack/gatherInfo-loader.ts

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ export default function (this: webpack.loader.LoaderContext, context: string):
3131

3232
console.log(`开始处理:${filepath.replace(configure.inputFullpath, '')} ...`.info)
3333
const ast = parseCode(context, npath.extname(filepath))
34+
35+
//TODO 暂时通过此方式,让ast和sourceCode/filepath 建立联系
36+
// @ts-ignore
37+
ast.__sourceCode = context
38+
// @ts-ignore
39+
ast.__filepath = filepath
40+
3441
const {isEntry, isRF, isFuncComp, imports, exports, JSXElements} = getFileInfo(ast, filepath)
3542

3643
setModuleInfo(filepath, imports, exports, isRF, isEntry, JSXElements)

src/tran/addEventHandler.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
9-
import traverse from "@babel/traverse";
8+
9+
import errorLogTraverse from '../util/ErrorLogTraverse'
10+
1011
import * as t from "@babel/types"
1112

1213
export default function touchableToView (ast) {
13-
traverse(ast, {
14+
errorLogTraverse(ast, {
1415
exit: path => {
1516
if (path.type === 'JSXOpeningElement'
1617
&& path.node.name.name === 'view'

src/tran/addTempName.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
9-
import traverse from "@babel/traverse";
8+
9+
import errorLogTraverse from '../util/ErrorLogTraverse'
1010
import * as t from "@babel/types"
1111
import {InnerTemplateNamePrefix} from "../constants";
1212
import {isJSXChild, isChildCompChild} from "../util/uast";
1313
import {geneOrder} from '../util/util'
1414
export default function addTempName (ast) {
1515
const go = geneOrder()
16-
traverse(ast, {
16+
errorLogTraverse(ast, {
1717
exit: path => {
1818
if (path.type === 'JSXElement'
1919
&& (!isJSXChild(path) || isChildCompChild(path))

src/tran/addWXPrefixHandler.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*
77
*/
88

9-
import traverse from "@babel/traverse";
9+
//import traverse from "@babel/traverse";
10+
import errorLogTraverse from '../util/ErrorLogTraverse'
1011
import {RNCOMPSET} from "../constants";
1112
import * as t from "@babel/types";
1213

@@ -19,7 +20,7 @@ import {textComp} from '../util/getAndStorecompInfos'
1920
*/
2021
export default function addWXPrefixHandler (ast, info?: any) {
2122
let hasTextInner = false
22-
traverse(ast, {
23+
errorLogTraverse(ast, {
2324
exit: path => {
2425
if (path.type === 'JSXOpeningElement') {
2526
addWXPrefix(path)

src/tran/childrenToTemplate.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
9-
import traverse from "@babel/traverse";
8+
9+
import errorLogTraverse from '../util/ErrorLogTraverse'
1010
import * as t from "@babel/types"
1111

1212
import {geneOrder} from '../util/util'
@@ -93,7 +93,7 @@ export default function childrenToTemplate(ast, info) {
9393
const goForCTNP = geneOrder()
9494
const goForCTDK = geneOrder()
9595

96-
traverse(ast, {
96+
errorLogTraverse(ast, {
9797

9898
exit: path => {
9999

src/tran/classNameHandler.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*
77
*/
88

9-
import traverse from "@babel/traverse";
9+
import errorLogTraverse from '../util/ErrorLogTraverse'
10+
1011
import * as t from "@babel/types"
1112

1213
/**
@@ -17,7 +18,7 @@ import * as t from "@babel/types"
1718
*/
1819
export default function classNameHandler (ast,info) {
1920

20-
traverse(ast, {
21+
errorLogTraverse(ast, {
2122
exit: path => {
2223
if (path.type === 'JSXOpeningElement'
2324
&& path.node.name.name === 'view'

src/tran/compOutElementToBlock.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
9-
import traverse from "@babel/traverse";
8+
9+
import errorLogTraverse from '../util/ErrorLogTraverse'
10+
1011

1112
export default function addTempName (ast, info) {
1213
if (info.isPageComp) return ast
1314

14-
traverse(ast, {
15+
errorLogTraverse(ast, {
1516
exit: path => {
1617
if (path.type === 'JSXOpeningElement'
1718
&& path.node.name.name === 'view'

src/tran/compPreHandle.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
9-
import traverse from "@babel/traverse";
8+
9+
import errorLogTraverse from '../util/ErrorLogTraverse'
10+
1011
import * as t from "@babel/types"
1112

1213

@@ -25,7 +26,7 @@ import * as t from "@babel/types"
2526
* @returns {*}
2627
*/
2728
export default function compPreHandle(ast, info) {
28-
traverse(ast, {
29+
errorLogTraverse(ast, {
2930
exit: path => {
3031
// 移除注释
3132
if (path.node.leadingComments) {

src/tran/cptCompHandler.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
9-
import traverse from "@babel/traverse"
8+
9+
import errorLogTraverse from '../util/ErrorLogTraverse'
1010
import {InnerComponentNamePrefix, RNCOMPSET} from "../constants"
1111
import * as t from "@babel/types"
1212
import {geneOrder, getGenericName} from "../util/util"
@@ -17,7 +17,7 @@ import {jsxPropsMap} from '../util/getAndStorecompInfos'
1717
export default function cptCompHandler (ast, info) {
1818
const ALLCPTCOMPMAP = jsxPropsMap
1919
const go = geneOrder()
20-
traverse(ast, {
20+
errorLogTraverse(ast, {
2121
enter: path => {
2222
// 处理直接是<V>{this.props.children}</V> 这种情况
2323
if (path.type === 'JSXExpressionContainer'

src/tran/funcCompToClassComp.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
*
77
*/
88

9-
import traverse from "@babel/traverse";
9+
//import traverse from "@babel/traverse";
1010
import * as t from "@babel/types";
1111
import {isReactComponent} from "../util/uast";
1212
import configure from '../configure'
13+
import errorLogTraverse from '../util/ErrorLogTraverse'
1314

1415
/**
1516
* 把函数声明的组件, 转化为FuncComponent 组件
@@ -41,7 +42,7 @@ export default function funcCompToClassComp(ast, info) {
4142

4243
let comps = new Set([])
4344

44-
traverse(ast, {
45+
errorLogTraverse(ast, {
4546
enter: path => {
4647

4748
// function x {}

src/tran/geneAllTemplate.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
*/
8-
9-
import traverse from '@babel/traverse';
8+
9+
import errorLogTraverse from '../util/ErrorLogTraverse'
10+
1011
import * as t from '@babel/types'
1112
import {decTemlate, isBindElement, isJSXChild, isChildCompChild, isChildComp} from '../util/uast';
1213
import { isEventProp } from '../util/util';
@@ -19,7 +20,7 @@ import { isEventProp } from '../util/util';
1920
*/
2021
export default function(ast, info) {
2122

22-
traverse(ast, {
23+
errorLogTraverse(ast, {
2324
exit: path => {
2425
if (path.type === 'JSXOpeningElement'
2526
&& path.node.name.name === 'view') {

src/tran/literalTemplate.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*
77
*/
88

9-
import traverse from "@babel/traverse";
9+
import errorLogTraverse from '../util/ErrorLogTraverse'
10+
1011
import * as t from "@babel/types"
1112
import {isTextElement} from '../util/uast'
1213

@@ -18,7 +19,7 @@ import {isTextElement} from '../util/uast'
1819
*/
1920
export default function literalTemplate (ast, info) {
2021

21-
traverse(ast, {
22+
errorLogTraverse(ast, {
2223
exit: path => {
2324
if (path.type === 'JSXElement'
2425
&& isTextElement(path.node.openingElement)

src/util/ErrorLogTraverse.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import traverse from "@babel/traverse"
2+
import { codeFrameColumns } from '@babel/code-frame'
3+
4+
import configure from '../configure'
5+
6+
export default function myTraverse (ast, opts) {
7+
8+
9+
const newOpts = wrappCatchOpts(ast, opts)
10+
11+
traverse(ast, newOpts)
12+
}
13+
14+
15+
function wrappCatchOpts(ast, opts) {
16+
17+
const newOpts = {}
18+
19+
const allKeys = Object.keys(opts)
20+
for(let i = 0; i < allKeys.length; i ++ ) {
21+
const k = allKeys[i]
22+
const v = opts[k]
23+
24+
if (typeof v === 'function') {
25+
newOpts[k] = wrapperCatch(ast, v)
26+
} else {
27+
newOpts[k] = v
28+
}
29+
}
30+
31+
return newOpts
32+
}
33+
34+
function wrapperCatch(ast, func) {
35+
return function (...args) {
36+
try {
37+
return func(...args)
38+
} catch(e) {
39+
40+
const relativeFilePath = ast.__filepath.replace(configure.inputFullpath, '')
41+
const code = ast.__sourceCode
42+
43+
const loc = args[0].node.loc
44+
const result = codeFrameColumns(code, loc, {
45+
highlightCode: true,
46+
linesAbove: 2,
47+
linesBelow: 2,
48+
});
49+
console.log(`${relativeFilePath} 转化出错,这可能是alita本身的bug,please submit an issue with below infomations。`.error)
50+
console.log(`代码片段:\n`.error, result)
51+
console.log(`异常信息:\n`.error, e)
52+
53+
throw e
54+
}
55+
}
56+
}
57+

0 commit comments

Comments
 (0)