Skip to content

Commit

Permalink
perf: upgrade to deno_ast 0.41 (#1301)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jul 31, 2024
1 parent 5b4632b commit 8db22f3
Show file tree
Hide file tree
Showing 18 changed files with 463 additions and 456 deletions.
707 changes: 303 additions & 404 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ default = []
docs = []

[dependencies]
deno_ast = { version = "0.40.0", features = ["scopes", "transforms", "utils", "visit", "view", "react"] }
deno_ast = { version = "0.41.1", features = ["scopes", "transforms", "utils", "visit", "view", "react"] }
log = "0.4.20"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.78.0"
channel = "1.80.0"
components = ["clippy", "rustfmt"]
2 changes: 2 additions & 0 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub trait Handler {
fn function(&mut self, _n: &ast_view::Function, _ctx: &mut Context) {}
fn getter_prop(&mut self, _n: &ast_view::GetterProp, _ctx: &mut Context) {}
fn ident(&mut self, _n: &ast_view::Ident, _ctx: &mut Context) {}
fn ident_name(&mut self, _n: &ast_view::IdentName, _ctx: &mut Context) {}
fn if_stmt(&mut self, _n: &ast_view::IfStmt, _ctx: &mut Context) {}
fn import(&mut self, _n: &ast_view::Import, _ctx: &mut Context) {}
fn import_decl(&mut self, _n: &ast_view::ImportDecl, _ctx: &mut Context) {}
Expand Down Expand Up @@ -588,6 +589,7 @@ pub trait Traverse: Handler {
Function(n) => self.function(n, ctx),
GetterProp(n) => self.getter_prop(n, ctx),
Ident(n) => self.ident(n, ctx),
IdentName(n) => self.ident_name(n, ctx),
IfStmt(n) => self.if_stmt(n, ctx),
Import(n) => self.import(n, ctx),
ImportDecl(n) => self.import_decl(n, ctx),
Expand Down
6 changes: 2 additions & 4 deletions src/rules/no_compare_neg_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ const CODE: &str = "no-compare-neg-zero";

#[derive(Display)]
enum NoCompareNegZeroMessage {
#[display(fmt = NoCompareNegZeroMessage::Unexpected)]
#[display(fmt = "Do not compare against -0")]
Unexpected,
}

#[derive(Display)]
enum NoCompareNegZeroHint {
#[display(
fmt = NoCompareNegZeroHint::ObjectIs
)]
#[display(fmt = "Use Object.is(x, -0) instead")]
ObjectIs,
}

Expand Down
38 changes: 33 additions & 5 deletions src/rules/no_constant_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{Context, LintRule};
use crate::Program;
use crate::ProgramRef;
use deno_ast::swc::ast::{BinaryOp, CondExpr, Expr, IfStmt, Lit, UnaryOp};
use deno_ast::swc::visit::{noop_visit_type, VisitAll, VisitAllWith};
use deno_ast::swc::visit::{noop_visit_type, Visit, VisitWith};
use deno_ast::SourceRange;
use deno_ast::SourceRangedForSpanned;
use derive_more::Display;
Expand Down Expand Up @@ -46,8 +46,8 @@ impl LintRule for NoConstantCondition {
let program = program_ref(program);
let mut visitor = NoConstantConditionVisitor::new(context);
match program {
ProgramRef::Module(m) => m.visit_all_with(&mut visitor),
ProgramRef::Script(s) => s.visit_all_with(&mut visitor),
ProgramRef::Module(m) => m.visit_with(&mut visitor),
ProgramRef::Script(s) => s.visit_with(&mut visitor),
}
}

Expand Down Expand Up @@ -151,7 +151,33 @@ impl<'c, 'view: 'c> NoConstantConditionVisitor<'c, 'view> {
Some(node),
in_boolean_position,
),
_ => false,
Expr::This(_)
| Expr::Update(_)
| Expr::Member(_)
| Expr::SuperProp(_)
| Expr::Cond(_)
| Expr::Call(_)
| Expr::New(_)
| Expr::Ident(_)
| Expr::TaggedTpl(_)
| Expr::Class(_)
| Expr::Yield(_)
| Expr::MetaProp(_)
| Expr::Await(_)
| Expr::JSXMember(_)
| Expr::JSXNamespacedName(_)
| Expr::JSXEmpty(_)
| Expr::JSXElement(_)
| Expr::JSXFragment(_)
| Expr::TsTypeAssertion(_)
| Expr::TsConstAssertion(_)
| Expr::TsNonNull(_)
| Expr::TsAs(_)
| Expr::TsInstantiation(_)
| Expr::TsSatisfies(_)
| Expr::PrivateName(_)
| Expr::OptChain(_)
| Expr::Invalid(_) => false,
}
}

