microsoft/TypeScript

Node#getText() will throw if node was made with another transformer

mohsen1 opened this issue · 6 comments

When a transformers adds a new node to the AST, reading that node text will throw an error because sourceFile is not reflecting the newly added node
TypeScript Version: 2.9.0

Search Terms:
Transformers, AST, getText()

Code
reproduction repo

See Travis Logs and comments in index.ts

Expected behavior:
No Error
Actual behavior:

Error

home/travis/build/mohsen1/ts-transformer-bug/node_modules/typescript/lib/typescript.js:102266
            ts.Debug.assert(!ts.positionIsSynthesized(this.pos) && !ts.positionIsSynthesized(this.end), message || "Node must have a real position for this operation");

Have you tried calling getTextOfNode instead?

I don't see getTextOfNode exported in typescript.d.ts.

mohsen1/ts-transformer-bug#1

@RyanCavanaugh Can you take a look?

@mohsen1 this call stack doesn't look related to what you were describing?

tsc
node dist/index.js

C:\Throwaway\ts-transformer-bug\node_modules\typescript\lib\typescript.js:13089
        return node.kind === 272 /* SourceFile */;
                    ^

TypeError: Cannot read property 'kind' of undefined
    at Object.isSourceFile (C:\Throwaway\ts-transformer-bug\node_modules\typescript\lib\typescript.js:13089:21)
    at Object.printNode (C:\Throwaway\ts-transformer-bug\node_modules\typescript\lib\typescript.js:72638:40)
    at compile (C:\Throwaway\ts-transformer-bug\dist\index.js:28:27)
    at Object.<anonymous> (C:\Throwaway\ts-transformer-bug\dist\index.js:53:1)
    at Module._compile (module.js:649:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
    at Function.Module._load (module.js:493:3)

@RyanCavanaugh Not sure how you got Cannot read property 'kind' of undefined Please run yarn test or npm test .

I'm still not able to read text of a synthetically added node.

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.