-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
fix #2305: do not apply UnnecessaryBlock for the ECMAScript (JavaScript) destructuring assignments #4829
fix #2305: do not apply UnnecessaryBlock for the ECMAScript (JavaScript) destructuring assignments #4829
Changes from all commits
e5247d1
a015835
df55c44
8350de9
b01d630
3b9dcc0
0ec6a4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,15 @@ for (var i in obj) { | |
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Ok, for of</description> | ||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
for (let value of obj) { | ||
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Ok, while</description> | ||
<expected-problems>0</expected-problems> | ||
|
@@ -192,4 +201,44 @@ try { | |
} | ||
]]></code> | ||
</test-code> | ||
|
||
<test-code> | ||
<description>Ok, destructure assigments</description> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, that this sample code doesn't produce a UnnecessaryBlock, even with your rule improvements. The only way to reproduce the bug is by not using semicolons. A simple example is import { foo } from './someScript.js' // missing semicolon
const { isLoggedIn } = useAuth(); // <--- violation here: unnecessary block (I've updated #2305 meanwhile) Please add a separate test case for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed by this rule:
First (//Scope) for root Blocks |
||
<expected-problems>0</expected-problems> | ||
<code><![CDATA[ | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you remove the semicolon at the end of this line, then the rule will find a unnecessary block for the following It seems, that Rhino produces an "EmptyStatement", if it doesn't understand the syntax and tries to continue parsing with the next statement. We can make use of this, and avoid some more false positives, if we check, whether the preceding node in the AST is a EmptyStatement, e.g.
Maybe we should duplicate this test case, but remove all semicolons. Note, that the original sample code on #2305 (which is only the import statement) doesn't reproduce the problem... since import is parsed into a EmptyStatement and there is neither a Block nor a Scope... |
||
|
||
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-data> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert the changes in this file completely