Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 251a46a

Browse files
committed
feat: report error context and source excerpt
When failing to infer types instead of print error trace, report source except #247
1 parent 0501d69 commit 251a46a

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

Diff for: package.json

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"license": "MIT",
3838
"devDependencies": {
3939
"@knisterpeter/standard-tslint": "^1.3.0",
40+
"@types/babel-generator": "6.7.14",
4041
"@types/chalk": "0.4.31",
4142
"@types/diff": "0.0.31",
4243
"@types/node": "7.0.5",
@@ -62,6 +63,7 @@
6263
},
6364
"dependencies": {
6465
"astq": "2.0.0",
66+
"babel-generator": "6.23.0",
6567
"babylon": "6.16.1",
6668
"dts-dom": "0.1.15",
6769
"get-stdin": "5.0.1",

Diff for: src/types.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import astToCode from 'babel-generator';
12
import * as dom from 'dts-dom';
23
import { propTypeQueryExpression, AstQuery } from './typings';
34

@@ -25,7 +26,12 @@ export function get(ast: AstQuery, propertyAst: any, propTypesName: string|undef
2526
}
2627
} catch (e) {
2728
console.error('Failed to infer PropType; Fallback to any');
28-
console.error(e.stack);
29+
if (e.loc) {
30+
const src = astToCode(ast.ast).code;
31+
console.error(`Line ${e.loc.start.line}: ${src.split('\n')[e.loc.start.line - 1]}`);
32+
} else {
33+
console.error(e.stack);
34+
}
2935
}
3036
return {
3137
type: 'any',
@@ -159,7 +165,12 @@ function getShapeProperties(ast: AstQuery, input: any): any[] {
159165
]
160166
/:init *
161167
`);
162-
return res[0].properties;
168+
if (res[0]) {
169+
return res[0].properties;
170+
}
171+
const error = new Error('Failed to lookup shape properties');
172+
(error as any).loc = input.loc;
173+
throw error;
163174
}
164175
return input.properties;
165176
}

Diff for: src/typings.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { InstanceOfResolver } from './index';
55
import * as types from './types';
66

77
export interface AstQuery {
8+
ast: any;
89
query(query: string): any[];
910
querySubtree(subtree: any, query: string): any[];
1011
}
@@ -13,6 +14,7 @@ export function createTypings(moduleName: string|null, programAst: any,
1314
instanceOfResolver: InstanceOfResolver | undefined): string {
1415
const astq = new ASTQ();
1516
const ast = {
17+
ast: programAst,
1618
query(query: string): any[] {
1719
return astq.query(programAst, query);
1820
},

Diff for: tests/error-reporting-test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import test from 'ava';
2+
3+
import * as react2dts from '../src/index';
4+
5+
const orignalConsoleError = console.error;
6+
7+
test.beforeEach(t => {
8+
console.error = function(): void {
9+
const args = Array.prototype.slice.call(arguments);
10+
if (!t.context.args) {
11+
t.context.args = [];
12+
}
13+
t.context.args.push(args);
14+
};
15+
});
16+
17+
test.afterEach(() => {
18+
console.error = orignalConsoleError;
19+
});
20+
21+
test('In case of error during type inference the error information should be retained', t => {
22+
react2dts.generateFromSource(null, `
23+
import React from 'react';
24+
25+
export class Component extends React.Component {
26+
static propTypes = {
27+
someShape: React.PropTypes.shape(shape)
28+
};
29+
}
30+
`);
31+
const args = t.context.args.reduce((akku: any[], args: any[]) => [...akku, ...args], []);
32+
const idx = args.indexOf('Line 6: someShape: React.PropTypes.shape(shape)');
33+
t.is(idx, 1);
34+
});

0 commit comments

Comments
 (0)