Skip to content

Commit 3ca26b5

Browse files
rishipalcopybara-github
authored andcommitted
Create a shared NodeUtil helper for duplicated function
PiperOrigin-RevId: 623959431
1 parent 91eb8ae commit 3ca26b5

File tree

4 files changed

+28
-59
lines changed

4 files changed

+28
-59
lines changed

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

+4-20
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ private void visitSuper(NodeTraversal t, ConstructorData constructorData) {
228228
Node declaration =
229229
IR.var(astFactory.createName(uniqueSuperThisName, typeOfThis))
230230
.srcrefTree(constructorBody);
231-
Node insertBeforePoint = getInsertBeforePoint(constructorBody);
231+
Node insertBeforePoint =
232+
NodeUtil.getInsertionPointAfterAllInnerFunctionDeclarations(constructorBody);
232233
if (insertBeforePoint != null) {
233234
declaration.insertBefore(insertBeforePoint);
234235
} else {
@@ -257,24 +258,6 @@ private void visitSuper(NodeTraversal t, ConstructorData constructorData) {
257258
}
258259
}
259260

260-
/**
261-
* Gets an insertion point in the function body before which we want to insert the new let
262-
* declaration. If there's not good insertion point (e.g. the function is empty or only contains
263-
* inner function declarations), return null.
264-
*/
265-
private Node getInsertBeforePoint(Node functionBody) {
266-
checkState(functionBody.getParent().isFunction());
267-
Node current = functionBody.getFirstChild();
268-
269-
// Do not insert the let declaration before any hoisted function declarations in this function
270-
// body as those function declarations are hoisted by normalization. We must maintain
271-
// normalization.
272-
while (current != null && NodeUtil.isFunctionDeclaration(current)) {
273-
current = current.getNext();
274-
}
275-
return current;
276-
}
277-
278261
/**
279262
* Change calls to `super` to use `$jscomp.construct` instead.
280263
*
@@ -331,7 +314,8 @@ private void convertSuperCallsToJsCompConstructCalls(
331314
astFactory
332315
.createSingleVarNameDeclaration(uniqueSuperThisName)
333316
.srcrefTree(constructorBody);
334-
Node insertBeforePoint = getInsertBeforePoint(constructorBody);
317+
Node insertBeforePoint =
318+
NodeUtil.getInsertionPointAfterAllInnerFunctionDeclarations(constructorBody);
335319
if (insertBeforePoint != null) {
336320
declaration.insertBefore(insertBeforePoint);
337321
} else {

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

+2-19
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ private void addVarDeclarations(NodeTraversal t, ThisAndArgumentsContext context
148148
private void insertVarDeclaration(Node varDeclaration, Node scopeBody) {
149149
if (scopeBody.getParent().isFunction()) {
150150
// for functions, we must find the correct insertion point to preserve normalization
151-
Node insertBeforePoint = getInsertBeforePoint(scopeBody);
151+
Node insertBeforePoint =
152+
NodeUtil.getInsertionPointAfterAllInnerFunctionDeclarations(scopeBody);
152153
if (insertBeforePoint != null) {
153154
varDeclaration.insertBefore(insertBeforePoint);
154155
} else {
@@ -160,24 +161,6 @@ private void insertVarDeclaration(Node varDeclaration, Node scopeBody) {
160161
}
161162
}
162163

163-
/**
164-
* Gets an insertion point in the function body before which we want to insert the new let
165-
* declaration. If there's not good insertion point (e.g. the function is empty or only contains
166-
* inner function declarations), return null.
167-
*/
168-
private Node getInsertBeforePoint(Node functionBody) {
169-
checkState(functionBody.getParent().isFunction());
170-
Node current = functionBody.getFirstChild();
171-
172-
// Do not insert the let declaration before any hoisted function declarations in this function
173-
// body as those function declarations are hoisted by normalization. We must maintain
174-
// normalization.
175-
while (current != null && NodeUtil.isFunctionDeclaration(current)) {
176-
current = current.getNext();
177-
}
178-
return current;
179-
}
180-
181164
private void makeTreeNonIndexable(Node n) {
182165
n.makeNonIndexable();
183166
for (Node child = n.getFirstChild(); child != null; child = child.getNext()) {

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

+2-20
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ private void visitRestParam(NodeTraversal t, Node restParam, Node paramList) {
107107
astFactory.createNumber(restIndex),
108108
astFactory.createArgumentsReference()))
109109
.srcrefTreeIfMissing(functionBody);
110-
Node insertBeforePoint = getInsertBeforePoint(functionBody);
110+
Node insertBeforePoint =
111+
NodeUtil.getInsertionPointAfterAllInnerFunctionDeclarations(functionBody);
111112
if (insertBeforePoint != null) {
112113
let.insertBefore(insertBeforePoint);
113114
} else {
@@ -119,25 +120,6 @@ private void visitRestParam(NodeTraversal t, Node restParam, Node paramList) {
119120
t.reportCodeChange();
120121
}
121122

122-
/**
123-
* Gets an insertion point in the function body before which we want to insert the new let
124-
* declaration. If there's not good insertion point (e.g. the function is empty or only contains
125-
* inner function declarations), return null.
126-
*/
127-
private Node getInsertBeforePoint(Node functionBody) {
128-
checkState(functionBody.getParent().isFunction());
129-
Node current = functionBody.getFirstChild();
130-
131-
// Do not insert the let declaration before any hoisted function declarations in this function
132-
// body as those function declarations are hoisted by normalization. We must maintain
133-
// normalization.
134-
while (current != null && NodeUtil.isFunctionDeclaration(current)) {
135-
current = current.getNext();
136-
}
137-
138-
return current;
139-
}
140-
141123
/**
142124
* Processes array literals or calls to eliminate spreads.
143125
*

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

+20
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,26 @@ public static boolean isLiteralValue(Node n, boolean includeFunctions) {
921921
}
922922
}
923923

924+
/**
925+
* Returns the node within the given function body at which something can be inserted such that
926+
* it's after all inner function declarations in that function body. We want this because
927+
* normalization expects all inner function declarations to be hoisted. If there's not good
928+
* insertion point (e.g. the function is empty or only contains inner function declarations),
929+
* return null.
930+
*/
931+
static Node getInsertionPointAfterAllInnerFunctionDeclarations(Node functionBody) {
932+
checkState(functionBody.getParent().isFunction());
933+
Node current = functionBody.getFirstChild();
934+
935+
// Do not insert the let declaration before any hoisted function declarations in this function
936+
// body as those function declarations are hoisted by normalization. We must maintain
937+
// normalization.
938+
while (current != null && NodeUtil.isFunctionDeclaration(current)) {
939+
current = current.getNext();
940+
}
941+
return current;
942+
}
943+
924944
/**
925945
* Returns true iff the value associated with the node is a JS string literal, a concatenation
926946
* thereof or a ternary operator choosing between string literals.

0 commit comments

Comments
 (0)