/
spec.ts
86 lines (80 loc) · 2.45 KB
/
spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import fs from 'fs';
import { fixturesToTest } from './fixtures-to-test';
import { parse } from './parse';
import * as parseUtils from './utils';
fixturesToTest.forEach(fixture => {
const filename = fixture.filename;
const source = fs.readFileSync(filename, 'utf8').replace(/\r\n/g, '\n');
/**
* Parse with typescript-estree
*/
const typeScriptESTreeResult = parse(source, {
parser: '@typescript-eslint/typescript-estree',
jsx: fixture.jsx,
});
/**
* Parse the source with @babel/parser typescript-plugin
*/
const babelParserResult = parse(source, {
parser: '@babel/parser',
jsx: fixture.jsx,
});
/**
* If babel fails to parse the source, ensure that typescript-estree has the same fundamental issue
*/
if (babelParserResult.parseError) {
/**
* FAIL: babel errored but typescript-estree did not
*/
if (!typeScriptESTreeResult.parseError) {
it(`TEST FAIL [BABEL ERRORED, BUT TS-ESTREE DID NOT] - ${filename}`, () => {
expect(typeScriptESTreeResult.parseError).toEqual(
babelParserResult.parseError,
);
});
return;
}
/**
* Both parsers errored - this is OK as long as the errors are of the same "type"
* E.g. Both must be a SyntaxError, or both must be a RangeError etc.
*/
it(`[Both parsers error as expected] - ${filename}`, () => {
expect(babelParserResult.parseError.name).toEqual(
typeScriptESTreeResult.parseError.name,
);
});
return;
}
/**
* FAIL: typescript-estree errored but babel did not
*/
if (typeScriptESTreeResult.parseError) {
it(`TEST FAIL [TS-ESTREE ERRORED, BUT BABEL DID NOT] - ${filename}`, () => {
expect(babelParserResult.parseError).toEqual(
typeScriptESTreeResult.parseError,
);
});
return;
}
/**
* No errors, assert the two ASTs match
*/
it(`${filename}`, () => {
expect(babelParserResult.ast).toBeTruthy();
expect(typeScriptESTreeResult.ast).toBeTruthy();
/**
* Perform some extra formatting steps on the babel AST before comparing
*/
expect(
parseUtils.removeLocationDataAndSourceTypeFromProgramNode(
parseUtils.preprocessBabylonAST(babelParserResult.ast),
fixture.ignoreSourceType,
),
).toEqual(
parseUtils.removeLocationDataAndSourceTypeFromProgramNode(
parseUtils.preprocessTypescriptAST(typeScriptESTreeResult.ast),
fixture.ignoreSourceType,
),
);
});
});