From 06731dfcb5a610520d737f758ca5456d7e08259e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 15 Jan 2018 10:33:18 +0900 Subject: [PATCH 1/2] Cleanup and documentations for swc_common. * Remove CanIUse trait, which will be reimpplemented in future using Visitor. * Remove some other unused stuffs. * Change public path of swc_common::fold::* to swc_common::*. --- .cargo/config | 2 +- common/src/ast_node.rs | 3 -- common/src/compat/mod.rs | 58 ------------------------------ common/src/fold.rs | 30 +++++++++++++--- common/src/gen_iter.rs | 24 ------------- common/src/lib.rs | 11 ++---- common/src/span.rs | 8 ++++- ecmascript/ast/src/class.rs | 3 +- ecmascript/ast/src/expr.rs | 3 +- ecmascript/parser/tests/test262.rs | 2 +- macros/ast_node/src/fold.rs | 11 ++---- macros/ast_node/tests/fold.rs | 2 +- testing/src/lib.rs | 3 +- 13 files changed, 44 insertions(+), 116 deletions(-) delete mode 100644 common/src/compat/mod.rs delete mode 100644 common/src/gen_iter.rs diff --git a/.cargo/config b/.cargo/config index 1c43e751adf..f4479071cbf 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,4 +1,4 @@ [build] rustflags = ["--cfg", "procmacro2_semver_exempt"] -rustdocflags = ["--cfg", "procmacro2_semver_exempt", "--document-private-items"] +rustdocflags = ["--cfg", "procmacro2_semver_exempt"] diff --git a/common/src/ast_node.rs b/common/src/ast_node.rs index c8e3e382355..90ace29bfb2 100644 --- a/common/src/ast_node.rs +++ b/common/src/ast_node.rs @@ -9,6 +9,3 @@ use std::fmt::Debug; /// This trait can be derived with `#[derive(AstNode)]`. /// pub trait AstNode: Debug + PartialEq + Clone {} - -/// Marker. -impl AstNode for ! {} diff --git a/common/src/compat/mod.rs b/common/src/compat/mod.rs deleted file mode 100644 index 42beea9645f..00000000000 --- a/common/src/compat/mod.rs +++ /dev/null @@ -1,58 +0,0 @@ -//! Tracking for used html5 features. - -use fnv::FnvHashSet; -use std::ops::{Add, AddAssign}; - -pub trait CanIUse { - fn report_used_features(&self, features: &mut UsedFeatures); -} - -/// Feature from `caniuse.com`. -/// TODO: Make this enum? -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Feature(&'static str); - -impl Feature { - /// not public api - #[doc(hidden)] - pub const fn new(s: &'static str) -> Self { - Feature(s) - } - - pub fn as_str(self) -> &'static str { - self.0 - } -} - -/// Creates a `caniuse::compat::Feature` with given string. -#[macro_export] -macro_rules! caniuse_feature { - ($s:expr) => {{ - $crate::compat::Feature::new($s) - }} -} - -#[derive(Debug, Clone, Default)] -pub struct UsedFeatures { - feats: FnvHashSet, -} - -impl UsedFeatures { - pub fn finalize(self) -> FnvHashSet { - self.feats - } -} - -impl Add for UsedFeatures { - type Output = Self; - fn add(mut self, rhs: Feature) -> Self { - self += rhs; - self - } -} - -impl AddAssign for UsedFeatures { - fn add_assign(&mut self, rhs: Feature) { - self.feats.insert(rhs); - } -} diff --git a/common/src/fold.rs b/common/src/fold.rs index 2cbe55873bb..d0afe152421 100644 --- a/common/src/fold.rs +++ b/common/src/fold.rs @@ -1,20 +1,42 @@ use either::Either; use string_cache::{Atom, StaticAtomSet}; -/// This trait requires `specialization` feature. +/// Folder based on a type system. +/// +/// This trait requires `#![feature(specialization)]`. pub trait Folder { - fn fold(&mut self, t: T) -> T; + /// By default, this folds fields of `node` + /// and reconstruct `node` with folded fields + fn fold(&mut self, node: T) -> T; } + +/// Trait implemented for types which know how to fold itself. +/// +/// +///#Derive +/// /// This trait can be derived with `#[derive(AstNode)]`. -pub trait FoldWith { +/// +/// Note that derive ignores all fields with primitive type +/// because it would encourage mistakes. Use new type instead. +/// +/// `#[fold(ignore)]` can be used to ignore a field. +pub trait FoldWith: Sized { + /// This is used by default implementation of `Folder::fold`. fn fold_children(self, f: &mut F) -> Self; + + /// Call `f.fold(self)`. + /// + /// This bypasses a type inference bug which is caused by specialization. + fn fold_with(self, f: &mut F) -> Self { + f.fold(self) + } } impl Folder for F where T: FoldWith, { - /// Default implementation which folds childrens with `self`. default fn fold(&mut self, t: T) -> T { t.fold_children(self) } diff --git a/common/src/gen_iter.rs b/common/src/gen_iter.rs deleted file mode 100644 index aea5f13e4aa..00000000000 --- a/common/src/gen_iter.rs +++ /dev/null @@ -1,24 +0,0 @@ -use std::ops::{Generator, GeneratorState}; - -struct GenIter>(G); -impl Iterator for GenIter -where - G: Generator, -{ - type Item = G::Yield; - #[inline] - fn next(&mut self) -> Option { - match self.0.resume() { - GeneratorState::Yielded(item) => Some(item), - GeneratorState::Complete(()) => None, - } - } -} - -/// Creates a new iterator from `gen`. -pub const fn gen_iter(gen: G) -> impl Iterator -where - G: Generator, -{ - GenIter(gen) -} diff --git a/common/src/lib.rs b/common/src/lib.rs index 632ca1f94eb..114b2104024 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,21 +1,14 @@ #![feature(box_syntax)] -#![feature(conservative_impl_trait)] -#![feature(const_fn)] #![feature(try_trait)] -#![feature(optin_builtin_traits)] #![feature(never_type)] #![feature(specialization)] -#![feature(generator_trait)] extern crate either; extern crate fnv; extern crate string_cache; pub use self::ast_node::AstNode; -pub use self::gen_iter::gen_iter; +pub use self::fold::{FoldWith, Folder}; pub use self::span::{BytePos, Span, Spanned}; - -pub mod compat; -pub mod fold; mod ast_node; -mod gen_iter; +mod fold; mod span; diff --git a/common/src/span.rs b/common/src/span.rs index 829c5e67788..79831609333 100644 --- a/common/src/span.rs +++ b/common/src/span.rs @@ -1,6 +1,9 @@ use fold::FoldWith; use std::fmt::{self, Debug, Display, Formatter}; +/// A new-type struct for position of specific byte. +/// +/// See [Span](./struct.Span.html). #[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct BytePos(pub u32); impl Display for BytePos { @@ -14,6 +17,7 @@ impl Debug for BytePos { } } +/// Byte range of a token or node. #[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct Span { /// Inclusive @@ -32,6 +36,7 @@ impl Debug for Span { } impl Span { + /// Dummy span. This is same as `Span::defult()`. pub const DUMMY: Span = Span { start: BytePos(0), end: BytePos(0), @@ -46,6 +51,7 @@ impl Default for Span { } pub trait Spanned: Sized { + /// Creates `Self` from `node` and `span. fn from_unspanned(node: T, span: Span) -> Self; } @@ -59,7 +65,7 @@ where } impl FoldWith for Span { - /// no-op + /// No op as span does not have any child. fn fold_children(self, _: &mut F) -> Span { self } diff --git a/ecmascript/ast/src/class.rs b/ecmascript/ast/src/class.rs index f22170735fb..277ea702292 100644 --- a/ecmascript/ast/src/class.rs +++ b/ecmascript/ast/src/class.rs @@ -1,6 +1,5 @@ use super::{Expr, Function, PropName}; -use swc_common::Span; -use swc_common::fold::FoldWith; +use swc_common::{FoldWith, Span}; use swc_macros::ast_node; #[ast_node] diff --git a/ecmascript/ast/src/expr.rs b/ecmascript/ast/src/expr.rs index cabef19b145..4ad473d23bf 100644 --- a/ecmascript/ast/src/expr.rs +++ b/ecmascript/ast/src/expr.rs @@ -1,6 +1,5 @@ use super::{BlockStmt, Class, Function, Ident, Lit, Pat, Prop}; -use swc_common::{Span, Spanned}; -use swc_common::fold::FoldWith; +use swc_common::{FoldWith, Span, Spanned}; use swc_macros::ast_node; #[ast_node] diff --git a/ecmascript/parser/tests/test262.rs b/ecmascript/parser/tests/test262.rs index f7f62e05fcf..74ec7bb8592 100644 --- a/ecmascript/parser/tests/test262.rs +++ b/ecmascript/parser/tests/test262.rs @@ -16,8 +16,8 @@ use std::fs::read_dir; use std::io::{self, Read}; use std::panic::{catch_unwind, resume_unwind}; use std::path::Path; +use swc_common::{FoldWith, Folder}; use swc_common::Span; -use swc_common::fold::{FoldWith, Folder}; use swc_ecma_parser::ast::*; use swc_ecma_parser::lexer::Lexer; use swc_ecma_parser::parser::{PResult, Parser}; diff --git a/macros/ast_node/src/fold.rs b/macros/ast_node/src/fold.rs index 71459705764..e8ff9a27075 100644 --- a/macros/ast_node/src/fold.rs +++ b/macros/ast_node/src/fold.rs @@ -17,7 +17,7 @@ pub fn derive_fold(input: &DeriveInput) -> ItemImpl { Quote::new_call_site() .quote_with(smart_quote!( Vars { Type: &ty }, - (Type: ::swc_common::fold::FoldWith<__Folder>) + (Type: ::swc_common::FoldWith<__Folder>) )) .parse() }); @@ -61,12 +61,7 @@ pub fn derive_fold(input: &DeriveInput) -> ItemImpl { FieldType: &binding.field().ty, binded_field: binding.name(), }, - { - ::swc_common::fold::Folder::::fold( - __folder, - binded_field, - ) - } + { ::swc_common::Folder::::fold(__folder, binded_field,) } )), }; @@ -146,7 +141,7 @@ pub fn derive_fold(input: &DeriveInput) -> ItemImpl { body, }, { - impl<__Folder> ::swc_common::fold::FoldWith<__Folder> for Type { + impl<__Folder> ::swc_common::FoldWith<__Folder> for Type { fn fold_children(self, __folder: &mut __Folder) -> Self { body } diff --git a/macros/ast_node/tests/fold.rs b/macros/ast_node/tests/fold.rs index 6f010fa0044..cc94cb11d43 100644 --- a/macros/ast_node/tests/fold.rs +++ b/macros/ast_node/tests/fold.rs @@ -2,7 +2,7 @@ extern crate swc_common; extern crate swc_macros; -use swc_common::fold::{FoldWith, Folder}; +use swc_common::{FoldWith, Folder}; use swc_macros::ast_node; pub trait AssertFolder: Folder {} diff --git a/testing/src/lib.rs b/testing/src/lib.rs index 06d1af45a7d..abaf4978547 100644 --- a/testing/src/lib.rs +++ b/testing/src/lib.rs @@ -10,8 +10,7 @@ extern crate swc_common; use slog::{Drain, Logger}; use std::io::{self, Write}; -use swc_common::Span; -use swc_common::fold::{FoldWith, Folder}; +use swc_common::{FoldWith, Folder, Span}; pub fn logger() -> Logger { fn no_timestamp(_: &mut Write) -> io::Result<()> { From 1f5db00585df68b887e2d078195b0bc8695b4026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 15 Jan 2018 11:00:02 +0900 Subject: [PATCH 2/2] Don't build pull requests. --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e1a97cfe469..910b61a0757 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,5 +45,5 @@ branches: - staging # This is where pull requests from "bors try" are built. - trying - # Enable building pull requests. - - master \ No newline at end of file + # Don't build pull requests. + # - master \ No newline at end of file