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

Add quick fix for S1172 ('no-unused-function-argument') #3038

Merged
merged 11 commits into from
Mar 17, 2022

Conversation

yassin-kammoun-sonarsource
Copy link
Contributor

Fixes #3033

data: {
param: paramVariable.name,
},
fix: fixer => fixer.replaceText(paramIdentifier, `_${paramVariable.name}`),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be using insertTextBefore will be more efficient?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will not work for parameter with type. Can we have a test?

fix: fixer => fixer.replaceText(paramIdentifier, `_${paramVariable.name}`),
},
];
const funct = paramVariable.defs[0].node as TSESTree.FunctionLike;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why such name for variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funct for function, but the latter is a keyword.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I will rename it to functionLike.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, indeed:)) it's just usually we use fun or func. I have never seen funct

@@ -196,5 +196,54 @@ ruleTester.run('Unused function parameters should be removed', rule, {
}`,
errors: 1,
},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add unit test with parameter type to verify that type is also removed?

fix: fixer => {
const paramIndex = funct.params.indexOf(paramIdentifier as TSESTree.Parameter);
if (funct.params.length === 1) {
return fixer.remove(funct.params[paramIndex] as estree.Node);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what will happen with trailing comma?

} else if (funct.params.length - 1 === paramIndex) {
const [, start] = funct.params[paramIndex - 1].range;
const [, end] = funct.params[paramIndex].range;
return fixer.removeRange([start, end]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be fine for trailing comma but can we add a test for it?

@yassin-kammoun-sonarsource yassin-kammoun-sonarsource changed the title Add quick fix for S1172: Unused function parameters should be removed Add quick fix for S1172 ('no-unused-function-argument') Mar 14, 2022
Copy link
Contributor

@vilchik-elena vilchik-elena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And same thing as with another PR, I think we should keep the trailing comma style

return fixer.replaceText(param, '()');
}
}
case 'FunctionDeclaration': {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this case and next one are very similar, can't we generalize the logic?

messages: {
removeOrRenameParameter:
'Remove the unused function parameter "{{param}}" or rename it to "_{{param}}" to make intention explicit.',
suggestRemoveParameter: 'Remove {{param}} (beware of call sites)',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should wrap parameter names into quotes.

switch (func.type) {
case 'ArrowFunctionExpression': {
const param = func.params[paramIndex] as estree.Node;
const token = context.getSourceCode().getTokenAfter(param);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

theoretically there could be comma ((a,) => foo())

code: `function fun(a, b, c, d, {e}, f, g) { b = d = f; }`,
errors: [
{
suggestions: [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you don't have to assert all suggestions and their messages. Assert only what matters for the case, otherwise it's hard to follow

const openingParenthesis = context
.getSourceCode()
.getTokenBefore(param, token => token.value === '(');
const closingParenthesis = context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will not work for a => foo() :(

eslint-bridge/src/rules/no-unused-function-argument.ts Outdated Show resolved Hide resolved
suggestions: [
{
desc: 'Rename "a" to "_a"',
output: '( _a, ) => {}',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see we can't remove this suggestion from assertion, but let's at least remove the messages to reduce some readablility noise

@vilchik-elena vilchik-elena enabled auto-merge (squash) March 17, 2022 10:07
@sonarsource-next
Copy link

@vilchik-elena vilchik-elena merged commit c39bf15 into master Mar 17, 2022
@vilchik-elena vilchik-elena deleted the issue-3033 branch March 17, 2022 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add quick fix for S1172: Unused function parameters should be removed
2 participants