Skip to content

Commit a55514c

Browse files
authored
v0.25.0
- Use "swc_ecmascript" crate instead of "swc_ecma_ast" and "swc_ecma_parser" - Remove SWC reexports
2 parents c085d21 + 2271ba2 commit a55514c

File tree

10 files changed

+209
-384
lines changed

10 files changed

+209
-384
lines changed

Cargo.lock

Lines changed: 77 additions & 238 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "dprint-plugin-typescript"
33
description = "TypeScript code formatting plugin for Dprint."
44
keywords = ["formatting", "formatter", "typescript"]
5-
version = "0.24.0"
5+
version = "0.25.0"
66
authors = ["David Sherret <[email protected]>"]
77
edition = "2018"
88
license = "MIT"
@@ -22,9 +22,8 @@ panic = "abort"
2222

2323
[dependencies]
2424
dprint-core = "0.24.1"
25-
swc_common = "=0.7.0"
26-
swc_ecma_ast = "=0.26.0"
27-
swc_ecma_parser = "=0.31.0"
25+
swc_common = "=0.8.0"
26+
swc_ecmascript = { version = "=0.1.0", features = ["parser"] }
2827
serde = { version = "1.0.88", features = ["derive"] }
2928
serde_json = "1.0"
3029

src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,3 @@ pub use formatter::Formatter;
1212

1313
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
1414
pub use wasm_plugin::*;
15-
16-
// Re-export swc for use in Deno
17-
#[doc(hidden)]
18-
pub use swc_common;
19-
#[doc(hidden)]
20-
pub use swc_ecma_ast;
21-
#[doc(hidden)]
22-
pub use swc_ecma_parser;

src/parsing/comments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use super::*;
33
use swc_common::{BytePos, comments::{Comment}};
4-
use swc_ecma_parser::{token::{Token, TokenAndSpan}};
4+
use swc_ecmascript::parser::{token::{Token, TokenAndSpan}};
55

66
pub struct CommentCollection<'a> {
77
leading: &'a HashMap<BytePos, Vec<Comment>>,

src/parsing/parser.rs

Lines changed: 70 additions & 70 deletions
Large diffs are not rendered by default.

src/parsing/parser_types.rs

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::str;
22
use std::collections::{HashSet, HashMap};
33
use dprint_core::{Info, ConditionReference};
4-
use swc_common::{SpanData, BytePos, comments::{Comment, CommentKind}, SourceFile, Spanned, Span};
5-
use swc_ecma_ast::*;
6-
use swc_ecma_parser::{token::TokenAndSpan};
4+
use swc_common::{BytePos, comments::{Comment, CommentKind}, SourceFile, Spanned, Span};
5+
use swc_ecmascript::ast::*;
6+
use swc_ecmascript::parser::{token::TokenAndSpan};
77
use super::*;
88
use super::super::configuration::*;
99
use super::super::utils::Stack;
@@ -20,7 +20,7 @@ pub struct Context<'a> {
2020
stored_infos: HashMap<(BytePos, BytePos), Info>,
2121
stored_info_ranges: HashMap<(BytePos, BytePos), (Info, Info)>,
2222
pub end_statement_or_member_infos: Stack<Info>,
23-
before_comments_start_info_stack: Stack<(SpanData, Info)>,
23+
before_comments_start_info_stack: Stack<(Span, Info)>,
2424
if_stmt_last_brace_condition_ref: Option<ConditionReference>,
2525
/// Used for ensuring nodes are parsed in order.
2626
#[cfg(debug_assertions)]
@@ -147,37 +147,31 @@ pub trait NodeKinded {
147147
}
148148

149149
pub trait SpanDataContainer {
150-
fn span_data(&self) -> SpanData;
150+
fn span_data(&self) -> Span;
151151
}
152152

