- Strip out const assertions (Closes #656)
 - Fix exclude of the file matcher (Closes #658) 
 - Automatic typescript detection (Closes #655)
This commit is contained in:
kdy1 2020-02-12 15:07:45 +00:00
parent 64f6e51b42
commit 82e73b1121
17 changed files with 127 additions and 40 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "swc_ecma_ast" name = "swc_ecma_ast"
version = "0.16.0" version = "0.17.0"
authors = ["강동윤 <kdy1997.dev@gmail.com>"] authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "swc_ecma_codegen" name = "swc_ecma_codegen"
version = "0.15.0" version = "0.16.0"
authors = ["강동윤 <kdy1997.dev@gmail.com>"] authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
@ -13,11 +13,11 @@ bitflags = "1"
hashbrown = "0.6" hashbrown = "0.6"
swc_atoms = { version = "0.2", path ="../../atoms" } swc_atoms = { version = "0.2", path ="../../atoms" }
swc_common = { version = "0.5", path ="../../common" } swc_common = { version = "0.5", path ="../../common" }
swc_ecma_ast = { version = "0.16.0", path ="../ast" } swc_ecma_ast = { version = "0.17.0", path ="../ast" }
swc_ecma_codegen_macros = { version = "0.4", path ="./macros" } swc_ecma_codegen_macros = { version = "0.4", path ="./macros" }
sourcemap = "4.1.1" sourcemap = "4.1.1"
num-bigint = { version = "0.2", features = ["serde"] } num-bigint = { version = "0.2", features = ["serde"] }
[dev-dependencies] [dev-dependencies]
testing = { version = "0.5", path ="../../testing" } testing = { version = "0.5", path ="../../testing" }
swc_ecma_parser = { version = "0.18", path ="../parser" } swc_ecma_parser = { version = "0.19", path ="../parser" }

View File

@ -1,6 +1,6 @@
[package] [package]
name = "swc_ecma_parser" name = "swc_ecma_parser"
version = "0.18.1" version = "0.19.0"
authors = ["강동윤 <kdy1997.dev@gmail.com>"] authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT" license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git" repository = "https://github.com/swc-project/swc.git"
@ -18,7 +18,7 @@ verify = ["fold"]
[dependencies] [dependencies]
swc_atoms = { version = "0.2", path ="../../atoms" } swc_atoms = { version = "0.2", path ="../../atoms" }
swc_common = { version = "0.5.0", path ="../../common" } swc_common = { version = "0.5.0", path ="../../common" }
swc_ecma_ast = { version = "0.16.0", path ="../ast" } swc_ecma_ast = { version = "0.17.0", path ="../ast" }
swc_ecma_parser_macros = { version = "0.4", path ="./macros" } swc_ecma_parser_macros = { version = "0.4", path ="./macros" }
enum_kind = { version = "0.2", path ="../../macros/enum_kind" } enum_kind = { version = "0.2", path ="../../macros/enum_kind" }
unicode-xid = "0.2" unicode-xid = "0.2"

View File

@ -330,7 +330,7 @@ pub enum JscTarget {
impl Default for JscTarget { impl Default for JscTarget {
fn default() -> Self { fn default() -> Self {
JscTarget::Es3 JscTarget::Es5
} }
} }

View File

@ -11,9 +11,9 @@ edition = "2018"
[dependencies] [dependencies]
swc_atoms = { version = "0.2.0", path ="../../atoms" } swc_atoms = { version = "0.2.0", path ="../../atoms" }
swc_common = { version = "0.5.0", path ="../../common" } swc_common = { version = "0.5.0", path ="../../common" }
swc_ecma_ast = { version = "0.16.0", path ="../ast" } swc_ecma_ast = { version = "0.17.0", path ="../ast" }
swc_ecma_utils = { version = "0.3.0", path ="../utils" } swc_ecma_utils = { version = "0.3.0", path ="../utils" }
swc_ecma_parser = { version = "0.18", path ="../parser", features = ["verify"] } swc_ecma_parser = { version = "0.19", path ="../parser", features = ["verify"] }
dashmap = "=3.4.0" dashmap = "=3.4.0"
either = "1.5" either = "1.5"
fxhash = "0.2" fxhash = "0.2"
@ -33,7 +33,7 @@ is-macro = "0.1"
[dev-dependencies] [dev-dependencies]
testing = { version = "0.5", path ="../../testing" } testing = { version = "0.5", path ="../../testing" }
swc_ecma_codegen = { version = "0.15.0", path ="../codegen" } swc_ecma_codegen = { version = "0.16.0", path ="../codegen" }
tempfile = "3" tempfile = "3"
pretty_assertions = "0.6" pretty_assertions = "0.6"
sourcemap = "4.1.1" sourcemap = "4.1.1"

View File

@ -623,6 +623,7 @@ impl Fold<Expr> for Strip {
Expr::TsAs(TsAsExpr { expr, .. }) => validate!(*expr), Expr::TsAs(TsAsExpr { expr, .. }) => validate!(*expr),
Expr::TsNonNull(TsNonNullExpr { expr, .. }) => validate!(*expr), Expr::TsNonNull(TsNonNullExpr { expr, .. }) => validate!(*expr),
Expr::TsTypeAssertion(TsTypeAssertion { expr, .. }) => validate!(*expr), Expr::TsTypeAssertion(TsTypeAssertion { expr, .. }) => validate!(*expr),
Expr::TsConstAssertion(TsConstAssertion { expr, .. }) => validate!(*expr),
Expr::TsTypeCast(TsTypeCastExpr { expr, .. }) => validate!(*expr), Expr::TsTypeCast(TsTypeCastExpr { expr, .. }) => validate!(*expr),
_ => validate!(expr), _ => validate!(expr),
} }

View File

@ -321,3 +321,12 @@ export const handler: Handler = async (event, context) => {};",
"export const handler = async (event, context) => {};", "export const handler = async (event, context) => {};",
ok_if_code_eq ok_if_code_eq
); );
test!(
::swc_ecma_parser::Syntax::Typescript(Default::default()),
|_| strip(),
issue_656,
"export const x = { text: 'hello' } as const;",
"export const x = { text: 'hello' };",
ok_if_code_eq
);

View File

@ -11,10 +11,10 @@ edition = "2018"
# 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]
swc_ecma_ast = { version = "0.16.0", path ="../ast" } swc_ecma_ast = { version = "0.17.0", path ="../ast" }
swc_atoms = { version = "0.2.0", path ="../../atoms" } swc_atoms = { version = "0.2.0", path ="../../atoms" }
swc_common = { version = "0.5.0", path ="../../common" } swc_common = { version = "0.5.0", path ="../../common" }
swc_ecma_parser = { version = "0.18", path ="../parser", features = ["verify"] } swc_ecma_parser = { version = "0.19", path ="../parser", features = ["verify"] }
once_cell = "1" once_cell = "1"
scoped-tls = "1" scoped-tls = "1"
unicode-xid = "0.2" unicode-xid = "0.2"

View File

@ -16,7 +16,7 @@ use swc_common::{errors::Handler, FileName, SourceMap};
pub use swc_ecmascript::parser::JscTarget; pub use swc_ecmascript::parser::JscTarget;
use swc_ecmascript::{ use swc_ecmascript::{
ast::{Expr, ExprStmt, ModuleItem, Stmt}, ast::{Expr, ExprStmt, ModuleItem, Stmt},
parser::{lexer::Lexer, Parser, Session as ParseSess, SourceFileInput, Syntax}, parser::{lexer::Lexer, Parser, Session as ParseSess, SourceFileInput, Syntax, TsConfig},
preset_env, preset_env,
transforms::{ transforms::{
const_modules, modules, const_modules, modules,
@ -278,16 +278,71 @@ fn default_cwd() -> PathBuf {
} }
/// `.swcrc` file /// `.swcrc` file
#[derive(Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
#[serde(untagged)] #[serde(untagged, rename = "swcrc")]
pub enum Rc { pub enum Rc {
Single(Config), Single(Config),
Multi(Vec<Config>), Multi(Vec<Config>),
} }
impl Default for Rc {
fn default() -> Self {
Rc::Multi(vec![
Config {
env: None,
test: None,
exclude: Some(FileMatcher::Regex("\\.tsx?$".into())),
jsc: JscConfig {
syntax: Some(Default::default()),
transform: None,
external_helpers: false,
target: Default::default(),
loose: false,
},
module: None,
minify: None,
},
Config {
env: None,
test: Some(FileMatcher::Regex("\\.tsx$".into())),
exclude: None,
jsc: JscConfig {
syntax: Some(Syntax::Typescript(TsConfig {
tsx: true,
..Default::default()
})),
transform: None,
external_helpers: false,
target: Default::default(),
loose: false,
},
module: None,
minify: None,
},
Config {
env: None,
test: Some(FileMatcher::Regex("\\.ts$".into())),
exclude: None,
jsc: JscConfig {
syntax: Some(Syntax::Typescript(TsConfig {
tsx: false,
..Default::default()
})),
transform: None,
external_helpers: false,
target: Default::default(),
loose: false,
},
module: None,
minify: None,
},
])
}
}
impl Rc { impl Rc {
pub fn into_config(self, filename: Option<&Path>) -> Result<Config, Error> { pub fn into_config(self, filename: Option<&Path>) -> Result<Config, Error> {
let cs = match self { let mut cs = match self {
Rc::Single(c) => match filename { Rc::Single(c) => match filename {
Some(filename) => { Some(filename) => {
if c.matches(filename)? { if c.matches(filename)? {
@ -311,20 +366,7 @@ impl Rc {
} }
} }
// TODO // TODO
None => { None => return Ok(cs.remove(0)),
let mut first = None;
for c in cs {
if c.test.is_none() {
return Ok(c);
}
if first.is_none() {
first = Some(c);
}
}
return Ok(first.unwrap_or_default());
}
} }
Err(Error::Unmatched) Err(Error::Unmatched)

View File

@ -258,7 +258,7 @@ impl Compiler {
} }
} }
if let Some(config_file) = config_file { let config_file = config_file.unwrap_or_else(|| Rc::default());
let built = opts.build( let built = opts.build(
&self.cm, &self.cm,
&self.handler, &self.handler,
@ -267,7 +267,6 @@ impl Compiler {
); );
return Ok(built); return Ok(built);
} }
}
_ => {} _ => {}
} }
@ -277,7 +276,7 @@ impl Compiler {
*is_module, *is_module,
match config_file { match config_file {
Some(config_file) => Some(config_file.into_config(None)?), Some(config_file) => Some(config_file.into_config(None)?),
None => None, None => Some(Rc::default().into_config(None)?),
}, },
); );
Ok(built) Ok(built)

View File

@ -370,3 +370,13 @@ fn await_expr_2() {
assert!(f.contains("await")); assert!(f.contains("await"));
assert!(f.contains("test")); assert!(f.contains("test"));
} }
#[test]
fn default_config() {
project("tests/projects/issue-655")
}
#[test]
fn issue_658() {
project("tests/projects/issue-658")
}

View File

@ -0,0 +1,3 @@
class Foo {
}

View File

@ -0,0 +1,3 @@
function foo(): string {
return 'Hello, world!';
}

View File

@ -0,0 +1,3 @@
function foo(): string {
return <div>{'Hello, world!'}</div>;
}

View File

@ -0,0 +1,11 @@
{
"test": ".*.tsx$",
"exclude": ".*.spec.tsx$",
"jsc": {
"target": "es2018",
"parser": {
"syntax": "typescript",
"tsx": true
}
}
}

View File

@ -0,0 +1,3 @@
function foo(): string {
return <any>'Hello, world!';
}

View File

@ -0,0 +1,3 @@
function foo(): string {
return <div>{'Hello, world!'}</div>;
}