Skip to content

Commit

Permalink
Switch "length" custom prop of Node to a class field. This saves a ba…
Browse files Browse the repository at this point in the history
…sic object when for every node in IDE mode and makes the length available everwhere so we can improve error message and such things.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129376537
  • Loading branch information
concavelenz authored and Dimitris Vardoulakis committed Aug 5, 2016
1 parent 6275277 commit 0beeabb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
18 changes: 7 additions & 11 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -850,7 +850,7 @@ static int charno(SourcePosition location) {
void setSourceInfo(Node node, Node ref) {
node.setLineno(ref.getLineno());
node.setCharno(ref.getCharno());
maybeSetLengthFrom(node, ref);
setLengthFrom(node, ref);
}

void setSourceInfo(Node irNode, ParseTree node) {
Expand All @@ -874,7 +874,7 @@ void setSourceInfo(
node.setLineno(lineno);
int charno = charno(start);
node.setCharno(charno);
maybeSetLength(node, start, end);
setLength(node, start, end);
}
}

Expand Down Expand Up @@ -936,17 +936,13 @@ private JSDocInfo parseInlineTypeDoc(Comment node) {
}

// Set the length on the node if we're in IDE mode.
void maybeSetLength(
void setLength(
Node node, SourcePosition start, SourcePosition end) {
if (config.preserveDetailedSourceInfo == Config.SourceLocationInformation.PRESERVE) {
node.setLength(end.offset - start.offset);
}
node.setLength(end.offset - start.offset);
}

void maybeSetLengthFrom(Node node, Node ref) {
if (config.preserveDetailedSourceInfo == Config.SourceLocationInformation.PRESERVE) {
node.setLength(ref.getLength());
}
void setLengthFrom(Node node, Node ref) {
node.setLength(ref.getLength());
}

private class TransformDispatcher {
Expand Down Expand Up @@ -1990,7 +1986,7 @@ Node processVariableDeclaration(VariableDeclarationTree decl) {
if (decl.initializer != null) {
Node initializer = transform(decl.initializer);
lhs.addChildToBack(initializer);
maybeSetLength(lhs, decl.location.start, decl.location.end);
setLength(lhs, decl.location.start, decl.location.end);
}
maybeProcessType(lhs, decl.declaredType);
return lhs;
Expand Down
14 changes: 7 additions & 7 deletions src/com/google/javascript/rhino/Node.java
Expand Up @@ -89,8 +89,6 @@ public class Node implements Serializable {
FREE_CALL = 50, // A CALL without an explicit "this" value.
STATIC_SOURCE_FILE = 51, // A StaticSourceFile indicating the file
// where this node lives.
LENGTH = 52, // The length of the code represented by
// this node.
INPUT_ID = 53, // The id of the input associated with this
// node.
SLASH_V = 54, // Whether a STRING node contains a \v
Expand Down Expand Up @@ -174,7 +172,6 @@ private static final String propToString(int propType) {
case FREE_CALL: return "free_call";
case STATIC_SOURCE_FILE: return "source_file";
case INPUT_ID: return "input_id";
case LENGTH: return "length";
case SLASH_V: return "slash_v";
case INFERRED_FUNCTION: return "inferred";
case CHANGE_TIME: return "change_time";
Expand Down Expand Up @@ -1254,6 +1251,9 @@ private static void toStringTreeHelper(Node n, int level, Appendable sb)
*/
private int sourcePosition;

/** The length of the code represented by the node. */
private int length;

private TypeI typei;

protected Node parent;
Expand Down Expand Up @@ -1320,11 +1320,11 @@ public boolean isFromExterns() {
}

public int getLength() {
return getIntProp(LENGTH);
return this.length;
}

public void setLength(int length) {
putIntProp(LENGTH, length);
this.length = length;
}

public int getLineno() {
Expand Down Expand Up @@ -2157,7 +2157,7 @@ public Node useSourceInfoFrom(Node other) {
putProp(ORIGINALNAME_PROP, other.getProp(ORIGINALNAME_PROP));
setStaticSourceFile(other.getStaticSourceFile());
sourcePosition = other.sourcePosition;
setLength(other.getLength());
length = other.length;
return this;
}

Expand Down Expand Up @@ -2194,7 +2194,7 @@ public Node useSourceInfoIfMissingFrom(Node other) {
if (getStaticSourceFile() == null) {
setStaticSourceFile(other.getStaticSourceFile());
sourcePosition = other.sourcePosition;
setLength(other.getLength());
length = other.length;
}

return this;
Expand Down

0 comments on commit 0beeabb

Please sign in to comment.