|
| 1 | +/* |
| 2 | + * eslint-plugin-sonarjs |
| 3 | + * Copyright (C) 2018-2021 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +import { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils'; |
| 22 | +import { EncodedMessage } from '../utils/locations'; |
| 23 | +import { isIdentifier, isIfStatement } from '../utils/nodes'; |
| 24 | +import { Rule } from '../utils/types'; |
| 25 | + |
| 26 | +const rule: Rule.RuleModule = { |
| 27 | + meta: { |
| 28 | + type: 'suggestion', |
| 29 | + schema: [ |
| 30 | + { |
| 31 | + // internal parameter for rules having secondary locations |
| 32 | + enum: ['sonar-runtime'], |
| 33 | + }, |
| 34 | + ], |
| 35 | + }, |
| 36 | + |
| 37 | + create(context: Rule.RuleContext) { |
| 38 | + const truthyMap: Map<TSESTree.Statement, TSESLint.Scope.Reference[]> = new Map(); |
| 39 | + const falsyMap: Map<TSESTree.Statement, TSESLint.Scope.Reference[]> = new Map(); |
| 40 | + |
| 41 | + return { |
| 42 | + IfStatement: (node: TSESTree.Node) => { |
| 43 | + const { test } = node as TSESTree.IfStatement; |
| 44 | + if (test.type === 'Literal' && typeof test.value === 'boolean') { |
| 45 | + report(test, undefined, context, test.value); |
| 46 | + } |
| 47 | + }, |
| 48 | + |
| 49 | + ':statement': (node: TSESTree.Node) => { |
| 50 | + const { parent } = node; |
| 51 | + if (isIfStatement(parent)) { |
| 52 | + // we visit 'consequent' and 'alternate' and not if-statement directly in order to get scope for 'consequent' |
| 53 | + const currentScope = context.getScope(); |
| 54 | + |
| 55 | + if (parent.consequent === node) { |
| 56 | + const { truthy, falsy } = collectKnownIdentifiers(parent.test); |
| 57 | + truthyMap.set(parent.consequent, transformAndFilter(truthy, currentScope)); |
| 58 | + falsyMap.set(parent.consequent, transformAndFilter(falsy, currentScope)); |
| 59 | + } else if (parent.alternate === node && isIdentifier(parent.test)) { |
| 60 | + falsyMap.set(parent.alternate, transformAndFilter([parent.test], currentScope)); |
| 61 | + } |
| 62 | + } |
| 63 | + }, |
| 64 | + |
| 65 | + ':statement:exit': (node: TSESTree.Node) => { |
| 66 | + const stmt = node as TSESTree.Statement; |
| 67 | + truthyMap.delete(stmt); |
| 68 | + falsyMap.delete(stmt); |
| 69 | + }, |
| 70 | + |
| 71 | + Identifier: (node: TSESTree.Node) => { |
| 72 | + const id = node as TSESTree.Identifier; |
| 73 | + const symbol = getSymbol(id, context.getScope()); |
| 74 | + const { parent } = node; |
| 75 | + if (!symbol || !parent) { |
| 76 | + return; |
| 77 | + } |
| 78 | + if ( |
| 79 | + !isLogicalAnd(parent) && |
| 80 | + !isLogicalOrLhs(id, parent) && |
| 81 | + !isIfStatement(parent) && |
| 82 | + !isLogicalNegation(parent) |
| 83 | + ) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const checkIfKnownAndReport = ( |
| 88 | + map: Map<TSESTree.Statement, TSESLint.Scope.Reference[]>, |
| 89 | + truthy: boolean, |
| 90 | + ) => { |
| 91 | + map.forEach(references => { |
| 92 | + const ref = references.find(ref => ref.resolved === symbol); |
| 93 | + if (ref) { |
| 94 | + report(id, ref, context, truthy); |
| 95 | + } |
| 96 | + }); |
| 97 | + }; |
| 98 | + |
| 99 | + checkIfKnownAndReport(truthyMap, true); |
| 100 | + checkIfKnownAndReport(falsyMap, false); |
| 101 | + }, |
| 102 | + |
| 103 | + Program: () => { |
| 104 | + truthyMap.clear(); |
| 105 | + falsyMap.clear(); |
| 106 | + }, |
| 107 | + }; |
| 108 | + }, |
| 109 | +}; |
| 110 | + |
| 111 | +function collectKnownIdentifiers(expression: TSESTree.Expression) { |
| 112 | + const truthy: TSESTree.Identifier[] = []; |
| 113 | + const falsy: TSESTree.Identifier[] = []; |
| 114 | + |
| 115 | + const checkExpr = (expr: TSESTree.Expression) => { |
| 116 | + if (isIdentifier(expr)) { |
| 117 | + truthy.push(expr); |
| 118 | + } else if (isLogicalNegation(expr)) { |
| 119 | + if (isIdentifier(expr.argument)) { |
| 120 | + falsy.push(expr.argument); |
| 121 | + } else if (isLogicalNegation(expr.argument) && isIdentifier(expr.argument.argument)) { |
| 122 | + truthy.push(expr.argument.argument); |
| 123 | + } |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + let current = expression; |
| 128 | + checkExpr(current); |
| 129 | + while (isLogicalAnd(current)) { |
| 130 | + checkExpr(current.right); |
| 131 | + current = current.left; |
| 132 | + } |
| 133 | + checkExpr(current); |
| 134 | + |
| 135 | + return { truthy, falsy }; |
| 136 | +} |
| 137 | + |
| 138 | +function isLogicalAnd(expression: TSESTree.Node): expression is TSESTree.LogicalExpression { |
| 139 | + return expression.type === 'LogicalExpression' && expression.operator === '&&'; |
| 140 | +} |
| 141 | + |
| 142 | +function isLogicalOrLhs( |
| 143 | + id: TSESTree.Identifier, |
| 144 | + expression: TSESTree.Node, |
| 145 | +): expression is TSESTree.LogicalExpression { |
| 146 | + return ( |
| 147 | + expression.type === 'LogicalExpression' && |
| 148 | + expression.operator === '||' && |
| 149 | + expression.left === id |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +function isLogicalNegation(expression: TSESTree.Node): expression is TSESTree.UnaryExpression { |
| 154 | + return expression.type === 'UnaryExpression' && expression.operator === '!'; |
| 155 | +} |
| 156 | + |
| 157 | +function isDefined<T>(x: T | undefined | null): x is T { |
| 158 | + return x != null; |
| 159 | +} |
| 160 | + |
| 161 | +function getSymbol(id: TSESTree.Identifier, scope: TSESLint.Scope.Scope) { |
| 162 | + const ref = scope.references.find(r => r.identifier === id); |
| 163 | + if (ref) { |
| 164 | + return ref.resolved; |
| 165 | + } |
| 166 | + return null; |
| 167 | +} |
| 168 | + |
| 169 | +function getFunctionScope(scope: TSESLint.Scope.Scope): TSESLint.Scope.Scope | null { |
| 170 | + if (scope.type === 'function') { |
| 171 | + return scope; |
| 172 | + } else if (!scope.upper) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + return getFunctionScope(scope.upper); |
| 176 | +} |
| 177 | + |
| 178 | +function mightBeWritten(symbol: TSESLint.Scope.Variable, currentScope: TSESLint.Scope.Scope) { |
| 179 | + return symbol.references |
| 180 | + .filter(ref => ref.isWrite()) |
| 181 | + .find(ref => { |
| 182 | + const refScope = ref.from; |
| 183 | + |
| 184 | + let cur: TSESLint.Scope.Scope | null = refScope; |
| 185 | + while (cur) { |
| 186 | + if (cur === currentScope) { |
| 187 | + return true; |
| 188 | + } |
| 189 | + cur = cur.upper; |
| 190 | + } |
| 191 | + |
| 192 | + const currentFunc = getFunctionScope(currentScope); |
| 193 | + const refFunc = getFunctionScope(refScope); |
| 194 | + return refFunc !== currentFunc; |
| 195 | + }); |
| 196 | +} |
| 197 | + |
| 198 | +function transformAndFilter(ids: TSESTree.Identifier[], currentScope: TSESLint.Scope.Scope) { |
| 199 | + return ids |
| 200 | + .map(id => currentScope.upper!.references.find(r => r.identifier === id)) |
| 201 | + .filter(isDefined) |
| 202 | + .filter(ref => isDefined(ref.resolved)) |
| 203 | + .filter(ref => !mightBeWritten(ref.resolved!, currentScope)); |
| 204 | +} |
| 205 | + |
| 206 | +function report( |
| 207 | + id: TSESTree.Node, |
| 208 | + ref: TSESLint.Scope.Reference | undefined, |
| 209 | + context: Rule.RuleContext, |
| 210 | + truthy: boolean, |
| 211 | +) { |
| 212 | + const value = truthy ? 'truthy' : 'falsy'; |
| 213 | + |
| 214 | + const encodedMessage: EncodedMessage = { |
| 215 | + message: `This always evaluates to ${value}. Consider refactoring this code.`, |
| 216 | + secondaryLocations: getSecondaryLocations(ref, value), |
| 217 | + }; |
| 218 | + |
| 219 | + context.report({ |
| 220 | + message: JSON.stringify(encodedMessage), |
| 221 | + node: id, |
| 222 | + }); |
| 223 | +} |
| 224 | + |
| 225 | +function getSecondaryLocations(ref: TSESLint.Scope.Reference | undefined, truthy: string) { |
| 226 | + if (ref) { |
| 227 | + const secLoc = ref.identifier.loc!; |
| 228 | + return [ |
| 229 | + { |
| 230 | + message: `Evaluated here to be ${truthy}`, |
| 231 | + line: secLoc.start.line, |
| 232 | + column: secLoc.start.column, |
| 233 | + endLine: secLoc.end.line, |
| 234 | + endColumn: secLoc.end.column, |
| 235 | + }, |
| 236 | + ]; |
| 237 | + } else { |
| 238 | + return []; |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +export = rule; |
0 commit comments