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

Commit e24f500

Browse files
committed
feat: retain error information
1 parent 91622cc commit e24f500

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Diff for: src/types.ts

+15
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,18 @@ function getEnumValues(ast: AstQuery, oneOfTypes: any): any[] {
143143
]
144144
/:init *
145145
`);
146+
if (!res[0]) {
147+
const error = new Error('Failed to lookup enum values');
148+
(error as any).loc = oneOfTypes.loc;
149+
throw error;
150+
}
146151
oneOfTypes = res[0];
147152
}
153+
if (!oneOfTypes.elements) {
154+
const error = new Error('Failed to lookup enum values');
155+
(error as any).loc = oneOfTypes.loc;
156+
throw error;
157+
}
148158
return (oneOfTypes.elements as any[]).map((element: any) => {
149159
// fixme: This are not named references!
150160
if (element.type === 'StringLiteral') {
@@ -172,5 +182,10 @@ function getShapeProperties(ast: AstQuery, input: any): any[] {
172182
(error as any).loc = input.loc;
173183
throw error;
174184
}
185+
if (!input.properties) {
186+
const error = new Error('Failed to lookup shape properties');
187+
(error as any).loc = input.loc;
188+
throw error;
189+
}
175190
return input.properties;
176191
}

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

+46-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test.afterEach(() => {
1818
console.error = orignalConsoleError;
1919
});
2020

21-
test('In case of error during type inference the error information should be retained', t => {
21+
test('In case of error during shape type inference the error information should be retained', t => {
2222
react2dts.generateFromSource(null, `
2323
import React from 'react';
2424
@@ -32,3 +32,48 @@ test('In case of error during type inference the error information should be ret
3232
const idx = args.indexOf('Line 6: someShape: React.PropTypes.shape(shape)');
3333
t.is(idx, 1);
3434
});
35+
36+
test('In case of error during enum type inference the error information should be retained', t => {
37+
react2dts.generateFromSource(null, `
38+
import React from 'react';
39+
40+
export class Component extends React.Component {
41+
static propTypes = {
42+
list: React.PropTypes.oneOf(list)
43+
};
44+
}
45+
`);
46+
const args = t.context.args.reduce((akku: any[], args: any[]) => [...akku, ...args], []);
47+
const idx = args.indexOf('Line 6: list: React.PropTypes.oneOf(list)');
48+
t.is(idx, 1);
49+
});
50+
51+
test('In case of error during enum value creation inference the error information should be retained', t => {
52+
react2dts.generateFromSource(null, `
53+
import React from 'react';
54+
55+
export class Component extends React.Component {
56+
static propTypes = {
57+
list: React.PropTypes.oneOf(Object.keys(object))
58+
};
59+
}
60+
`);
61+
const args = t.context.args.reduce((akku: any[], args: any[]) => [...akku, ...args], []);
62+
const idx = args.indexOf('Line 6: list: React.PropTypes.oneOf(Object.keys(object))');
63+
t.is(idx, 1);
64+
});
65+
66+
test('In case of error during shape type inference the error information should be retained', t => {
67+
react2dts.generateFromSource(null, `
68+
import React from 'react';
69+
70+
export class Component extends React.Component {
71+
static propTypes = {
72+
shape: React.PropTypes.shape(some.shape)
73+
};
74+
}
75+
`);
76+
const args = t.context.args.reduce((akku: any[], args: any[]) => [...akku, ...args], []);
77+
const idx = args.indexOf('Line 6: shape: React.PropTypes.shape(some.shape)');
78+
t.is(idx, 1);
79+
});

0 commit comments

Comments
 (0)