-
Notifications
You must be signed in to change notification settings - Fork 72
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
Rule S2589: Boolean expressions should not be gratuitous #231
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bab369f
Rule S2589: Boolean expressions should not be gratuitous
yassin-kammoun-sonarsource 858cbcc
Fix after rebase
yassin-kammoun-sonarsource 7d70f28
Merge branch 'master' of github.com:SonarSource/eslint-plugin-sonarjs…
yassin-kammoun-sonarsource e3d7843
Drop Jira link
yassin-kammoun-sonarsource 649b733
Sort by rule key
yassin-kammoun-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# no-gratuitous-expressions | ||
|
||
If a boolean expression doesn’t change the evaluation of the condition, then it is entirely unnecessary, and can be removed. If it is gratuitous | ||
because it does not match the programmer’s intent, then it’s a bug and the expression should be fixed. | ||
|
||
## Noncompliant Code Example | ||
|
||
```javascript | ||
if (a) { | ||
if (a) { // Noncompliant | ||
doSomething(); | ||
} | ||
} | ||
``` | ||
|
||
## Compliant Solution | ||
|
||
```javascript | ||
if (a) { | ||
if (b) { | ||
doSomething(); | ||
} | ||
} | ||
|
||
// or | ||
if (a) { | ||
doSomething(); | ||
} | ||
``` | ||
|
||
## See | ||
|
||
<ul> | ||
<li> <a href="http://cwe.mitre.org/data/definitions/571">MITRE, CWE-571</a> - Expression is Always True </li> | ||
<li> <a href="http://cwe.mitre.org/data/definitions/570">MITRE, CWE-570</a> - Expression is Always False </li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
src/brackets/src/search/QuickOpen.js: 444 | ||
src/jest/packages/jest-cli/src/reporters/summary_reporter.js: 101 | ||
src/react-native/Libraries/Animated/src/AnimatedImplementation.js: 98 | ||
src/react-native/Libraries/Renderer/ReactFabric-prod.js: 254 | ||
src/react-native/Libraries/Renderer/ReactNativeRenderer-prod.js: 350 | ||
src/spectrum/src/components/avatar/hoverProfile.js: 131 | ||
src/spectrum/src/components/stripeCardForm/modalWell.js: 54 | ||
src/spectrum/src/views/navbar/index.js: 200,215 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
/* | ||
* eslint-plugin-sonarjs | ||
* Copyright (C) 2018-2021 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import { EncodedMessage } from '../utils/locations'; | ||
import { isIdentifier, isIfStatement } from '../utils/nodes'; | ||
import { Rule } from '../utils/types'; | ||
|
||
const rule: Rule.RuleModule = { | ||
meta: { | ||
type: 'suggestion', | ||
schema: [ | ||
{ | ||
// internal parameter for rules having secondary locations | ||
enum: ['sonar-runtime'], | ||
}, | ||
], | ||
}, | ||
|
||
create(context: Rule.RuleContext) { | ||
const truthyMap: Map<TSESTree.Statement, TSESLint.Scope.Reference[]> = new Map(); | ||
const falsyMap: Map<TSESTree.Statement, TSESLint.Scope.Reference[]> = new Map(); | ||
|
||
return { | ||
IfStatement: (node: TSESTree.Node) => { | ||
const { test } = node as TSESTree.IfStatement; | ||
if (test.type === 'Literal' && typeof test.value === 'boolean') { | ||
report(test, undefined, context, test.value); | ||
} | ||
}, | ||
|
||
':statement': (node: TSESTree.Node) => { | ||
const { parent } = node; | ||
if (isIfStatement(parent)) { | ||
// we visit 'consequent' and 'alternate' and not if-statement directly in order to get scope for 'consequent' | ||
const currentScope = context.getScope(); | ||
|
||
if (parent.consequent === node) { | ||
const { truthy, falsy } = collectKnownIdentifiers(parent.test); | ||
truthyMap.set(parent.consequent, transformAndFilter(truthy, currentScope)); | ||
falsyMap.set(parent.consequent, transformAndFilter(falsy, currentScope)); | ||
} else if (parent.alternate === node && isIdentifier(parent.test)) { | ||
falsyMap.set(parent.alternate, transformAndFilter([parent.test], currentScope)); | ||
} | ||
} | ||
}, | ||
|
||
':statement:exit': (node: TSESTree.Node) => { | ||
const stmt = node as TSESTree.Statement; | ||
truthyMap.delete(stmt); | ||
falsyMap.delete(stmt); | ||
}, | ||
|
||
Identifier: (node: TSESTree.Node) => { | ||
const id = node as TSESTree.Identifier; | ||
const symbol = getSymbol(id, context.getScope()); | ||
const { parent } = node; | ||
if (!symbol || !parent) { | ||
return; | ||
} | ||
if ( | ||
!isLogicalAnd(parent) && | ||
!isLogicalOrLhs(id, parent) && | ||
!isIfStatement(parent) && | ||
!isLogicalNegation(parent) | ||
) { | ||
return; | ||
} | ||
|
||
const checkIfKnownAndReport = ( | ||
map: Map<TSESTree.Statement, TSESLint.Scope.Reference[]>, | ||
truthy: boolean, | ||
) => { | ||
map.forEach(references => { | ||
const ref = references.find(ref => ref.resolved === symbol); | ||
if (ref) { | ||
report(id, ref, context, truthy); | ||
} | ||
}); | ||
}; | ||
|
||
checkIfKnownAndReport(truthyMap, true); | ||
checkIfKnownAndReport(falsyMap, false); | ||
}, | ||
|
||
Program: () => { | ||
truthyMap.clear(); | ||
falsyMap.clear(); | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
function collectKnownIdentifiers(expression: TSESTree.Expression) { | ||
const truthy: TSESTree.Identifier[] = []; | ||
const falsy: TSESTree.Identifier[] = []; | ||
|
||
const checkExpr = (expr: TSESTree.Expression) => { | ||
if (isIdentifier(expr)) { | ||
truthy.push(expr); | ||
} else if (isLogicalNegation(expr)) { | ||
if (isIdentifier(expr.argument)) { | ||
falsy.push(expr.argument); | ||
} else if (isLogicalNegation(expr.argument) && isIdentifier(expr.argument.argument)) { | ||
truthy.push(expr.argument.argument); | ||
} | ||
} | ||
}; | ||
|
||
let current = expression; | ||
checkExpr(current); | ||
while (isLogicalAnd(current)) { | ||
checkExpr(current.right); | ||
current = current.left; | ||
} | ||
checkExpr(current); | ||
|
||
return { truthy, falsy }; | ||
} | ||
|
||
function isLogicalAnd(expression: TSESTree.Node): expression is TSESTree.LogicalExpression { | ||
return expression.type === 'LogicalExpression' && expression.operator === '&&'; | ||
} | ||
|
||
function isLogicalOrLhs( | ||
id: TSESTree.Identifier, | ||
expression: TSESTree.Node, | ||
): expression is TSESTree.LogicalExpression { | ||
return ( | ||
expression.type === 'LogicalExpression' && | ||
expression.operator === '||' && | ||
expression.left === id | ||
); | ||
} | ||
|
||
function isLogicalNegation(expression: TSESTree.Node): expression is TSESTree.UnaryExpression { | ||
return expression.type === 'UnaryExpression' && expression.operator === '!'; | ||
} | ||
|
||
function isDefined<T>(x: T | undefined | null): x is T { | ||
return x != null; | ||
} | ||
|
||
function getSymbol(id: TSESTree.Identifier, scope: TSESLint.Scope.Scope) { | ||
const ref = scope.references.find(r => r.identifier === id); | ||
if (ref) { | ||
return ref.resolved; | ||
} | ||
return null; | ||
} | ||
|
||
function getFunctionScope(scope: TSESLint.Scope.Scope): TSESLint.Scope.Scope | null { | ||
if (scope.type === 'function') { | ||
return scope; | ||
} else if (!scope.upper) { | ||
return null; | ||
} | ||
return getFunctionScope(scope.upper); | ||
} | ||
|
||
function mightBeWritten(symbol: TSESLint.Scope.Variable, currentScope: TSESLint.Scope.Scope) { | ||
return symbol.references | ||
.filter(ref => ref.isWrite()) | ||
.find(ref => { | ||
const refScope = ref.from; | ||
|
||
let cur: TSESLint.Scope.Scope | null = refScope; | ||
while (cur) { | ||
if (cur === currentScope) { | ||
return true; | ||
} | ||
cur = cur.upper; | ||
} | ||
|
||
const currentFunc = getFunctionScope(currentScope); | ||
const refFunc = getFunctionScope(refScope); | ||
return refFunc !== currentFunc; | ||
}); | ||
} | ||
|
||
function transformAndFilter(ids: TSESTree.Identifier[], currentScope: TSESLint.Scope.Scope) { | ||
return ids | ||
.map(id => currentScope.upper!.references.find(r => r.identifier === id)) | ||
.filter(isDefined) | ||
.filter(ref => isDefined(ref.resolved)) | ||
.filter(ref => !mightBeWritten(ref.resolved!, currentScope)); | ||
} | ||
|
||
function report( | ||
id: TSESTree.Node, | ||
ref: TSESLint.Scope.Reference | undefined, | ||
context: Rule.RuleContext, | ||
truthy: boolean, | ||
) { | ||
const value = truthy ? 'truthy' : 'falsy'; | ||
|
||
const encodedMessage: EncodedMessage = { | ||
message: `This always evaluates to ${value}. Consider refactoring this code.`, | ||
secondaryLocations: getSecondaryLocations(ref, value), | ||
}; | ||
|
||
context.report({ | ||
message: JSON.stringify(encodedMessage), | ||
node: id, | ||
}); | ||
} | ||
|
||
function getSecondaryLocations(ref: TSESLint.Scope.Reference | undefined, truthy: string) { | ||
if (ref) { | ||
const secLoc = ref.identifier.loc!; | ||
return [ | ||
{ | ||
message: `Evaluated here to be ${truthy}`, | ||
line: secLoc.start.line, | ||
column: secLoc.start.column, | ||
endLine: secLoc.end.line, | ||
endColumn: secLoc.end.column, | ||
}, | ||
]; | ||
} else { | ||
return []; | ||
} | ||
} | ||
|
||
export = rule; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think those should be sorted by rule key