forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonParserRecovery.ts
44 lines (40 loc) · 1.48 KB
/
jsonParserRecovery.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
import * as Harness from "../_namespaces/Harness.js";
import * as ts from "../_namespaces/ts.js";
describe("unittests:: jsonParserRecovery", () => {
function parsesToValidSourceFileWithErrors(name: string, text: string) {
it(name, () => {
const file = ts.parseJsonText(name, text);
assert(file.parseDiagnostics.length, "Should have parse errors");
Harness.Baseline.runBaseline(
`jsonParserRecovery/${name.replace(/[^a-z0-9_-]/ig, "_")}.errors.txt`,
Harness.Compiler.getErrorBaseline([{
content: text,
unitName: name,
}], file.parseDiagnostics),
);
// Will throw if parse tree does not cover full input text
file.getChildren();
});
}
parsesToValidSourceFileWithErrors("trailing identifier", "{} blah");
parsesToValidSourceFileWithErrors("TypeScript code", "interface Foo {} blah");
parsesToValidSourceFileWithErrors("Two comma-separated objects", "{}, {}");
parsesToValidSourceFileWithErrors("Two objects", "{} {}");
parsesToValidSourceFileWithErrors(
"JSX",
`
interface Test {}
const Header = () => (
<div>
<h1>Header</h1>
<style jsx>
{\`
h1 {
color: red;
}
\`}
</style>
</div>
)`,
);
});