Skip to content

Commit

Permalink
fix(@angular/build): do not mark Babel _defineProperty helper functio…
Browse files Browse the repository at this point in the history
…n as pure

Fixes #29145
  • Loading branch information
jsaguet authored and alan-agius4 committed Jan 6, 2025
1 parent ccf3650 commit a561869
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ function isTslibHelperName(name: string): boolean {
return tslibHelpers.has(originalName);
}

const babelHelpers = new Set<string>(['_defineProperty']);

/**
* Determinates whether an identifier name matches one of the Babel helper function names.
*
* @param name The identifier name to check.
* @returns True, if the name matches a Babel helper name; otherwise, false.
*/
function isBabelHelperName(name: string): boolean {
return babelHelpers.has(name);
}

/**
* A babel plugin factory function for adding the PURE annotation to top-level new and call expressions.
*
Expand All @@ -53,9 +65,12 @@ export default function (): PluginObj {
) {
return;
}
// Do not annotate TypeScript helpers emitted by the TypeScript compiler.
// TypeScript helpers are intended to cause side effects.
if (callee.isIdentifier() && isTslibHelperName(callee.node.name)) {
// Do not annotate TypeScript helpers emitted by the TypeScript compiler or Babel helpers.
// They are intended to cause side effects.
if (
callee.isIdentifier() &&
(isTslibHelperName(callee.node.name) || isBabelHelperName(callee.node.name))
) {
return;
}

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

it(
'does not annotate _defineProperty function',
testCaseNoChange(`
class LanguageState {}
_defineProperty(
LanguageState,
'property',
'value'
);
`),
);

it(
'does not annotate object literal methods',
testCaseNoChange(`
Expand Down

0 comments on commit a561869

Please sign in to comment.