@@ -23,12 +23,16 @@ import { Rule } from "eslint";
2323import { Node , BinaryExpression , LogicalExpression } from "estree" ;
2424import { isIdentifier , isLiteral } from "../utils/nodes" ;
2525import { areEquivalent } from "../utils/equivalence" ;
26+ import { report , issueLocation , IssueLocation } from "../utils/locations" ;
2627
2728const EQUALITY_OPERATOR_TOKEN_KINDS = new Set ( [ "==" , "===" , "!=" , "!==" ] ) ;
2829
2930// consider only binary expressions with these operators
3031const RELEVANT_OPERATOR_TOKEN_KINDS = new Set ( [ "&&" , "||" , "/" , "-" , "<<" , ">>" , "<" , "<=" , ">" , ">=" ] ) ;
3132
33+ const message = ( operator : string ) =>
34+ `Correct one of the identical sub-expressions on both sides of operator "${ operator } "` ;
35+
3236function hasRelevantOperator ( node : BinaryExpression | LogicalExpression ) {
3337 return (
3438 RELEVANT_OPERATOR_TOKEN_KINDS . has ( node . operator ) ||
@@ -45,6 +49,14 @@ function isOneOntoOneShifting(node: BinaryExpression | LogicalExpression) {
4549}
4650
4751const rule : Rule . RuleModule = {
52+ meta : {
53+ schema : [
54+ {
55+ // internal parameter
56+ enum : [ "sonar-runtime" ] ,
57+ } ,
58+ ] ,
59+ } ,
4860 create ( context : Rule . RuleContext ) {
4961 return {
5062 LogicalExpression ( node : Node ) {
@@ -61,13 +73,24 @@ const rule: Rule.RuleModule = {
6173 ! isOneOntoOneShifting ( expr ) &&
6274 areEquivalent ( expr . left , expr . right , context . getSourceCode ( ) )
6375 ) {
64- context . report ( {
65- message : `Correct one of the identical sub-expressions on both sides of operator "{{operator}}"` ,
66- data : { operator : expr . operator } ,
67- node : expr ,
68- } ) ;
76+ const secondaryLocations : IssueLocation [ ] = [ ] ;
77+ if ( expr . left . loc ) {
78+ secondaryLocations . push ( issueLocation ( expr . left . loc ) ) ;
79+ }
80+ report (
81+ context ,
82+ {
83+ message : message ( expr . operator ) ,
84+ node : isSonarRuntime ( ) ? expr . right : expr ,
85+ } ,
86+ secondaryLocations ,
87+ ) ;
6988 }
7089 }
90+
91+ function isSonarRuntime ( ) {
92+ return context . options [ context . options . length - 1 ] === "sonar-runtime" ;
93+ }
7194 } ,
7295} ;
7396
0 commit comments