Skip to content

Commit 49e6f9c

Browse files
committed
fix(patch-outline): fix outline crash in ts5.0. Make patching more stable
1 parent e849dba commit 49e6f9c

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

typescript/src/getPatchedNavTree.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getCancellationToken, isTs5, nodeModules } from './utils'
22
import { createLanguageService } from './dummyLanguageService'
33
import { getCannotFindCodes } from './utils/cannotFindCodes'
4+
import { ensureArray } from '@zardoy/utils'
45

56
// used at testing only
67
declare const __TS_SEVER_PATH__: string | undefined
@@ -20,17 +21,18 @@ const getPatchedNavModule = (additionalFeatures: AdditionalFeatures): { getNavig
2021
returnModuleCode: string
2122
}
2223
type PatchLocation = {
23-
searchString: string
24+
searchString: string | string[]
2425
linesOffset: number
2526
addString?: string
2627
removeLines?: number
2728
// transform?: (found: string, content: string, position: number) => [string?, string?]
2829
}
30+
const addChildrenRecursivelySwitchFirstCase = ['function addChildrenRecursively(node)', 'switch (node.kind)']
2931

3032
const patchLocations: PatchLocation[] = [
3133
{
32-
searchString: 'function addChildrenRecursively(node)',
33-
linesOffset: 7,
34+
searchString: addChildrenRecursivelySwitchFirstCase,
35+
linesOffset: 1,
3436
addString: /* js */ `
3537
case ts.SyntaxKind.JsxSelfClosingElement:
3638
addLeafNode(node)
@@ -58,8 +60,8 @@ const getPatchedNavModule = (additionalFeatures: AdditionalFeatures): { getNavig
5860
},
5961
// prettier-ignore
6062
...additionalFeatures.arraysTuplesNumberedItems ? [{
61-
searchString: 'function addChildrenRecursively(node)',
62-
linesOffset: 7,
63+
searchString: addChildrenRecursivelySwitchFirstCase,
64+
linesOffset: 1,
6365
addString: /* js */ `
6466
case ts.SyntaxKind.TupleType:
6567
case ts.SyntaxKind.ArrayLiteralExpression:
@@ -106,11 +108,15 @@ const getPatchedNavModule = (additionalFeatures: AdditionalFeatures): { getNavig
106108
const lines = contentAfterModuleStart.slice(0, contentAfterModuleStart.indexOf(markerModuleEnd) + markerModuleEnd.length).split(/\r?\n/)
107109

108110
for (let { addString, linesOffset, searchString, removeLines = 0 } of patches) {
109-
const addTypeIndex = lines.findIndex(line => line.includes(searchString))
111+
let addTypeIndex = -1
112+
for (const search of ensureArray(searchString)) {
113+
const newIndexStart = addTypeIndex + 1
114+
addTypeIndex = newIndexStart + lines.slice(newIndexStart).findIndex(line => line.includes(search))
115+
}
110116
if (addTypeIndex !== -1) {
111117
lines.splice(addTypeIndex + linesOffset, removeLines, ...(addString ? [addString] : []))
112118
} else {
113-
console.warn(`TS Essentials: Failed to patch NavBar module (outline): ${searchString}`)
119+
console.error(`TS Essentials: Failed to patch NavBar module (outline): ${JSON.stringify(searchString)}`)
114120
}
115121
}
116122
const getModuleString = () => `module.exports = (ts, getNameFromJsxTag) => {\n${lines.join('\n')}\nreturn ${returnModuleCode}}`

0 commit comments

Comments
 (0)