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

View File

@@ -126,7 +126,7 @@ class FunctionReader(private val config: JsConfig, private val currentModuleName
}
operator fun get(descriptor: CallableDescriptor): JsFunction = functionCache.get(descriptor)
private fun readFunction(descriptor: CallableDescriptor): JsFunction? {
if (descriptor !in this) return null

View File

@@ -16,6 +16,7 @@
package com.google.gwt.dev.js;
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.TokenStream;
import com.intellij.util.SmartList;
@@ -1071,7 +1072,7 @@ public class JsAstMapper {
//
String fromName = fromVar.getString();
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();
if (fromInit != null) {
@@ -1103,14 +1104,14 @@ public class JsAstMapper {
}
private <T extends JsNode> T withLocation(T astNode, Node node) {
int lineNumber = node.getLineno();
if (lineNumber >= 0) {
JsLocation location = new JsLocation(fileName, lineNumber, 0);
CodePosition location = node.getPosition();
if (location != null) {
JsLocation jsLocation = new JsLocation(fileName, location.getLine(), location.getOffset());
if (astNode instanceof SourceInfoAwareJsNode) {
astNode.setSource(location);
astNode.setSource(jsLocation);
}
else if (astNode instanceof JsExpressionStatement) {
((JsExpressionStatement) astNode).getExpression().setSource(location);
((JsExpressionStatement) astNode).getExpression().setSource(jsLocation);
}
}
return astNode;

View File

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

View File

@@ -96,7 +96,7 @@ abstract class BasicOptimizerTest(private var basePath: String) {
}
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]) {
x.synthetic = true
}