Skip to content

Commit 5f6e0cd

Browse files
authored
test: refactor tests (#275)
1 parent bc067cb commit 5f6e0cd

File tree

3 files changed

+251
-135
lines changed

3 files changed

+251
-135
lines changed

tests/__snapshots__/transform.test.ts.snap

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,60 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[` (EmptyExpr -> NGEmptyExpression) ast 1`] = `
4-
NGEmptyExpression {
3+
exports[`(BindingPipe -> NGPipeExpression) parseBinding(" a | b ") 1`] = `
4+
NGPipeExpression {
5+
"left": "Identifier",
6+
"right": "Identifier",
7+
"arguments": [],
58
"comments": []
69
}
7-
> 1 |
8-
| ^
10+
> 1 | a | b
11+
| ^^^^^
12+
--------------------------------------------------------------------------------
13+
Identifier {
14+
"name": "a"
15+
}
16+
> 1 | a | b
17+
| ^
18+
--------------------------------------------------------------------------------
19+
Identifier {
20+
"name": "b"
21+
}
22+
> 1 | a | b
23+
| ^
924
`;
1025

11-
exports[` a ; b (Chain -> NGChainedExpression) ast 1`] = `
12-
NGChainedExpression {
13-
"expressions": [
14-
"Identifier",
26+
exports[`(BindingPipe -> NGPipeExpression) parseBinding(" a | b : c ") 1`] = `
27+
NGPipeExpression {
28+
"left": "Identifier",
29+
"right": "Identifier",
30+
"arguments": [
1531
"Identifier"
1632
],
1733
"comments": []
1834
}
19-
> 1 | a ; b
20-
| ^^^^^
35+
> 1 | a | b : c
36+
| ^^^^^^^^^
2137
--------------------------------------------------------------------------------
2238
Identifier {
2339
"name": "a"
2440
}
25-
> 1 | a ; b
41+
> 1 | a | b : c
2642
| ^
2743
--------------------------------------------------------------------------------
2844
Identifier {
2945
"name": "b"
3046
}
31-
> 1 | a ; b
47+
> 1 | a | b : c
3248
| ^
49+
--------------------------------------------------------------------------------
50+
Identifier {
51+
"name": "c"
52+
}
53+
> 1 | a | b : c
54+
| ^
3355
`;
3456

35-
exports[` a | b (BindingPipe -> NGPipeExpression) ast 1`] = `
57+
exports[`(BindingPipe -> NGPipeExpression) parseInterpolationExpression(" a | b ") 1`] = `
3658
NGPipeExpression {
3759
"left": "Identifier",
3860
"right": "Identifier",
@@ -55,7 +77,7 @@ Identifier {
5577
| ^
5678
`;
5779

58-
exports[` a | b : c (BindingPipe -> NGPipeExpression) ast 1`] = `
80+
exports[`(BindingPipe -> NGPipeExpression) parseInterpolationExpression(" a | b : c ") 1`] = `
5981
NGPipeExpression {
6082
"left": "Identifier",
6183
"right": "Identifier",
@@ -85,3 +107,59 @@ Identifier {
85107
> 1 | a | b : c
86108
| ^
87109
`;
110+
111+
exports[`(Chain -> NGChainedExpression) parseAction(" a ; b ") 1`] = `
112+
NGChainedExpression {
113+
"expressions": [
114+
"Identifier",
115+
"Identifier"
116+
],
117+
"comments": []
118+
}
119+
> 1 | a ; b
120+
| ^^^^^
121+
--------------------------------------------------------------------------------
122+
Identifier {
123+
"name": "a"
124+
}
125+
> 1 | a ; b
126+
| ^
127+
--------------------------------------------------------------------------------
128+
Identifier {
129+
"name": "b"
130+
}
131+
> 1 | a ; b
132+
| ^
133+
`;
134+
135+
exports[`(EmptyExpr -> NGEmptyExpression) parseAction("") 1`] = `
136+
NGEmptyExpression {
137+
"comments": []
138+
}
139+
> 1 |
140+
| ^
141+
`;
142+
143+
exports[`(EmptyExpr -> NGEmptyExpression) parseBinding("") 1`] = `
144+
NGEmptyExpression {
145+
"comments": []
146+
}
147+
> 1 |
148+
| ^
149+
`;
150+
151+
exports[`(EmptyExpr -> NGEmptyExpression) parseInterpolationExpression("") 1`] = `
152+
NGEmptyExpression {
153+
"comments": []
154+
}
155+
> 1 |
156+
| ^
157+
`;
158+
159+
exports[`(EmptyExpr -> NGEmptyExpression) parseSimpleBinding("") 1`] = `
160+
NGEmptyExpression {
161+
"comments": []
162+
}
163+
> 1 |
164+
| ^
165+
`;

tests/helpers.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { codeFrameColumns } from '@babel/code-frame';
2+
import type * as b from '@babel/types';
23
import * as babelParser from '@babel/parser';
34
import { LinesAndColumns } from 'lines-and-columns';
45
import { wrap } from 'jest-snapshot-serializer-raw';
@@ -10,20 +11,31 @@ const babelParserOptions: babelParser.ParserOptions = {
1011
ranges: true,
1112
};
1213

13-
export function parseBabelExpression(input: string) {
14-
return babelParser.parseExpression(input, babelParserOptions);
15-
}
16-
17-
export function parseBabel(input: string) {
18-
const ast = babelParser.parse(input, babelParserOptions);
14+
function fixBabelCommentsRange(
15+
ast: (
16+
| ReturnType<typeof babelParser.parse>
17+
| ReturnType<typeof babelParser.parseExpression>
18+
) & { comments?: b.Comment[] | null },
19+
) {
1920
// https://github.com/babel/babel/issues/15115
2021
for (const comment of ast.comments!) {
2122
// @ts-expect-error -- missing types
2223
comment.range ??= [comment.start, comment.end];
2324
}
25+
2426
return ast;
2527
}
2628

29+
export function parseBabelExpression(input: string) {
30+
return fixBabelCommentsRange(
31+
babelParser.parseExpression(input, babelParserOptions),
32+
);
33+
}
34+
35+
export function parseBabel(input: string) {
36+
return fixBabelCommentsRange(babelParser.parse(input, babelParserOptions));
37+
}
38+
2739
export function massageAst(ast: any): any {
2840
if (!ast || typeof ast !== 'object') {
2941
return ast;

0 commit comments

Comments
 (0)