Skip to content

TypeError: Cannot read property 'length' of undefined at Object.forEachChild #18301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lambda-fairy opened this issue Sep 7, 2017 · 9 comments
Assignees
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue

Comments

@lambda-fairy
Copy link

lambda-fairy commented Sep 7, 2017

TypeScript Version: 2.5.2 and 2.6.0-dev.20170906 (can reproduce on both versions)

Code

/**
 * @typedef LoadCallback
 * @type {function}
 */
type LoadCallback = void;

Expected behavior: Code compiles successfully.

Actual behavior:
Compiler crashes with the following error:

/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:9921
                for (var _i = 0, _a = node.jsDocPropertyTags; _i < _a.length; _i++) {
                                                                     ^

TypeError: Cannot read property 'length' of undefined
    at Object.forEachChild (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:9921:70)
    at setParentPointers (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:17392:12)
    at /tmp/chris/downloads/node_modules/typescript/lib/tsc.js:17392:64
    at visitNode (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:9533:24)
    at Object.forEachChild (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:9918:25)
    at setParentPointers (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:17392:12)
    at /tmp/chris/downloads/node_modules/typescript/lib/tsc.js:17392:64
    at visitNodes (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:9542:30)
    at Object.forEachChild (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:9891:24)
    at setParentPointers (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:17392:12)
    at bindChildrenWorker (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:15178:25)
    at bindChildren (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:15140:17)
    at bindContainer (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:15122:17)
    at bind (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:16214:21)
    at bindEach (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:15157:21)
    at visitNodes (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:9538:24)
    at Object.forEachChild (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:9707:24)
    at bindEachChild (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:15165:16)
    at bindChildrenWorker (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:15254:21)
    at bindChildren (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:15140:17)
    at bindContainer (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:15093:17)
    at bind (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:16214:21)
    at bindSourceFile (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:14851:17)
    at Object.bindSourceFile (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:14809:9)
    at initializeTypeChecker (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:36496:20)
    at Object.createTypeChecker (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:18543:9)
    at getDiagnosticsProducingTypeChecker (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:56042:93)
    at Object.getGlobalDiagnostics (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:56370:53)
    at compileProgram (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:59092:78)
    at compile (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:59051:26)
    at performCompilation (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:58940:33)
    at Object.executeCommandLine (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:58883:9)
    at Object.<anonymous> (/tmp/chris/downloads/node_modules/typescript/lib/tsc.js:59241:4)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/tmp/chris/downloads/node_modules/typescript/bin/tsc:2:1)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:598:3
@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation labels Sep 7, 2017
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 2.6 milestone Sep 7, 2017
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Sep 7, 2017

Verified . The fix is simple, but why are we even touching JSDoc in .ts files?

@weswigham
Copy link
Member

@DanielRosenwasser we still parse it to get documentation, I believe

@lambda-fairy
Copy link
Author

Thanks for the quick response! Good to hear that it's an easy fix.

Will this be fixed in 2.5.x as well? A few backend services at work cannot update their compiler yet, because they depend on an old version of protobufjs which triggers this bug. Low priority of course, but it would be nice not having this bit of friction in the mean time.

@sandersn
Copy link
Member

sandersn commented Sep 7, 2017

@weswigham We do parse it but should only be binding it in .js files.

@sandersn
Copy link
Member

sandersn commented Sep 7, 2017

This crashes in the parser, not the binder, for me, from #17352 onwards. #17352 is where I modified @typedef parsing heavily to support similar parsing in @param locations. Here's my call stack:

     TypeError: Cannot read property 'length' of undefined
      at forEachChild (src/compiler/parser.ts:441:35)
      at visitNode (src/compiler/parser.ts:770:21)
      at visitNode (src/compiler/parser.ts:36:24)
      at forEachChild (src/compiler/parser.ts:438:25)
      at visitNode (src/compiler/parser.ts:770:21)
      at visitNodes (src/compiler/parser.ts:45:32)
      at forEachChild (src/compiler/parser.ts:411:24)
      at visitNode (src/compiler/parser.ts:775:29)
      at visitNodes (src/compiler/parser.ts:45:32)
      at forEachChild (src/compiler/parser.ts:224:24)
      at fixupParentReferences (src/compiler/parser.ts:758:13)
      at parseSourceFileWorker (src/compiler/parser.ts:725:17)
      at Object.parseSourceFile (src/compiler/parser.ts:620:28)
      at Object.createSourceFile (src/compiler/parser.ts:452:31)
      at createSourceFileAndAssertInvariants (src/harness/harness.ts:824:31)
      at createCompilerHost (src/harness/harness.ts:918:40)
      at Object.compileFiles (src/harness/harness.ts:1174:34)
      at Context.<anonymous> (src/harness/compilerRunner.ts:122:49)

@sandersn
Copy link
Member

sandersn commented Sep 7, 2017

forEachChild only visits JSDocTypeLiteral.jsDocPropertyTags, not JSDocTypeLiteral.jsDocTypeTag. Since one and only one should be defined, it should visit whichever one is defined.

Alternatively, JSDocTypeLiterals should only be created for type definitions with jsdocPropertyTags. It would be nice to raise subsequent @type tags to their parent @typedef. I'll have to make sure that doesn't break anything with the ability of services to find definitions.

@sandersn
Copy link
Member

sandersn commented Sep 8, 2017

Fix is up at #18333. I implemented the second alternative.

@lambda-fairy
Copy link
Author

Should this issue have the "fixed" label? The other bugs closed in 2.5.3 all do.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Oct 4, 2017
@s2zaman
Copy link

s2zaman commented Nov 29, 2017

If this may be related angular/angular-cli#5053 (comment)
and may help here.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

7 participants
@sandersn @DanielRosenwasser @lambda-fairy @weswigham @s2zaman @mhegazy and others