Skip to content

[Bug]: Babel crashes when using typescript union type #14345

@danieltroger

Description

@danieltroger

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

const selector_map = Math.random > 0.5 ? new Map<string, string>() : undefined;
const selectors = selector_map?.keys();

for (const selector of (selectors as IterableIterator<string> | undefined) || []) {
}

Configuration file name

babel.config.js

Configuration

(BUILD_TARGET is in this case ie11)

let extraPlugins = process.env.BUILD_TARGET === "ie11" ? ["@parcel/babel-plugin-transform-runtime"] : [];
module.exports = {
  "presets": ["@parcel/babel-preset-env", "@babel/preset-typescript"],
  "plugins": extraPlugins,
};

Current and expected behavior

Current behavior:

babel prints this error and something (maybe parcel?) segfaults:

🚨 Build failed.

@parcel/transformer-babel: [project_dir]/src/main.ts: Property types[0] of UnionTypeAnnotation expected node to be of a type ["FlowType"] but instead got "TSUnionType"

  TypeError: [project_dir]/src/main.ts: Property types[0] of UnionTypeAnnotation expected node to be of a type ["FlowType"] but instead got "TSUnionType"
  at validate ([project_dir]/.yarn/cache/@babel-types-npm-7.17.0-3c936b54e4-12e5a28798.zip/node_modules/@babel/types/lib/definitions/utils.js:131:11)
  at validator ([project_dir]/.yarn/cache/@babel-types-npm-7.17.0-3c936b54e4-12e5a28798.zip/node_modules/@babel/types/lib/definitions/utils.js:102:7)
  at Object.validate ([project_dir]/.yarn/cache/@babel-types-npm-7.17.0-3c936b54e4-12e5a28798.zip/node_modules/@babel/types/lib/definitions/utils.js:228:7)
  at validateField ([project_dir]/.yarn/cache/@babel-types-npm-7.17.0-3c936b54e4-12e5a28798.zip/node_modules/@babel/types/lib/validators/validate.js:24:9)
  at validate ([project_dir]/.yarn/cache/@babel-types-npm-7.17.0-3c936b54e4-12e5a28798.zip/node_modules/@babel/types/lib/validators/validate.js:17:3)
  at String.builder ([project_dir]/.yarn/cache/@babel-types-npm-7.17.0-3c936b54e4-12e5a28798.zip/node_modules/@babel/types/lib/builders/builder.js:39:27)
  at unionTypeAnnotation ([project_dir]/.yarn/cache/@babel-types-npm-7.17.0-3c936b54e4-12e5a28798.zip/node_modules/@babel/types/lib/builders/generated/index.js:813:27)
  at createFlowUnionType ([project_dir]/.yarn/cache/@babel-types-npm-7.17.0-3c936b54e4-12e5a28798.zip/node_modules/@babel/types/lib/builders/flow/createFlowUnionType.js:18:47)
  at NodePath.LogicalExpression ([project_dir]/.yarn/cache/@babel-traverse-npm-7.17.3-c2bff3e671-780d7ecf71.zip/node_modules/@babel/traverse/lib/path/inference/inferers.js:142:12)
  at NodePath._getTypeAnnotation ([project_dir]/.yarn/cache/@babel-traverse-npm-7.17.3-c2bff3e671-780d7ecf71.zip/node_modules/@babel/traverse/lib/path/inference/index.js:82:22)

PID 55422 received SIGSEGV for address: 0xb4f8
0   segfault-handler.node               0x00000001051b3800 _ZL16segfault_handleriP9__siginfoPv + 288
1   libsystem_platform.dylib            0x00007ff801d85e2d _sigtramp + 29
2   ???                                 0x0000700011b44e88 0x0 + 123145599340168
3   lmdb.node                           0x000000010540bbdf _ZN3Nan11AsyncWorker12WorkCompleteEv + 31
4   lmdb.node                           0x000000010540d602 _ZN3Nan20AsyncExecuteCompleteEP9uv_work_si + 18
5   libuv.1.dylib                       0x000000010513e9bc uv__work_done + 176
6   libuv.1.dylib                       0x00000001051421f1 uv__async_io + 279
7   libuv.1.dylib                       0x0000000105151f8c uv__io_poll + 1881
8   libuv.1.dylib                       0x000000010514261c uv_run + 349
9   node                                0x0000000102e4194a _ZN4node6worker16WorkerThreadDataD2Ev + 220
10  node                                0x0000000102e41046 _ZN4node6worker6Worker3RunEv + 1770
11  node                                0x0000000102e439bb _ZZN4node6worker6Worker11StartThreadERKN2v820FunctionCallbackInfoINS2_5ValueEEEEN3$_38__invokeEPv + 49
12  libsystem_pthread.dylib             0x00007ff801d704f4 _pthread_start + 125
13  libsystem_pthread.dylib             0x00007ff801d6c00f thread_start + 15

Expected behavior: types are stripped, transpilation succeeds and output code runs.

Environment

  • Babel version: 7.17.6 (@babel/core 7.17.5)
  • Node: v17.6.0
  • Yarn: 3.1.1
  • OS: macOS 12.2 (21D49)

Possible solution

No response

Additional context

It also crashes the babel REPL:
https://babeljs.io/repl/#?browsers=IE%2011&build=&builtIns=false&corejs=3.6&spec=false&loose=false&code_lz=MYewdgzgLgBBCmAbewogE4H0C2BDADjALwwCyuUAFgHTq5gAmI2MAfDAAzUCsMA_DDDwA7mQIAeaOgCWYAOYAaOFBnzWACgCUMAFwwAro3gAzWfAYBuAFChIsBMlQYIxOEhRosefH2oBreABPCC1rK2MMGHVbaDdHTxgQYyiHD2cYXBcASSh4OgAjZBy8igxJFVk5dgAfAyNTIQZtatqAbQBdbQBvKwBfIA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Ctypescript&prettier=false&targets=&version=7.17.6&externalPlugins=&assumptions=%7B%7D

With:

/repl.ts: Property types[0] of UnionTypeAnnotation expected node to be of a type ["FlowType"] but instead got "TSUnionType"

Related: #9530 but it was already fixed, yet this error still occurs

Metadata

Metadata

Assignees

No one assigned

    Labels

    i: needs triageoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions