Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(common): Cleanup & rustfmt #3495

Merged
merged 13 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .kodiak.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ do_not_merge = true

[update]
autoupdate_label = "S-approved"
require_automerge_label = false

# I want to use auto-merge feature of github
[approve]
Expand Down
3 changes: 3 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
format_strings = true
group_imports = "StdExternalCrate"
hex_literal_case = "Lower"
imports_granularity = "Crate"
reorder_impl_items = true
use_field_init_shorthand = true
wrap_comments = true
3 changes: 2 additions & 1 deletion crates/enum_kind/src/expand.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::{input::*, util::is_bool};
use pmutil::{smart_quote, Quote, SpanExt};
use swc_macros_common::prelude::*;
use syn::*;

use crate::{input::*, util::is_bool};

pub fn expand(
Input {
attrs,
Expand Down
4 changes: 3 additions & 1 deletion crates/enum_kind/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::{input::*, util::is_bool};
use std::{fmt::Display, ops::AddAssign, result::Result as StdResult};

use swc_macros_common::prelude::*;
use syn::{
parse::{Parse, ParseStream},
*,
};

use crate::{input::*, util::is_bool};

impl From<DeriveInput> for Input {
fn from(
DeriveInput {
Expand Down
6 changes: 4 additions & 2 deletions crates/jsdoc/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::ast::Text;
use nom::{Compare, CompareResult, InputIter, InputLength, InputTake, Slice, UnspecializedInput};
use std::{
ops::{Deref, Range, RangeFrom, RangeTo},
str::{CharIndices, Chars},
};

use nom::{Compare, CompareResult, InputIter, InputLength, InputTake, Slice, UnspecializedInput};
use swc_common::{comments::Comment, BytePos, Span};

use crate::ast::Text;

#[derive(Debug, Clone, Copy)]
pub struct Input<'i> {
start: BytePos,
Expand Down
8 changes: 5 additions & 3 deletions crates/jsdoc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
pub use self::input::Input;
use crate::ast::*;
use nom::{
bytes::complete::{tag, take_while},
error::ErrorKind,
IResult, InputIter, Slice,
};
use swc_common::{Span, Spanned, SyntaxContext};

pub use self::input::Input;
use crate::ast::*;

pub mod ast;
mod input;

Expand Down Expand Up @@ -662,9 +663,10 @@ fn skip(i: Input) -> Input {

#[cfg(test)]
mod tests {
use super::*;
use swc_common::BytePos;

use super::*;

fn input(s: &str) -> Input {
Input::new(BytePos(0), BytePos(s.as_bytes().len() as _), s)
}
Expand Down
3 changes: 2 additions & 1 deletion crates/jsdoc/tests/fixture.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![feature(bench_black_box)]

use dashmap::DashMap;
use std::path::PathBuf;

use dashmap::DashMap;
use swc_common::{
comments::{Comment, CommentKind, Comments},
BytePos, DUMMY_SP,
Expand Down
20 changes: 11 additions & 9 deletions crates/node/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use crate::{
get_compiler,
util::{get_deserialized, MapErr},
use std::{
panic::{catch_unwind, AssertUnwindSafe},
sync::Arc,
};

use anyhow::{bail, Error};
use napi::{
bindgen_prelude::{AbortSignal, AsyncTask, Buffer},
Env, Status, Task,
};
use serde::Deserialize;
use std::{
panic::{catch_unwind, AssertUnwindSafe},
sync::Arc,
};
use swc::{
config::SourceMapsConfig,
resolver::{environment_resolver, paths_resolver},
Expand All @@ -26,6 +23,11 @@ use swc_ecma_ast::{
};
use swc_ecma_loader::{TargetEnv, NODE_BUILTINS};

use crate::{
get_compiler,
util::{get_deserialized, MapErr},
};

struct ConfigItem {
loader: Box<dyn Load>,
resolver: Box<dyn Resolve>,
Expand All @@ -48,8 +50,8 @@ pub(crate) struct BundleTask {
#[cfg(feature = "swc_v1")]
#[napi]
impl Task for BundleTask {
type Output = AHashMap<String, TransformOutput>;
type JsValue = AHashMap<String, TransformOutput>;
type Output = AHashMap<String, TransformOutput>;

fn compute(&mut self) -> napi::Result<Self::Output> {
let builtins = if let TargetEnv::Node = self.config.static_items.config.target {
Expand Down Expand Up @@ -166,8 +168,8 @@ impl Task for BundleTask {

#[cfg(feature = "swc_v2")]
impl Task for BundleTask {
type Output = AHashMap<String, TransformOutput>;
type JsValue = AHashMap<String, TransformOutput>;
type Output = AHashMap<String, TransformOutput>;

fn compute(&mut self) -> napi::Result<Self::Output> {
todo!()
Expand Down
3 changes: 2 additions & 1 deletion crates/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ extern crate napi_derive;
/// Explicit extern crate to use allocator.
extern crate swc_node_base;

use backtrace::Backtrace;
use std::{env, panic::set_hook, sync::Arc};

use backtrace::Backtrace;
use swc::Compiler;
use swc_common::{self, sync::Lazy, FilePathMapping, SourceMap};
use tracing_subscriber::EnvFilter;
Expand Down
15 changes: 8 additions & 7 deletions crates/node/src/minify.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::{
get_compiler,
util::{deserialize_json, get_deserialized, try_with, MapErr},
};
use std::sync::Arc;

use napi::{
bindgen_prelude::{AbortSignal, AsyncTask, Buffer},
Task,
};
use serde::Deserialize;
use std::sync::Arc;
use swc::{config::JsMinifyOptions, TransformOutput};
use swc_common::{collections::AHashMap, sync::Lrc, FileName, SourceFile, SourceMap};

use crate::{
get_compiler,
util::{deserialize_json, get_deserialized, try_with, MapErr},
};

struct MinifyTask {
c: Arc<swc::Compiler>,
code: String,
Expand Down Expand Up @@ -47,9 +49,8 @@ impl MinifyTarget {

#[napi]
impl Task for MinifyTask {
type Output = TransformOutput;

type JsValue = TransformOutput;
type Output = TransformOutput;

fn compute(&mut self) -> napi::Result<Self::Output> {
let input: MinifyTarget = deserialize_json(&self.code)?;
Expand Down
20 changes: 11 additions & 9 deletions crates/node/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
use crate::{
get_compiler,
util::{deserialize_json, get_deserialized, try_with, MapErr},
use std::{
path::{Path, PathBuf},
sync::Arc,
};

use anyhow::Context as _;
use napi::{
bindgen_prelude::{AbortSignal, AsyncTask, Buffer},
Env, Task,
};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use swc::{config::ParseOptions, Compiler};
use swc_common::FileName;

use crate::{
get_compiler,
util::{deserialize_json, get_deserialized, try_with, MapErr},
};

// ----- Parsing -----

pub struct ParseTask {
Expand All @@ -31,8 +33,8 @@ pub struct ParseFileTask {

#[napi]
impl Task for ParseTask {
type Output = String;
type JsValue = String;
type Output = String;

fn compute(&mut self) -> napi::Result<Self::Output> {
let options: ParseOptions = deserialize_json(&self.options)?;
Expand Down Expand Up @@ -65,8 +67,8 @@ impl Task for ParseTask {

#[napi]
impl Task for ParseFileTask {
type Output = String;
type JsValue = String;
type Output = String;

fn compute(&mut self) -> napi::Result<Self::Output> {
let program = try_with(self.c.cm.clone(), false, |handler| {
Expand Down
14 changes: 8 additions & 6 deletions crates/node/src/print.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use crate::{
get_compiler,
util::{deserialize_json, get_deserialized, MapErr},
};
use std::sync::Arc;

use napi::{
bindgen_prelude::{AbortSignal, AsyncTask, Buffer},
Env, Task,
};
use std::sync::Arc;
use swc::{
config::{Options, SourceMapsConfig},
Compiler, TransformOutput,
};
use swc_ecma_ast::{EsVersion, Program};

use crate::{
get_compiler,
util::{deserialize_json, get_deserialized, MapErr},
};

// ----- Printing -----

pub struct PrintTask {
Expand All @@ -23,8 +25,8 @@ pub struct PrintTask {

#[napi]
impl Task for PrintTask {
type Output = TransformOutput;
type JsValue = TransformOutput;
type Output = TransformOutput;

fn compute(&mut self) -> napi::Result<Self::Output> {
let program: Program = deserialize_json(&self.program_json)?;
Expand Down
18 changes: 10 additions & 8 deletions crates/node/src/transform.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use crate::{
get_compiler,
util::{deserialize_json, get_deserialized, try_with, MapErr},
use std::{
path::{Path, PathBuf},
sync::Arc,
};

use anyhow::Context as _;
use napi::{
bindgen_prelude::{AbortSignal, AsyncTask, Buffer},
Env, JsBuffer, JsBufferValue, Ref, Task,
};
use path_clean::clean;
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use swc::{config::Options, Compiler, TransformOutput};
use swc_common::FileName;
use swc_ecma_ast::Program;

use crate::{
get_compiler,
util::{deserialize_json, get_deserialized, try_with, MapErr},
};

/// Input to transform
#[derive(Debug)]
pub enum Input {
Expand All @@ -35,8 +37,8 @@ pub struct TransformTask {

#[napi]
impl Task for TransformTask {
type Output = TransformOutput;
type JsValue = TransformOutput;
type Output = TransformOutput;

fn compute(&mut self) -> napi::Result<Self::Output> {
let mut options: Options = serde_json::from_slice(self.options.as_ref())?;
Expand Down
7 changes: 4 additions & 3 deletions crates/node/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::{anyhow, Context, Error};
use napi::Status;
use serde::de::DeserializeOwned;
use std::{
any::type_name,
panic::{catch_unwind, AssertUnwindSafe},
};

use anyhow::{anyhow, Context, Error};
use napi::Status;
use serde::de::DeserializeOwned;
use swc::try_with_handler;
use swc_common::{errors::Handler, sync::Lrc, SourceMap};

Expand Down
1 change: 1 addition & 0 deletions crates/string_enum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ fn make_from_str(i: &DeriveInput) -> ItemImpl {
{
impl ::std::str::FromStr for Type {
type Err = ();

fn from_str(s: &str) -> Result<Self, ()> {
body
}
Expand Down
1 change: 1 addition & 0 deletions crates/string_enum/tests/simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::{Debug, Display};

use string_enum::*;

pub trait Assert: Debug + Display {}
Expand Down
1 change: 1 addition & 0 deletions crates/swc/benches/bugs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate swc_node_base;
extern crate test;

use std::{fs::read_to_string, io::stderr, path::Path};

use swc::config::{IsModule, Options};
use swc_common::{errors::Handler, sync::Lrc, FilePathMapping, SourceMap};
use test::Bencher;
Expand Down
1 change: 1 addition & 0 deletions crates/swc/benches/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
io::{self, stderr},
sync::Arc,
};

use swc::config::{Config, IsModule, JscConfig, Options, SourceMapsConfig};
use swc_common::{errors::Handler, FileName, FilePathMapping, Mark, SourceFile, SourceMap};
use swc_ecma_ast::{EsVersion, Program};
Expand Down
1 change: 1 addition & 0 deletions crates/swc/examples/usage.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{path::Path, sync::Arc};

use swc::{self, config::Options};
use swc_common::{
errors::{ColorConfig, Handler},
Expand Down
12 changes: 7 additions & 5 deletions crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
config::{util::BoolOrObject, CompiledPaths, GlobalPassOption, JsMinifyOptions, ModuleConfig},
SwcComments,
};
use std::{cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc, sync::Arc};

use compat::{es2015::regenerator, es2020::export_namespace_from};
use either::Either;
use std::{cell::RefCell, collections::HashMap, path::PathBuf, rc::Rc, sync::Arc};
use swc_atoms::JsWord;
use swc_common::{
chain, comments::Comments, errors::Handler, sync::Lrc, util::take::Take, FileName, Mark,
Expand All @@ -20,6 +17,11 @@ use swc_ecma_transforms::{
};
use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut};

use crate::{
config::{util::BoolOrObject, CompiledPaths, GlobalPassOption, JsMinifyOptions, ModuleConfig},
SwcComments,
};

/// Builder is used to create a high performance `Compiler`.
pub struct PassBuilder<'a, 'b, P: swc_ecma_visit::Fold> {
cm: &'a Arc<SourceMap>,
Expand Down
Loading