Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[javascript] UnnecessaryBlock - false positives with destructuring assignments #2305

Closed
bkdotcom opened this issue Feb 21, 2020 · 7 comments · Fixed by #4857
Closed

[javascript] UnnecessaryBlock - false positives with destructuring assignments #2305

bkdotcom opened this issue Feb 21, 2020 · 7 comments · Fixed by #4857
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@bkdotcom
Copy link

bkdotcom commented Feb 21, 2020

Rule: UnnecessaryBlock

Description:
Unfortunately I do not know what version of PMD is being used..
PMD is one of the test suites that codacy.com uses..

Code Sample demonstrating the issue:

import { foo } from './someScript.js' // missing semicolon
const { isLoggedIn } = useAuth();  // <--- violation here: unnecessary block

throws "Unnecessary block."

However, the brackets are quite necessary and designate a named import.

@oowekyala
Copy link
Member

Weirdly enough the parser produces EmptyStatement nodes for imports. Eg for the example on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

import defaultExport from "module-name";
import * as name from "module-name";
import { export1 } from "module-name";
import { export1 as alias1 } from "module-name";
import { export1 , export2 } from "module-name";
import { foo , bar } from "module-name/path/to/specific/un-exported/file";
import { export1 , export2 as alias2 } from "module-name";
import defaultExport, { export1 } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";

var promise = import("module-name");
└─ AstRoot
   ├─ EmptyStatement
   ├─ EmptyStatement
   ├─ EmptyStatement
   ├─ EmptyStatement
   ├─ EmptyStatement
   ├─ EmptyStatement
   ├─ EmptyStatement
   ├─ EmptyStatement
   ├─ EmptyStatement
   ├─ EmptyStatement
   └─ VariableDeclaration
      └─ VariableInitializer
         ├─ Name
         └─ FunctionCall
            ├─ Name
            └─ StringLiteral

@adangel
Copy link
Member

adangel commented Feb 24, 2020

import seems to be a new feature, that is not supported by the Rhino parser version, we are using.
With #699 we plan to update the version with PMD 7. I don't know though, whether Rhino at all supports such imports.

@adangel
Copy link
Member

adangel commented Apr 16, 2020

I've just tried it with Rhino 1.7.12 and it seems, that import is not supported unfortunately.

@christian-hawk
Copy link

any news on this? #2906 will fix it?

@adangel
Copy link
Member

adangel commented Feb 25, 2021

any news on this? #2906 will fix it?

No - see #2906:

The new rhino version still doesn't support import statements (#2305)

@simoami
Copy link

simoami commented Jan 2, 2024

Import is not the only trigger for this error. It also happens with destructuring assignments where pmd sees the curly brackets as an extra block.

  const { isLoggedIn } = useAuth();

Why is this an issue?
Since: PMD 5.0
An unnecessary Block is present. Such Blocks are often used in other languages to introduce a new variable scope. Blocks > do not behave like this in ECMAScipt, and using them can be misleading. Considering removing this unnecessary Block.

@oleksandr-shvets
Copy link
Contributor

fixed in #4829

@adangel adangel added the a:false-positive PMD flags a piece of code that is not problematic label Feb 22, 2024
@adangel adangel changed the title [javascript] import statement : unnecessary block [javascript] UnnecessaryBlock - false positives with destructuring assignments Feb 22, 2024
@adangel adangel added this to the 7.0.0 milestone Mar 14, 2024
adangel added a commit to oleksandr-shvets/pmd that referenced this issue Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
6 participants