Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 2fb7649

Browse files
Rule S930: improve highlighting for no-extra-arguments (#250)
Co-authored-by: Daniel Graf Hoyos <[email protected]>
1 parent bdd8338 commit 2fb7649

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/rules/no-extra-arguments.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
issueLocation,
3333
getMainFunctionTokenLocation,
3434
IssueLocation,
35+
toSecondaryLocation,
3536
} from '../utils/locations';
3637
import { Rule } from '../utils/types';
3738
import docsUrl from '../utils/docs-url';
@@ -171,30 +172,35 @@ const rule: Rule.RuleModule = {
171172
context,
172173
{
173174
message,
174-
node: callExpr,
175+
node: callExpr.callee,
175176
},
176-
getSecondaryLocations(functionNode),
177+
getSecondaryLocations(callExpr, functionNode),
177178
);
178179
}
179180

180-
function getSecondaryLocations(functionNode: TSESTree.FunctionLike) {
181+
function getSecondaryLocations(
182+
callExpr: TSESTree.CallExpression,
183+
functionNode: TSESTree.FunctionLike,
184+
) {
181185
const paramLength = functionNode.params.length;
182186
const secondaryLocations: IssueLocation[] = [];
183187
if (paramLength > 0) {
184188
const startLoc = functionNode.params[0].loc;
185189
const endLoc = functionNode.params[paramLength - 1].loc;
186-
// defensive check as `loc` property may be undefined according to
187-
// its type declaration
188-
if (startLoc && endLoc) {
189-
secondaryLocations.push(issueLocation(startLoc, endLoc, 'Formal parameters'));
190-
}
190+
secondaryLocations.push(issueLocation(startLoc, endLoc, 'Formal parameters'));
191191
} else {
192192
// as we're not providing parent node, `getMainFunctionTokenLocation` may return `undefined`
193193
const fnToken = getMainFunctionTokenLocation(functionNode, undefined, context);
194194
if (fnToken) {
195195
secondaryLocations.push(issueLocation(fnToken, fnToken, 'Formal parameters'));
196196
}
197197
}
198+
// find actual extra arguments to highlight
199+
callExpr.arguments.forEach((argument, index) => {
200+
if (index >= paramLength) {
201+
secondaryLocations.push(toSecondaryLocation(argument, 'Extra argument'));
202+
}
203+
});
198204
return secondaryLocations;
199205
}
200206
},

src/utils/locations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ export function toEncodedMessage(
128128
return JSON.stringify(encodedMessage);
129129
}
130130

131-
function toSecondaryLocation(
131+
export function toSecondaryLocation(
132132
locationHolder: TSESLint.AST.Token | TSESTree.Node,
133133
message?: string,
134134
): IssueLocation {
135-
const loc = locationHolder.loc!;
135+
const loc = locationHolder.loc;
136136
return {
137137
message,
138138
column: loc.start.column,

tests/rules/no-extra-arguments.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,22 @@ ruleTester.run('no-extra-arguments', rule, {
8383
foo(1, 2, 3, 4);
8484
`,
8585
errors: [
86-
message(2, 3, { line: 3, column: 9, endColumn: 21 }),
87-
message(2, 4, { line: 4, column: 9, endColumn: 24 }),
86+
message(2, 3, { line: 3, column: 9, endColumn: 12 }),
87+
message(2, 4, { line: 4, column: 9, endColumn: 12 }),
8888
],
8989
},
9090
{
9191
code: `
9292
function foo(p1, p2) {}
9393
// ^^^^^^>
9494
foo(1, 2, 3);
95-
//^^^^^^^^^^^^
95+
//^^^ <^
9696
`,
9797
errors: [
9898
{
9999
message: encodedMessage(2, 3, [
100100
{ line: 2, column: 21, endLine: 2, endColumn: 27, message: 'Formal parameters' },
101+
{ message: 'Extra argument', column: 18, line: 4, endColumn: 19, endLine: 4 },
101102
]),
102103
},
103104
],
@@ -110,12 +111,12 @@ ruleTester.run('no-extra-arguments', rule, {
110111
}
111112
foo(1);
112113
`,
113-
errors: [message(0, 1, { line: 5, column: 9, endColumn: 15 })],
114+
errors: [message(0, 1, { line: 5, column: 9, endColumn: 12 })],
114115
},
115116
{
116117
code: `
117118
foo(1);
118-
//^^^^^^
119+
// ^
119120
var foo = function() {
120121
// ^^^^^^^^>
121122
console.log('hello');
@@ -125,43 +126,43 @@ ruleTester.run('no-extra-arguments', rule, {
125126
{
126127
message: encodedMessage(0, 1, [
127128
{ line: 4, column: 18, endLine: 4, endColumn: 26, message: 'Formal parameters' },
129+
{ message: 'Extra argument', column: 12, line: 2, endColumn: 13, endLine: 2 },
128130
]),
129131
},
130132
],
131133
options: ['sonar-runtime'],
132134
},
133-
134135
{
135136
code: `
136137
(function(p1, p2){
137138
doSomething1;
138139
doSomething2;
139140
})(1, 2, 3);
140141
`,
141-
errors: [message(2, 3, { line: 2, column: 9, endLine: 5, endColumn: 20 })],
142+
errors: [message(2, 3, { line: 2, column: 10, endLine: 5, endColumn: 10 })],
142143
},
143144
{
144145
code: `
145146
let x = function(a, b) {
146147
return a + b;
147148
}(1, 2, 3);
148149
`,
149-
errors: [message(2, 3, { line: 2, column: 17, endLine: 4, endColumn: 19 })],
150+
errors: [message(2, 3, { line: 2, column: 17, endLine: 4, endColumn: 10 })],
150151
},
151152
{
152153
code: `
153154
((a, b) => {
154155
return a + b;
155156
})(1, 2, 3);
156157
`,
157-
errors: [message(2, 3, { line: 2, column: 9, endLine: 4, endColumn: 20 })],
158+
errors: [message(2, 3, { line: 2, column: 10, endLine: 4, endColumn: 10 })],
158159
},
159160
{
160161
code: `
161162
let arrow_function = (a, b) => {};
162163
arrow_function(1, 2, 3);
163164
`,
164-
errors: [message(2, 3, { line: 3, column: 9, endColumn: 32 })],
165+
errors: [message(2, 3, { line: 3, column: 9, endColumn: 23 })],
165166
},
166167
],
167168
});
@@ -176,7 +177,7 @@ ruleTesterScript.run('no-extra-arguments script', rule, {
176177
}
177178
foo(1, 2);
178179
`,
179-
errors: [message(1, 2, { line: 5, column: 9, endColumn: 18 })],
180+
errors: [message(1, 2, { line: 5, column: 9, endColumn: 12 })],
180181
},
181182
{
182183
code: `
@@ -186,7 +187,7 @@ ruleTesterScript.run('no-extra-arguments script', rule, {
186187
}
187188
foo(1, 2);
188189
`,
189-
errors: [message(0, 2, { line: 6, column: 9, endColumn: 18 })],
190+
errors: [message(0, 2, { line: 6, column: 9, endColumn: 12 })],
190191
},
191192
],
192193
});

0 commit comments

Comments
 (0)