JamesHenry / typescript-estree

:sparkles: A parser that converts TypeScript source code into an ESTree-compatible form
https://jameshenry.blog
Other
84 stars 13 forks source link

non-existent file: missing parent field in all ts nodes #77

Closed armano2 closed 5 years ago

armano2 commented 5 years ago

@uniqueiniquity i was doing some refactoring on converter and i found one issue when you try running tests for non-existent file.

in this test scenario non of nodes has parent field filled with value, as opposed to normal run when for same sourceCode you have parent fields filled.

example test scenario:

// tests/fixtures/typescript/basics/class-with-mixin.src.ts
function M<T extends Constructor<{}>>(Base: T) {
    return class extends Base { }
}

class X extends M<any>(C) implements I { }

class C { }
interface I { }
type Constructor<T> = new (...args: any[]) => T;

will fail because of that with error

TypeError: Cannot read property 'kind' of undefined
      at Object.canContainDirective (src/node-utils.ts:5672:33)
      at convertBodyExpressionsToDirectives (src/convert.ts:18875:155)
      at convert (src/convert.ts:19401:13)
      at converter (src/convert.ts:18818:16)
      at convertChild (src/convert.ts:18835:16)
      at convert (src/convert.ts:19627:80)
      at converter (src/convert.ts:18818:16)
      at convertChild (src/convert.ts:18835:16)
      at node.statements.forEach.statement (src/convert.ts:19373:95)
          at Array.forEach (<anonymous>)

from my investigation its seems like a lot of tests will produce incorrect AST when running as non-existent file.

armano2 commented 5 years ago

Looks like there is difference in behaviour between those cases:

when i check if ast.statements[0].parent is present in this function i'm gettin SourceFile node

function getASTFromProject(code: string, options: ParserOptions) {

when i do same thing in

function getASTAndDefaultProject(code: string, options: ParserOptions) {

parent is undefined for same sourceCode.

JamesHenry commented 5 years ago

:tada: This issue has been resolved in version 9.0.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket: