nonara / ts-patch

Augment the TypeScript compiler to support extended functionality
MIT License
760 stars 26 forks source link

Program transform errors when patching tsserver.js #121

Closed GoogleFeud closed 11 months ago

GoogleFeud commented 1 year ago

I have an existing typescript transformer that can potentially add new types and edit existing ones, and I'd like for the new / edited types to be shown in the editor, so I'm making a program transform out of the already existing transformer. The program transformer works correctly when passed to the patched tsc script, however, I'm getting a few different errors when trying to view the code in VSC when the tsserver and tsserverlibrary scripts are patched:

Adding anything to the code:

Error: Debug Failure.
    at Object.releaseDocumentWithKey (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:131754:28)
    at Object.onReleaseOldSourceFile (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:140031:26)
    at Proxy.originalCreateProgram (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:117283:24)
    at Object.createProgram (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:508:28)
    at createProgram (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:117077:41)
    at synchronizeHostData (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:139989:17)
    at Object.getProgram (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:140063:7)
    at ConfiguredProject2.updateGraphWorker (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:178868:43)
    at ConfiguredProject2.updateGraph (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:178720:34)
    at ConfiguredProject2.updateGraph (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:179973:26)
    at updateProjectIfDirty (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:180426:37)
    at ConfiguredProject2.getLanguageService (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:178338:9)

Clicking anywhere in the code:

TypeError: Cannot read properties of undefined (reading 'kind')
    at isVariableDeclaration (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:26869:17)
    at isSingleVariableDeclaration (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:136362:12)
    at tryGetFunctionFromVariableDeclaration (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:136365:10)
    at getFunctionInfo (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:136349:18)
    at Object.getRefactorActionsToConvertFunctionExpressions [as getAvailableActions] (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:136270:18)
    at c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:134094:224
    at flatMapIterator (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:907:21)
    at flatMapIterator.next (<anonymous>)
    at arrayFrom (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:1444:16)
    at Object.getApplicableRefactors (c:\Users\me\Desktop\testProject\node_modules\typescript\lib\tsserver.js:134092:12)

Something important to note about the original transformer is that it doesn't use the instance of ts the program transformer function provides. I'm not sure if this is of importance as it works with tsc just fine, but I've started to rewrite it to use the provided instance.

Source code for node transformer Source code for program transformer

Edit: Maybe the second error happens because the code in the text editor doesn't match the code passed to the new program? Edit: The initial transpilation works correctly, errors start coming after that when I interact with the editor in some way, and code errors are displayed in the editor incorrectly, so the discrepancy between the code is definitely to blame for the second error.

GoogleFeud commented 1 year ago

After some more testing, changing the source file in any way seems to break the language server, which makes sense. I wonder if it's possible to somehow keep the types but not emit the nodes?