Merge branch 'master' into pmd/7.0.x

This commit is contained in:
Andreas Dangel
2020-01-10 14:41:27 +01:00
130 changed files with 1820 additions and 2567 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

@ -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,24 +4,21 @@
package net.sourceforge.pmd.lang.ecmascript.ast;
import java.io.Reader;
import java.io.StringReader;
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;
}
}

View File

@ -0,0 +1,22 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ecmascript.ast;
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(Params params) {
super(EcmascriptLanguageModule.NAME, ASTAstRoot.class, params);
}
@Override
protected JsParsingHelper clone(Params params) {
return new JsParsingHelper(params);
}
}