Abstract away parser utils

Convert a few pieces of code

Share with modelica

Remove yet another dup

Fix tests

Doc

Share with plsql

Fix tests

Fix build

Cleanup

Minimize diff

Share with JSP module

Share with JS module

Share with XML module

Share with VisualForce module

Share with Scala module

Fix last tests
This commit is contained in:
Clément Fournier
2019-12-19 17:01:05 +01:00
parent f67819e05a
commit 8afe5ae7a8
131 changed files with 1714 additions and 2367 deletions

View File

@ -96,5 +96,15 @@
<artifactId>pmd-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-lang-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -6,7 +6,9 @@ package net.sourceforge.pmd.lang.ecmascript.ast;
import org.mozilla.javascript.ast.AstRoot;
public class ASTAstRoot extends AbstractEcmascriptNode<AstRoot> {
import net.sourceforge.pmd.lang.ast.RootNode;
public class ASTAstRoot extends AbstractEcmascriptNode<AstRoot> implements RootNode {
public ASTAstRoot(AstRoot astRoot) {
super(astRoot);
}

View File

@ -11,7 +11,7 @@ public class ASTFunctionNodeTest extends EcmascriptParserTestBase {
@Test
public void testGetBody() {
ASTAstRoot node = parse("function foo() { var a = 'a'; }");
ASTAstRoot node = js.parse("function foo() { var a = 'a'; }");
ASTFunctionNode fn = node.getFirstDescendantOfType(ASTFunctionNode.class);
Assert.assertFalse(fn.isClosure());
EcmascriptNode<?> body = fn.getBody();
@ -20,7 +20,7 @@ public class ASTFunctionNodeTest extends EcmascriptParserTestBase {
@Test
public void testGetBodyFunctionClosureExpression() {
ASTAstRoot node = parse18("(function(x) x*x)");
ASTAstRoot node = js18.parse("(function(x) x*x)");
ASTFunctionNode fn = node.getFirstDescendantOfType(ASTFunctionNode.class);
Assert.assertTrue(fn.isClosure());
EcmascriptNode<?> body = fn.getBody();

View File

@ -17,7 +17,7 @@ import org.mozilla.javascript.ast.AstRoot;
public class ASTTryStatementTest extends EcmascriptParserTestBase {
private ASTTryStatement getTryStmt(String js) {
EcmascriptNode<AstRoot> node = parse(js);
EcmascriptNode<AstRoot> node = this.js.parse(js);
List<ASTTryStatement> trys = node.findDescendantsOfType(ASTTryStatement.class);
Assert.assertEquals(1, trys.size());
ASTTryStatement tryStmt = trys.get(0);

View File

@ -32,7 +32,7 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
@Test
public void testLineNumbers() {
final String SOURCE_CODE = "function a() {" + PMD.EOL + " alert('hello');" + PMD.EOL + "}" + PMD.EOL;
EcmascriptNode<AstRoot> node = parse(SOURCE_CODE);
EcmascriptNode<AstRoot> node = js.parse(SOURCE_CODE);
assertEquals(1, node.getBeginLine());
assertEquals(1, node.getBeginColumn());
assertEquals(3, node.getEndLine());
@ -69,7 +69,7 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
MyEcmascriptRule rule = new MyEcmascriptRule();
RuleContext ctx = new RuleContext();
rule.apply(Arrays.asList(parse(source)), ctx);
rule.apply(Arrays.asList(js.parse(source)), ctx);
assertEquals("Scope from 2 to 4", output.get(0));
assertEquals("Scope from 4 to 6", output.get(1));
@ -80,7 +80,7 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
*/
@Test
public void testArrayAccess() {
EcmascriptNode<AstRoot> node = parse("function a() { b['a'] = 1; c[1] = 2; }");
EcmascriptNode<AstRoot> node = js.parse("function a() { b['a'] = 1; c[1] = 2; }");
List<ASTElementGet> arrays = node.findDescendantsOfType(ASTElementGet.class);
assertEquals("b", arrays.get(0).getTarget().getImage());
assertEquals("a", arrays.get(0).getElement().getImage());
@ -94,9 +94,9 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
*/
@Test
public void testArrayMethod() {
EcmascriptNode<AstRoot> rootNode = parse(
"function test(){\n" + " a(); // OK\n" + " b.c(); // OK\n" + " d[0](); // OK\n"
+ " e[0].f(); // OK\n" + " y.z[0](); // FAIL ==> java.lang.NullPointerException\n" + "}");
EcmascriptNode<AstRoot> rootNode = js.parse(
"function test(){\n" + " a(); // OK\n" + " b.c(); // OK\n" + " d[0](); // OK\n"
+ " e[0].f(); // OK\n" + " y.z[0](); // FAIL ==> java.lang.NullPointerException\n" + "}");
List<ASTFunctionCall> calls = rootNode.findDescendantsOfType(ASTFunctionCall.class);
List<String> results = new ArrayList<>();
@ -129,7 +129,7 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
*/
@Test
public void testCaseAsIdentifier() {
ASTAstRoot rootNode = parse("function f(a){\n" + " a.case.flag = 1;\n" + " return;\n" + "}");
ASTAstRoot rootNode = js.parse("function f(a){\n" + " a.case.flag = 1;\n" + " return;\n" + "}");
ASTBlock block = rootNode.getFirstDescendantOfType(ASTBlock.class);
assertFalse(block.jjtGetChild(0) instanceof ASTEmptyExpression);
assertTrue(block.jjtGetChild(0) instanceof ASTExpressionStatement);
@ -163,9 +163,9 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
*/
@Test
public void testVoidKeyword() {
ASTAstRoot rootNode = parse("function f(matchFn, fieldval, n){\n"
+ " return (matchFn)?(matcharray = eval(matchFn+\"('\"+fieldval+\"','\"+n.id+\"')\")):void(0);\n"
+ "}\n");
ASTAstRoot rootNode = js.parse("function f(matchFn, fieldval, n){\n"
+ " return (matchFn)?(matcharray = eval(matchFn+\"('\"+fieldval+\"','\"+n.id+\"')\")):void(0);\n"
+ "}\n");
ASTUnaryExpression unary = rootNode.getFirstDescendantOfType(ASTUnaryExpression.class);
assertEquals("void", unary.getImage());
}
@ -175,9 +175,9 @@ public class EcmascriptParserTest extends EcmascriptParserTestBase {
*/
@Test
public void testXorAssignment() {
ASTAstRoot rootNode = parse("function f() { var x = 2; x ^= 2; x &= 2; x |= 2; "
+ "x &&= true; x ||= false; x *= 2; x /= 2; x %= 2; x += 2; x -= 2; "
+ "x <<= 2; x >>= 2; x >>>= 2; }");
ASTAstRoot rootNode = js.parse("function f() { var x = 2; x ^= 2; x &= 2; x |= 2; "
+ "x &&= true; x ||= false; x *= 2; x /= 2; x %= 2; x += 2; x -= 2; "
+ "x <<= 2; x >>= 2; x >>>= 2; }");
ASTAssignment infix = rootNode.getFirstDescendantOfType(ASTAssignment.class);
assertEquals("^=", infix.getImage());
}

View File

@ -4,32 +4,21 @@
package net.sourceforge.pmd.lang.ecmascript.ast;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.ecmascript.EcmascriptParserOptions;
import net.sourceforge.pmd.lang.ecmascript.EcmascriptParserOptions.Version;
public abstract class EcmascriptParserTestBase {
public ASTAstRoot parse(String code) {
EcmascriptParser parser = new EcmascriptParser(new EcmascriptParserOptions());
Reader sourceCode = new StringReader(code);
return (ASTAstRoot) parser.parse(sourceCode);
}
public ASTAstRoot parse18(String code) {
protected final JsParsingHelper js = JsParsingHelper.DEFAULT.withResourceContext(getClass());
protected final JsParsingHelper js18 = JsParsingHelper.DEFAULT.withResourceContext(getClass())
.withParserOptions(parserVersion(Version.VERSION_1_8));
public ParserOptions parserVersion(EcmascriptParserOptions.Version version) {
EcmascriptParserOptions parserOptions = new EcmascriptParserOptions();
parserOptions.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_1_8);
EcmascriptParser parser = new EcmascriptParser(parserOptions);
Reader sourceCode = new StringReader(code);
return (ASTAstRoot) parser.parse(sourceCode);
parserOptions.setRhinoLanguageVersion(version);
return parserOptions;
}
public String dump(EcmascriptNode<?> node) {
DumpFacade dumpFacade = new DumpFacade();
StringWriter writer = new StringWriter();
dumpFacade.initializeWith(writer, "", true, node);
dumpFacade.visit(node, "");
return writer.toString();
}
}

View File

@ -0,0 +1,25 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ecmascript.ast;
import org.jetbrains.annotations.NotNull;
import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper;
import net.sourceforge.pmd.lang.ecmascript.EcmascriptLanguageModule;
public final class JsParsingHelper extends BaseParsingHelper<JsParsingHelper, ASTAstRoot> {
public static final JsParsingHelper DEFAULT = new JsParsingHelper(Params.getDefaultProcess());
private JsParsingHelper(@NotNull Params params) {
super(EcmascriptLanguageModule.NAME, ASTAstRoot.class, params);
}
@NotNull
@Override
protected JsParsingHelper clone(@NotNull Params params) {
return new JsParsingHelper(params);
}
}