Add source information to parsed JS AST

This commit is contained in:
Alexey Andreev
2017-05-17 15:06:01 +03:00
parent 1d1da326be
commit 93ec81d2eb
4 changed files with 14 additions and 13 deletions
@@ -16,6 +16,7 @@
package com.google.gwt.dev.js; package com.google.gwt.dev.js;
import com.google.gwt.dev.js.parserExceptions.JsParserException; import com.google.gwt.dev.js.parserExceptions.JsParserException;
import com.google.gwt.dev.js.rhino.CodePosition;
import com.google.gwt.dev.js.rhino.Node; import com.google.gwt.dev.js.rhino.Node;
import com.google.gwt.dev.js.rhino.TokenStream; import com.google.gwt.dev.js.rhino.TokenStream;
import com.intellij.util.SmartList; import com.intellij.util.SmartList;
@@ -1071,7 +1072,7 @@ public class JsAstMapper {
// //
String fromName = fromVar.getString(); String fromName = fromVar.getString();
JsName toName = scopeContext.localNameFor(fromName); JsName toName = scopeContext.localNameFor(fromName);
JsVars.JsVar toVar = new JsVars.JsVar(toName); JsVars.JsVar toVar = withLocation(new JsVars.JsVar(toName), fromVar);
Node fromInit = fromVar.getFirstChild(); Node fromInit = fromVar.getFirstChild();
if (fromInit != null) { if (fromInit != null) {
@@ -1103,14 +1104,14 @@ public class JsAstMapper {
} }
private <T extends JsNode> T withLocation(T astNode, Node node) { private <T extends JsNode> T withLocation(T astNode, Node node) {
int lineNumber = node.getLineno(); CodePosition location = node.getPosition();
if (lineNumber >= 0) { if (location != null) {
JsLocation location = new JsLocation(fileName, lineNumber, 0); JsLocation jsLocation = new JsLocation(fileName, location.getLine(), location.getOffset());
if (astNode instanceof SourceInfoAwareJsNode) { if (astNode instanceof SourceInfoAwareJsNode) {
astNode.setSource(location); astNode.setSource(jsLocation);
} }
else if (astNode instanceof JsExpressionStatement) { else if (astNode instanceof JsExpressionStatement) {
((JsExpressionStatement) astNode).getExpression().setSource(location); ((JsExpressionStatement) astNode).getExpression().setSource(jsLocation);
} }
} }
return astNode; return astNode;
@@ -96,7 +96,7 @@ abstract class BasicOptimizerTest(private var basePath: String) {
} }
override fun visitIf(x: JsIf) { override fun visitIf(x: JsIf) {
val line = x.getData<Int?>("line") val line = (x.source as? JsLocation)?.startLine
if (line != null && line in comments.indices && comments[line]) { if (line != null && line in comments.indices && comments[line]) {
x.synthetic = true x.synthetic = true
} }