14: Cleanup and documentations for swc_common. r=kdy1 a=kdy1

 * Remove CanIUse trait, which will be reimplemented as a Visitor in future.

 * Remove some other unused stuffs.

 * Change public path of swc_common::fold::* to swc_common::*.
This commit is contained in:
bors[bot] 2018-01-15 02:00:54 +00:00
commit 7f3e889773
14 changed files with 46 additions and 118 deletions

View File

@ -1,4 +1,4 @@
[build]
rustflags = ["--cfg", "procmacro2_semver_exempt"]
rustdocflags = ["--cfg", "procmacro2_semver_exempt", "--document-private-items"]
rustdocflags = ["--cfg", "procmacro2_semver_exempt"]

View File

@ -45,5 +45,5 @@ branches:
- staging
# This is where pull requests from "bors try" are built.
- trying
# Enable building pull requests.
- master
# Don't build pull requests.
# - master

View File

@ -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 ! {}

View File

@ -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<Feature>,
}
impl UsedFeatures {
pub fn finalize(self) -> FnvHashSet<Feature> {
self.feats
}
}
impl Add<Feature> for UsedFeatures {
type Output = Self;
fn add(mut self, rhs: Feature) -> Self {
self += rhs;
self
}
}
impl AddAssign<Feature> for UsedFeatures {
fn add_assign(&mut self, rhs: Feature) {
self.feats.insert(rhs);
}
}

View File

@ -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<T> {
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<F> {
///
/// 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<F>: Sized {
/// This is used by default implementation of `Folder<Self>::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<T, F> Folder<T> for F
where
T: FoldWith<F>,
{
/// Default implementation which folds childrens with `self`.
default fn fold(&mut self, t: T) -> T {
t.fold_children(self)
}

View File

@ -1,24 +0,0 @@
use std::ops::{Generator, GeneratorState};
struct GenIter<G: Generator<Return = ()>>(G);
impl<G> Iterator for GenIter<G>
where
G: Generator<Return = ()>,
{
type Item = G::Yield;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match self.0.resume() {
GeneratorState::Yielded(item) => Some(item),
GeneratorState::Complete(()) => None,
}
}
}
/// Creates a new iterator from `gen`.
pub const fn gen_iter<G>(gen: G) -> impl Iterator<Item = G::Yield>
where
G: Generator<Return = ()>,
{
GenIter(gen)
}

View File

@ -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;

View File

@ -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<T>: Sized {
/// Creates `Self` from `node` and `span.
fn from_unspanned(node: T, span: Span) -> Self;
}
@ -59,7 +65,7 @@ where
}
impl<F> FoldWith<F> for Span {
/// no-op
/// No op as span does not have any child.
fn fold_children(self, _: &mut F) -> Span {
self
}

View File

@ -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]

View File

@ -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]

View File

@ -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};

View File

@ -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::<FieldType>::fold(
__folder,
binded_field,
)
}
{ ::swc_common::Folder::<FieldType>::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
}

View File

@ -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<T>: Folder<T> {}

View File

@ -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<()> {