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

feat(rome_js_analyze): add noExtraNonNullAssertion rule #3797

Merged
merged 6 commits into from
Nov 23, 2022

Conversation

kaioduarte
Copy link
Contributor

Summary

Implement no-extra-non-null-assertion.

Test Plan

Unit tests

@netlify
Copy link

netlify bot commented Nov 19, 2022

Deploy Preview for docs-rometools ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 5d00415
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/637e09cc774260000aa27349
😎 Deploy Preview https://deploy-preview-3797--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.

}

impl Rule for NoExtraNonNullAssertion {
type Query = Ast<TsNonNullAssertionExpression>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Rome also has a TsNonNullAssertionAssignment for null assertions in assignment positions:

a!!.b = null

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 added a test to cover this case and it's working with the current implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, because it is in the object path. This example should work:

a!!! = null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the example! I handled that on f434c37.

fn has_extra_non_null_assertion(expression: JsAnyExpression) -> bool {
match expression {
JsAnyExpression::TsNonNullAssertionExpression(_) => return true,
JsAnyExpression::JsStaticMemberExpression(static_member_exp) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function should have a similar branch for JsComputedMemberExpression nodes with an optional_chain_token

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, the optional chain check doesn't work well in this case because it checks the entire expression, not only what's inside the brackets, invalidating this case 😕

obj?.[key!]

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah right, in order to correctly detect the case where the expressions are nested as obj!?.[key] we could check that computed_member_expr.object() == query to ignore the case where the non-null assertion is nested in the member position

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Checking the object also can create a false positive, like:

a![b]

Also, the ESLint rule doesn't emit a diagnostic for the above example. Maybe it's okay to ignore that branch?

@ematipico ematipico added the A-Linter Area: linter label Nov 21, 2022
@ematipico ematipico added this to the 11.0.0 milestone Nov 21, 2022
@kaioduarte kaioduarte force-pushed the feat/no-extra-non-null-assertion branch 4 times, most recently from 5186531 to f434c37 Compare November 22, 2022 09:01
@kaioduarte kaioduarte force-pushed the feat/no-extra-non-null-assertion branch from f8d9b07 to 5d00415 Compare November 23, 2022 11:53
@ematipico ematipico merged commit d8e5f1a into rome:main Nov 23, 2022
@kaioduarte kaioduarte deleted the feat/no-extra-non-null-assertion branch November 23, 2022 13:11
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
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants