-
-
Notifications
You must be signed in to change notification settings - Fork 497
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
Conversation
} | ||
DeclarationQuery::TsDeclareStatement(decl) => { | ||
let decl = decl.declaration().ok()?; | ||
let decl = decl.as_js_class_declaration()?; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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." |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
+1 for |
yes, I also like noMisleadingInstantiator, I'll change the name to it. |
15e5ac8
to
ee4746d
Compare
noMisusedNew
noMisleadingInstantiator
52754b9
to
6e028df
Compare
18f85b1
to
e607cf7
Compare
1e6173a
to
ae65f27
Compare
196eaac
to
ef40630
Compare
crates/biome_js_analyze/src/analyzers/nursery/no_misleading_instantiator.rs
Outdated
Show resolved
Hide resolved
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
…stantiator.rs Co-authored-by: Victorien Elvinger <[email protected]>
Co-authored-by: Victorien Elvinger <[email protected]>
1a4c75e
to
7cb3a1b
Compare
…to no-misused-new
Summary
Closes #46
As I described in the rule doc, this rule triggers warnings in the following scenarios:
new
.constructor
ornew
that returns the interface type.constructor
method.So, the lint name can be changed more descriptive, butno-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