Skip to content

Commit 5f3da8c

Browse files
committed
fix: New tests and fix for expressionTo with Spread and Methods
1 parent 66961d8 commit 5f3da8c

File tree

5 files changed

+203
-5
lines changed

5 files changed

+203
-5
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`resolveGenericTypeAnnotation resolves type 1`] = `
4+
Node {
5+
"callProperties": Array [],
6+
"end": 57,
7+
"exact": false,
8+
"extra": undefined,
9+
"indexers": Array [],
10+
"inexact": false,
11+
"innerComments": undefined,
12+
"internalSlots": Array [],
13+
"leadingComments": undefined,
14+
"loc": SourceLocation {
15+
"end": Position {
16+
"column": 34,
17+
"line": 3,
18+
},
19+
"filename": undefined,
20+
"identifierName": undefined,
21+
"start": Position {
22+
"column": 21,
23+
"line": 3,
24+
},
25+
},
26+
"properties": Array [
27+
Node {
28+
"end": 55,
29+
"extra": undefined,
30+
"innerComments": undefined,
31+
"key": Node {
32+
"end": 47,
33+
"extra": undefined,
34+
"innerComments": undefined,
35+
"leadingComments": undefined,
36+
"loc": SourceLocation {
37+
"end": Position {
38+
"column": 24,
39+
"line": 3,
40+
},
41+
"filename": undefined,
42+
"identifierName": "x",
43+
"start": Position {
44+
"column": 23,
45+
"line": 3,
46+
},
47+
},
48+
"name": "x",
49+
"range": undefined,
50+
"start": 46,
51+
"trailingComments": undefined,
52+
"type": "Identifier",
53+
},
54+
"kind": "init",
55+
"leadingComments": undefined,
56+
"loc": SourceLocation {
57+
"end": Position {
58+
"column": 32,
59+
"line": 3,
60+
},
61+
"filename": undefined,
62+
"identifierName": undefined,
63+
"start": Position {
64+
"column": 23,
65+
"line": 3,
66+
},
67+
},
68+
"method": false,
69+
"optional": false,
70+
"proto": false,
71+
"range": undefined,
72+
"start": 46,
73+
"static": false,
74+
"trailingComments": undefined,
75+
"type": "ObjectTypeProperty",
76+
"value": Node {
77+
"end": 55,
78+
"extra": undefined,
79+
"innerComments": undefined,
80+
"leadingComments": undefined,
81+
"loc": SourceLocation {
82+
"end": Position {
83+
"column": 32,
84+
"line": 3,
85+
},
86+
"filename": undefined,
87+
"identifierName": undefined,
88+
"start": Position {
89+
"column": 26,
90+
"line": 3,
91+
},
92+
},
93+
"range": undefined,
94+
"start": 49,
95+
"trailingComments": undefined,
96+
"type": "StringTypeAnnotation",
97+
},
98+
"variance": null,
99+
},
100+
],
101+
"range": undefined,
102+
"start": 44,
103+
"trailingComments": undefined,
104+
"type": "ObjectTypeAnnotation",
105+
}
106+
`;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { expression, noopImporter } from '../../../tests/utils';
2+
import { Array as expressionToArray } from '../expressionTo';
3+
4+
describe('expressionTo', () => {
5+
describe('MemberExpression', () => {
6+
it('with only identifiers', () => {
7+
expect(
8+
expressionToArray(expression('foo.bar.baz'), noopImporter),
9+
).toEqual(['foo', 'bar', 'baz']);
10+
});
11+
12+
it('with one computed literal', () => {
13+
expect(
14+
expressionToArray(expression('foo["bar"].baz'), noopImporter),
15+
).toEqual(['foo', '"bar"', 'baz']);
16+
});
17+
18+
it('with one computed identifier', () => {
19+
expect(
20+
expressionToArray(expression('foo[bar].baz'), noopImporter),
21+
).toEqual(['foo', 'bar', 'baz']);
22+
});
23+
24+
it('with one computed object', () => {
25+
expect(
26+
expressionToArray(expression('foo[{ a: "true"}].baz'), noopImporter),
27+
).toEqual(['foo', '{a: "true"}', 'baz']);
28+
});
29+
30+
it('with one computed object with spread', () => {
31+
expect(
32+
expressionToArray(expression('foo[{ ...a }].baz'), noopImporter),
33+
).toEqual(['foo', '{...a}', 'baz']);
34+
});
35+
36+
it('with one computed object with method', () => {
37+
expect(
38+
expressionToArray(expression('foo[{ a(){} }].baz'), noopImporter),
39+
).toEqual(['foo', '{a: <function>}', 'baz']);
40+
});
41+
});
42+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { expression, statement } from '../../../tests/utils';
2+
import isUnreachableFlowType from '../isUnreachableFlowType';
3+
4+
describe('isUnreachableFlowType', () => {
5+
it('considers Identifier as unreachable', () => {
6+
expect(isUnreachableFlowType(expression('foo'))).toBe(true);
7+
});
8+
9+
it('considers ImportDeclaration as unreachable', () => {
10+
expect(isUnreachableFlowType(statement('import x from "";'))).toBe(true);
11+
});
12+
13+
it('considers CallExpression as unreachable', () => {
14+
expect(isUnreachableFlowType(expression('foo()'))).toBe(true);
15+
});
16+
17+
it('considers VariableDeclaration not as unreachable', () => {
18+
expect(isUnreachableFlowType(statement('const x = 1;'))).toBe(false);
19+
});
20+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { statement, noopImporter } from '../../../tests/utils';
2+
import resolveGenericTypeAnnotation from '../resolveGenericTypeAnnotation';
3+
4+
describe('resolveGenericTypeAnnotation', () => {
5+
it('resolves type', () => {
6+
const code = `
7+
var x: Props;
8+
type Props = { x: string };
9+
`;
10+
expect(
11+
resolveGenericTypeAnnotation(
12+
statement(code).get(
13+
'declarations',
14+
0,
15+
'id',
16+
'typeAnnotation',
17+
'typeAnnotation',
18+
),
19+
noopImporter,
20+
),
21+
).toMatchSnapshot();
22+
});
23+
});

src/utils/expressionTo.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,23 @@ function toArray(path: NodePath, importer: Importer): string[] {
4040
// @ts-ignore
4141
result.push(node.raw);
4242
continue;
43+
} else if (t.FunctionExpression.check(node)) {
44+
result.push('<function>');
45+
continue;
4346
} else if (t.ThisExpression.check(node)) {
4447
result.push('this');
4548
continue;
4649
} else if (t.ObjectExpression.check(node)) {
4750
const properties = path.get('properties').map(function (property) {
48-
return (
49-
toString(property.get('key'), importer) +
50-
': ' +
51-
toString(property.get('value'), importer)
52-
);
51+
if (t.SpreadElement.check(property.node)) {
52+
return `...${toString(property.get('argument'), importer)}`;
53+
} else {
54+
return (
55+
toString(property.get('key'), importer) +
56+
': ' +
57+
toString(property.get('value'), importer)
58+
);
59+
}
5360
});
5461
result.push('{' + properties.join(', ') + '}');
5562
continue;

0 commit comments

Comments
 (0)