UNPKG

3.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Loci table is a lookup table for syntax location data.
5 */
6class LociTable {
7 constructor(locations = new Map()) {
8 this.locations = locations;
9 }
10 static apiClassKey() {
11 return "api_class";
12 }
13 static apiDecoratorKey() {
14 return "api_decorator";
15 }
16 static apiNameKey() {
17 return "api_name";
18 }
19 static apiDescriptionKey() {
20 return "api_description";
21 }
22 static endpointClassKey(endpointName) {
23 return `endpoint_<${endpointName}>_class`;
24 }
25 static endpointDecoratorKey(endpointName) {
26 return `endpoint_<${endpointName}>_decorator`;
27 }
28 static endpointMethodKey(endpointName) {
29 return `endpoint_<${endpointName}>_method`;
30 }
31 static endpointPathKey(endpointName) {
32 return `endpoint_<${endpointName}>_path`;
33 }
34 static endpointTagsKey(endpointName) {
35 return `endpoint_<${endpointName}>_tags`;
36 }
37 static endpointTagKey(endpointName, tag) {
38 return `endpoint_<${endpointName}>_tag_<${tag}>`;
39 }
40 static endpointRequestKey(endpointName) {
41 return `endpoint_<${endpointName}>_request`;
42 }
43 static endpointDescriptionKey(endpointName) {
44 return `endpoint_<${endpointName}>_description`;
45 }
46 static typeKey(typeName) {
47 return `type_<${typeName}>`;
48 }
49 /**
50 * Add a locus to the loci table. If the lookup key is already present, `add` will throw an error.
51 *
52 * @param key lookup key
53 * @param locus target locus
54 */
55 add(key, locus) {
56 if (this.locations.has(key)) {
57 throw new Error(`Key already present in type table: ${key}`);
58 }
59 this.locations.set(key, locus);
60 }
61 /**
62 * Add a locus to the loci table by inferring from a ts-morph node. If the lookup key is already present, `addMorphNode` will throw an error.
63 *
64 * @param key lookup key
65 * @param node ts-morph node
66 */
67 addMorphNode(key, node) {
68 this.add(key, {
69 file: node.getSourceFile().getFilePath(),
70 line: node.getStartLineNumber(),
71 column: node.getStartLinePos()
72 });
73 }
74 /**
75 * Retrieve a locus by lookup key.
76 *
77 * @param key lookup key
78 */
79 get(key) {
80 return this.locations.get(key);
81 }
82 /**
83 * Retrieve a locus by lookup key or error.
84 *
85 * @param key lookup key
86 */
87 getOrError(key) {
88 const location = this.get(key);
89 if (location === undefined) {
90 throw new Error(`Key not present in location table: ${key}`);
91 }
92 return location;
93 }
94 /**
95 * Check if an exsiting locus matches a ts-morph node.
96 *
97 * @param key lookup key
98 * @param node ts-morph node
99 */
100 equalsMorphNode(key, node) {
101 const existingLocus = this.getOrError(key);
102 if (existingLocus.file !== node.getSourceFile().getFilePath() ||
103 existingLocus.line !== node.getStartLineNumber() ||
104 existingLocus.column !== node.getStartLinePos()) {
105 return false;
106 }
107 return true;
108 }
109}
110exports.LociTable = LociTable;