@@ -2,11 +2,14 @@ import { nodeModules } from './utils'
2
2
import * as semver from 'semver'
3
3
import { createLanguageService } from './dummyLanguageService'
4
4
import { getCannotFindCodes } from './utils/cannotFindCodes'
5
+ import { Configuration } from './types'
5
6
6
7
// used at testing only
7
8
declare const __TS_SEVER_PATH__ : string | undefined
8
9
9
- const getPatchedNavModule = ( ) : { getNavigationTree ( ...args ) } => {
10
+ type AdditionalFeatures = Record < 'arraysTuplesNumberedItems' , boolean >
11
+
12
+ const getPatchedNavModule = ( additionalFeatures : AdditionalFeatures ) : { getNavigationTree ( ...args ) } => {
10
13
// what is happening here: grabbing & patching NavigationBar module contents from actual running JS
11
14
const tsServerPath = typeof __TS_SEVER_PATH__ !== 'undefined' ? __TS_SEVER_PATH__ : require . main ! . filename
12
15
// current lib/tsserver.js
@@ -23,30 +26,60 @@ const getPatchedNavModule = (): { getNavigationTree(...args) } => {
23
26
linesOffset : number
24
27
addString ?: string
25
28
removeLines ?: number
29
+ // transform?: (found: string, content: string, position: number) => [string?, string?]
26
30
}
27
31
28
32
const patchLocations : PatchLocation [ ] = [
29
33
{
30
34
searchString : 'function addChildrenRecursively(node)' ,
31
35
linesOffset : 7 ,
32
- addString : `
36
+ addString : /* js */ `
33
37
case ts.SyntaxKind.JsxSelfClosingElement:
34
38
addLeafNode(node)
35
39
break;
36
40
case ts.SyntaxKind.JsxElement:
37
41
startNode(node)
38
42
ts.forEachChild(node, addChildrenRecursively);
39
43
endNode()
40
- break` ,
44
+ break; ` ,
41
45
} ,
46
+ {
47
+ searchString : 'case 262 /* SyntaxKind.TypeAliasDeclaration */' ,
48
+ linesOffset : 3 ,
49
+ // https://github.com/microsoft/TypeScript/pull/52558/
50
+ addString : /* js */ `
51
+ case ts.SyntaxKind.TypeAliasDeclaration:
52
+ addNodeWithRecursiveChild(node, node.type);
53
+ break;
54
+ ` ,
55
+ } ,
56
+ {
57
+ searchString : 'case 262 /* SyntaxKind.TypeAliasDeclaration */' ,
58
+ linesOffset : 0 ,
59
+ removeLines : 1 ,
60
+ } ,
61
+ // prettier-ignore
62
+ ...additionalFeatures . arraysTuplesNumberedItems ? [ {
63
+ searchString : 'function addChildrenRecursively(node)' ,
64
+ linesOffset : 7 ,
65
+ addString : /* js */ `
66
+ case ts.SyntaxKind.TupleType:
67
+ case ts.SyntaxKind.ArrayLiteralExpression:
68
+ const { elements } = node;
69
+ for (const [i, element] of elements.entries()) {
70
+ addNodeWithRecursiveChild(element, element, ts.setTextRange(ts.factory.createIdentifier(i.toString()), element));
71
+ }
72
+ break;
73
+ ` ,
74
+ } ] : [ ] ,
42
75
{
43
76
searchString : 'return "<unknown>";' ,
44
77
linesOffset : - 1 ,
45
- addString : `
78
+ addString : /* js */ `
46
79
case ts.SyntaxKind.JsxSelfClosingElement:
47
- return getNameFromJsxTag(node);
80
+ return getNameFromJsxTag(node);
48
81
case ts.SyntaxKind.JsxElement:
49
- return getNameFromJsxTag(node.openingElement);` ,
82
+ return getNameFromJsxTag(node.openingElement);` ,
50
83
} ,
51
84
]
52
85
@@ -131,10 +164,11 @@ const getPatchedNavModule = (): { getNavigationTree(...args) } => {
131
164
132
165
let navModule : { getNavigationTree : any }
133
166
134
- export const getNavTreeItems = ( info : ts . server . PluginCreateInfo , fileName : string ) => {
135
- if ( ! navModule ) navModule = getPatchedNavModule ( )
167
+ export const getNavTreeItems = ( info : ts . server . PluginCreateInfo , fileName : string , additionalFeatures : AdditionalFeatures ) => {
168
+ if ( ! navModule ) navModule = getPatchedNavModule ( additionalFeatures )
136
169
const program = info . languageService . getProgram ( )
137
170
if ( ! program ) throw new Error ( 'no program' )
171
+ // sourceFile is unreliable to use here, as it's not the same as the one used within original getNavigationTree
138
172
const sourceFile = program ?. getSourceFile ( fileName )
139
173
if ( ! sourceFile ) throw new Error ( 'no sourceFile' )
140
174
0 commit comments