mirror of
https://github.com/swc-project/swc.git
synced 2024-12-29 08:34:06 +03:00
chore: Publish babel crates (#2835)
This commit is contained in:
parent
6b96a3d8ed
commit
d0cabc37ed
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -2426,6 +2426,7 @@ dependencies = [
|
|||||||
"swc_ecma_visit",
|
"swc_ecma_visit",
|
||||||
"swc_ecmascript",
|
"swc_ecmascript",
|
||||||
"swc_node_base",
|
"swc_node_base",
|
||||||
|
"swc_node_comments",
|
||||||
"swc_visit",
|
"swc_visit",
|
||||||
"testing",
|
"testing",
|
||||||
"tracing",
|
"tracing",
|
||||||
@ -2483,6 +2484,7 @@ dependencies = [
|
|||||||
"swc_ecma_utils",
|
"swc_ecma_utils",
|
||||||
"swc_ecma_visit",
|
"swc_ecma_visit",
|
||||||
"swc_node_base",
|
"swc_node_base",
|
||||||
|
"swc_node_comments",
|
||||||
"walkdir",
|
"walkdir",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -3216,6 +3218,15 @@ dependencies = [
|
|||||||
"walkdir",
|
"walkdir",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "swc_node_comments"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"ahash",
|
||||||
|
"dashmap",
|
||||||
|
"swc_common",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "swc_plugin"
|
name = "swc_plugin"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
|
@ -70,6 +70,7 @@ swc_ecma_transforms_optimization = {version = "0.65.0", path = "../swc_ecma_tran
|
|||||||
swc_ecma_utils = {version = "0.52.0", path = "../swc_ecma_utils"}
|
swc_ecma_utils = {version = "0.52.0", path = "../swc_ecma_utils"}
|
||||||
swc_ecma_visit = {version = "0.44.0", path = "../swc_ecma_visit"}
|
swc_ecma_visit = {version = "0.44.0", path = "../swc_ecma_visit"}
|
||||||
swc_ecmascript = {version = "0.87.0", path = "../swc_ecmascript"}
|
swc_ecmascript = {version = "0.87.0", path = "../swc_ecmascript"}
|
||||||
|
swc_node_comments = {version = "0.1", path = "../swc_node_comments"}
|
||||||
swc_visit = {version = "0.2.3", path = "../swc_visit"}
|
swc_visit = {version = "0.2.3", path = "../swc_visit"}
|
||||||
tracing = "0.1.28"
|
tracing = "0.1.28"
|
||||||
|
|
||||||
|
@ -121,7 +121,6 @@ use common::{
|
|||||||
errors::{EmitterWriter, HANDLER},
|
errors::{EmitterWriter, HANDLER},
|
||||||
};
|
};
|
||||||
use config::{util::BoolOrObject, IsModule, JsMinifyCommentOption, JsMinifyOptions};
|
use config::{util::BoolOrObject, IsModule, JsMinifyCommentOption, JsMinifyOptions};
|
||||||
use dashmap::DashMap;
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json::error::Category;
|
use serde_json::error::Category;
|
||||||
@ -135,7 +134,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
use swc_common::{
|
use swc_common::{
|
||||||
chain,
|
chain,
|
||||||
comments::{Comment, CommentKind, Comments},
|
comments::{Comment, Comments},
|
||||||
errors::Handler,
|
errors::Handler,
|
||||||
input::StringInput,
|
input::StringInput,
|
||||||
source_map::SourceMapGenConfig,
|
source_map::SourceMapGenConfig,
|
||||||
@ -158,6 +157,7 @@ use swc_ecma_transforms::{
|
|||||||
resolver_with_mark,
|
resolver_with_mark,
|
||||||
};
|
};
|
||||||
use swc_ecma_visit::{noop_visit_type, FoldWith, Visit, VisitMutWith, VisitWith};
|
use swc_ecma_visit::{noop_visit_type, FoldWith, Visit, VisitMutWith, VisitWith};
|
||||||
|
pub use swc_node_comments::SwcComments;
|
||||||
|
|
||||||
mod builder;
|
mod builder;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
@ -1110,122 +1110,6 @@ fn load_swcrc(path: &Path) -> Result<Rc, Error> {
|
|||||||
.map_err(convert_json_err)
|
.map_err(convert_json_err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommentMap = Arc<DashMap<BytePos, Vec<Comment>, ahash::RandomState>>;
|
|
||||||
|
|
||||||
/// Multi-threaded implementation of [Comments]
|
|
||||||
#[derive(Clone, Default)]
|
|
||||||
pub struct SwcComments {
|
|
||||||
pub leading: CommentMap,
|
|
||||||
pub trailing: CommentMap,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Comments for SwcComments {
|
|
||||||
fn add_leading(&self, pos: BytePos, cmt: Comment) {
|
|
||||||
self.leading.entry(pos).or_default().push(cmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_leading_comments(&self, pos: BytePos, comments: Vec<Comment>) {
|
|
||||||
self.leading.entry(pos).or_default().extend(comments);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_leading(&self, pos: BytePos) -> bool {
|
|
||||||
if let Some(v) = self.leading.get(&pos) {
|
|
||||||
!v.is_empty()
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_leading(&self, from: BytePos, to: BytePos) {
|
|
||||||
let cmt = self.leading.remove(&from);
|
|
||||||
|
|
||||||
if let Some(cmt) = cmt {
|
|
||||||
self.leading.entry(to).or_default().extend(cmt.1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn take_leading(&self, pos: BytePos) -> Option<Vec<Comment>> {
|
|
||||||
self.leading.remove(&pos).map(|v| v.1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_leading(&self, pos: BytePos) -> Option<Vec<Comment>> {
|
|
||||||
self.leading.get(&pos).map(|v| v.to_owned())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_trailing(&self, pos: BytePos, cmt: Comment) {
|
|
||||||
self.trailing.entry(pos).or_default().push(cmt)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_trailing_comments(&self, pos: BytePos, comments: Vec<Comment>) {
|
|
||||||
self.trailing.entry(pos).or_default().extend(comments)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_trailing(&self, pos: BytePos) -> bool {
|
|
||||||
if let Some(v) = self.trailing.get(&pos) {
|
|
||||||
!v.is_empty()
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_trailing(&self, from: BytePos, to: BytePos) {
|
|
||||||
let cmt = self.trailing.remove(&from);
|
|
||||||
|
|
||||||
if let Some(cmt) = cmt {
|
|
||||||
self.trailing.entry(to).or_default().extend(cmt.1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn take_trailing(&self, pos: BytePos) -> Option<Vec<Comment>> {
|
|
||||||
self.trailing.remove(&pos).map(|v| v.1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_trailing(&self, pos: BytePos) -> Option<Vec<Comment>> {
|
|
||||||
self.trailing.get(&pos).map(|v| v.to_owned())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_pure_comment(&self, pos: BytePos) {
|
|
||||||
let mut leading = self.leading.entry(pos).or_default();
|
|
||||||
let pure_comment = Comment {
|
|
||||||
kind: CommentKind::Block,
|
|
||||||
span: DUMMY_SP,
|
|
||||||
text: "#__PURE__".into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
if !leading.iter().any(|c| c.text == pure_comment.text) {
|
|
||||||
leading.push(pure_comment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_leading<F, Ret>(&self, pos: BytePos, f: F) -> Ret
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: FnOnce(&[Comment]) -> Ret,
|
|
||||||
{
|
|
||||||
let ret = if let Some(cmts) = self.leading.get(&pos) {
|
|
||||||
f(&cmts)
|
|
||||||
} else {
|
|
||||||
f(&[])
|
|
||||||
};
|
|
||||||
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
|
|
||||||
fn with_trailing<F, Ret>(&self, pos: BytePos, f: F) -> Ret
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
F: FnOnce(&[Comment]) -> Ret,
|
|
||||||
{
|
|
||||||
let ret = if let Some(cmts) = &self.trailing.get(&pos) {
|
|
||||||
f(&cmts)
|
|
||||||
} else {
|
|
||||||
f(&[])
|
|
||||||
};
|
|
||||||
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct IdentCollector {
|
pub struct IdentCollector {
|
||||||
names: AHashMap<BytePos, JsWord>,
|
names: AHashMap<BytePos, JsWord>,
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
[package]
|
[package]
|
||||||
authors = ["Daniel Woznicki <daniel.woznicki@gmail.com>"]
|
authors = ["강동윤 <kdy1997.dev@gmail.com>", "Daniel Woznicki <daniel.woznicki@gmail.com>"]
|
||||||
description = "Babel AST node definitions"
|
description = "Babel AST node definitions"
|
||||||
|
documentation = "https://rustdoc.swc.rs/swc_babel_ast/"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "Apache-2.0/MIT"
|
||||||
name = "swc_babel_ast"
|
name = "swc_babel_ast"
|
||||||
publish = false
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde = {version = "1", features = ["derive"]}
|
serde = {version = "1", features = ["derive"]}
|
||||||
serde_json = "1.0.62"
|
serde_json = "1.0.62"
|
||||||
swc_atoms = {path = "../swc_atoms"}
|
swc_atoms = {version = "0.2", path = "../swc_atoms"}
|
||||||
swc_common = {path = "../swc_common"}
|
swc_common = {version = "0.14", path = "../swc_common"}
|
||||||
swc_node_base = {path = "../swc_node_base"}
|
swc_node_base = {version = "0.5", path = "../swc_node_base"}
|
||||||
|
@ -5,7 +5,6 @@ documentation = "https://rustdoc.swc.rs/swc_babel_compat/"
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "Apache-2.0/MIT"
|
license = "Apache-2.0/MIT"
|
||||||
name = "swc_babel_compat"
|
name = "swc_babel_compat"
|
||||||
publish = false
|
|
||||||
repository = "https://github.com/swc-project/swc.git"
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
@ -18,18 +17,19 @@ copyless = "0.1.5"
|
|||||||
rayon = "1.5.0"
|
rayon = "1.5.0"
|
||||||
serde = {version = "1", features = ["derive"]}
|
serde = {version = "1", features = ["derive"]}
|
||||||
serde_json = "1.0.62"
|
serde_json = "1.0.62"
|
||||||
swc = {path = "../swc"}
|
swc_atoms = {version = "0.2", path = "../swc_atoms"}
|
||||||
swc_atoms = {path = "../swc_atoms"}
|
swc_babel_ast = {version = "0.1", path = "../swc_babel_ast"}
|
||||||
swc_babel_ast = {path = "../swc_babel_ast"}
|
swc_babel_visit = {version = "0.1", path = "../swc_babel_visit"}
|
||||||
swc_babel_visit = {path = "../swc_babel_visit"}
|
swc_common = {version = "0.14", path = "../swc_common", features = ["sourcemap", "tty-emitter"]}
|
||||||
swc_common = {path = "../swc_common", features = ["sourcemap", "tty-emitter"]}
|
swc_ecma_ast = {version = "0.58", path = "../swc_ecma_ast"}
|
||||||
swc_ecma_ast = {path = "../swc_ecma_ast"}
|
swc_ecma_parser = {version = "0.78", path = "../swc_ecma_parser"}
|
||||||
swc_ecma_parser = {path = "../swc_ecma_parser"}
|
swc_ecma_utils = {version = "0.52.2", path = "../swc_ecma_utils"}
|
||||||
swc_ecma_utils = {path = "../swc_ecma_utils"}
|
swc_ecma_visit = {version = "0.44", path = "../swc_ecma_visit"}
|
||||||
swc_ecma_visit = {path = "../swc_ecma_visit"}
|
swc_node_comments = {version = "0.1", path = "../swc_node_comments/"}
|
||||||
swc_node_base = {path = "../swc_node_base"}
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_assertions = "0.7.1"
|
pretty_assertions = "0.7.1"
|
||||||
swc_ecma_transforms = {path = "../swc_ecma_transforms/"}
|
swc = {version = "0.86.0", path = "../swc"}
|
||||||
|
swc_ecma_transforms = {version = "0.95", path = "../swc_ecma_transforms/"}
|
||||||
|
swc_node_base = {version = "0.5", path = "../swc_node_base"}
|
||||||
walkdir = "2"
|
walkdir = "2"
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use swc::SwcComments;
|
|
||||||
use swc_babel_ast::{BaseComment, BaseNode, Comment, LineCol, Loc};
|
use swc_babel_ast::{BaseComment, BaseNode, Comment, LineCol, Loc};
|
||||||
use swc_common::{
|
use swc_common::{
|
||||||
comments::{CommentKind, Comments},
|
comments::{CommentKind, Comments},
|
||||||
@ -9,6 +8,7 @@ use swc_common::{
|
|||||||
BytePos, SourceFile, SourceMap, Span,
|
BytePos, SourceFile, SourceMap, Span,
|
||||||
};
|
};
|
||||||
use swc_ecma_ast::Class;
|
use swc_ecma_ast::Class;
|
||||||
|
use swc_node_comments::SwcComments;
|
||||||
|
|
||||||
mod class;
|
mod class;
|
||||||
mod decl;
|
mod decl;
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::babelify::{Babelify, Context};
|
use crate::babelify::{Babelify, Context};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use swc::SwcComments;
|
|
||||||
use swc_babel_ast::{
|
use swc_babel_ast::{
|
||||||
BaseNode, File, InterpreterDirective, LineCol, Loc, ModuleDeclaration, Program as BabelProgram,
|
BaseNode, File, InterpreterDirective, LineCol, Loc, ModuleDeclaration, Program as BabelProgram,
|
||||||
SrcType, Statement,
|
SrcType, Statement,
|
||||||
@ -8,6 +7,7 @@ use swc_babel_ast::{
|
|||||||
use swc_common::{comments::Comment, Span};
|
use swc_common::{comments::Comment, Span};
|
||||||
use swc_ecma_ast::{Invalid, Module, ModuleItem, Program, Script};
|
use swc_ecma_ast::{Invalid, Module, ModuleItem, Program, Script};
|
||||||
use swc_ecma_visit::{Node, Visit, VisitWith};
|
use swc_ecma_visit::{Node, Visit, VisitWith};
|
||||||
|
use swc_node_comments::SwcComments;
|
||||||
|
|
||||||
impl Babelify for Program {
|
impl Babelify for Program {
|
||||||
type Output = File;
|
type Output = File;
|
||||||
@ -85,10 +85,10 @@ impl Babelify for Script {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Babel adds a trailing newline to the end of files when parsing, while swc
|
/// Babel adds a trailing newline to the end of files when parsing, while swc
|
||||||
// truncates trailing whitespace. In order to get the converted base node to
|
/// truncates trailing whitespace. In order to get the converted base node to
|
||||||
// locations to match babel, we immitate the trailing newline for Script and
|
/// locations to match babel, we immitate the trailing newline for Script and
|
||||||
// Module nodes.
|
/// Module nodes.
|
||||||
fn base_with_trailing_newline(span: Span, ctx: &Context) -> BaseNode {
|
fn base_with_trailing_newline(span: Span, ctx: &Context) -> BaseNode {
|
||||||
let mut base = ctx.base(span);
|
let mut base = ctx.base(span);
|
||||||
base.end = base.end.map(|num| num + 1);
|
base.end = base.end.map(|num| num + 1);
|
||||||
@ -102,10 +102,10 @@ fn base_with_trailing_newline(span: Span, ctx: &Context) -> BaseNode {
|
|||||||
base
|
base
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should return true if the first line in parsed file is a comment.
|
/// Should return true if the first line in parsed file is a comment.
|
||||||
// Required because babel and swc have slightly different handlings for first
|
/// Required because babel and swc have slightly different handlings for first
|
||||||
// line comments. Swc ignores them and starts the program on the next line down,
|
/// line comments. Swc ignores them and starts the program on the next line
|
||||||
// while babel includes them in the file start/end.
|
/// down, while babel includes them in the file start/end.
|
||||||
fn has_comment_first_line(sp: Span, ctx: &Context) -> bool {
|
fn has_comment_first_line(sp: Span, ctx: &Context) -> bool {
|
||||||
if let Some(comments) = ctx.comments.leading.get(&sp.hi) {
|
if let Some(comments) = ctx.comments.leading.get(&sp.hi) {
|
||||||
!comments
|
!comments
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use swc::SwcComments;
|
|
||||||
use swc_babel_ast::{BaseNode, LineCol, Loc};
|
use swc_babel_ast::{BaseNode, LineCol, Loc};
|
||||||
use swc_common::{BytePos, FileName, SourceFile, SourceMap, Span, SyntaxContext, DUMMY_SP};
|
use swc_common::{BytePos, FileName, SourceFile, SourceMap, Span, SyntaxContext, DUMMY_SP};
|
||||||
|
use swc_node_comments::SwcComments;
|
||||||
|
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
authors = ["Daniel Woznicki <daniel.woznicki@gmail.com>"]
|
authors = ["강동윤 <kdy1997.dev@gmail.com>", "Daniel Woznicki <daniel.woznicki@gmail.com>"]
|
||||||
description = "Visitor implementation for babel nodes"
|
description = "Visitor implementation for babel nodes"
|
||||||
|
documentation = "https://rustdoc.swc.rs/swc_babel_visit/"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "Apache-2.0/MIT"
|
||||||
name = "swc_babel_visit"
|
name = "swc_babel_visit"
|
||||||
publish = false
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
@ -12,6 +13,6 @@ version = "0.1.0"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
serde = {version = "1", features = ["derive"]}
|
serde = {version = "1", features = ["derive"]}
|
||||||
serde_json = "1.0.62"
|
serde_json = "1.0.62"
|
||||||
swc_atoms = {path = "../swc_atoms"}
|
swc_atoms = {version = "0.2", path = "../swc_atoms"}
|
||||||
swc_babel_ast = {path = "../swc_babel_ast"}
|
swc_babel_ast = {version = "0.1", path = "../swc_babel_ast"}
|
||||||
swc_visit = {path = "../swc_visit"}
|
swc_visit = {version = "0.2", path = "../swc_visit"}
|
||||||
|
16
crates/swc_node_comments/Cargo.toml
Normal file
16
crates/swc_node_comments/Cargo.toml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
[package]
|
||||||
|
authors = ["강동윤 <kdy1997.dev@gmail.com>", "Daniel Woznicki <daniel.woznicki@gmail.com>"]
|
||||||
|
description = "Implementation of Comments of swc_common"
|
||||||
|
documentation = "https://rustdoc.swc.rs/swc_node_comments/"
|
||||||
|
edition = "2018"
|
||||||
|
license = "Apache-2.0"
|
||||||
|
name = "swc_node_comments"
|
||||||
|
repository = "https://github.com/swc-project/swc.git"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
ahash = "0.7.6"
|
||||||
|
dashmap = "4"
|
||||||
|
swc_common = {version = "0.14.6", path = "../swc_common"}
|
124
crates/swc_node_comments/src/lib.rs
Normal file
124
crates/swc_node_comments/src/lib.rs
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
#![cfg_attr(test, deny(warnings))]
|
||||||
|
|
||||||
|
use dashmap::DashMap;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use swc_common::{
|
||||||
|
comments::{Comment, CommentKind, Comments},
|
||||||
|
BytePos, DUMMY_SP,
|
||||||
|
};
|
||||||
|
|
||||||
|
type CommentMap = Arc<DashMap<BytePos, Vec<Comment>, ahash::RandomState>>;
|
||||||
|
|
||||||
|
/// Multi-threaded implementation of [Comments]
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
pub struct SwcComments {
|
||||||
|
pub leading: CommentMap,
|
||||||
|
pub trailing: CommentMap,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Comments for SwcComments {
|
||||||
|
fn add_leading(&self, pos: BytePos, cmt: Comment) {
|
||||||
|
self.leading.entry(pos).or_default().push(cmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_leading_comments(&self, pos: BytePos, comments: Vec<Comment>) {
|
||||||
|
self.leading.entry(pos).or_default().extend(comments);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_leading(&self, pos: BytePos) -> bool {
|
||||||
|
if let Some(v) = self.leading.get(&pos) {
|
||||||
|
!v.is_empty()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_leading(&self, from: BytePos, to: BytePos) {
|
||||||
|
let cmt = self.leading.remove(&from);
|
||||||
|
|
||||||
|
if let Some(cmt) = cmt {
|
||||||
|
self.leading.entry(to).or_default().extend(cmt.1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn take_leading(&self, pos: BytePos) -> Option<Vec<Comment>> {
|
||||||
|
self.leading.remove(&pos).map(|v| v.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_leading(&self, pos: BytePos) -> Option<Vec<Comment>> {
|
||||||
|
self.leading.get(&pos).map(|v| v.to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_trailing(&self, pos: BytePos, cmt: Comment) {
|
||||||
|
self.trailing.entry(pos).or_default().push(cmt)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_trailing_comments(&self, pos: BytePos, comments: Vec<Comment>) {
|
||||||
|
self.trailing.entry(pos).or_default().extend(comments)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_trailing(&self, pos: BytePos) -> bool {
|
||||||
|
if let Some(v) = self.trailing.get(&pos) {
|
||||||
|
!v.is_empty()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_trailing(&self, from: BytePos, to: BytePos) {
|
||||||
|
let cmt = self.trailing.remove(&from);
|
||||||
|
|
||||||
|
if let Some(cmt) = cmt {
|
||||||
|
self.trailing.entry(to).or_default().extend(cmt.1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn take_trailing(&self, pos: BytePos) -> Option<Vec<Comment>> {
|
||||||
|
self.trailing.remove(&pos).map(|v| v.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_trailing(&self, pos: BytePos) -> Option<Vec<Comment>> {
|
||||||
|
self.trailing.get(&pos).map(|v| v.to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_pure_comment(&self, pos: BytePos) {
|
||||||
|
let mut leading = self.leading.entry(pos).or_default();
|
||||||
|
let pure_comment = Comment {
|
||||||
|
kind: CommentKind::Block,
|
||||||
|
span: DUMMY_SP,
|
||||||
|
text: "#__PURE__".into(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if !leading.iter().any(|c| c.text == pure_comment.text) {
|
||||||
|
leading.push(pure_comment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with_leading<F, Ret>(&self, pos: BytePos, f: F) -> Ret
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: FnOnce(&[Comment]) -> Ret,
|
||||||
|
{
|
||||||
|
let ret = if let Some(cmts) = self.leading.get(&pos) {
|
||||||
|
f(&cmts)
|
||||||
|
} else {
|
||||||
|
f(&[])
|
||||||
|
};
|
||||||
|
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with_trailing<F, Ret>(&self, pos: BytePos, f: F) -> Ret
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: FnOnce(&[Comment]) -> Ret,
|
||||||
|
{
|
||||||
|
let ret = if let Some(cmts) = &self.trailing.get(&pos) {
|
||||||
|
f(&cmts)
|
||||||
|
} else {
|
||||||
|
f(&[])
|
||||||
|
};
|
||||||
|
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user