Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ export default function (): PluginObject {
return;
}

// Do not annotate standalone expression statements.
// Standalone expression statements are executed solely for their side effects;
// marking them pure causes minifiers to drop them as dead code.
if (path.parentPath.isExpressionStatement()) {
Comment thread
clydin marked this conversation as resolved.
return;
}

const callee = path.get('callee');
if (
(callee.isFunctionExpression() || callee.isArrowFunctionExpression()) &&
Expand All @@ -96,6 +103,13 @@ export default function (): PluginObject {
return;
}

// Do not annotate standalone expression statements.
// Standalone expression statements are executed solely for their side effects;
// marking them pure causes minifiers to drop them as dead code.
if (path.parentPath.isExpressionStatement()) {
Comment thread
clydin marked this conversation as resolved.
return;
}

const { topLevelSafeMode = false } = state.opts;

if (!topLevelSafeMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,28 @@ describe('pure-toplevel-functions Babel plugin', () => {
`),
);

it('does not annotate top-level call expression statements', testCaseNoChange('someCall();'));

it(
'does not annotate top-level IIFE expression statements',
testCaseNoChange(`
(() => {
_defineProperty(_Injector, 'ɵprov', ɵɵdefineInjectable({ token: _Injector }));
_defineProperty(_Injector, '__NG_ELEMENT_ID__', -1);
})();
`),
);

it(
'does not annotate top-level static block helper calls',
testCaseNoChange(`
_staticBlock();
_staticBlock2();
`),
);

it('does not annotate top-level new expression statements', testCaseNoChange('new SomeClass();'));

describe('topLevelSafeMode: true', () => {
it(
'annotates top-level `new InjectionToken` expressions',
Expand Down
Loading