Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): elide setClassDebugInfo calls
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
crisbeto authored and alan-agius4 committed Oct 31, 2023
1 parent 056982f commit f806e34
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ const SET_CLASS_METADATA_NAME = 'ɵsetClassMetadata';
*/
const SET_CLASS_METADATA_ASYNC_NAME = 'ɵsetClassMetadataAsync';

/**
* Name of the function that sets debug information on classes.
*/
const SET_CLASS_DEBUG_INFO_NAME = 'ɵsetClassDebugInfo';

/**
* Provides one or more keywords that if found within the content of a source file indicate
* that this plugin should be used with a source file.
*
* @returns An a string iterable containing one or more keywords.
*/
export function getKeywords(): Iterable<string> {
return [SET_CLASS_METADATA_NAME, SET_CLASS_METADATA_ASYNC_NAME];
return [SET_CLASS_METADATA_NAME, SET_CLASS_METADATA_ASYNC_NAME, SET_CLASS_DEBUG_INFO_NAME];
}

/**
Expand All @@ -51,7 +56,8 @@ export default function (): PluginObj {
if (
calleeName !== undefined &&
(isRemoveClassMetadataCall(calleeName, callArguments) ||
isRemoveClassmetadataAsyncCall(calleeName, callArguments))
isRemoveClassmetadataAsyncCall(calleeName, callArguments) ||
isSetClassDebugInfoCall(calleeName, callArguments))
) {
// The metadata function is always emitted inside a function expression
const parent = path.getFunctionParent();
Expand Down Expand Up @@ -98,6 +104,16 @@ function isRemoveClassmetadataAsyncCall(
);
}

/** Determines if a function call is a call to `setClassDebugInfo`. */
function isSetClassDebugInfoCall(name: string, args: types.CallExpression['arguments']): boolean {
return (
name === SET_CLASS_DEBUG_INFO_NAME &&
args.length === 2 &&
types.isIdentifier(args[0]) &&
types.isObjectExpression(args[1])
);
}

/** Determines if a node is an inline function expression. */
function isInlineFunction(node: types.Node): boolean {
return types.isFunctionExpression(node) || types.isArrowFunctionExpression(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,25 @@ describe('elide-angular-metadata Babel plugin', () => {
`,
}),
);

it(
'elides arrow-function-based ɵsetClassMetadataAsync',
testCase({
input: `
import { Component } from '@angular/core';
class SomeClass {}
(() => {
(typeof ngDevMode === 'undefined' || ngDevMode) &&
i0.ɵsetClassDebugInfo(SomeClass, { className: 'SomeClass' });
})();
`,
expected: `
import { Component } from "@angular/core";
class SomeClass {}
(() => {
(typeof ngDevMode === "undefined" || ngDevMode) && void 0;
})();
`,
}),
);
});

0 comments on commit f806e34

Please sign in to comment.