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

feat(rome_js_analyze): noUselessRename #4116

Merged
merged 1 commit into from
Feb 7, 2023
Merged

feat(rome_js_analyze): noUselessRename #4116

merged 1 commit into from
Feb 7, 2023

Conversation

Conaclos
Copy link
Contributor

@Conaclos Conaclos commented Dec 29, 2022

Summary

Closes #4115

This implements ESLint's rule no-useless-rename.

In contrast to ESLint, this rule does not support options and does not handle cases where identifier do not match:

let {\u0061: a} = obj;
import {a as \u0061} from 'foo';
import {\u0061 as a} from 'foo';
export {\u0061 as a};
export {a as \u0061};

let {"a": a} = obj;
import { "a" as a}
export { a as "a"}

Note that the formatter normalizes \u0061 to a.

Computed properties are not supported (similar to ESLint):

let {["a"]: a} = obj;

Test Plan

Include ESLint unit tests (except unsupported cases).

Documentation

  • The PR requires documentation
  • I will create a new PR to update the documentation

@Conaclos Conaclos requested review from leops, ematipico, xunilrj and a team as code owners December 29, 2022 19:53
@netlify
Copy link

netlify bot commented Dec 29, 2022

Deploy Preview for docs-rometools ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 76268d2
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/63e296996a2d3a0008a94179
😎 Deploy Preview https://deploy-preview-4116--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.

Comment on lines +80 to +106
let (old_name, new_name) = match renaming {
JsRenaming::JsExportNamedFromSpecifier(x) => (
x.source_name().ok()?.value().ok()?,
x.export_as()?.exported_name().ok()?.value().ok()?,
),
JsRenaming::JsExportNamedSpecifier(x) => (
x.local_name().ok()?.value_token().ok()?,
x.exported_name().ok()?.value().ok()?,
),
JsRenaming::JsNamedImportSpecifier(x) => (
x.name().ok()?.value().ok()?,
x.local_name()
.ok()?
.as_js_identifier_binding()?
.name_token()
.ok()?,
),
JsRenaming::JsObjectBindingPatternProperty(x) => (
x.member().ok()?.as_js_literal_member_name()?.value().ok()?,
x.pattern()
.ok()?
.as_any_js_binding()?
.as_js_identifier_binding()?
.name_token()
.ok()?,
),
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: this is not something specific to this rule but a pattern we could apply in general to avoid the number of ok() calls necessary in lint rules.

The idea would be to introduce a new function that returns a Result instead of an Option:

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
  Self::is_useless_rename(ctx).ok()
}

fn is_useless_rename(ctx) -> SyntaxResult<bool> {
	// existing code but without any `ok` call
}

Copy link
Contributor Author

@Conaclos Conaclos Dec 30, 2022

Choose a reason for hiding this comment

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

The issue is that some calls return an option. e.g. x.export_as()? or .as_js_identifier_binding()?. I can use an if statement and return an early Ok(false). However I am not sure if it is worth it?

Copy link
Contributor

@ematipico ematipico Jan 4, 2023

Choose a reason for hiding this comment

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

I went down that road, and the APIs are good like this. Because some APIs return Option, trying to fit them inside a function that returns Result makes everything weird and doesn't feel right.

@github-actions
Copy link

This PR is stale because it has been open 14 days with no activity.

@Conaclos Conaclos requested review from ematipico and removed request for xunilrj and leops February 7, 2023 17:02
Copy link
Contributor

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

A rebase with the newly generated files should prepare the PR for merge.

@ematipico ematipico merged commit 5c60450 into rome:main Feb 7, 2023
@Conaclos Conaclos deleted the noUselessRename branch March 7, 2023 16:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

noUselessRename, no-useless-rename
3 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载