Skip to content

TypeError: traveler[child.type] is not a functionΒ #39

@morganney

Description

@morganney

Example code to reproduce the issue:

const comments = []
const source = [
  "Promise.all([import('foo')])",
  '// Some comment'
].join('\n')
const ast = acorn.parse(source, {
  onComment: comments,
  sourceType: 'module',
  locations: true,
  ecmaVersion: 2023
})
astravel.attachComments(ast, comments)

You can see a demo on the astring demo site (be sure to check the Include comments checkbox). It produces the following stack trace:

TypeError: traveler2[child.type] is not a function

TypeError: traveler2[child.type] is not a function
    at attachCommentsToNode (https://cdn.skypack.dev/-/astravel@v0.6.1-J7Ht2wGP9zrf7okX2giu/dist=es2019,mode=imports/optimized/astravel.js:456:26)
    at Object.ArrayExpression (https://cdn.skypack.dev/-/astravel@v0.6.1-J7Ht2wGP9zrf7okX2giu/dist=es2019,mode=imports/optimized/astravel.js:481:5)
    at Object.go (https://cdn.skypack.dev/-/astravel@v0.6.1-J7Ht2wGP9zrf7okX2giu/dist=es2019,mode=imports/optimized/astravel.js:12:22)
    at Object.CallExpression (https://cdn.skypack.dev/-/astravel@v0.6.1-J7Ht2wGP9zrf7okX2giu/dist=es2019,mode=imports/optimized/astravel.js:271:12)
    at Object.go (https://cdn.skypack.dev/-/astravel@v0.6.1-J7Ht2wGP9zrf7okX2giu/dist=es2019,mode=imports/optimized/astravel.js:12:22)
    at Object.ExpressionStatement (https://cdn.skypack.dev/-/astravel@v0.6.1-J7Ht2wGP9zrf7okX2giu/dist=es2019,mode=imports/optimized/astravel.js:58:10)
    at attachCommentsToNode (https://cdn.skypack.dev/-/astravel@v0.6.1-J7Ht2wGP9zrf7okX2giu/dist=es2019,mode=imports/optimized/astravel.js:456:26)
    at Object.Block$1 (https://cdn.skypack.dev/-/astravel@v0.6.1-J7Ht2wGP9zrf7okX2giu/dist=es2019,mode=imports/optimized/astravel.js:471:3)
    at Module.attachComments (https://cdn.skypack.dev/-/astravel@v0.6.1-J7Ht2wGP9zrf7okX2giu/dist=es2019,mode=imports/optimized/astravel.js:491:22)
    at HTMLTextAreaElement.update (https://david.bonnet.cc/astring/demo/:78:22)

Seems to be specific to a Promise.all expression that includes an ImportExpression, e.g., Promise.all([import('foo')]). Any comment type following that will produce the error.

EDIT: The error can be triggered with this simplified source

[import('foo')]
// Some comment

Potentially related to davidbonnet/astring#562.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions