Skip to content

Commit e973643

Browse files
jsaguetalan-agius4
authored andcommittedJan 6, 2025·
fix(@angular/build): do not mark Babel _defineProperty helper function as pure
Fixes #29145 (cherry picked from commit a561869)
1 parent 5a3d41b commit e973643

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed
 

‎packages/angular/build/src/tools/babel/plugins/pure-toplevel-functions.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ function isTslibHelperName(name: string): boolean {
3232
return tslibHelpers.has(originalName);
3333
}
3434

35+
const babelHelpers = new Set<string>(['_defineProperty']);
36+
37+
/**
38+
* Determinates whether an identifier name matches one of the Babel helper function names.
39+
*
40+
* @param name The identifier name to check.
41+
* @returns True, if the name matches a Babel helper name; otherwise, false.
42+
*/
43+
function isBabelHelperName(name: string): boolean {
44+
return babelHelpers.has(name);
45+
}
46+
3547
/**
3648
* A babel plugin factory function for adding the PURE annotation to top-level new and call expressions.
3749
*
@@ -53,9 +65,12 @@ export default function (): PluginObj {
5365
) {
5466
return;
5567
}
56-
// Do not annotate TypeScript helpers emitted by the TypeScript compiler.
57-
// TypeScript helpers are intended to cause side effects.
58-
if (callee.isIdentifier() && isTslibHelperName(callee.node.name)) {
68+
// Do not annotate TypeScript helpers emitted by the TypeScript compiler or Babel helpers.
69+
// They are intended to cause side effects.
70+
if (
71+
callee.isIdentifier() &&
72+
(isTslibHelperName(callee.node.name) || isBabelHelperName(callee.node.name))
73+
) {
5974
return;
6075
}
6176

‎packages/angular/build/src/tools/babel/plugins/pure-toplevel-functions_spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ describe('pure-toplevel-functions Babel plugin', () => {
130130
`),
131131
);
132132

133+
it(
134+
'does not annotate _defineProperty function',
135+
testCaseNoChange(`
136+
class LanguageState {}
137+
_defineProperty(
138+
LanguageState,
139+
'property',
140+
'value'
141+
);
142+
`),
143+
);
144+
133145
it(
134146
'does not annotate object literal methods',
135147
testCaseNoChange(`

0 commit comments

Comments
 (0)
Please sign in to comment.