Skip to main content



      Home
Home » Language IDEs » ServerTools (WTP) » [jsdt] possible bug in ASTParser parsing FunctionExpression
[jsdt] possible bug in ASTParser parsing FunctionExpression [message #647086] Tue, 04 January 2011 09:21
Eclipse UserFriend
Hello,

could one of the JSDT-ASTParser-experts please have a look at the following?

If I try to parse the ObjectLiteral in the JavaScript compilation unit below

var x = {
lit : {} ,
func : function() { var x=0; } ,
str : "Hello"
};

the length of the FunctionExpression, i.e. the initializer of the 'func'
ObjectLiteralField seems to be incorrect (the closing '}' seems to be missing).

If I calculate the start and length of the initializers and write the
associated sections of the document, I get the following output:

lit : {} (correct)
func: function() { var x=0; (with trailing space) (incorrect, i.e. the } is missing
str : "Hello" (correct)

If I write the initializers using "toString()" everything seems to be fine,
i.e. I get

lit : {}

func : function (){
var x=0;
}

str : "Hello"



Please find a quick-and-dirty test program below to reproduce the error:

--- snip ---

package de.topsystem.tsdd.model.test.bugreport;

import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.wst.jsdt.core.dom.AST;
import org.eclipse.wst.jsdt.core.dom.ASTNode;
import org.eclipse.wst.jsdt.core.dom.ASTParser;
import org.eclipse.wst.jsdt.core.dom.JavaScriptUnit;
import org.eclipse.wst.jsdt.core.dom.ObjectLiteral;
import org.eclipse.wst.jsdt.core.dom.ObjectLiteralField;
import org.eclipse.wst.jsdt.core.dom.VariableDeclarationFragment;
import org.eclipse.wst.jsdt.core.dom.VariableDeclarationStatement;

public class TestASTFunction {

public static void main(String[] args) throws BadLocationException {

Document document = new Document(
"var x = { lit : {} , func : function() { var x=0; } , str : \"Hello\" };");

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(document.get().toCharArray());

parser.setKind(ASTParser.K_COMPILATION_UNIT);

JavaScriptUnit unit = (JavaScriptUnit) parser.createAST(null);

System.out.println("unit = " + unit);

ObjectLiteral objectLiteral = getObjectLiteral(unit);

System.out.println("ObjectLiteral = " + objectLiteral);

for (ObjectLiteralField field : (List<ObjectLiteralField>) objectLiteral
.fields()) {
ASTNode initializer = field.getInitializer();
System.out.println("initializer class = " + initializer.getClass());
System.out.println("initializer = `" + initializer + '`');

int start = initializer.getStartPosition();
int length = initializer.getLength();

String initializerText = document.get(start, length);

System.out.println("initializer text =`" + initializerText + "`");

}

}

//
// just gets the ObjectLiteral from the CompilationUnit
//
static ObjectLiteral getObjectLiteral(JavaScriptUnit unit) {

VariableDeclarationStatement statement = (VariableDeclarationStatement) unit
.statements().get(0);

VariableDeclarationFragment fragment = (VariableDeclarationFragment) statement
.fragments().get(0);

return (ObjectLiteral) fragment.getInitializer();

}

}


--- snip ---


Version info (from MANIFEST.MF)

Bundle-SymbolicName: org.eclipse.wst.jsdt.core; singleton:=true
Bundle-Version: 1.0.201.qualifier


Thanks for any feedback!

Greetings Harald
Previous Topic:HTML/JSP Formatting
Next Topic:Web Page editor only shows me some part of a file
Goto Forum:
  


Current Time: Thu Jul 03 07:03:52 EDT 2025

Powered by FUDForum. Page generated in 0.04289 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top