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

feat(lint): noMisleadingInstantiator #215

Merged
merged 14 commits into from
Sep 17, 2023
Merged

Conversation

unvalley
Copy link
Member

@unvalley unvalley commented Sep 9, 2023

Summary

Closes #46

As I described in the rule doc, this rule triggers warnings in the following scenarios:

  • When a class has a method named new.
  • When an interface defines a method named constructor or new that returns the interface type.
  • When a type alias has a constructor method.

So, the lint name can be changed more descriptive, but no-misused-new-and-constructor is a bit subtle.
no-misleading-instantiation?
no-misleading-instantiator

Test Plan

cargo test -p rome_js_analyze -- no_misleading_instantiator

@github-actions github-actions bot added A-Project Area: project A-Linter Area: linter A-Formatter Area: formatter A-Website Area: website L-JavaScript Language: JavaScript and super languages L-JSON Language: JSON and super languages A-Diagnostic Area: diagnostocis A-Changelog Area: changelog and removed A-Formatter Area: formatter labels Sep 9, 2023
}
DeclarationQuery::TsDeclareStatement(decl) => {
let decl = decl.declaration().ok()?;
let decl = decl.as_js_class_declaration()?;
Copy link
Member

Choose a reason for hiding this comment

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

This class declaration should already be handled in DeclarationQuery::JsClassDeclaration? Am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's not handled by JsClassDeclaration but I don't know why, need to investigate it.

let any_ts_type = construct.type_annotation()?.ty().ok()?;
let reference_type = any_ts_type.as_ts_reference_type()?;
let return_type_ident = extract_return_type_ident(&reference_type)?;
if interface_ident.text_trimmed() == return_type_ident.text_trimmed() {
Copy link
Member

@Conaclos Conaclos Sep 9, 2023

Choose a reason for hiding this comment

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

Shoudl we also handle the case where the type is the this type?
For example:

interface I {
  new(): this;
}

}
AnyTsTypeMember::TsMethodSignatureTypeMember(method) => {
let method_name = method.name().ok()?;
let method_name = method_name.as_js_literal_member_name()?.syntax();
Copy link
Member

Choose a reason for hiding this comment

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

To simplify the code, we could implement a method AnyJsClassMemberName::name with a similar implementation to AnyJsObjectMemberName::name.

}

declare_node_union! {
pub(crate) DeclarationQuery = TsInterfaceDeclaration | TsTypeAliasDeclaration | JsClassDeclaration | TsDeclareStatement
Copy link
Member

Choose a reason for hiding this comment

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

ARe there some advantages to query on these types instead of querying AnyTsTypeMember / AnyJsClassMember?

Copy link
Member Author

@unvalley unvalley Sep 12, 2023

Choose a reason for hiding this comment

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

To know class, interface, type aliases identifier, I think we need to hit the node types.
for example,

interface I { // we can access this `I` through the TsInterfaceDeclaration
  new (): I;
}

so I set those node types.

}
}

fn note(&self) -> MarkupBuf {
Copy link
Member

Choose a reason for hiding this comment

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

This 'notes' seems more helpful than the previous "messages". I think we could use them as main diagnostic? We could add a note in some cases. For example for a misued new in a class, we could ask the user if they mean constructor. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, I feel that in the existing rules, the title is simplified and explain the detailed reason in the note.
But useful information should be show fast and we need to define roles for title and note. I think that in the previous rules, title stands for what, note for why.

Copy link
Member

Choose a reason for hiding this comment

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

@ematipico Have you some opinion to share?

Copy link
Member

Choose a reason for hiding this comment

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

I have no strong opinions, but I think the current implementation is fine

})
.to_owned(),
RuleState::InterfaceMisusedNew(_) => (markup! {
"`new` is misleading in interface."
Copy link
Member

Choose a reason for hiding this comment

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

We could use a more detailled message such as new makes an interface a constructor. The returned type should certainly a different type from its constructor.. Any thought?

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree with that. I'll update it more descriptive.

@Conaclos
Copy link
Member

Conaclos commented Sep 9, 2023

So, the lint name can be changed more descriptive, but no-misused-new-and-constructor is a bit subtle.
no-misleading-instantiation?

instantiation makes me think about a new C().
Maybe noMisleadingConstructor or noMisleadingInstantiator?

@ematipico
Copy link
Member

+1 for noMisleadingInstantiator

@unvalley
Copy link
Member Author

yes, I also like noMisleadingInstantiator, I'll change the name to it.

@github-actions github-actions bot added the A-Formatter Area: formatter label Sep 12, 2023
@unvalley unvalley changed the title feat(lint): noMisusedNew feat(lint): noMisleadingInstantiator Sep 13, 2023
@github-actions github-actions bot added the A-Parser Area: parser label Sep 13, 2023
@github-actions github-actions bot added A-Changelog Area: changelog and removed A-Changelog Area: changelog A-Formatter Area: formatter labels Sep 14, 2023
@unvalley unvalley force-pushed the no-misused-new branch 2 times, most recently from 18f85b1 to e607cf7 Compare September 15, 2023 16:50
@github-actions github-actions bot added A-Formatter Area: formatter and removed A-Formatter Area: formatter labels Sep 15, 2023
@github-actions github-actions bot added A-Formatter Area: formatter and removed A-Formatter Area: formatter labels Sep 15, 2023
@github-actions github-actions bot added A-Formatter Area: formatter and removed A-Formatter Area: formatter labels Sep 15, 2023
@unvalley unvalley marked this pull request as ready for review September 15, 2023 18:30
@unvalley unvalley requested a review from Conaclos September 15, 2023 18:31
CHANGELOG.md Outdated Show resolved Hide resolved
unvalley and others added 9 commits September 17, 2023 03:21
chore: codegen

chore: documentation

wip

chore: resolve conflicts

refactor: filter_map to flat_map

Discard changes to crates/biome_js_unicode_table/src/tables.rs

Discard changes to crates/biome_js_formatter/src/generated.rs

chore: update message

refactor: impl name method for AnyJsClassMemberName

refactor: use name method and update test

chore: rename to no-misleading-instantiator

feat: handle returning this case

refactor: note and method names

chore: codegen and changelog

Discard changes to CHANGELOG.md

chore: changelog

chore: changelog.mdx

chore: codegen

Discard changes to crates/biome_js_formatter/src/generated.rs

Discard changes to crates/biome_js_unicode_table/src/tables.rs

Discard changes to crates/biome_json_formatter/src/generated.rs
Co-authored-by: Victorien Elvinger <[email protected]>
@github-actions github-actions bot added the A-Formatter Area: formatter label Sep 16, 2023
@github-actions github-actions bot removed the A-Formatter Area: formatter label Sep 17, 2023
@unvalley unvalley merged commit d1488cb into biomejs:main Sep 17, 2023
15 checks passed
@unvalley unvalley deleted the no-misused-new branch September 17, 2023 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Parser Area: parser A-Project Area: project A-Website Area: website L-JavaScript Language: JavaScript and super languages L-JSON Language: JSON and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📎 Implement lint/noMisleadingInstantiator - typescript-eslint/no-misused-new
3 participants