emberjs / ember-cli-babel

Ember CLI plugin for Babel
MIT License
153 stars 119 forks source link

Ember assert is not correctly transformed when using optional chaining #410

Open mongoose700 opened 3 years ago

mongoose700 commented 3 years ago

If I start with this code

import { assert } from '@ember/debug';

const foo = { bar: { baz: 'something' } };
assert('baz', foo.bar?.baz);

it gets transformed into

const foo = {
  bar: {
    baz: 'something'
  }
};
(true && !(_foo$bar.baz) && Ember.assert('baz', (_foo$bar = foo.bar) === null || _foo$bar === void 0 ? void 0 : _foo$bar.baz));

The first check for !(_foo$bar.baz) is performed before the (_foo$bar = foo.bar) assignment, which means it will always throw an error, regardless of the actual value.

Techn1x commented 6 months ago

We found this occurred specifically in our CI environment, when CI=true

Techn1x commented 6 months ago

Some starting points for investigation

CI flags look to be used here https://github.com/emberjs/ember-cli-babel/blob/b61e71bfa923fc3d9aca9c3639ed6946e7ba8c68/lib/ember-plugins.js#L12 https://github.com/emberjs/ember-cli-babel/blob/b61e71bfa923fc3d9aca9c3639ed6946e7ba8c68/lib/babel-options-util.js#L66

From https://github.com/ember-cli/babel-plugin-debug-macros/tree/master

The assert macro can expand in a more intelligent way with the correct configuration. When babel-plugin-debug-macros is provided with the assertPredicateIndex the predicate is injected in front of the assertion in order to avoid costly assertion message generation when not needed.

And then; https://github.com/ember-cli/babel-plugin-debug-macros/blob/f40c21bdd5d6332ae1862826ed9127317a1778a6/src/utils/builder.js#L14-L41

And; https://github.com/ember-cli/babel-plugin-debug-macros/blob/f40c21bdd5d6332ae1862826ed9127317a1778a6/src/utils/builder.js#L110-L114

Techn1x commented 6 months ago

Looks like it has been reported over there as an issue https://github.com/ember-cli/babel-plugin-debug-macros/issues/89