Skip to content

Commit

Permalink
Fix inefficient validation of template strings (don't call getChildCo…
Browse files Browse the repository at this point in the history
…unt and getChildAtIndex for each child).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=130889034
  • Loading branch information
concavelenz authored and brad4d committed Aug 22, 2016
1 parent 8ef54d6 commit d3b49ed
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions src/com/google/javascript/jscomp/AstValidator.java
Expand Up @@ -405,16 +405,16 @@ private void validateImport(Node n) {


private void validateImportSpecifiers(Node n) { private void validateImportSpecifiers(Node n) {
validateNodeType(Token.IMPORT_SPECS, n); validateNodeType(Token.IMPORT_SPECS, n);
for (Node child : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
validateImportSpecifier(child); validateImportSpecifier(c);
} }
} }


private void validateImportSpecifier(Node n) { private void validateImportSpecifier(Node n) {
validateNodeType(Token.IMPORT_SPEC, n); validateNodeType(Token.IMPORT_SPEC, n);
validateChildCountIn(n, 1, 2); validateChildCountIn(n, 1, 2);
for (Node child : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
validateName(child); validateName(c);
} }
} }


Expand Down Expand Up @@ -442,16 +442,16 @@ private void validateExport(Node n, boolean isAmbient) {


private void validateExportSpecifiers(Node n) { private void validateExportSpecifiers(Node n) {
validateNodeType(Token.EXPORT_SPECS, n); validateNodeType(Token.EXPORT_SPECS, n);
for (Node child : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
validateExportSpecifier(child); validateExportSpecifier(c);
} }
} }


private void validateExportSpecifier(Node n) { private void validateExportSpecifier(Node n) {
validateNodeType(Token.EXPORT_SPEC, n); validateNodeType(Token.EXPORT_SPEC, n);
validateChildCountIn(n, 1, 2); validateChildCountIn(n, 1, 2);
for (Node child : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
validateName(child); validateName(c);
} }
} }


Expand All @@ -469,8 +469,8 @@ private void validateTemplateLit(Node n) {
if (!n.hasChildren()) { if (!n.hasChildren()) {
return; return;
} }
for (int i = 0; i < n.getChildCount(); i++) { int i = 0;
Node child = n.getChildAtIndex(i); for (Node child = n.getFirstChild(); child != null; child = child.getNext(), i++) {
// If the first child is not a STRING, this is a tagged template. // If the first child is not a STRING, this is a tagged template.
if (i == 0 && !child.isString()) { if (i == 0 && !child.isString()) {
validateExpression(child); validateExpression(child);
Expand Down Expand Up @@ -505,15 +505,15 @@ private void validateInterface(Node n) {


private void validateInterfaceExtends(Node n) { private void validateInterfaceExtends(Node n) {
validateNodeType(Token.INTERFACE_EXTENDS, n); validateNodeType(Token.INTERFACE_EXTENDS, n);
for (Node child : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
validateNamedType(child); validateNamedType(c);
} }
} }


private void validateInterfaceMembers(Node n) { private void validateInterfaceMembers(Node n) {
validateNodeType(Token.INTERFACE_MEMBERS, n); validateNodeType(Token.INTERFACE_MEMBERS, n);
for (Node child : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
validateInterfaceMember(child); validateInterfaceMember(c);
} }
} }


Expand Down Expand Up @@ -546,8 +546,8 @@ private void validateEnum(Node n) {


private void validateEnumMembers(Node n) { private void validateEnumMembers(Node n) {
validateNodeType(Token.ENUM_MEMBERS, n); validateNodeType(Token.ENUM_MEMBERS, n);
for (Node child : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
validateObjectLitStringKey(child); validateObjectLitStringKey(c);
} }
} }


Expand Down Expand Up @@ -588,7 +588,7 @@ private void validateClassHelper(Node n, boolean isAmbient) {


private void validateClassMembers(Node n, boolean isAmbient) { private void validateClassMembers(Node n, boolean isAmbient) {
validateNodeType(Token.CLASS_MEMBERS, n); validateNodeType(Token.CLASS_MEMBERS, n);
for (Node c : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
validateClassMember(c, isAmbient); validateClassMember(c, isAmbient);
} }
} }
Expand Down Expand Up @@ -1419,11 +1419,11 @@ private void validateNamespaceName(Node n) {


private void validateNamespaceElements(Node n, boolean isAmbient) { private void validateNamespaceElements(Node n, boolean isAmbient) {
validateNodeType(Token.NAMESPACE_ELEMENTS, n); validateNodeType(Token.NAMESPACE_ELEMENTS, n);
for (Node child : n.children()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
if (isAmbient) { if (isAmbient) {
validateAmbientDeclarationHelper(child); validateAmbientDeclarationHelper(c);
} else { } else {
validateStatement(child); validateStatement(c);
} }
} }
} }
Expand Down Expand Up @@ -1471,9 +1471,7 @@ private void validateMinimumChildCount(Node n, int i) {
} }


if (!valid) { if (!valid) {
violation( violation("Expected at least " + i + " children, but was " + n.getChildCount(), n);
"Expected at least " + i + " children, but was "
+ n.getChildCount(), n);
} }
} }


Expand All @@ -1487,9 +1485,7 @@ private void validateMaximumChildCount(Node n, int i) {
valid = n.getChildCount() <= i; valid = n.getChildCount() <= i;
} }
if (!valid) { if (!valid) {
violation( violation("Expected no more than " + i + " children, but was " + n.getChildCount(), n);
"Expected no more than " + i + " children, but was "
+ n.getChildCount(), n);
} }
} }


Expand Down

0 comments on commit d3b49ed

Please sign in to comment.