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(linter): implement adjacent-overload-signature #578

Merged
merged 6 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ impl<'a> PropertyKey<'a> {
Self::PrivateIdentifier(_) => None,
Self::Expression(expr) => match expr {
Expression::StringLiteral(lit) => Some(lit.value.clone()),
Expression::RegExpLiteral(lit) => Some(Atom::from(lit.regex.to_string())),
Expression::NumberLiteral(lit) => Some(Atom::from(lit.value.to_string())),
Expression::BigintLiteral(lit) => Some(Atom::from(lit.value.to_string())),
Expression::RegExpLiteral(lit) => Some(Atom::from(format!("{0}", lit.regex))),
cijiugechu marked this conversation as resolved.
Show resolved Hide resolved
Expression::NumberLiteral(lit) => Some(Atom::from(lit.raw)),
Expression::BigintLiteral(lit) => Some(Atom::from(format!("{0}", lit.value))),
Expression::NullLiteral(_) => Some("null".into()),
Expression::TemplateLiteral(lit) => {
lit.expressions.is_empty().then(|| lit.quasi()).flatten().cloned()
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ pub fn get_name_from_property_key(key: &PropertyKey<'_>) -> Option<Atom> {
}
PropertyKey::Expression(expr) => match expr {
Expression::StringLiteral(lit) => Some(lit.value.clone()),
Expression::RegExpLiteral(lit) => Some(Atom::from(lit.regex.to_string())),
Expression::NumberLiteral(lit) => Some(Atom::from(lit.value.to_string())),
Expression::BigintLiteral(lit) => Some(Atom::from(lit.value.to_string())),
Expression::RegExpLiteral(lit) => Some(Atom::from(format!("{0}", lit.regex))),
Expression::NumberLiteral(lit) => Some(Atom::from(lit.raw)),
Expression::BigintLiteral(lit) => Some(Atom::from(format!("{0}", lit.value))),
Expression::NullLiteral(_) => Some("null".into()),
Expression::TemplateLiteral(lit) => {
lit.expressions.is_empty().then(|| lit.quasi()).flatten().cloned()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{context::LintContext, rule::Rule, AstNode, ast_util::get_name_from_p
#[error("typescript-eslint(adjacent-overload-signatures): All {0:?} signatures should be adjacent.")]
#[diagnostic(severity(warning))]
struct AdjacentOverloadSignaturesDiagnostic(
Atom, #[label] pub Span
Atom, #[label] pub Option<Span>, #[label] pub Span
);

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -129,7 +129,7 @@ impl GetMethod for ClassElement<'_> {
r#static: def.r#static,
call_signature: false,
kind: get_kind_from_key(&def.key),
span: def.key.span(),
span: Span::new(def.span.start, def.key.span().end),
}
)
}
Expand Down Expand Up @@ -167,7 +167,7 @@ impl GetMethod for TSSignature<'_> {
r#static: false,
call_signature: false,
kind: MethodKind::Normal,
span: decl.span,
span: Span::new(decl.span.start, decl.span.start + 3),
})
}
_ => None
Expand Down Expand Up @@ -281,14 +281,22 @@ fn check_and_report(methods: &Vec<Option<Method>>, ctx: &LintContext<'_>) {
} else {
method.name.clone()
};

let last_same_method = seen_methods
.iter()
.rev()
.find(|m| m.is_same_method(Some(method)));

ctx.diagnostic(
AdjacentOverloadSignaturesDiagnostic(name, method.span)
AdjacentOverloadSignaturesDiagnostic(
name,
last_same_method.map(|m| m.span),
method.span
)
);
} else if index.is_none() {
} else {
seen_methods.push(method);
}

last_method = Some(method);
} else {
last_method = None;
Expand Down
Loading