Skip to content

Commit f806e34

Browse files
crisbetoalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): elide setClassDebugInfo calls
Angular v17 adds another dev-mode-only function that needs to be removed called `ɵsetClassDebugInfo`. These changes update the Babel plugin to account for it. (cherry picked from commit 2602679)
1 parent 056982f commit f806e34

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,19 @@ const SET_CLASS_METADATA_NAME = 'ɵsetClassMetadata';
1818
*/
1919
const SET_CLASS_METADATA_ASYNC_NAME = 'ɵsetClassMetadataAsync';
2020

21+
/**
22+
* Name of the function that sets debug information on classes.
23+
*/
24+
const SET_CLASS_DEBUG_INFO_NAME = 'ɵsetClassDebugInfo';
25+
2126
/**
2227
* Provides one or more keywords that if found within the content of a source file indicate
2328
* that this plugin should be used with a source file.
2429
*
2530
* @returns An a string iterable containing one or more keywords.
2631
*/
2732
export function getKeywords(): Iterable<string> {
28-
return [SET_CLASS_METADATA_NAME, SET_CLASS_METADATA_ASYNC_NAME];
33+
return [SET_CLASS_METADATA_NAME, SET_CLASS_METADATA_ASYNC_NAME, SET_CLASS_DEBUG_INFO_NAME];
2934
}
3035

3136
/**
@@ -51,7 +56,8 @@ export default function (): PluginObj {
5156
if (
5257
calleeName !== undefined &&
5358
(isRemoveClassMetadataCall(calleeName, callArguments) ||
54-
isRemoveClassmetadataAsyncCall(calleeName, callArguments))
59+
isRemoveClassmetadataAsyncCall(calleeName, callArguments) ||
60+
isSetClassDebugInfoCall(calleeName, callArguments))
5561
) {
5662
// The metadata function is always emitted inside a function expression
5763
const parent = path.getFunctionParent();
@@ -98,6 +104,16 @@ function isRemoveClassmetadataAsyncCall(
98104
);
99105
}
100106

107+
/** Determines if a function call is a call to `setClassDebugInfo`. */
108+
function isSetClassDebugInfoCall(name: string, args: types.CallExpression['arguments']): boolean {
109+
return (
110+
name === SET_CLASS_DEBUG_INFO_NAME &&
111+
args.length === 2 &&
112+
types.isIdentifier(args[0]) &&
113+
types.isObjectExpression(args[1])
114+
);
115+
}
116+
101117
/** Determines if a node is an inline function expression. */
102118
function isInlineFunction(node: types.Node): boolean {
103119
return types.isFunctionExpression(node) || types.isArrowFunctionExpression(node);

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,25 @@ describe('elide-angular-metadata Babel plugin', () => {
186186
`,
187187
}),
188188
);
189+
190+
it(
191+
'elides arrow-function-based ɵsetClassMetadataAsync',
192+
testCase({
193+
input: `
194+
import { Component } from '@angular/core';
195+
class SomeClass {}
196+
(() => {
197+
(typeof ngDevMode === 'undefined' || ngDevMode) &&
198+
i0.ɵsetClassDebugInfo(SomeClass, { className: 'SomeClass' });
199+
})();
200+
`,
201+
expected: `
202+
import { Component } from "@angular/core";
203+
class SomeClass {}
204+
(() => {
205+
(typeof ngDevMode === "undefined" || ngDevMode) && void 0;
206+
})();
207+
`,
208+
}),
209+
);
189210
});

0 commit comments

Comments
 (0)