mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 21:54:36 +03:00
Fix bugs (#659)
- Strip out const assertions (Closes #656) - Fix exclude of the file matcher (Closes #658) - Automatic typescript detection (Closes #655)
This commit is contained in:
parent
64f6e51b42
commit
82e73b1121
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "swc_ecma_ast"
|
||||
version = "0.16.0"
|
||||
version = "0.17.0"
|
||||
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
|
||||
license = "Apache-2.0/MIT"
|
||||
repository = "https://github.com/swc-project/swc.git"
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "swc_ecma_codegen"
|
||||
version = "0.15.0"
|
||||
version = "0.16.0"
|
||||
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
|
||||
license = "Apache-2.0/MIT"
|
||||
repository = "https://github.com/swc-project/swc.git"
|
||||
@ -13,11 +13,11 @@ bitflags = "1"
|
||||
hashbrown = "0.6"
|
||||
swc_atoms = { version = "0.2", path ="../../atoms" }
|
||||
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" }
|
||||
sourcemap = "4.1.1"
|
||||
num-bigint = { version = "0.2", features = ["serde"] }
|
||||
|
||||
[dev-dependencies]
|
||||
testing = { version = "0.5", path ="../../testing" }
|
||||
swc_ecma_parser = { version = "0.18", path ="../parser" }
|
||||
swc_ecma_parser = { version = "0.19", path ="../parser" }
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "swc_ecma_parser"
|
||||
version = "0.18.1"
|
||||
version = "0.19.0"
|
||||
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
|
||||
license = "Apache-2.0/MIT"
|
||||
repository = "https://github.com/swc-project/swc.git"
|
||||
@ -18,7 +18,7 @@ verify = ["fold"]
|
||||
[dependencies]
|
||||
swc_atoms = { version = "0.2", path ="../../atoms" }
|
||||
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" }
|
||||
enum_kind = { version = "0.2", path ="../../macros/enum_kind" }
|
||||
unicode-xid = "0.2"
|
||||
|
@ -330,7 +330,7 @@ pub enum JscTarget {
|
||||
|
||||
impl Default for JscTarget {
|
||||
fn default() -> Self {
|
||||
JscTarget::Es3
|
||||
JscTarget::Es5
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,9 +11,9 @@ edition = "2018"
|
||||
[dependencies]
|
||||
swc_atoms = { version = "0.2.0", path ="../../atoms" }
|
||||
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_parser = { version = "0.18", path ="../parser", features = ["verify"] }
|
||||
swc_ecma_parser = { version = "0.19", path ="../parser", features = ["verify"] }
|
||||
dashmap = "=3.4.0"
|
||||
either = "1.5"
|
||||
fxhash = "0.2"
|
||||
@ -33,7 +33,7 @@ is-macro = "0.1"
|
||||
|
||||
[dev-dependencies]
|
||||
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"
|
||||
pretty_assertions = "0.6"
|
||||
sourcemap = "4.1.1"
|
||||
|
@ -623,6 +623,7 @@ impl Fold<Expr> for Strip {
|
||||
Expr::TsAs(TsAsExpr { expr, .. }) => validate!(*expr),
|
||||
Expr::TsNonNull(TsNonNullExpr { expr, .. }) => validate!(*expr),
|
||||
Expr::TsTypeAssertion(TsTypeAssertion { expr, .. }) => validate!(*expr),
|
||||
Expr::TsConstAssertion(TsConstAssertion { expr, .. }) => validate!(*expr),
|
||||
Expr::TsTypeCast(TsTypeCastExpr { expr, .. }) => validate!(*expr),
|
||||
_ => validate!(expr),
|
||||
}
|
||||
|
@ -321,3 +321,12 @@ export const handler: Handler = async (event, context) => {};",
|
||||
"export const handler = async (event, context) => {};",
|
||||
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
|
||||
);
|
||||
|
@ -11,10 +11,10 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[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_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"
|
||||
scoped-tls = "1"
|
||||
unicode-xid = "0.2"
|
||||
|
@ -16,7 +16,7 @@ use swc_common::{errors::Handler, FileName, SourceMap};
|
||||
pub use swc_ecmascript::parser::JscTarget;
|
||||
use swc_ecmascript::{
|
||||
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,
|
||||
transforms::{
|
||||
const_modules, modules,
|
||||
@ -278,16 +278,71 @@ fn default_cwd() -> PathBuf {
|
||||
}
|
||||
|
||||
/// `.swcrc` file
|
||||
#[derive(Clone, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(untagged, rename = "swcrc")]
|
||||
pub enum Rc {
|
||||
Single(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 {
|
||||
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 {
|
||||
Some(filename) => {
|
||||
if c.matches(filename)? {
|
||||
@ -311,20 +366,7 @@ impl Rc {
|
||||
}
|
||||
}
|
||||
// TODO
|
||||
None => {
|
||||
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());
|
||||
}
|
||||
None => return Ok(cs.remove(0)),
|
||||
}
|
||||
|
||||
Err(Error::Unmatched)
|
||||
|
19
src/lib.rs
19
src/lib.rs
@ -258,15 +258,14 @@ impl Compiler {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(config_file) = config_file {
|
||||
let built = opts.build(
|
||||
&self.cm,
|
||||
&self.handler,
|
||||
*is_module,
|
||||
Some(config_file.into_config(Some(path))?),
|
||||
);
|
||||
return Ok(built);
|
||||
}
|
||||
let config_file = config_file.unwrap_or_else(|| Rc::default());
|
||||
let built = opts.build(
|
||||
&self.cm,
|
||||
&self.handler,
|
||||
*is_module,
|
||||
Some(config_file.into_config(Some(path))?),
|
||||
);
|
||||
return Ok(built);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@ -277,7 +276,7 @@ impl Compiler {
|
||||
*is_module,
|
||||
match config_file {
|
||||
Some(config_file) => Some(config_file.into_config(None)?),
|
||||
None => None,
|
||||
None => Some(Rc::default().into_config(None)?),
|
||||
},
|
||||
);
|
||||
Ok(built)
|
||||
|
@ -370,3 +370,13 @@ fn await_expr_2() {
|
||||
assert!(f.contains("await"));
|
||||
assert!(f.contains("test"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_config() {
|
||||
project("tests/projects/issue-655")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issue_658() {
|
||||
project("tests/projects/issue-658")
|
||||
}
|
||||
|
3
tests/projects/issue-655/index.js
Normal file
3
tests/projects/issue-655/index.js
Normal file
@ -0,0 +1,3 @@
|
||||
class Foo {
|
||||
|
||||
}
|
3
tests/projects/issue-655/ts.ts
Normal file
3
tests/projects/issue-655/ts.ts
Normal file
@ -0,0 +1,3 @@
|
||||
function foo(): string {
|
||||
return 'Hello, world!';
|
||||
}
|
3
tests/projects/issue-655/tsx.tsx
Normal file
3
tests/projects/issue-655/tsx.tsx
Normal file
@ -0,0 +1,3 @@
|
||||
function foo(): string {
|
||||
return <div>{'Hello, world!'}</div>;
|
||||
}
|
11
tests/projects/issue-658/.swcrc
Normal file
11
tests/projects/issue-658/.swcrc
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"test": ".*.tsx$",
|
||||
"exclude": ".*.spec.tsx$",
|
||||
"jsc": {
|
||||
"target": "es2018",
|
||||
"parser": {
|
||||
"syntax": "typescript",
|
||||
"tsx": true
|
||||
}
|
||||
}
|
||||
}
|
3
tests/projects/issue-658/invalid.spec.tsx
Normal file
3
tests/projects/issue-658/invalid.spec.tsx
Normal file
@ -0,0 +1,3 @@
|
||||
function foo(): string {
|
||||
return <any>'Hello, world!';
|
||||
}
|
3
tests/projects/issue-658/valid.tsx
Normal file
3
tests/projects/issue-658/valid.tsx
Normal file
@ -0,0 +1,3 @@
|
||||
function foo(): string {
|
||||
return <div>{'Hello, world!'}</div>;
|
||||
}
|
Loading…
Reference in New Issue
Block a user