Skip to content

Commit

Permalink
fix(api): narrow declarations to identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 27, 2022
1 parent 1d3a021 commit 5b8448f
Show file tree
Hide file tree
Showing 44 changed files with 1,300 additions and 1,276 deletions.
3 changes: 3 additions & 0 deletions packages/api/src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
getNodeSymbol,
TypescriptContext,
removeDuplicates,
narrowDeclarationForLocation,
} from "./util"

const maxDepthExceeded: TypeInfo = { kind: "max_depth", id: getEmptyTypeId() }
Expand Down Expand Up @@ -613,6 +614,8 @@ function _generateTypeTree(
function getDeclarationInfo(
declaration: ts.Declaration
): DeclarationInfo | undefined {
declaration = narrowDeclarationForLocation(declaration)

const sourceFile = declaration.getSourceFile()
const location = getSourceFileLocation(sourceFile, declaration)

Expand Down
21 changes: 21 additions & 0 deletions packages/api/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type NodeWithTypeArguments = ts.Node & {
}
type NodeWithJsDoc = ts.Node & { jsDoc?: ts.Node[] | undefined }

type DeclarationInternal = ts.Declaration & { name?: ts.Identifier }

export type UnionTypeInternal = ts.UnionType & { id: number }
export type IntersectionTypeInternal = ts.IntersectionType & { id: number }
export type TypeReferenceInternal = ts.TypeReference & {
Expand Down Expand Up @@ -849,3 +851,22 @@ export function getSourceFileLocation(
},
}
}

/**
* Tries to find subnode that can be retrieved later
* by the client
*
* For example, consider:
*
* `export enum Enum { ... }`
*
* If the client tries to retrieve the entire `EnumDeclaration`
* node, then it will go from character 0, which will find the
* `export` token, which does not have a type. In this case,
* the node is narrowed to its identifier - `Enum` in this case
*
* @param node
*/
export function narrowDeclarationForLocation(node: ts.Declaration) {
return (node as DeclarationInternal).name ?? node
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/array.localized.tree
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type arrayOfStrings = string[]
"range": {
"start": {
"line": 0,
"character": 0
"character": 5
},
"end": {
"line": 0,
"character": 30
"character": 19
}
}
}
Expand All @@ -30,11 +30,11 @@ type arrayOfStrings = string[]
"range": {
"start": {
"line": 0,
"character": 0
"character": 5
},
"end": {
"line": 0,
"character": 30
"character": 19
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/array.tree
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type arrayOfStrings = string[]
"range": {
"start": {
"line": 0,
"character": 0
"character": 5
},
"end": {
"line": 0,
"character": 30
"character": 19
}
}
}
Expand Down
56 changes: 28 additions & 28 deletions tests/baselines/reference/arrayObjectAlias.localized.tree
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type Obj = { a: string, b: number }
"range": {
"start": {
"line": 0,
"character": 0
"character": 5
},
"end": {
"line": 0,
"character": 35
"character": 8
}
}
}
Expand All @@ -28,11 +28,11 @@ type Obj = { a: string, b: number }
"range": {
"start": {
"line": 0,
"character": 0
"character": 5
},
"end": {
"line": 0,
"character": 35
"character": 8
}
}
}
Expand All @@ -55,7 +55,7 @@ type Obj = { a: string, b: number }
},
"end": {
"line": 0,
"character": 23
"character": 14
}
}
}
Expand All @@ -71,7 +71,7 @@ type Obj = { a: string, b: number }
},
"end": {
"line": 0,
"character": 23
"character": 14
}
}
}
Expand All @@ -96,7 +96,7 @@ type Obj = { a: string, b: number }
},
"end": {
"line": 0,
"character": 33
"character": 25
}
}
}
Expand All @@ -112,7 +112,7 @@ type Obj = { a: string, b: number }
},
"end": {
"line": 0,
"character": 33
"character": 25
}
}
}
Expand Down Expand Up @@ -143,7 +143,7 @@ type Obj = { a: string, b: number }
},
"end": {
"line": 0,
"character": 23
"character": 14
}
}
}
Expand All @@ -159,7 +159,7 @@ type Obj = { a: string, b: number }
},
"end": {
"line": 0,
"character": 23
"character": 14
}
}
}
Expand All @@ -185,7 +185,7 @@ type Obj = { a: string, b: number }
},
"end": {
"line": 0,
"character": 33
"character": 25
}
}
}
Expand All @@ -201,7 +201,7 @@ type Obj = { a: string, b: number }
},
"end": {
"line": 0,
"character": 33
"character": 25
}
}
}
Expand All @@ -223,11 +223,11 @@ type arrObj = Obj[]
"range": {
"start": {
"line": 1,
"character": 0
"character": 5
},
"end": {
"line": 1,
"character": 19
"character": 11
}
}
}
Expand All @@ -240,11 +240,11 @@ type arrObj = Obj[]
"range": {
"start": {
"line": 0,
"character": 0
"character": 5
},
"end": {
"line": 0,
"character": 35
"character": 8
}
}
}
Expand All @@ -267,7 +267,7 @@ type arrObj = Obj[]
},
"end": {
"line": 0,
"character": 23
"character": 14
}
}
}
Expand All @@ -283,7 +283,7 @@ type arrObj = Obj[]
},
"end": {
"line": 0,
"character": 23
"character": 14
}
}
}
Expand All @@ -308,7 +308,7 @@ type arrObj = Obj[]
},
"end": {
"line": 0,
"character": 33
"character": 25
}
}
}
Expand All @@ -324,7 +324,7 @@ type arrObj = Obj[]
},
"end": {
"line": 0,
"character": 33
"character": 25
}
}
}
Expand All @@ -348,11 +348,11 @@ type arrObj = Obj[]
"range": {
"start": {
"line": 0,
"character": 0
"character": 5
},
"end": {
"line": 0,
"character": 35
"character": 8
}
}
}
Expand All @@ -364,11 +364,11 @@ type arrObj = Obj[]
"range": {
"start": {
"line": 0,
"character": 0
"character": 5
},
"end": {
"line": 0,
"character": 35
"character": 8
}
}
}
Expand All @@ -391,7 +391,7 @@ type arrObj = Obj[]
},
"end": {
"line": 0,
"character": 23
"character": 14
}
}
}
Expand All @@ -407,7 +407,7 @@ type arrObj = Obj[]
},
"end": {
"line": 0,
"character": 23
"character": 14
}
}
}
Expand All @@ -432,7 +432,7 @@ type arrObj = Obj[]
},
"end": {
"line": 0,
"character": 33
"character": 25
}
}
}
Expand All @@ -448,7 +448,7 @@ type arrObj = Obj[]
},
"end": {
"line": 0,
"character": 33
"character": 25
}
}
}
Expand Down
Loading

0 comments on commit 5b8448f

Please sign in to comment.