Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): noVoidTypeReturn #3806

Merged
merged 1 commit into from
Nov 23, 2022
Merged

feat(rome_js_analyze): noVoidTypeReturn #3806

merged 1 commit into from
Nov 23, 2022

Conversation

Conaclos
Copy link
Contributor

Summary

This is an exclusive rule for Rome!
This disallows returning a value when the return-type of a function is void.

Test Plan

Unit tests and doc-tests included

@Conaclos Conaclos requested a review from a team November 20, 2022 18:41
@netlify
Copy link

netlify bot commented Nov 20, 2022

Deploy Preview for docs-rometools ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit e3f7559
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/637e5a7a06bfdd0008159b85
😎 Deploy Preview https://deploy-preview-3806--docs-rometools.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Contributor

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I see how this rule can be useful in catching bugs, but it may be controversial because using return iReturnVoid() can be useful.

But I think we can add it as a nursery rule and see if users find it useful.

I recommend adding support for the void operator that explicitly returns void.

function f(): void {
	return void 0;
}

Comment on lines 96 to 97
declare_node_union! {
pub(crate) JsFunctionMethod = JsArrowFunctionExpression | JsFunctionDeclaration | JsFunctionExportDefaultDeclaration | JsFunctionExpression | JsMethodClassMember | JsMethodObjectMember
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You also want to handle getter and setter methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

setters cannot be handled because they cannot be annotated with a return type. I added getters. I noted that getters have a different interface for the return type. Is this intentional?

Copy link
Contributor

Choose a reason for hiding this comment

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

That's fair, but doesn't that mean that a return with a non-void argument in a setter is always an error?

I noted that getters have a different interface for the return type. Is this intentional?

The reason is that getters are more limited regarding the return type annotation.

Copy link
Contributor Author

@Conaclos Conaclos Nov 22, 2022

Choose a reason for hiding this comment

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

That's fair, but doesn't that mean that a return with a non-void argument in a setter is always an error?

I suppose. This is why we have noSetterReturn. Do you suggest adding the check of noSetterReturn, and certainly noCOntrsutorReturn, in this rule? I first hesitated. However, I think it is better to keep distinct rules: this one relies on type annotation, while the others rely on constructor identifier/set keyword.

The reason is that getters are more limited regarding the return type annotation.

This makes sense. However, it is often simpler to handle types with uniform interfaces. I could open a discussion about that?

@Conaclos Conaclos requested review from ematipico and removed request for xunilrj and leops November 21, 2022 21:56
@jeysal
Copy link
Contributor

jeysal commented Nov 22, 2022

I see how this rule can be useful in catching bugs, but it may be controversial because using return iReturnVoid() can be useful.

I always find it a bit confusing in unfamiliar codebases if I don't know the function already. I'll check the tooltip of the function to see if it returns something despite having a name that sounds a lot like it returns void.

From the existing rules in Rome core and even in the recommended set I'd say there are more opinionated rules than that already. Plus style will at least be consistent across a large codebase and not sometimes return voidFn() and sometimes voidFn(); return.

So all in all would argue that's a good thing to forbid by default.

@Conaclos Conaclos requested a review from MichaReiser November 23, 2022 17:40
@MichaReiser MichaReiser merged commit 17750a1 into rome:main Nov 23, 2022
jeysal added a commit to jeysal/rometools that referenced this pull request Nov 24, 2022
* upstream/main: (73 commits)
  fix(semantic_analyzers): style/noShoutyConstants does not recognize multiple uses of a constant. (rome#3789)
  feat(rome_js_analyze): useDefaultSwitchClauseLast (rome#3791)
  chore: run rustfmt and typo fix (rome#3840)
  feat(rome_js_analyze): use exhaustive deps support properties (rome#3581)
  website(docs): Fix text formatting (rome#3828)
  feat(rome_js_analyze): `noVoidTypeReturn` (rome#3806)
  feat(rome_cli): expose the `--verbose` flag to the CLI (rome#3812)
  fix(rome_diagnostics): allow diagnostic locations to be created without a resource (rome#3834)
  feat(rome_js_analyze): add noExtraNonNullAssertion rule (rome#3797)
  fix(rome_lsp): lsp friendly catch unwind (rome#3740)
  feat(rome_js_semantic): model improvements (rome#3825)
  feat(rome_json_parser): JSON Lexer (rome#3809)
  feat(rome_js_analyze): implement `noDistractingElements` (rome#3820)
  fix(rome_js_formatter): shothanded named import line break with default import (rome#3826)
  feat(rome_js_analyze): `noConstructorReturn` (rome#3805)
  feat(website): change enabledNurseryRules to All/Recommended select (rome#3810)
  feat(rome_js_analyze): noSetterReturn
  feat(rome_js_analyze): noConstructorReturn
  feat(rome_analyze): suppress rule via code actions (rome#3572)
  feat(rome_js_analyze): `noVar` (rome#3765)
  ...
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Linter Area: linter L-JavaScript Langauge: JavaScript
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants