chore: Publish babel crates (#2835)

This commit is contained in:
Donny/강동윤 2021-11-22 21:41:03 +09:00 committed by GitHub
parent 6b96a3d8ed
commit d0cabc37ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 191 additions and 154 deletions

11
Cargo.lock generated
View File

@ -2426,6 +2426,7 @@ dependencies = [
"swc_ecma_visit",
"swc_ecmascript",
"swc_node_base",
"swc_node_comments",
"swc_visit",
"testing",
"tracing",
@ -2483,6 +2484,7 @@ dependencies = [
"swc_ecma_utils",
"swc_ecma_visit",
"swc_node_base",
"swc_node_comments",
"walkdir",
]
@ -3216,6 +3218,15 @@ dependencies = [
"walkdir",
]
[[package]]
name = "swc_node_comments"
version = "0.1.0"
dependencies = [
"ahash",
"dashmap",
"swc_common",
]
[[package]]
name = "swc_plugin"
version = "0.13.0"

View File

@ -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_visit = {version = "0.44.0", path = "../swc_ecma_visit"}
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"}
tracing = "0.1.28"

View File

@ -121,7 +121,6 @@ use common::{
errors::{EmitterWriter, HANDLER},
};
use config::{util::BoolOrObject, IsModule, JsMinifyCommentOption, JsMinifyOptions};
use dashmap::DashMap;
use once_cell::sync::Lazy;
use serde::Serialize;
use serde_json::error::Category;
@ -135,7 +134,7 @@ use std::{
};
use swc_common::{
chain,
comments::{Comment, CommentKind, Comments},
comments::{Comment, Comments},
errors::Handler,
input::StringInput,
source_map::SourceMapGenConfig,
@ -158,6 +157,7 @@ use swc_ecma_transforms::{
resolver_with_mark,
};
use swc_ecma_visit::{noop_visit_type, FoldWith, Visit, VisitMutWith, VisitWith};
pub use swc_node_comments::SwcComments;
mod builder;
pub mod config;
@ -1110,122 +1110,6 @@ fn load_swcrc(path: &Path) -> Result<Rc, Error> {
.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 {
names: AHashMap<BytePos, JsWord>,
}

View File

@ -1,17 +1,17 @@
[package]
authors = ["Daniel Woznicki <daniel.woznicki@gmail.com>"]
authors = ["강동윤 <kdy1997.dev@gmail.com>", "Daniel Woznicki <daniel.woznicki@gmail.com>"]
description = "Babel AST node definitions"
documentation = "https://rustdoc.swc.rs/swc_babel_ast/"
edition = "2018"
license = "MIT"
license = "Apache-2.0/MIT"
name = "swc_babel_ast"
publish = false
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]
serde = {version = "1", features = ["derive"]}
serde_json = "1.0.62"
swc_atoms = {path = "../swc_atoms"}
swc_common = {path = "../swc_common"}
swc_node_base = {path = "../swc_node_base"}
swc_atoms = {version = "0.2", path = "../swc_atoms"}
swc_common = {version = "0.14", path = "../swc_common"}
swc_node_base = {version = "0.5", path = "../swc_node_base"}

View File

@ -5,7 +5,6 @@ documentation = "https://rustdoc.swc.rs/swc_babel_compat/"
edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_babel_compat"
publish = false
repository = "https://github.com/swc-project/swc.git"
version = "0.1.0"
@ -18,18 +17,19 @@ copyless = "0.1.5"
rayon = "1.5.0"
serde = {version = "1", features = ["derive"]}
serde_json = "1.0.62"
swc = {path = "../swc"}
swc_atoms = {path = "../swc_atoms"}
swc_babel_ast = {path = "../swc_babel_ast"}
swc_babel_visit = {path = "../swc_babel_visit"}
swc_common = {path = "../swc_common", features = ["sourcemap", "tty-emitter"]}
swc_ecma_ast = {path = "../swc_ecma_ast"}
swc_ecma_parser = {path = "../swc_ecma_parser"}
swc_ecma_utils = {path = "../swc_ecma_utils"}
swc_ecma_visit = {path = "../swc_ecma_visit"}
swc_node_base = {path = "../swc_node_base"}
swc_atoms = {version = "0.2", path = "../swc_atoms"}
swc_babel_ast = {version = "0.1", path = "../swc_babel_ast"}
swc_babel_visit = {version = "0.1", path = "../swc_babel_visit"}
swc_common = {version = "0.14", path = "../swc_common", features = ["sourcemap", "tty-emitter"]}
swc_ecma_ast = {version = "0.58", path = "../swc_ecma_ast"}
swc_ecma_parser = {version = "0.78", path = "../swc_ecma_parser"}
swc_ecma_utils = {version = "0.52.2", path = "../swc_ecma_utils"}
swc_ecma_visit = {version = "0.44", path = "../swc_ecma_visit"}
swc_node_comments = {version = "0.1", path = "../swc_node_comments/"}
[dev-dependencies]
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"

View File

@ -1,7 +1,6 @@
use rayon::prelude::*;
use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;
use swc::SwcComments;
use swc_babel_ast::{BaseComment, BaseNode, Comment, LineCol, Loc};
use swc_common::{
comments::{CommentKind, Comments},
@ -9,6 +8,7 @@ use swc_common::{
BytePos, SourceFile, SourceMap, Span,
};
use swc_ecma_ast::Class;
use swc_node_comments::SwcComments;
mod class;
mod decl;

View File

@ -1,6 +1,5 @@
use crate::babelify::{Babelify, Context};
use serde::{Deserialize, Serialize};
use swc::SwcComments;
use swc_babel_ast::{
BaseNode, File, InterpreterDirective, LineCol, Loc, ModuleDeclaration, Program as BabelProgram,
SrcType, Statement,
@ -8,6 +7,7 @@ use swc_babel_ast::{
use swc_common::{comments::Comment, Span};
use swc_ecma_ast::{Invalid, Module, ModuleItem, Program, Script};
use swc_ecma_visit::{Node, Visit, VisitWith};
use swc_node_comments::SwcComments;
impl Babelify for Program {
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
// truncates trailing whitespace. In order to get the converted base node to
// locations to match babel, we immitate the trailing newline for Script and
// Module nodes.
/// 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
/// locations to match babel, we immitate the trailing newline for Script and
/// Module nodes.
fn base_with_trailing_newline(span: Span, ctx: &Context) -> BaseNode {
let mut base = ctx.base(span);
base.end = base.end.map(|num| num + 1);
@ -102,10 +102,10 @@ fn base_with_trailing_newline(span: Span, ctx: &Context) -> BaseNode {
base
}
// Should return true if the first line in parsed file is a comment.
// 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,
// while babel includes them in the file start/end.
/// Should return true if the first line in parsed file is a comment.
/// 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, while babel includes them in the file start/end.
fn has_comment_first_line(sp: Span, ctx: &Context) -> bool {
if let Some(comments) = ctx.comments.leading.get(&sp.hi) {
!comments

View File

@ -1,7 +1,7 @@
use std::sync::Arc;
use swc::SwcComments;
use swc_babel_ast::{BaseNode, LineCol, Loc};
use swc_common::{BytePos, FileName, SourceFile, SourceMap, Span, SyntaxContext, DUMMY_SP};
use swc_node_comments::SwcComments;
pub struct Context {
#[allow(unused)]

View File

@ -1,10 +1,11 @@
[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"
documentation = "https://rustdoc.swc.rs/swc_babel_visit/"
edition = "2018"
license = "MIT"
license = "Apache-2.0/MIT"
name = "swc_babel_visit"
publish = false
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
@ -12,6 +13,6 @@ version = "0.1.0"
[dependencies]
serde = {version = "1", features = ["derive"]}
serde_json = "1.0.62"
swc_atoms = {path = "../swc_atoms"}
swc_babel_ast = {path = "../swc_babel_ast"}
swc_visit = {path = "../swc_visit"}
swc_atoms = {version = "0.2", path = "../swc_atoms"}
swc_babel_ast = {version = "0.1", path = "../swc_babel_ast"}
swc_visit = {version = "0.2", path = "../swc_visit"}

View 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"}

View 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
}
}