Skip to content
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

fix optional chaining undefined for non macros #2215

Open
wants to merge 2 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/macros/src/babel/macros-babel-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,32 @@ export default function main(context: typeof Babel): unknown {
// our getConfig and getOwnConfig macros are supposed to be able to absorb
// optional chaining. To make that work we need to see the optional chaining
// before preset-env compiles them away.

function fromGetConfig(path: NodePath<t.MemberExpression | t.OptionalMemberExpression> | null) {
while (path) {
const obj = path.get('object');
if (obj.isCallExpression()) {
const callee = obj.get('callee');
if (
callee.referencesImport('@embroider/macros', 'getOwnConfig') ||
callee.referencesImport('@embroider/macros', 'getGlobalConfig') ||
callee.referencesImport('@embroider/macros', 'getConfig')
) {
return true;
}
}
if (obj.isMemberExpression() || obj.isOptionalMemberExpression()) {
path = obj;
} else {
return false;
}
}
return false;
}

(visitor as any).OptionalMemberExpression = {
enter(path: NodePath<t.OptionalMemberExpression>, state: State) {
if (state.opts.mode === 'compile-time') {
if (state.opts.mode === 'compile-time' && fromGetConfig(path as any)) {
let result = new Evaluator({ state }).evaluate(path);
if (result.confident) {
path.replaceWith(buildLiterals(result.value, context));
Expand Down
9 changes: 9 additions & 0 deletions packages/macros/tests/babel/get-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ describe(`getConfig`, function () {
expect(code).toMatch(/doSomething\(undefined\)/);
});

buildTimeTest(`does not collapse nullish coalescing for non embroider macros, nullish case`, () => {
let code = transform(`
const aKnownValue = {};
aKnownValue.foo = true;
result = aKnownValue?.foo;
`);
expect(code).toMatch(`result = aKnownValue`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it still ?.foo?

Copy link
Contributor Author

@patricklx patricklx Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 2 runs with the same test. with and without preset env. So one converts it to manual null check, cannot make one version that matches both results

});

runTimeTest(`runtime getConfig is still present in runtime mode when using optional chaining`, () => {
let code = transform(`
import { getConfig } from '@embroider/macros';
Expand Down
Loading