forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser-utils.test.ts
69 lines (63 loc) · 2.15 KB
/
parser-utils.test.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
import { ok, strictEqual } from "assert";
import { describe, it } from "vitest";
import { SyntaxKind, TypeSpecScriptNode } from "../src/core/index.js";
import { getNodeAtPosition, parse } from "../src/core/parser.js";
import { Node } from "../src/core/types.js";
import { extractCursor } from "../src/testing/test-server-host.js";
import { dumpAST } from "./ast-test-utils.js";
describe("compiler: parser utils", () => {
describe("getNodeAtPosition", () => {
async function getNodeAtCursor(
sourceWithCursor: string
): Promise<{ root: TypeSpecScriptNode; node: Node | undefined }> {
const { source, pos } = extractCursor(sourceWithCursor);
const root = parse(source);
dumpAST(root);
return { node: getNodeAtPosition(root, pos), root };
}
it("return namespace when in namespace body", async () => {
const { node } = await getNodeAtCursor(`
namespace Foo {
┆
}
`);
ok(node);
strictEqual(node.kind, SyntaxKind.NamespaceStatement as const);
strictEqual(node.id.sv, "Foo");
});
it("return identifier for property return type", async () => {
const { node } = await getNodeAtCursor(`
model Foo {
prop: stri┆ng
}
`);
ok(node);
strictEqual(node.kind, SyntaxKind.Identifier as const);
strictEqual(node.sv, "string");
});
it("return string literal when in non completed string", async () => {
const { node } = await getNodeAtCursor(`
import "┆
`);
ok(node);
strictEqual(node.kind, SyntaxKind.StringLiteral);
});
it("return string literal when in non completed multi line string", async () => {
const { node } = await getNodeAtCursor(`
model Foo {
prop: """┆
}
`);
ok(node);
strictEqual(node.kind, SyntaxKind.StringLiteral);
});
it("return missing identifier between dot and close paren", async () => {
const { node } = await getNodeAtCursor(`
@myDecN.┆)
`);
ok(node);
strictEqual(node.kind, SyntaxKind.Identifier as const);
strictEqual(node.sv, "<missing identifier>1");
});
});
});