Skip to content

Commit 434cec1

Browse files
trevoradecopybara-github
authored andcommitted
Fix handling of TypedArray polyfill methods in RemoveUnusedCode.
PiperOrigin-RevId: 826578479
1 parent 5bf37d6 commit 434cec1

File tree

3 files changed

+6
-50
lines changed

3 files changed

+6
-50
lines changed

src/com/google/javascript/jscomp/RemoveUnusedCode.java

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,6 @@ class RemoveUnusedCode implements CompilerPass {
8787
private static final ImmutableSet<String> IMPLICITLY_USED_PROPERTIES =
8888
ImmutableSet.of("length", "toString", "valueOf", "constructor", "prototype");
8989

90-
/**
91-
* Supported TypedArray subclasses.
92-
*
93-
* <p>Keep in sync with TYPED_ARRAY_CLASSES in js/build_polyfill_table.js.
94-
*/
95-
// LINT.IfChange(typed_array_classes)
96-
private static final ImmutableSet<String> TYPED_ARRAY_CLASSES =
97-
ImmutableSet.of(
98-
"Int8Array",
99-
"Uint8Array",
100-
"Uint8ClampedArray",
101-
"Int16Array",
102-
"Uint16Array",
103-
"Int32Array",
104-
"Uint32Array",
105-
"Float32Array",
106-
"Float64Array",
107-
"BigInt64Array",
108-
"BigUint64Array");
109-
110-
// LINT.ThenChange(
111-
// //depot/google3/third_party/java_src/jscomp/java/com/google/javascript/jscomp/js/build_polyfill_table.js:typed_array_classes,
112-
// //depot/google3/third_party/java_src/jscomp/java/com/google/javascript/jscomp/js/util/polyfill.js:typed_array_classes
113-
// )
114-
11590
private final AbstractCompiler compiler;
11691
private final AstAnalyzer astAnalyzer;
11792

@@ -688,26 +663,13 @@ private void traverseCall(Node callNode, Scope scope) {
688663
// TODO(bradfordcsmith): Should also handle Object.create() and Object.defineProperty().
689664
traverseObjectDefinePropertiesCall(callNode, scope);
690665
} else if (removeUnusedPolyfills && isJscompPolyfill(callee)) {
691-
boolean isPolyfillTypedArrayMethod =
692-
callee.isName()
693-
? callee.getString().equals("$jscomp$polyfillTypedArrayMethod")
694-
: callee.isGetProp() && callee.getString().equals("polyfillTypedArrayMethod");
695-
696666
Node firstArg = callee.getNext();
697-
String polyfillOrMethodName = firstArg.getString();
698-
699-
if (isPolyfillTypedArrayMethod) {
700-
String methodName = polyfillOrMethodName;
701-
for (String className : TYPED_ARRAY_CLASSES) {
702-
String polyfillName = className + ".prototype." + methodName;
703-
PolyfillInfo info = createPolyfillInfo(callNode, scope, polyfillName);
704-
polyfills.put(info.key, info);
705-
}
706-
} else {
707-
String polyfillName = polyfillOrMethodName;
708-
PolyfillInfo info = createPolyfillInfo(callNode, scope, polyfillName);
709-
polyfills.put(info.key, info);
667+
String polyfillName = firstArg.getString();
668+
if (callee.getString().endsWith("polyfillTypedArrayMethod")) {
669+
polyfillName = "TypedArray.prototype." + polyfillName;
710670
}
671+
PolyfillInfo info = createPolyfillInfo(callNode, scope, polyfillName);
672+
polyfills.put(info.key, info);
711673
// Only traverse the callee (to mark it as used). The arguments may be traversed later.
712674
traverseNode(callNode.getFirstChild(), scope);
713675
} else if (NodeUtil.isGoogWeakUsageCall(callNode)
@@ -2305,11 +2267,7 @@ private class Polyfill extends Removable {
23052267

23062268
@Override
23072269
public void removeInternal(AbstractCompiler compiler) {
2308-
// When usages of $jscomp.polyfillTypedArrayMethod are removed, it results in multiple
2309-
// attempts to delete the same polyfill function due to the various TypedArray subclasses.
2310-
if (!alreadyRemoved(polyfillNode)) {
2311-
NodeUtil.deleteNode(polyfillNode, compiler);
2312-
}
2270+
NodeUtil.deleteNode(polyfillNode, compiler);
23132271
}
23142272

23152273
@Override

src/com/google/javascript/jscomp/js/build_polyfill_table.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const TYPED_ARRAY_CLASSES = [
5151
'BigUint64Array',
5252
];
5353
// LINT.ThenChange(
54-
// //depot/google3/third_party/java_src/jscomp/java/com/google/javascript/jscomp/RemoveUnusedCode.java:typed_array_classes,
5554
// //depot/google3/third_party/java_src/jscomp/java/com/google/javascript/jscomp/js/util/polyfill.js:typed_array_classes
5655
// )
5756

src/com/google/javascript/jscomp/js/util/polyfill.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ $jscomp.TYPED_ARRAY_CLASSES = (
120120
classes.push('BigUint64');
121121
}
122122
// LINT.ThenChange(
123-
// //depot/google3/third_party/java_src/jscomp/java/com/google/javascript/jscomp/RemoveUnusedCode.java:typed_array_classes,
124123
// //depot/google3/third_party/java_src/jscomp/java/com/google/javascript/jscomp/js/build_polyfill_table.js:typed_array_classes
125124
// )
126125
return classes;

0 commit comments

Comments
 (0)