Skip to content

Commit 342c363

Browse files
authored
chore: upgrade to Rust 2024 edition (#317)
1 parent 070cce6 commit 342c363

File tree

15 files changed

+206
-194
lines changed

15 files changed

+206
-194
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "deno_ast"
33
version = "0.50.0"
44
authors = ["the Deno authors"]
55
documentation = "https://docs.rs/deno_ast"
6-
edition = "2021"
6+
edition = "2024"
77
homepage = "https://deno.land/"
88
license = "MIT"
99
repository = "https://github.com/denoland/deno_ast"

src/cjs_parse.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
use std::collections::HashMap;
44
use std::collections::HashSet;
55

6+
use crate::ParsedSource;
7+
use crate::ProgramRef;
68
use crate::swc::ast::*;
79
use crate::swc::atoms::Atom;
8-
use crate::swc::ecma_visit::noop_visit_type;
910
use crate::swc::ecma_visit::Visit;
1011
use crate::swc::ecma_visit::VisitWith;
11-
use crate::ParsedSource;
12-
use crate::ProgramRef;
12+
use crate::swc::ecma_visit::noop_visit_type;
1313

1414
pub type CjsAnalysis = crate::ModuleExportsAndReExports;
1515

@@ -104,18 +104,17 @@ impl CjsVisitor {
104104
}
105105
// Object.defineProperty(exports, key, { ... });
106106
Expr::Ident(object_define_prop_ident) => {
107-
if let Some(obj_lit) = call_expr.args[2].expr.as_object() {
108-
if let Some(get_prop) = get_object_define_get_prop(obj_lit) {
109-
// get: function () {
110-
// return _external002[key];
111-
// }
112-
if let Some(Expr::Member(member)) = get_prop_return_expr(get_prop) {
113-
if let Some(require_value) = self
114-
.get_member_require_value(member, &object_define_prop_ident.sym)
115-
{
116-
self.add_reexport(&require_value);
117-
}
118-
}
107+
if let Some(obj_lit) = call_expr.args[2].expr.as_object()
108+
&& let Some(get_prop) = get_object_define_get_prop(obj_lit)
109+
{
110+
// get: function () {
111+
// return _external002[key];
112+
// }
113+
if let Some(Expr::Member(member)) = get_prop_return_expr(get_prop)
114+
&& let Some(require_value) = self
115+
.get_member_require_value(member, &object_define_prop_ident.sym)
116+
{
117+
self.add_reexport(&require_value);
119118
}
120119
}
121120
}
@@ -167,14 +166,13 @@ impl Visit for CjsVisitor {
167166

168167
fn visit_var_decl(&mut self, stmt: &VarDecl) {
169168
for decl in &stmt.decls {
170-
if let Some(id) = decl.name.as_ident() {
171-
if let Some(init) = &decl.init {
172-
if let Some(require_value) = get_expr_require_value(init) {
173-
self
174-
.var_assignments
175-
.insert(id.id.sym.to_string(), require_value.to_string());
176-
}
177-
}
169+
if let Some(id) = decl.name.as_ident()
170+
&& let Some(init) = &decl.init
171+
&& let Some(require_value) = get_expr_require_value(init)
172+
{
173+
self
174+
.var_assignments
175+
.insert(id.id.sym.to_string(), require_value.to_string());
178176
}
179177
}
180178
stmt.visit_children_with(self);
@@ -214,10 +212,10 @@ impl Visit for CjsVisitor {
214212
fn is_export_callee(callee: &Callee) -> bool {
215213
if let Some(member) = get_callee_member_expr(callee) {
216214
// ex. tslib.__exportStar
217-
if member.obj.as_ident().is_some() {
218-
if let Some(right_side) = get_member_prop_ident_text(&member.prop) {
219-
return matches!(right_side, "__exportStar" | "__export");
220-
}
215+
if member.obj.as_ident().is_some()
216+
&& let Some(right_side) = get_member_prop_ident_text(&member.prop)
217+
{
218+
return matches!(right_side, "__exportStar" | "__export");
221219
}
222220
} else if let Some(ident) = get_callee_ident(callee) {
223221
return matches!(&*ident.sym, "__exportStar" | "__export");
@@ -273,12 +271,11 @@ impl Visit for CjsVisitor {
273271
// * `exports[key] = _something[key];
274272
let computed = left_member.prop.as_computed();
275273
let computed_ident = computed.and_then(|c| c.expr.as_ident());
276-
if let Some(computed_ident) = computed_ident {
277-
if let Some(require_value) =
274+
if let Some(computed_ident) = computed_ident
275+
&& let Some(require_value) =
278276
self.get_member_require_value(right_member, &computed_ident.sym)
279-
{
280-
self.add_reexport(&require_value);
281-
}
277+
{
278+
self.add_reexport(&require_value);
282279
}
283280
}
284281
} else if let Some(right_expr) = assign_expr.right.as_assign() {
@@ -307,10 +304,10 @@ fn is_module_exports_expr(expr: &Expr) -> bool {
307304
}
308305

309306
fn is_module_exports_member(member_expr: &MemberExpr) -> bool {
310-
if let Some(obj_ident) = member_expr.obj.as_ident() {
311-
if obj_ident.sym == *"module" {
312-
return get_member_prop_text(&member_expr.prop) == Some("exports");
313-
}
307+
if let Some(obj_ident) = member_expr.obj.as_ident()
308+
&& obj_ident.sym == *"module"
309+
{
310+
return get_member_prop_text(&member_expr.prop) == Some("exports");
314311
}
315312
false
316313
}
@@ -559,10 +556,10 @@ mod test {
559556
use pretty_assertions::assert_eq;
560557
use std::cell::RefCell;
561558

562-
use crate::parse_script;
563559
use crate::MediaType;
564560
use crate::ModuleSpecifier;
565561
use crate::ParseParams;
562+
use crate::parse_script;
566563

567564
use super::*;
568565

src/comments.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// implement swc's `Comments` trait
55
#![allow(clippy::disallowed_types)]
66

7+
use crate::SourcePos;
8+
use crate::swc::common::BytePos as SwcBytePos;
79
use crate::swc::common::comments::Comment;
810
use crate::swc::common::comments::Comments as SwcComments;
911
use crate::swc::common::comments::SingleThreadedComments;
1012
use crate::swc::common::comments::SingleThreadedCommentsMapInner;
11-
use crate::swc::common::BytePos as SwcBytePos;
12-
use crate::SourcePos;
1313

1414
use std::cell::RefCell;
1515
use std::rc::Rc;
@@ -196,13 +196,13 @@ fn panic_readonly() -> ! {
196196

197197
#[cfg(test)]
198198
mod test {
199-
use crate::parse_module;
200-
use crate::swc::common::comments::SingleThreadedComments;
201199
use crate::MediaType;
202200
use crate::ModuleSpecifier;
203201
use crate::MultiThreadedComments;
204202
use crate::ParseParams;
205203
use crate::StartSourcePos;
204+
use crate::parse_module;
205+
use crate::swc::common::comments::SingleThreadedComments;
206206

207207
#[test]
208208
fn general_use() {

src/diagnostics.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,11 @@ fn print_snippet(
491491
write!(io, "{}", RepeatingCharFmt(' ', padding_width))?;
492492
write!(io, "{}", highlight.style.style_underline(underline))?;
493493

494-
if line_number == end_line_number {
495-
if let Some(description) = &highlight.description {
496-
write!(io, " {}", highlight.style.style_underline(description))?;
497-
wrote_description = true;
498-
}
494+
if line_number == end_line_number
495+
&& let Some(description) = &highlight.description
496+
{
497+
write!(io, " {}", highlight.style.style_underline(description))?;
498+
wrote_description = true;
499499
}
500500

501501
writeln!(io)?;

src/emit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
use base64::Engine;
44
use thiserror::Error;
55

6-
use crate::swc::codegen::text_writer::JsWriter;
7-
use crate::swc::codegen::Node;
8-
use crate::swc::common::FileName;
96
use crate::ModuleSpecifier;
107
use crate::ProgramRef;
118
use crate::SourceMap;
9+
use crate::swc::codegen::Node;
10+
use crate::swc::codegen::text_writer::JsWriter;
11+
use crate::swc::common::FileName;
1212

1313
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
1414
pub enum SourceMapOption {

src/exports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
use serde::Deserialize;
44
use serde::Serialize;
55

6+
use crate::ParsedSource;
7+
use crate::ProgramRef;
68
use crate::swc::ast::ExportSpecifier;
79
use crate::swc::ast::ModuleDecl;
810
use crate::swc::ast::ModuleItem;
911
use crate::swc::atoms::Atom;
1012
use crate::swc::utils::find_pat_ids;
11-
use crate::ParsedSource;
12-
use crate::ProgramRef;
1313

1414
#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
1515
pub struct ModuleExportsAndReExports {
@@ -121,9 +121,9 @@ mod test {
121121

122122
use deno_media_type::MediaType;
123123

124-
use crate::parse_module;
125124
use crate::ModuleSpecifier;
126125
use crate::ParseParams;
126+
use crate::parse_module;
127127

128128
use super::ModuleExportsAndReExports;
129129

src/lexing.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
use std::rc::Rc;
44

5+
use crate::ES_VERSION;
6+
use crate::MediaType;
7+
use crate::SourceRangedForSpanned;
8+
use crate::StartSourcePos;
59
use crate::get_syntax;
610
use crate::swc::atoms::Atom;
711
use crate::swc::common::comments::Comment;
812
use crate::swc::common::comments::CommentKind;
913
use crate::swc::common::comments::SingleThreadedComments;
1014
use crate::swc::common::input::StringInput;
11-
use crate::swc::lexer::token::Token;
1215
use crate::swc::lexer::Lexer;
13-
use crate::MediaType;
14-
use crate::SourceRangedForSpanned;
15-
use crate::StartSourcePos;
16-
use crate::ES_VERSION;
16+
use crate::swc::lexer::token::Token;
1717

1818
#[derive(Debug, Clone)]
1919
pub enum TokenOrComment {

src/parsed_source.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ use swc_ecma_ast::ModuleDecl;
1414
use swc_ecma_ast::ModuleItem;
1515
use swc_ecma_ast::Stmt;
1616

17+
use crate::MediaType;
18+
use crate::ModuleSpecifier;
19+
use crate::ParseDiagnostic;
20+
use crate::SourceRangedForSpanned;
1721
use crate::comments::MultiThreadedComments;
1822
use crate::scope_analysis_transform;
1923
use crate::swc::ast::Module;
2024
use crate::swc::ast::Program;
2125
use crate::swc::ast::Script;
22-
use crate::swc::common::comments::Comment;
2326
use crate::swc::common::SyntaxContext;
27+
use crate::swc::common::comments::Comment;
2428
use crate::swc::parser::token::TokenAndSpan;
25-
use crate::MediaType;
26-
use crate::ModuleSpecifier;
27-
use crate::ParseDiagnostic;
28-
use crate::SourceRangedForSpanned;
2929

3030
#[derive(Debug, Clone)]
3131
pub struct Marks {
@@ -544,14 +544,14 @@ impl ParsedSource {
544544
#[cfg(test)]
545545
mod test {
546546
use super::*;
547-
use crate::parse_program;
548547
use crate::ParseParams;
548+
use crate::parse_program;
549549

550550
#[cfg(feature = "view")]
551551
#[test]
552552
fn should_parse_program() {
553-
use crate::view::NodeTrait;
554553
use crate::ModuleSpecifier;
554+
use crate::view::NodeTrait;
555555

556556
let program = parse_program(ParseParams {
557557
specifier: ModuleSpecifier::parse("file:///my_file.js").unwrap(),

src/parsing.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ use dprint_swc_ext::common::SourceTextInfo;
66
use dprint_swc_ext::common::StartSourcePos;
77
use swc_ecma_lexer::common::parser::Parser as _;
88

9+
use crate::Globals;
10+
use crate::MediaType;
11+
use crate::ModuleSpecifier;
12+
use crate::ParseDiagnostic;
13+
use crate::ParseDiagnostics;
14+
use crate::ParsedSource;
915
use crate::comments::MultiThreadedComments;
1016
use crate::swc::ast::EsVersion;
1117
use crate::swc::ast::Module;
1218
use crate::swc::ast::Program;
1319
use crate::swc::ast::Script;
1420
use crate::swc::common::comments::SingleThreadedComments;
1521
use crate::swc::common::input::StringInput;
16-
use crate::swc::parser::error::Error as SwcError;
17-
use crate::swc::parser::token::TokenAndSpan;
1822
use crate::swc::parser::EsSyntax;
1923
use crate::swc::parser::Syntax;
2024
use crate::swc::parser::TsSyntax;
21-
use crate::Globals;
22-
use crate::MediaType;
23-
use crate::ModuleSpecifier;
24-
use crate::ParseDiagnostic;
25-
use crate::ParseDiagnostics;
26-
use crate::ParsedSource;
25+
use crate::swc::parser::error::Error as SwcError;
26+
use crate::swc::parser::token::TokenAndSpan;
2727

2828
/// Ecmascript version used for lexing and parsing.
2929
pub const ES_VERSION: EsVersion = EsVersion::Es2021;
@@ -407,7 +407,9 @@ fn strip_bom_from_arc(s: Arc<str>, should_panic_in_debug: bool) -> Arc<str> {
407407
if let Some(stripped_text) = s.strip_prefix('\u{FEFF}') {
408408
// this is only a perf concern, so don't crash in release
409409
if cfg!(debug_assertions) && should_panic_in_debug {
410-
panic!("BOM should be stripped from text before providing it to deno_ast to avoid a file text allocation");
410+
panic!(
411+
"BOM should be stripped from text before providing it to deno_ast to avoid a file text allocation"
412+
);
411413
}
412414
stripped_text.into()
413415
} else {
@@ -419,8 +421,8 @@ fn strip_bom_from_arc(s: Arc<str>, should_panic_in_debug: bool) -> Arc<str> {
419421
mod test {
420422
use pretty_assertions::assert_eq;
421423

422-
use crate::diagnostics::Diagnostic;
423424
use crate::LineAndColumnDisplay;
425+
use crate::diagnostics::Diagnostic;
424426

425427
use super::*;
426428

@@ -762,8 +764,8 @@ function _bar(...Foo: Foo) {
762764
}
763765

764766
#[test]
765-
fn should_error_without_issue_when_there_exists_multi_byte_char_on_line_with_syntax_error(
766-
) {
767+
fn should_error_without_issue_when_there_exists_multi_byte_char_on_line_with_syntax_error()
768+
{
767769
let diagnostic = parse_ts_module(concat!(
768770
"test;\n",
769771
r#"console.log("x", `duration ${d} not in range - ${min} ≥ ${d} && ${max} ≥ ${d}`),;"#,

src/scopes.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,16 @@ impl Visit for Analyzer<'_> {
204204

205205
// If the class name and the variable name are the same like `let Foo = class Foo {}`,
206206
// this binding should be treated as `BindingKind::Class`.
207-
if let Some(expr) = &v.init {
208-
if let Expr::Class(ClassExpr {
207+
if let Some(expr) = &v.init
208+
&& let Expr::Class(ClassExpr {
209209
ident: Some(class_name),
210210
..
211211
}) = &**expr
212-
{
213-
if let Pat::Ident(var_name) = &v.name {
214-
if var_name.id.sym == class_name.sym {
215-
self.declare(BindingKind::Class, class_name);
216-
return;
217-
}
218-
}
219-
}
212+
&& let Pat::Ident(var_name) = &v.name
213+
&& var_name.id.sym == class_name.sym
214+
{
215+
self.declare(BindingKind::Class, class_name);
216+
return;
220217
}
221218

222219
self.declare_pat(
@@ -353,11 +350,11 @@ impl Visit for Analyzer<'_> {
353350
#[cfg(test)]
354351
mod tests {
355352
use super::{BindingKind, Scope, ScopeKind, Var};
356-
use crate::parse_module;
357-
use crate::swc::ast::Id;
358353
use crate::MediaType;
359354
use crate::ModuleSpecifier;
360355
use crate::ParseParams;
356+
use crate::parse_module;
357+
use crate::swc::ast::Id;
361358

362359
fn test_scope(source_code: &str, test: impl Fn(Scope)) {
363360
let parsed_source = parse_module(ParseParams {

0 commit comments

Comments
 (0)