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;
@@ -30,11 +30,11 @@ import java.util.*
private val FAKE_SOURCE_INFO = SourceInfoImpl(null, 0, 0, 0, 0) private val FAKE_SOURCE_INFO = SourceInfoImpl(null, 0, 0, 0, 0)
fun parse(code: String, reporter: ErrorReporter, scope: JsScope, fileName: String): List<JsStatement> { fun parse(code: String, reporter: ErrorReporter, scope: JsScope, fileName: String): List<JsStatement> {
val insideFunction = scope is JsFunctionScope val insideFunction = scope is JsFunctionScope
val node = parse(code, 0, reporter, insideFunction, Parser::parse) val node = parse(code, 0, reporter, insideFunction, Parser::parse)
return node.toJsAst(scope, fileName) { return node.toJsAst(scope, fileName) {
mapStatements(it) mapStatements(it)
} }
} }
fun parseFunction(code: String, fileName: String, offset: Int, reporter: ErrorReporter, scope: JsScope): JsFunction = fun parseFunction(code: String, fileName: String, offset: Int, reporter: ErrorReporter, scope: JsScope): JsFunction =
@@ -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
} }