ember-cli / ember-cli/babel-plugin-debug-macros

Wrong transpilation when optional chaining is used in `assert()`

未关闭
#89 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
TypeScript
星标
13
派生
13
PR 合并指标
30 天内没有已合并 PR

描述

I have this code:

```
assert('Need profile data', this.args.window.profile?.data);
```

When optional chaining is *not* transpiled, things work fine. The line above is transformed to:

```
(true && !(this.args.window.profile?.data) && (0, _debug.assert)('Need profile data', this.args.window.profile?.data));
```

However some automated dependency updates including most Babel stuff changed behavior, in that optional chaining is now transpiled, I believe due to some bug in Chrome, see https://github.com/babel/babel/issues/13145.

But now the transform yields this broken code:

```
var _this$args$window$pro;

(true && !(_this$args$window$pro.data) && (0, _debug.assert)('Need profile data', (_this$args$window$pro = this.args.window.profile) === null || _this$args$window$pro === void 0 ? void 0 : _this$args$window$pro.data));
```

Which throws with `Cannot read properties of undefined (reading 'data')` as at the time the `!(_this$args$window$pro.data)` expression is evaluated `_this$args$window$pro` is still undefined!

贡献指南

这个仓库没有索引到贡献指南

调研方向

首先,使用 Babel 对 optional-chaining 的处理以及所示的 assert 表达式,复现 issue 中的转换。跟踪生成的 guard 为什么会在为临时变量赋值之前读取它,然后验证转换后的代码是否保留了原始的短路行为,并且在 profile 为 undefined 时不会抛出异常。

由索引模型根据 Issue 内容生成。

评估

技术栈
babel, typescript
领域
tooling
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。