Expand Down Expand Up @@ -183,15 +209,17 @@ fn check_short_circuit(expr: &Expr, operator: BinaryOp) -> bool {
}
}

impl<'c, 'view> VisitAll for NoConstantConditionVisitor<'c, 'view> {
impl<'c, 'view> Visit for NoConstantConditionVisitor<'c, 'view> {
noop_visit_type!();

fn visit_cond_expr(&mut self, cond_expr: &CondExpr) {
self.report(&cond_expr.test);
cond_expr.visit_children_with(self);
}

fn visit_if_stmt(&mut self, if_stmt: &IfStmt) {
self.report(&if_stmt.test);
if_stmt.visit_children_with(self);
}

/* TODO(bartlomieju): temporarly disabled because
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no_deprecated_deno_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn extract_symbol<'a>(
use deno_ast::view::{Expr, Lit, MemberProp, Tpl};
match member_prop {
MemberProp::Ident(ident) => Some(ident.sym()),
MemberProp::PrivateName(ident) => Some(ident.id.sym()),
MemberProp::PrivateName(ident) => Some(ident.name()),
MemberProp::Computed(prop) => match &prop.expr {
Expr::Lit(Lit::Str(s)) => Some(s.value()),
Expr::Ident(ident) => Some(ident.sym()),
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no_dupe_class_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{Context, LintRule};
use crate::Program;
use crate::ProgramRef;
use deno_ast::swc::ast::{
BigInt, Bool, Class, ClassMethod, ComputedPropName, Expr, Ident, Lit,
BigInt, Bool, Class, ClassMethod, ComputedPropName, Expr, IdentName, Lit,
MethodKind, Null, Number, PropName, Str, Tpl,
};
use deno_ast::swc::visit::{noop_visit_type, Visit, VisitWith};
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<'a, 'b, 'view> Visit for ClassVisitor<'a, 'b, 'view> {

fn normalize_prop_name(name: &PropName) -> Option<String> {
let normalized = match *name {
PropName::Ident(Ident { ref sym, .. }) => sym.to_string(),
PropName::Ident(IdentName { ref sym, .. }) => sym.to_string(),
PropName::Str(Str { ref value, .. }) => value.to_string(),
PropName::Num(Number { ref value, .. }) => value.to_string(),
PropName::BigInt(BigInt { ref value, .. }) => value.to_string(),
Expand Down
21 changes: 13 additions & 8 deletions src/rules/no_dupe_else_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

use super::program_ref;
use super::{Context, LintRule};
use crate::swc_util::span_and_ctx_drop;
use crate::Program;
use crate::ProgramRef;
use deno_ast::swc::ast::{BinExpr, BinaryOp, Expr, IfStmt, ParenExpr, Stmt};
use deno_ast::swc::utils::drop_span;
use deno_ast::swc::visit::{noop_visit_type, VisitAll, VisitAllWith};
use deno_ast::swc::visit::{noop_visit_type, Visit, VisitWith};
use deno_ast::{SourceRange, SourceRangedForSpanned};
use derive_more::Display;
use std::collections::HashSet;
Expand Down Expand Up @@ -49,8 +49,8 @@ impl LintRule for NoDupeElseIf {
let program = program_ref(program);
let mut visitor = NoDupeElseIfVisitor::new(context);
match program {
ProgramRef::Module(m) => m.visit_all_with(&mut visitor),
ProgramRef::Script(s) => s.visit_all_with(&mut visitor),
ProgramRef::Module(m) => m.visit_with(&mut visitor),
ProgramRef::Script(s) => s.visit_with(&mut visitor),
}
}

Expand All @@ -77,7 +77,7 @@ impl<'c, 'view> NoDupeElseIfVisitor<'c, 'view> {
}
}

impl<'c, 'view> VisitAll for NoDupeElseIfVisitor<'c, 'view> {
impl<'c, 'view> Visit for NoDupeElseIfVisitor<'c, 'view> {
noop_visit_type!();

fn visit_if_stmt(&mut self, if_stmt: &IfStmt) {
Expand All @@ -86,7 +86,7 @@ impl<'c, 'view> VisitAll for NoDupeElseIfVisitor<'c, 'view> {
// This check is necessary to avoid outputting the same errors multiple times.
if !self.checked_ranges.contains(&range) {
self.checked_ranges.insert(range);
let span_dropped_test = drop_span(if_stmt.test.clone());
let span_dropped_test = span_and_ctx_drop(if_stmt.test.clone());
let mut appeared_conditions: Vec<Vec<Vec<Expr>>> = Vec::new();
append_test(&mut appeared_conditions, *span_dropped_test);

Expand All @@ -98,7 +98,7 @@ impl<'c, 'view> VisitAll for NoDupeElseIfVisitor<'c, 'view> {
{
// preserve the range before dropping
let range = test.range();
let span_dropped_test = drop_span(test.clone());
let span_dropped_test = span_and_ctx_drop(test.clone());
let mut current_condition_to_check: Vec<Vec<Vec<Expr>>> =
mk_condition_to_check(*span_dropped_test.clone())
.into_iter()
Expand Down Expand Up @@ -142,6 +142,8 @@ impl<'c, 'view> VisitAll for NoDupeElseIfVisitor<'c, 'view> {
}
}
}

if_stmt.visit_children_with(self);
}
}

Expand Down Expand Up @@ -208,10 +210,13 @@ fn equal_in_if_else(expr1: &Expr, expr2: &Expr) -> bool {
}
(Paren(ParenExpr { ref expr, .. }), _) => equal_in_if_else(expr, expr2),
(_, Paren(ParenExpr { ref expr, .. })) => equal_in_if_else(expr1, expr),
(Fn(a), Fn(b)) => {
eprintln!("fn:\n{:?}\n{:?}", a, b);
a == b
}
(This(_), This(_))
| (Array(_), Array(_))
| (Object(_), Object(_))
| (Fn(_), Fn(_))
| (Unary(_), Unary(_))
| (Update(_), Update(_))
| (Bin(_), Bin(_))
Expand Down
13 changes: 7 additions & 6 deletions src/rules/no_duplicate_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

use super::program_ref;
use super::{Context, LintRule};
use crate::swc_util::span_and_ctx_drop;
use crate::Program;
use crate::ProgramRef;
use deno_ast::swc::ast::{Expr, SwitchStmt};
use deno_ast::swc::utils::drop_span;
use deno_ast::swc::visit::noop_visit_type;
use deno_ast::swc::visit::{VisitAll, VisitAllWith};
use deno_ast::swc::visit::{Visit, VisitWith};
use deno_ast::SourceRangedForSpanned;
use derive_more::Display;
use std::collections::HashSet;
Expand Down Expand Up @@ -46,8 +46,8 @@ impl LintRule for NoDuplicateCase {
let program = program_ref(program);
let mut visitor = NoDuplicateCaseVisitor::new(context);
match program {
ProgramRef::Module(m) => m.visit_all_with(&mut visitor),
ProgramRef::Script(s) => s.visit_all_with(&mut visitor),
ProgramRef::Module(m) => m.visit_with(&mut visitor),
ProgramRef::Script(s) => s.visit_with(&mut visitor),
}
}

Expand All @@ -67,7 +67,7 @@ impl<'c, 'view> NoDuplicateCaseVisitor<'c, 'view> {
}
}

impl<'c, 'view> VisitAll for NoDuplicateCaseVisitor<'c, 'view> {
impl<'c, 'view> Visit for NoDuplicateCaseVisitor<'c, 'view> {
noop_visit_type!();

fn visit_switch_stmt(&mut self, switch_stmt: &SwitchStmt) {
Expand All @@ -76,7 +76,7 @@ impl<'c, 'view> VisitAll for NoDuplicateCaseVisitor<'c, 'view> {

for case in &switch_stmt.cases {
if let Some(test) = &case.test {
let span_dropped_test = drop_span(test.clone());
let span_dropped_test = span_and_ctx_drop(test.clone());
if !seen.insert(span_dropped_test) {
self.context.add_diagnostic_with_hint(
case.range(),
Expand All @@ -87,6 +87,7 @@ impl<'c, 'view> VisitAll for NoDuplicateCaseVisitor<'c, 'view> {
}
}
}
switch_stmt.visit_children_with(self);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules/no_import_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<'c, 'view> NoImportAssignVisitor<'c, 'view> {
}
}

fn is_modifier(&self, obj: &Expr, prop: &Ident) -> bool {
fn is_modifier(&self, obj: &Expr, prop: &IdentName) -> bool {
let obj = if let Expr::Ident(obj) = obj {
obj
} else {
Expand Down
13 changes: 9 additions & 4 deletions src/rules/no_inferrable_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use deno_ast::swc::ast::{
TsKeywordTypeKind, TsType, TsTypeAnn, TsTypeRef, UnaryExpr, VarDecl,
};
use deno_ast::swc::ast::{Callee, PropName};
use deno_ast::swc::visit::{VisitAll, VisitAllWith};
use deno_ast::swc::visit::{Visit, VisitWith};
use deno_ast::SourceRange;
use deno_ast::SourceRangedForSpanned;
use derive_more::Display;
Expand Down Expand Up @@ -45,8 +45,8 @@ impl LintRule for NoInferrableTypes {
let program = program_ref(program);
let mut visitor = NoInferrableTypesVisitor::new(context);
match program {
ProgramRef::Module(m) => m.visit_all_with(&mut visitor),
ProgramRef::Script(s) => s.visit_all_with(&mut visitor),
ProgramRef::Module(m) => m.visit_with(&mut visitor),
ProgramRef::Script(s) => s.visit_with(&mut visitor),
}
}

Expand Down Expand Up @@ -296,7 +296,7 @@ impl<'c, 'view> NoInferrableTypesVisitor<'c, 'view> {
}
}

impl<'c, 'view> VisitAll for NoInferrableTypesVisitor<'c, 'view> {
impl<'c, 'view> Visit for NoInferrableTypesVisitor<'c, 'view> {
fn visit_function(&mut self, function: &Function) {
for param in &function.params {
if let Pat::Assign(assign_pat) = &param.pat {
Expand All @@ -311,6 +311,7 @@ impl<'c, 'view> VisitAll for NoInferrableTypesVisitor<'c, 'view> {
}
}
}
function.visit_children_with(self);
}

fn visit_arrow_expr(&mut self, arr_expr: &ArrowExpr) {
Expand All @@ -327,6 +328,7 @@ impl<'c, 'view> VisitAll for NoInferrableTypesVisitor<'c, 'view> {
}
}
}
arr_expr.visit_children_with(self);
}

fn visit_class_prop(&mut self, prop: &ClassProp) {
Expand All @@ -340,6 +342,7 @@ impl<'c, 'view> VisitAll for NoInferrableTypesVisitor<'c, 'view> {
}
}
}
prop.visit_children_with(self);
}

fn visit_private_prop(&mut self, prop: &PrivateProp) {
Expand All @@ -351,6 +354,7 @@ impl<'c, 'view> VisitAll for NoInferrableTypesVisitor<'c, 'view> {
self.check_ts_type(init, ident_type_ann, prop.range());
}
}
prop.visit_children_with(self);
}

fn visit_var_decl(&mut self, var_decl: &VarDecl) {
Expand All @@ -363,6 +367,7 @@ impl<'c, 'view> VisitAll for NoInferrableTypesVisitor<'c, 'view> {
}
}
}
var_decl.visit_children_with(self);
}
}

Expand Down
Loading

0 comments on commit 8db22f3

Please sign in to comment.