@@ -2,11 +2,14 @@ import { nodeModules } from './utils'
22import * as semver from 'semver'
33import { createLanguageService } from './dummyLanguageService'
44import { getCannotFindCodes } from './utils/cannotFindCodes'
5+ import { Configuration } from './types'
56
67// used at testing only
78declare const __TS_SEVER_PATH__ : string | undefined
89
9- const getPatchedNavModule = ( ) : { getNavigationTree ( ...args ) } => {
10+ type AdditionalFeatures = Record < 'arraysTuplesNumberedItems' , boolean >
11+
12+ const getPatchedNavModule = ( additionalFeatures : AdditionalFeatures ) : { getNavigationTree ( ...args ) } => {
1013 // what is happening here: grabbing & patching NavigationBar module contents from actual running JS
1114 const tsServerPath = typeof __TS_SEVER_PATH__ !== 'undefined' ? __TS_SEVER_PATH__ : require . main ! . filename
1215 // current lib/tsserver.js
@@ -23,30 +26,60 @@ const getPatchedNavModule = (): { getNavigationTree(...args) } => {
2326 linesOffset : number
2427 addString ?: string
2528 removeLines ?: number
29+ // transform?: (found: string, content: string, position: number) => [string?, string?]
2630 }
2731
2832 const patchLocations : PatchLocation [ ] = [
2933 {
3034 searchString : 'function addChildrenRecursively(node)' ,
3135 linesOffset : 7 ,
32- addString : `
36+ addString : /* js */ `
3337 case ts.SyntaxKind.JsxSelfClosingElement:
3438 addLeafNode(node)
3539 break;
3640 case ts.SyntaxKind.JsxElement:
3741 startNode(node)
3842 ts.forEachChild(node, addChildrenRecursively);
3943 endNode()
40- break` ,
44+ break; ` ,
4145 } ,
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+ } ] : [ ] ,
4275 {
4376 searchString : 'return "<unknown>";' ,
4477 linesOffset : - 1 ,
45- addString : `
78+ addString : /* js */ `
4679 case ts.SyntaxKind.JsxSelfClosingElement:
47- return getNameFromJsxTag(node);
80+ return getNameFromJsxTag(node);
4881 case ts.SyntaxKind.JsxElement:
49- return getNameFromJsxTag(node.openingElement);` ,
82+ return getNameFromJsxTag(node.openingElement);` ,
5083 } ,
5184 ]
5285
@@ -131,10 +164,11 @@ const getPatchedNavModule = (): { getNavigationTree(...args) } => {
131164
132165let navModule : { getNavigationTree : any }
133166
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 )
136169 const program = info . languageService . getProgram ( )
137170 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
138172 const sourceFile = program ?. getSourceFile ( fileName )
139173 if ( ! sourceFile ) throw new Error ( 'no sourceFile' )
140174
0 commit comments