[javascript] Simplify UnnecessaryBlock rule, update tests

This commit is contained in:
Andreas Dangel 2024-03-14 20:39:56 +01:00
parent ff2ebf5026
commit c3ec240794
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 50 additions and 13 deletions

View File

@ -223,15 +223,9 @@ be misleading. Considering removing this unnecessary Block.
<properties>
<property name="xpath">
<value><![CDATA[
//Scope[
count(ancestor::*) = 1
and not(preceding::EmptyStatement)
]
| //SwitchCase[./Scope]
| //(Scope|Block)[
./(Scope|Block)
and count(./*) = 1
]
/AstRoot/Scope[not(preceding::EmptyStatement)]
| //SwitchCase[Scope]
| //(Scope|Block)[Scope|Block][count(*) = 1]
]]></value>
</property>
</properties>

View File

@ -6,6 +6,6 @@ package net.sourceforge.pmd.lang.ecmascript.rule.codestyle;
import net.sourceforge.pmd.test.PmdRuleTst;
public class UnnecessaryBlockTest extends PmdRuleTst {
class UnnecessaryBlockTest extends PmdRuleTst {
// no additional unit tests
}

View File

@ -175,6 +175,7 @@ do {
<test-code>
<description>Bad, switch</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>2,6</expected-linenumbers>
<code><![CDATA[
switch(1) {
case 1:
@ -185,7 +186,7 @@ switch(1) {
{
1 + 2;
}
return;
break;
}
]]></code>
</test-code>
@ -208,7 +209,7 @@ try {
</test-code>
<test-code>
<description>Ok, destructure assigments</description>
<description>Ok, destructure assigments (#2305)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import {foo} from 'bar'
@ -246,8 +247,50 @@ let a, b, a1, b1, c, d, rest, pop, push;
function fn( ({arg}) ){ return arg}
]]></code>
</test-code>
<test-code>
<description>not using semicolons case</description>
<description>Ok, destructure assigments (#2305) - without semicolons</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
// note: this code can't be parsed by rhino, we get several syntax errors
import {foo} from 'bar'
const [a, b] = array
const [a, , b] = array
const [a = aDefault, b] = array
const [a, b, ...rest] = array
const [a, , b, ...rest] = array
const [a, b, ...{ pop, push }] = array
const [a, b, ...[c, d]] = array
const { a, b } = obj
const { a: a1, b: b1 } = obj
const { a: a1 = aDefault, b = bDefault } = obj
const { a, b, ...rest } = obj
const { a: a1, b: b1, ...rest } = obj
const { [key]: a } = obj
let a, b, a1, b1, c, d, rest, pop, push
[a, b] = array
[a, , b] = array
[a = aDefault, b] = array
[a, b, ...rest] = array
[a, , b, ...rest] = array
[a, b, ...{ pop, push }] = array
[a, b, ...[c, d]] = array
({ a, b } = obj) // parentheses are required
({ a: a1, b: b1 } = obj)
({ a: a1 = aDefault, b = bDefault } = obj)
({ a, b, ...rest } = obj)
({ a: a1, b: b1, ...rest } = obj)
function fn( ({arg}) ){ return arg}
]]></code>
</test-code>
<test-code>
<description>not using semicolons case (#2305)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import { foo } from './someScript.js' // missing semicolon