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

"singleGroups" option gives spurious warnings for negative instanceof condition #2064

Closed
Qantas94Heavy opened this issue Dec 31, 2014 · 9 comments · Fixed by #2109
Closed
Labels

Comments

@Qantas94Heavy
Copy link

Using JSHint v2.5.11, when I try the following code:

// jshint singleGroups:true
if (!(a instanceof b)) alert(1);

It will give out a spurious warning that "Grouping operator is unnecessary for lone expressions". It is in fact necessary, because the unary negation operator has a higher precedence than the instanceof operator.

@Qantas94Heavy Qantas94Heavy changed the title "singleGroups" options giving spurious warnings for negative instanceof condition "singleGroups" option giving spurious warnings for negative instanceof condition Dec 31, 2014
@Qantas94Heavy Qantas94Heavy changed the title "singleGroups" option giving spurious warnings for negative instanceof condition "singleGroups" option gives spurious warnings for negative instanceof condition Dec 31, 2014
@rwaldron rwaldron added ES6 P1 and removed ES6 labels Dec 31, 2014
@rwaldron
Copy link
Member

The "ES6" label was a finger slip, sorry about that.

jugglinmike added a commit to jugglinmike/jshint that referenced this issue Dec 31, 2014
Honor expression binding power when enforcing the `singleGroups` option.

Resolves jshintgh-2064
@jugglinmike
Copy link
Member

This is also the case for the in operator. I've opened gh-2065 to address the bug.

@Krinkle
Copy link

Krinkle commented Jan 1, 2015

Should we exclude !! (boolean cast) as well?

      4 |return !!( window.JSON && JSON.stringify && JSON.parse );
                                                                 ^ Grouping operator is unnecessary for lone expressions.

@rwaldron
Copy link
Member

rwaldron commented Jan 2, 2015

Grouping operator is unnecessary for lone expressions

In the example you gave, this error just seems wrong.

@jugglinmike
Copy link
Member

@Krinkle Definitely. @rwaldron is right, that is another bug. It's covered by my patch in gh-2065, but I've added another regression test to be safe. Thanks!

jugglinmike added a commit to jugglinmike/jshint that referenced this issue Jan 3, 2015
Honor expression binding power when enforcing the `singleGroups` option.

Resolves jshintgh-2064
jugglinmike added a commit to jugglinmike/jshint that referenced this issue Jan 3, 2015
Honor expression binding power when enforcing the `singleGroups` option.

Resolves jshintgh-2064
@gagle
Copy link

gagle commented Jan 5, 2015

Same issue here:

//Warning
path.resolve(alias.dir, alias.path) + (obj.trail ? '/' + obj.trail : '');

//No warning & invalid
path.resolve(alias.dir, alias.path) + obj.trail ? '/' + obj.trail : '';
//Same as
(path.resolve(alias.dir, alias.path) + obj.trail) ? '/' + obj.trail : '';

@dsl101
Copy link

dsl101 commented Jan 5, 2015

Another example?

var a = 2;
var b = 3;
var resultStr;

// Correct answer (5), but jshint gives warning:
resultStr = 'Total: ' + (a + b) + '.';
                                ^ Grouping operator is unnecessary for lone expressions.

// Incorrect answer (23), but jshint gives no warning:
resultStr = 'Total: ' + a + b + '.';

jugglinmike added a commit to jugglinmike/jshint that referenced this issue Jan 5, 2015
Honor expression binding power when enforcing the `singleGroups` option.

Resolves jshintgh-2064
@Qantas94Heavy
Copy link
Author

Also, should this trigger the warning or not?

// jshint singleGroups:true
if ( (foo = 1) ) alert(1);

This is usually used to indicate that the assignment was deliberate, but currently triggers a warning with the singleGroups option enabled even though adding the parentheses silences the "assignment instead of conditional expression" warning.

jugglinmike added a commit to jugglinmike/jshint that referenced this issue Jan 17, 2015
Honor expression binding power when enforcing the `singleGroups` option.

Resolves jshintgh-2064
jugglinmike added a commit to jugglinmike/jshint that referenced this issue Jan 24, 2015
Honor expression binding power when enforcing the `singleGroups` option.

Resolves jshintgh-2064
@gagle
Copy link

gagle commented Jan 31, 2015

I still receive the warning:

Warning & valid

> 'foo' + ('bar' ? 'baz' : 'qux');
foobaz

No warning & invalid

> 'foo' + 'bar' ? 'baz' : 'qux';
baz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants