Skip to content

Commit 6b08efa

Browse files
crisbetodgp1130
authored andcommitted
fix(@angular-devkit/build-angular): account for arrow function IIFE
Updates the logic for removing Angular metadata and pure top-level functions to account for arrow-function-based IIFEs. Currently Angular doesn't generate arrow functions, but it's being explored in angular/angular#51637.
1 parent 99e153a commit 6b08efa

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

packages/angular_devkit/build_angular/src/tools/babel/plugins/elide-angular-metadata.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ export default function (): PluginObj {
5858
}
5959

6060
// The metadata function is always emitted inside a function expression
61-
if (!path.getFunctionParent()?.isFunctionExpression()) {
62-
return;
63-
}
61+
const parent = path.getFunctionParent();
6462

65-
// Replace the metadata function with `void 0` which is the equivalent return value
66-
// of the metadata function.
67-
path.replaceWith(path.scope.buildUndefinedNode());
63+
if (parent && (parent.isFunctionExpression() || parent.isArrowFunctionExpression())) {
64+
// Replace the metadata function with `void 0` which is the equivalent return value
65+
// of the metadata function.
66+
path.replaceWith(path.scope.buildUndefinedNode());
67+
}
6868
},
6969
},
7070
};

packages/angular_devkit/build_angular/src/tools/babel/plugins/elide-angular-metadata_spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,27 @@ describe('elide-angular-metadata Babel plugin', () => {
7979
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && void 0 })();`,
8080
}),
8181
);
82+
83+
it(
84+
'elides ɵsetClassMetadata inside an arrow-function-based IIFE',
85+
testCase({
86+
input: `
87+
import { Component } from '@angular/core';
88+
export class SomeClass {}
89+
/*@__PURE__*/ (() => { i0.ɵsetClassMetadata(Clazz, [{
90+
type: Component,
91+
args: [{
92+
selector: 'app-lazy',
93+
template: 'very lazy',
94+
styles: []
95+
}]
96+
}], null, null); })();
97+
`,
98+
expected: `
99+
import { Component } from '@angular/core';
100+
export class SomeClass {}
101+
/*@__PURE__*/ (() => { void 0 })();
102+
`,
103+
}),
104+
);
82105
});

packages/angular_devkit/build_angular/src/tools/babel/plugins/pure-toplevel-functions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ export default function (): PluginObj {
4747
}
4848

4949
const callee = path.node.callee;
50-
if (types.isFunctionExpression(callee) && path.node.arguments.length !== 0) {
50+
if (
51+
(types.isFunctionExpression(callee) || types.isArrowFunctionExpression(callee)) &&
52+
path.node.arguments.length !== 0
53+
) {
5154
return;
5255
}
5356
// Do not annotate TypeScript helpers emitted by the TypeScript compiler.

packages/angular_devkit/build_angular/src/tools/babel/plugins/pure-toplevel-functions_spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,29 @@ describe('pure-toplevel-functions Babel plugin', () => {
6565
}),
6666
);
6767

68+
it(
69+
'annotates top-level arrow-function-based IIFE assignments with no arguments',
70+
testCase({
71+
input: 'var SomeClass = (() => { function SomeClass() { } return SomeClass; })();',
72+
expected:
73+
'var SomeClass = /*#__PURE__*/(() => { function SomeClass() { } return SomeClass; })();',
74+
}),
75+
);
76+
6877
it(
6978
'does not annotate top-level IIFE assignments with arguments',
7079
testCaseNoChange(
7180
'var SomeClass = (function () { function SomeClass() { } return SomeClass; })(abc);',
7281
),
7382
);
7483

84+
it(
85+
'does not annotate top-level arrow-function-based IIFE assignments with arguments',
86+
testCaseNoChange(
87+
'var SomeClass = (() => { function SomeClass() { } return SomeClass; })(abc);',
88+
),
89+
);
90+
7591
it(
7692
'does not annotate call expressions inside function declarations',
7793
testCaseNoChange('function funcDecl() { const result = someFunction(); }'),

0 commit comments

Comments
 (0)