153153
impl SpanDataContainer for &dyn SpanDataContainer {
154-
fn span_data(&self) -> SpanData {
154+
fn span_data(&self) -> Span {
155155
(**self).span_data()
156156
}
157157
}
158158

159159
impl SpanDataContainer for TokenAndSpan {
160-
fn span_data(&self) -> SpanData {
160+
fn span_data(&self) -> Span {
161161
self.span
162162
}
163163
}
164164

165165
impl SpanDataContainer for Comment {
166-
fn span_data(&self) -> SpanData {
167-
self.span.data()
168-
}
169-
}
170-
171-
impl SpanDataContainer for SpanData {
172-
fn span_data(&self) -> SpanData {
173-
self.clone()
166+
fn span_data(&self) -> Span {
167+
self.span
174168
}
175169
}
176170

177171
impl SpanDataContainer for BytePos {
178-
fn span_data(&self) -> SpanData {
172+
fn span_data(&self) -> Span {
179173
// todo: change this so this allocation isn't necessary
180-
SpanData {
174+
Span {
181175
lo: *self,
182176
hi: *self,
183177
ctxt: Default::default(),
@@ -186,7 +180,7 @@ impl SpanDataContainer for BytePos {
186180
}
187181

188182
impl<T> SpanDataContainer for std::boxed::Box<T> where T : SpanDataContainer {
189-
fn span_data(&self) -> SpanData {
183+
fn span_data(&self) -> Span {
190184
(**self).span_data()
191185
}
192186
}
@@ -332,8 +326,8 @@ macro_rules! generate_node {
332326
}
333327
}
334328
impl SpanDataContainer for $node_name {
335-
fn span_data(&self) -> SpanData {
336-
self.span().data()
329+
fn span_data(&self) -> Span {
330+
self.span()
337331
}
338332
}
339333
)*
@@ -353,9 +347,9 @@ macro_rules! generate_node {
353347
)*
354348

355349
impl<'a> SpanDataContainer for Node<'a> {
356-
fn span_data(&self) -> SpanData {
350+
fn span_data(&self) -> Span {
357351
match self {
358-
$(Node::$node_name(node) => node.span().data()),*
352+
$(Node::$node_name(node) => node.span()),*
359353
}
360354
}
361355
}
@@ -588,9 +582,9 @@ macro_rules! generate_traits {
588582
}
589583

590584
impl SpanDataContainer for $enum_name {
591-
fn span_data(&self) -> SpanData {
585+
fn span_data(&self) -> Span {
592586
match self {
593-
$($enum_name::$member_name(node) => node.span().data()),*
587+
$($enum_name::$member_name(node) => node.span()),*
594588
}
595589
}
596590
}
@@ -648,32 +642,31 @@ generate_traits![JSXObject, JSXMemberExpr, Ident];
648642
/* InnerSpanned */
649643

650644
pub trait InnerSpanned {
651-
fn get_inner_span_data(&self, context: &mut Context) -> SpanData;
645+
fn get_inner_span_data(&self, context: &mut Context) -> Span;
652646
}
653647

654648
impl InnerSpanned for BlockStmt {
655-
fn get_inner_span_data(&self, _: &mut Context) -> SpanData {
649+
fn get_inner_span_data(&self, _: &mut Context) -> Span {
656650
get_inner_span_for_object_like(&self.span)
657651
}
658652
}
659653

660654
impl InnerSpanned for ObjectLit {
661-
fn get_inner_span_data(&self, _: &mut Context) -> SpanData {
655+
fn get_inner_span_data(&self, _: &mut Context) -> Span {
662656
get_inner_span_for_object_like(&self.span)
663657
}
664658
}
665659

666660
impl InnerSpanned for ObjectPat {
667-
fn get_inner_span_data(&self, _: &mut Context) -> SpanData {
661+
fn get_inner_span_data(&self, _: &mut Context) -> Span {
668662
get_inner_span_for_object_like(&self.span)
669663
}
670664
}
671665

672-
fn get_inner_span_for_object_like(span: &Span) -> SpanData {
673-
let span_data = span.data();
674-
SpanData {
675-
lo: BytePos(span_data.lo.0 + 1),
676-
hi: BytePos(span_data.hi.0 - 1),
666+
fn get_inner_span_for_object_like(span: &Span) -> Span {
667+
Span {
668+
lo: BytePos(span.lo.0 + 1),
669+
hi: BytePos(span.hi.0 - 1),
677670
ctxt: Default::default()
678671
}
679672
}
@@ -702,11 +695,11 @@ impl<'a> Node<'a> {
702695
/* ParametersSpanned */
703696

704697
pub trait ParametersSpanned {
705-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData>;
698+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span>;
706699
}
707700

708701
impl ParametersSpanned for Function {
709-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
702+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
710703
get_params_or_args_span_data(
711704
self.lo(),
712705
self.params.iter().map(|x| x.span_data()).collect(),
@@ -719,13 +712,13 @@ impl ParametersSpanned for Function {
719712
}
720713

721714
impl ParametersSpanned for ClassMethod {
722-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
715+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
723716
self.function.get_parameters_span_data(context)
724717
}
725718
}
726719

727720
impl ParametersSpanned for Constructor {
728-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
721+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
729722
get_params_or_args_span_data(
730723
self.lo(),
731724
self.params.iter().map(|x| x.span_data()).collect(),
@@ -737,13 +730,13 @@ impl ParametersSpanned for Constructor {
737730
}
738731

739732
impl ParametersSpanned for MethodProp {
740-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
733+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
741734
self.function.get_parameters_span_data(context)
742735
}
743736
}
744737

745738
impl ParametersSpanned for GetterProp {
746-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
739+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
747740
get_params_or_args_span_data(
748741
self.lo(),
749742
vec![],
@@ -756,7 +749,7 @@ impl ParametersSpanned for GetterProp {
756749
}
757750

758751
impl ParametersSpanned for SetterProp {
759-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
752+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
760753
get_params_or_args_span_data(
761754
self.lo(),
762755
vec![self.param.span_data()],
@@ -767,7 +760,7 @@ impl ParametersSpanned for SetterProp {
767760
}
768761

769762
impl ParametersSpanned for ArrowExpr {
770-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
763+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
771764
get_params_or_args_span_data(
772765
self.lo(),
773766
self.params.iter().map(|x| x.span_data()).collect(),
@@ -778,7 +771,7 @@ impl ParametersSpanned for ArrowExpr {
778771
}
779772

780773
impl ParametersSpanned for CallExpr {
781-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
774+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
782775
get_params_or_args_span_data(
783776
self.lo(),
784777
self.args.iter().map(|a| a.span_data()).collect(),
@@ -789,7 +782,7 @@ impl ParametersSpanned for CallExpr {
789782
}
790783

791784
impl ParametersSpanned for NewExpr {
792-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
785+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
793786
get_params_or_args_span_data(
794787
self.lo(),
795788
self.args.as_ref().map(|args| args.iter().map(|a| a.span_data()).collect()).unwrap_or_default(),
@@ -800,7 +793,7 @@ impl ParametersSpanned for NewExpr {
800793
}
801794

802795
impl ParametersSpanned for TsCallSignatureDecl {
803-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
796+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
804797
get_params_or_args_span_data(
805798
self.lo(),
806799
self.params.iter().map(|x| x.span_data()).collect(),
@@ -811,7 +804,7 @@ impl ParametersSpanned for TsCallSignatureDecl {
811804
}
812805

813806
impl ParametersSpanned for TsConstructSignatureDecl {
814-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
807+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
815808
get_params_or_args_span_data(
816809
self.lo(),
817810
self.params.iter().map(|x| x.span_data()).collect(),
@@ -822,7 +815,7 @@ impl ParametersSpanned for TsConstructSignatureDecl {
822815
}
823816

824817
impl ParametersSpanned for TsMethodSignature {
825-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
818+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
826819
get_params_or_args_span_data(
827820
self.lo(),
828821
self.params.iter().map(|x| x.span_data()).collect(),
@@ -833,7 +826,7 @@ impl ParametersSpanned for TsMethodSignature {
833826
}
834827

835828
impl ParametersSpanned for TsConstructorType {
836-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
829+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
837830
get_params_or_args_span_data(
838831
self.lo(),
839832
self.params.iter().map(|x| x.span_data()).collect(),
@@ -844,7 +837,7 @@ impl ParametersSpanned for TsConstructorType {
844837
}
845838

846839
impl ParametersSpanned for TsFnType {
847-
fn get_parameters_span_data(&self, context: &mut Context) -> Option<SpanData> {
840+
fn get_parameters_span_data(&self, context: &mut Context) -> Option<Span> {
848841
get_params_or_args_span_data(
849842
self.lo(),
850843
self.params.iter().map(|x| x.span_data()).collect(),
@@ -854,7 +847,7 @@ impl ParametersSpanned for TsFnType {
854847
}
855848
}
856849

857-
fn get_params_or_args_span_data(start_pos: BytePos, params: Vec<SpanData>, following_pos: BytePos, context: &mut Context) -> Option<SpanData> {
850+
fn get_params_or_args_span_data(start_pos: BytePos, params: Vec<Span>, following_pos: BytePos, context: &mut Context) -> Option<Span> {
858851
let close_token_end = {
859852
let close_paren = if let Some(last_param) = params.last() {
860853
context.token_finder.get_first_close_paren_after(last_param)
@@ -893,7 +886,7 @@ fn get_params_or_args_span_data(start_pos: BytePos, params: Vec<SpanData>, follo
893886
}
894887
}?;
895888

896-
Some(SpanData {
889+
Some(Span {
897890
lo: close_token_start,
898891
hi: close_token_end,
899892
ctxt: Default::default(),

src/parsing/swc/extensions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use swc_ecma_ast::BinaryOp;
1+
use swc_ecmascript::ast::BinaryOp;
22

33
pub trait BinaryOpExtensions {
44
fn is_add_sub(&self) -> bool;

src/parsing/swc/flatten_binary_expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use swc_ecma_ast::*;
2-
use swc_ecma_parser::{token::{TokenAndSpan}};
1+
use swc_ecmascript::ast::*;
2+
use swc_ecmascript::parser::{token::{TokenAndSpan}};
33

44
use super::*;
55
use super::super::*;

src/parsing/tokens.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::str;
22
use super::*;
3-
use swc_common::{BytePos, SpanData};
4-
use swc_ecma_parser::{token::{Token, TokenAndSpan}};
3+
use swc_common::{BytePos, Span};
4+
use swc_ecmascript::parser::{token::{Token, TokenAndSpan}};
55
use dprint_core::tokens::{TokenFinder as CoreTokenFinder, TokenCollection};
66

77
pub struct TokenFinder<'a> {
@@ -191,7 +191,7 @@ impl<'a> TokenFinder<'a> {
191191
}
192192
}
193193

194-
fn get_text<'a>(file_bytes: &'a [u8], span_data: &SpanData) -> &'a str {
194+
fn get_text<'a>(file_bytes: &'a [u8], span_data: &Span) -> &'a str {
195195
let bytes = &file_bytes[(span_data.lo.0 as usize)..(span_data.hi.0 as usize)];
196196
str::from_utf8(&bytes).unwrap()
197197
}

0 commit comments

Comments
 (0)