kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/typescript/SCHEMA.md (about) 1 # TypeScript Indexer VName Schema 2 3 This specification defines the schema for how the indexer expresses VNames for 4 TypeScript declarations. You may find this useful if you are developing an 5 application that relies on TypeScript code you don't want to re-index. 6 7 A formal listing of the specification is provided below, but the 8 [schema tests](./testdata/schema.ts) may be more useful for real examples. 9 10 ### VName signature 11 12 The signature description language used in this document is similar to regex. 13 14 - A substring matching `\b\$.*\b` is a variable. 15 - e.g. `$DECLARATION_NAME` is a variable refering to the name of 16 declaration. 17 - A substring matching `\[.*\]\?` is matched 0 or 1 times. 18 - e.g. `(hidden)?` is really `hidden` or ``. 19 - A substring matching `\[.*\]\*` is matched 0 or more times. 20 - e.g. `[a]*` is ``, or`a`, or`aa`, ... 21 - All other substrings are literals. 22 - e.g. `get#$NAME` is really `get#foo` if `$NAME = foo`. 23 24 The signature of a TypeScript declaration is defined by the following schema: 25 26 ```regex 27 $PART[.$PART]*[#type]? 28 ``` 29 30 where `$PART` is a component of the enclosing declaration scope and `#type` is 31 appended to the signature of types. `SyntaxKind`s that are types are: 32 33 - `ClassDeclaration` (also a value) 34 - `EnumDeclaration` (also a value) 35 - `InterfaceDeclaration` 36 - `TypeAliasDeclaration` 37 - `TypeParameter` 38 39 As an example of this schema, in 40 41 ```typescript 42 class A { 43 public foo: string; 44 } 45 46 type B = A; 47 ``` 48 49 `foo` has the signature `A.foo` and `B` has the signature `B#type`. 50 51 Signature components (`$PART`s in the schema) are defined below. 52 53 #### File Module 54 55 **Form**: `module` 56 57 **Notes**: The first character of TypeScript source file binds to a VName 58 describing the module. The module path is the file path of the module, stripped 59 of `.ts` and `.d.ts` extensions and relative to the project root. See __VName 60 Path__ for more. 61 62 **SyntaxKind**: 63 64 - `SourceFile` 65 66 ```typescript 67 //- FileModule=VName("module", _, _, _, _).node/kind record 68 //- FileModuleAnchor.node/kind anchor 69 //- FileModuleAnchor./kythe/loc/start 0 70 //- FileModuleAnchor./kythe/loc/end 1 71 //- FileModuleAnchor defines/binding FileModule 72 ``` 73 74 #### Named Declaration 75 76 **Form**: `$DECLARATION_NAME | "$DECLARATION_NAME"` 77 78 **Notes**: Every named declaration is guaranteed to have a signature of the form 79 `$SCOPE[.$SCOPE]*` where `$SCOPE` is the name of each encompassing named 80 declaration. 81 82 If the declaration is a string literal, its name is wrapped in quotes. 83 84 **SyntaxKind**: 85 86 - `NamespaceImport` 87 - `Constructor` 88 - `MethodDeclaration` 89 - `EnumDeclaration` 90 - `EnumMember` 91 - `FunctionDeclaration` 92 - `Parameter` 93 - `InterfaceDeclaration` 94 - `PropertySignature` 95 - `MethodSignature` 96 - `VariableDeclaration` 97 - `PropertyAssignment` 98 - `TypeAliasDeclaration` 99 - `TypeParameter` 100 101 ```typescript 102 export class Klass { 103 //- @property defines/binding VName("Klass#type.method", _, _, _, _) 104 method() { 105 //- @property defines/binding VName("Klass#type.method.val", _, _, _, _) 106 let val; 107 }; 108 109 //- @"'propliteral'" defines/binding VName("Klass#type.\"propliteral\"", _, _, _, _) 110 'propliteral' = 0; 111 } 112 ``` 113 114 #### Class Declaration 115 116 **Form**: 117 118 - `$CLASS#type` 119 - `$CLASS` 120 121 **Notes**: Because a class is both a type and a value, it has to be labeled as 122 such. Class members belong to an instance of the class type and have a signature 123 with class component of `$CLASS#type`. Static members belong to the class value 124 and have a signature with class component of `$CLASS`. 125 126 **SyntaxKind**: 127 128 - ClassDeclaration 129 130 ```typescript 131 //- @Klass defines/binding VName("Klass#type", _, _, _, _) 132 //- @Klass defines/binding VName("Klass", _, _, _, _) 133 class Klass { 134 //- @member defines/binding VName("Klass#type.member", _, _, _, _) 135 member = 0; 136 137 //- @staticMember defines/binding VName("Klass.staticMember", _, _, _, _) 138 static staticMember = 0; 139 140 //- @constructor defines/binding VName("Klass#type.constructor", _, _, _, _) 141 constructor() {} 142 } 143 ``` 144 145 #### Property Declaration 146 147 **Form**: `$CLASS[#type]?.$PROPERTY` 148 149 **Notes**: Instance members on a class have a signature of form 150 `$CLASS#type.$PROPERTY`, as they belong to instances of the class type. Static 151 members, which belong to the class value, have a form of `$CLASS.$PROPERTY`. 152 153 **SyntaxKind**: 154 155 - PropertyDeclaration 156 - ParameterPropertyDeclaration 157 - GetAccessor 158 - SetAccessor 159 - MethodDeclaration 160 161 ```typescript 162 class Klass { 163 //- @prop defines/binding VName("Klass.prop", _, _, _, _) 164 prop; 165 166 //- @prop defines/binding VName("Klass#type.prop", _, _, _, _) 167 static prop; 168 169 //- @cprop defines/binding VName("Klass#type.cprop", _, _, _, _) 170 constructor(private cprop: number) {} 171 } 172 ``` 173 174 #### Getter/Setter 175 176 **Form**: 177 178 - getter: `$DECLARATION_NAME:getter` 179 - setter: `$DECLARATION_NAME:setter` 180 - getter if present, otherwise setter: `$DECLARATION_NAME` 181 182 **Notes**: Because getters and setters refer to the same property on a class but 183 provide multiple declarations, several distinctions between the declarations are 184 made. Each declaration of a getter and setter will take `:getter` and `:setter` 185 suffix, respectively. 186 187 If a getter for a property is present, the getter will emit a binding the same 188 form as the property it defines. If only a setter is present, the setter will 189 emit a binding of the same form as the property it defines. 190 191 **SyntaxKind**: 192 193 - GetAccessor 194 - SetAccessor 195 196 ```typescript 197 class Klass { 198 //- @foo defines/binding VName("Klass.foo:getter", _, _, _, _) 199 //- @foo defines/binding VName("Klass.foo", _, _, _, _) 200 get foo() {} 201 //- @foo defines/binding VName("Klass.foo:setter", _, _, _, _) 202 set foo(newFoo) {} 203 } 204 205 class KlassNoGetter { 206 //- @foo defines/binding VName("KlassNoGetter.foo:setter", _, _, _, _) 207 //- @foo defines/binding VName("KlassNoGetter.foo", _, _, _, _) 208 set foo(newFoo) {} 209 } 210 ``` 211 212 #### Export Assignment 213 214 **Form**: 215 216 - default export: `default` 217 - equals export: `export=` 218 219 **Notes**: `export default` is semantically equivalent to exporting a variable 220 named `default`. `export =` exports a variable named `export=`. 221 222 **SyntaxKind**: 223 224 - `ExportAssignment` 225 226 ```typescript 227 //- @default defines/binding VName("default", _, _, _, _) 228 export default myExport; 229 //- @"export =" defines/binding VName("export=", _, _, _, _) 230 export = { myExport }; 231 ``` 232 233 #### Anonymous Block 234 235 **Form**: a unique, non-deterministic block name. 236 237 **Notes**: The block name is not guaranteed, because declarations within an 238 anonymous block cannot be accessed outside it. 239 240 **SyntaxKind**: 241 242 - `ArrowFunction` 243 - `Block` that does not have a `FunctionDeclaration` or `MethodDeclaration` 244 parent 245 246 ```typescript 247 let af = () => { 248 //- @decl defines/binding VName(_, _, _, _, _) 249 let decl; 250 }; 251 ``` 252 253 ### VName corpus 254 255 Project-specific, defined by the compilation unit passed to the indexer. 256 257 ### VName root 258 259 Project-specific, defined by the compilation unit passed to the indexer. 260 261 ### VName path 262 263 See the module name discussion in the [README](./README.md). In short, 264 265 - For file nodes 266 - the entire file path, relative to the corpus and root. A file `a/b/c.ts` 267 will have a file node with path `a/b/c.ts`. 268 - For TypeScript nodes 269 - the file path stripped of `.d.ts` or `.ts` extensions, relative to the 270 project root. A TypeScript anchor in a file `a/b/c.ts` with project root 271 `a/` will define a node with path `b/c`. 272 273 ### VName language 274 275 Always `typescript`.