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 S1186 ('no-empty-function') #3056

Merged
merged 3 commits into from
Mar 18, 2022
Merged

Conversation

yassin-kammoun-sonarsource
Copy link
Contributor

Fixes #3005

@yassin-kammoun-sonarsource
Copy link
Contributor Author

Inspired from EmptyMethodsCheck (SonarJava).

export function decorateNoEmptyFunction(rule: Rule.RuleModule): Rule.RuleModule {
rule.meta!.hasSuggestions = true;
return interceptReport(rule, (context, reportDescriptor) => {
if ('node' in reportDescriptor) {
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 if node is not there? Issue will not be reported at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the issue won't be reported. I am starting to think that this condition is useless because once we intercept a report, we know for sure that there is a node, at least for this rule. Should I just remove the condition ?

return interceptReport(rule, (context, reportDescriptor) => {
if ('node' in reportDescriptor) {
const func = reportDescriptor.node as FunctionLike;
const name = reportDescriptor.data!.name;
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 be consistent, if we check node presence, let's also check data.name presence. Or let's assume that they are always available (I tend to second option)

const name = reportDescriptor.data!.name;
const openingBrace = context
.getSourceCode()
.getFirstToken(func.body, token => token.value === '{')!;
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 can remove filter by value as we know that body is empty

} else {
const columnOffset = closingBrace.loc.start.column;
const padding = ' '.repeat(columnOffset);
commentPlaceholder = `\n${padding} // TODO document why this ${name} is empty\n${padding}`;
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 that java impl has that, but I don't get why we need second new line. Do you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No idea. SONARJAVA-3906 mentions that there are situations when a newline is needed, but I didn't see an example in the unit tests.

}
};
`,
errors: [
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 the arrow function to see what will be the name of it?

@sonarsource-next
Copy link

sonarsource-next bot commented Mar 18, 2022

@vilchik-elena vilchik-elena merged commit e6fdf8a into master Mar 18, 2022
@vilchik-elena vilchik-elena deleted the issue-3005 branch March 18, 2022 12:58
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 S1186: Functions should not be empty
2 participants