- The parser now supports parsing optional patterns in .d.ts files (Closes #709)
 - The source map is handled properly (Closes #705, Closes #707)
This commit is contained in:
강동윤 2020-03-09 21:18:41 +09:00 committed by GitHub
parent ffbb5163aa
commit e448a8910c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
154 changed files with 1895 additions and 1037 deletions

View File

@ -1002,7 +1002,7 @@ impl SourceMap {
// mappings.sort_by_key(|v| v.0);
let mut cur_file: Option<Arc<SourceFile>> = None;
// let mut src_id = None;
let mut src_id;
let mut ch_start = 0;
let mut line_ch_start = 0;
@ -1021,11 +1021,11 @@ impl SourceMap {
Some(ref f) if f.start_pos <= pos && pos < f.end_pos => f,
_ => {
f = self.lookup_source_file(pos);
builder.add_source(&f.src);
src_id = builder.add_source(&f.name.to_string());
builder.set_source_contents(src_id, Some(&f.src));
cur_file = Some(f.clone());
ch_start = 0;
line_ch_start = 0;
// src_id = Some(builder.add_source(&f.src));
&f
}
};

View File

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

View File

@ -35,6 +35,10 @@ pub struct ArrayPat {
#[serde(rename = "elements")]
pub elems: Vec<Option<Pat>>,
/// Only in .d.ts file
#[serde(rename = "elements")]
pub optional: bool,
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
}
@ -47,6 +51,10 @@ pub struct ObjectPat {
#[serde(rename = "properties")]
pub props: Vec<ObjectPatProp>,
/// Only in .d.ts file
#[serde(rename = "elements")]
pub optional: bool,
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
}

View File

@ -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.17.0", path ="../ast" }
swc_ecma_ast = { version = "0.18.0", path ="../ast" }
swc_ecma_codegen_macros = { version = "0.5", path ="./macros" }
sourcemap = "5"
num-bigint = { version = "0.2", features = ["serde"] }
swc_ecma_parser = { version = "0.20", path ="../parser" }
swc_ecma_parser = { version = "0.21", path ="../parser" }
[dev-dependencies]
testing = { version = "0.5", path ="../../testing" }

View File

@ -5,7 +5,7 @@ use swc_ecma_codegen_macros::emitter;
impl<'a> Emitter<'a> {
#[emitter]
pub fn emit_decl(&mut self, node: &Decl) -> Result {
fn emit_decl(&mut self, node: &Decl) -> Result {
match *node {
Decl::Class(ref n) => emit!(n),
Decl::Fn(ref n) => emit!(n),
@ -22,23 +22,35 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_class_decl(&mut self, node: &ClassDecl) -> Result {
fn emit_class_decl(&mut self, node: &ClassDecl) -> Result {
self.emit_leading_comments_of_pos(node.span().lo())?;
if node.declare {
keyword!("declare");
space!();
}
for dec in &node.class.decorators {
emit!(dec);
}
keyword!("class");
space!();
emit!(node.ident);
emit!(node.class.type_params);
formatting_space!();
self.emit_class_trailing(&node.class)?;
}
#[emitter]
pub fn emit_fn_decl(&mut self, node: &FnDecl) -> Result {
fn emit_fn_decl(&mut self, node: &FnDecl) -> Result {
self.emit_leading_comments_of_pos(node.span().lo())?;
if node.declare {
keyword!("declare");
space!();
}
if node.function.is_async {
keyword!("async");
space!();
@ -56,9 +68,14 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_var_decl(&mut self, node: &VarDecl) -> Result {
fn emit_var_decl(&mut self, node: &VarDecl) -> Result {
self.emit_leading_comments_of_pos(node.span.lo())?;
if node.declare {
keyword!("declare");
space!();
}
keyword!(node.kind.as_str());
space!();
@ -70,7 +87,7 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_var_declator(&mut self, node: &VarDeclarator) -> Result {
fn emit_var_declarator(&mut self, node: &VarDeclarator) -> Result {
self.emit_leading_comments_of_pos(node.span().lo())?;
emit!(node.name);

View File

@ -93,6 +93,7 @@ mod tests {
}
#[test]
#[ignore]
fn class_expression() {
assert_min("(class {})", "(class{});");
assert_min("(class Foo {})", "(class Foo{});");

View File

@ -6,7 +6,7 @@ use swc_ecma_codegen_macros::emitter;
impl<'a> Emitter<'a> {
#[emitter]
pub fn emit_jsx_element(&mut self, node: &JSXElement) -> Result {
fn emit_jsx_element(&mut self, node: &JSXElement) -> Result {
emit!(node.opening);
self.emit_list(
node.span(),
@ -19,7 +19,7 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_opening_element(&mut self, node: &JSXOpeningElement) -> Result {
fn emit_jsx_opening_element(&mut self, node: &JSXOpeningElement) -> Result {
punct!("<");
emit!(node.name);
@ -36,7 +36,7 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_element_name(&mut self, node: &JSXElementName) -> Result {
fn emit_jsx_element_name(&mut self, node: &JSXElementName) -> Result {
match *node {
JSXElementName::Ident(ref n) => emit!(n),
JSXElementName::JSXMemberExpr(ref n) => emit!(n),
@ -45,7 +45,7 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_attr(&mut self, node: &JSXAttr) -> Result {
fn emit_jsx_attr(&mut self, node: &JSXAttr) -> Result {
emit!(node.name);
if let Some(ref value) = node.value {
@ -55,7 +55,7 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_attr_value(&mut self, node: &JSXAttrValue) -> Result {
fn emit_jsx_attr_value(&mut self, node: &JSXAttrValue) -> Result {
match *node {
JSXAttrValue::Lit(ref n) => emit!(n),
JSXAttrValue::JSXExprContainer(ref n) => emit!(n),
@ -65,7 +65,7 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_attr_name(&mut self, node: &JSXAttrName) -> Result {
fn emit_jsx_attr_name(&mut self, node: &JSXAttrName) -> Result {
match *node {
JSXAttrName::Ident(ref n) => emit!(n),
JSXAttrName::JSXNamespacedName(ref n) => emit!(n),
@ -73,7 +73,7 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_attr_or_spread(&mut self, node: &JSXAttrOrSpread) -> Result {
fn emit_jsx_attr_or_spread(&mut self, node: &JSXAttrOrSpread) -> Result {
match *node {
JSXAttrOrSpread::JSXAttr(ref n) => emit!(n),
JSXAttrOrSpread::SpreadElement(ref n) => emit!(n),
@ -81,7 +81,7 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_element_child(&mut self, node: &JSXElementChild) -> Result {
fn emit_jsx_element_child(&mut self, node: &JSXElementChild) -> Result {
match *node {
JSXElementChild::JSXElement(ref n) => emit!(n),
JSXElementChild::JSXExprContainer(ref n) => emit!(n),
@ -92,7 +92,7 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_spread_child(&mut self, node: &JSXSpreadChild) -> Result {
fn emit_jsx_spread_child(&mut self, node: &JSXSpreadChild) -> Result {
punct!("{");
punct!("...");
emit!(node.expr);
@ -100,14 +100,14 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_expr_container(&mut self, node: &JSXExprContainer) -> Result {
fn emit_jsx_expr_container(&mut self, node: &JSXExprContainer) -> Result {
punct!("{");
emit!(node.expr);
punct!("}");
}
#[emitter]
pub fn emit_jsx_expr(&mut self, node: &JSXExpr) -> Result {
fn emit_jsx_expr(&mut self, node: &JSXExpr) -> Result {
match *node {
JSXExpr::Expr(ref n) => emit!(n),
JSXExpr::JSXEmptyExpr(ref n) => emit!(n),
@ -115,14 +115,14 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_closing_element(&mut self, node: &JSXClosingElement) -> Result {
fn emit_jsx_closing_element(&mut self, node: &JSXClosingElement) -> Result {
punct!("</");
emit!(node.name);
punct!(">");
}
#[emitter]
pub fn emit_jsx_fragment(&mut self, node: &JSXFragment) -> Result {
fn emit_jsx_fragment(&mut self, node: &JSXFragment) -> Result {
emit!(node.opening);
self.emit_list(
@ -135,39 +135,39 @@ impl<'a> Emitter<'a> {
}
#[emitter]
pub fn emit_jsx_opening_fragment(&mut self, node: &JSXOpeningFragment) -> Result {
fn emit_jsx_opening_fragment(&mut self, node: &JSXOpeningFragment) -> Result {
punct!("<>")
}
#[emitter]
pub fn emit_jsx_closing_fragment(&mut self, node: &JSXClosingFragment) -> Result {
fn emit_jsx_closing_fragment(&mut self, node: &JSXClosingFragment) -> Result {
punct!("</>")
}
#[emitter]
pub fn emit_jsx_namespaced_name(&mut self, node: &JSXNamespacedName) -> Result {
fn emit_jsx_namespaced_name(&mut self, node: &JSXNamespacedName) -> Result {
emit!(node.ns);
punct!(":");
emit!(node.name);
}
#[emitter]
pub fn emit_jsx_empty_expr(&mut self, node: &JSXEmptyExpr) -> Result {}
fn emit_jsx_empty_expr(&mut self, node: &JSXEmptyExpr) -> Result {}
#[emitter]
pub fn emit_jsx_text(&mut self, node: &JSXText) -> Result {
fn emit_jsx_text(&mut self, node: &JSXText) -> Result {
self.emit_js_word(node.span(), &node.value)?;
}
#[emitter]
pub fn emit_jsx_member_expr(&mut self, node: &JSXMemberExpr) -> Result {
fn emit_jsx_member_expr(&mut self, node: &JSXMemberExpr) -> Result {
emit!(node.obj);
punct!(".");
emit!(node.prop);
}
#[emitter]
pub fn emit_jsx_object(&mut self, node: &JSXObject) -> Result {
fn emit_jsx_object(&mut self, node: &JSXObject) -> Result {
match *node {
JSXObject::Ident(ref n) => emit!(n),
JSXObject::JSXMemberExpr(ref n) => emit!(n),

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
(class{
prototype() {
(class {
prototype() {
}
});

View File

@ -1,2 +1,2 @@
var a = class b extends 1{
var a = class b extends 1 {
};

View File

@ -1,6 +1,6 @@
class a{
b() {
class a {
b() {
}
c() {
c() {
}
}

View File

@ -1,4 +1,4 @@
class a{
class a {
set b(c) {
}
get b() {

View File

@ -1,2 +1,2 @@
var a = class extends b{
var a = class extends b {
};

View File

@ -1,4 +1,4 @@
class a{
class a {
constructor(){
}
}

View File

@ -1,4 +1,4 @@
(class{
(class {
get a() {
}
});

View File

@ -1,4 +1,4 @@
class a extends b{
class a extends b {
constructor(){
()=>{
super();

View File

@ -1,5 +1,5 @@
class a extends b{
c() {
class a extends b {
c() {
return super.d;
}
}

View File

@ -1,5 +1,5 @@
class a{
b() {
class a {
b() {
new super.c();
}
}

View File

@ -1,4 +1,4 @@
class a{
class a {
get b() {
}
set b(c) {

View File

@ -1,4 +1,4 @@
class a{
class a {
static get b() {
}
static get c() {

View File

@ -1,6 +1,6 @@
class a{
b() {
class a {
b() {
}
c() {
c() {
}
}

View File

@ -1,2 +1,2 @@
class a{
class a {
}

View File

@ -1,2 +1,2 @@
export default class a{
export default class a {
};

View File

@ -1,7 +1,7 @@
function a() {
(class b{
(class b {
});
class c{
class c {
}
;
}

View File

@ -1,4 +1,4 @@
function* a() {
(class extends (yield){
(class extends (yield) {
});
}

View File

@ -1,6 +1,6 @@
class a{
b() {
class a {
b() {
}
c() {
c() {
}
}

View File

@ -1,4 +1,4 @@
class a{
static ['prototype']() {
class a {
static ['prototype']() {
}
}

View File

@ -1,4 +1,4 @@
class a extends b{
class a extends b {
constructor(){
super();
}

View File

@ -1,2 +1,2 @@
(class{
(class {
});

View File

@ -1,2 +1,2 @@
(class a{
(class a {
});

View File

@ -1,4 +1,4 @@
class a extends b{
class a extends b {
static get c() {
}
}

View File

@ -1,3 +1,3 @@
export class a{
export class a {
}
;

View File

@ -1,4 +1,4 @@
class a extends b{
class a extends b {
constructor(){
()=>super()
;

View File

@ -1,5 +1,5 @@
class a{
static *b(c) {
class a {
static *b(c) {
yield c;
}
}

View File

@ -1,4 +1,4 @@
class a extends b{
class a extends b {
constructor(){
super.c;
}

View File

@ -1,4 +1,4 @@
(class{
(class {
set a(b) {
'use strict';
}

View File

@ -1,4 +1,4 @@
class a{
static *[b]() {
class a {
static *[b]() {
}
}

View File

@ -1,5 +1,5 @@
class a extends b{
c() {
class a extends b {
c() {
return super[1];
}
}

View File

@ -1,4 +1,4 @@
class a{
set(b) {
class a {
set(b) {
}
}

View File

@ -1,4 +1,4 @@
class a{
static [b]() {
class a {
static [b]() {
}
}

View File

@ -1,4 +1,4 @@
(class{
3() {
(class {
3() {
}
});

View File

@ -1,4 +1,4 @@
class a{
static b() {
class a {
static b() {
}
}

View File

@ -1,4 +1,4 @@
(class{
a() {
(class {
a() {
}
});

View File

@ -1,2 +1,2 @@
(class a extends a{
(class a extends a {
});

View File

@ -1,2 +1,2 @@
export default (class{
export default (class {
});

View File

@ -1,4 +1,4 @@
class a{
b() {
class a {
b() {
}
}

View File

@ -1,5 +1,5 @@
class a extends b{
c() {
class a extends b {
c() {
[super.d] = e;
}
}

View File

@ -1,6 +1,6 @@
class a{
class a {
constructor(){
}
['constructor']() {
['constructor']() {
}
}

View File

@ -1,2 +1,2 @@
class a{
class a {
}

View File

@ -1,4 +1,4 @@
class a extends b{
class a extends b {
constructor(){
({
c: super()

View File

@ -1,6 +1,6 @@
class a{
static [b]() {
class a {
static [b]() {
}
static [c]() {
static [c]() {
}
}

View File

@ -1,4 +1,4 @@
class a{
static static() {
class a {
static static() {
}
}

View File

@ -1,4 +1,4 @@
class a{
class a {
constructor(){
}
}

View File

@ -1,4 +1,4 @@
class a{
prototype() {
class a {
prototype() {
}
}

View File

@ -1,21 +1,21 @@
class a{
class a {
constructor(){
}
b() {
b() {
}
}
;
class c{
class c {
constructor(...d){
}
b() {
b() {
}
}
;
class e extends a{
class e extends a {
}
;
var f = class g{
var f = class g {
};
var h = class{
var h = class {
};

View File

@ -1,2 +1,2 @@
(class{
(class {
});

View File

@ -1,2 +1,2 @@
var a = class extends (b, c){
var a = class extends (b, c) {
};

View File

@ -1,4 +1,4 @@
class a extends b{
class a extends b {
constructor(){
super();
}

View File

@ -1,4 +1,4 @@
(class{
(class {
constructor(){
super.a;
}

View File

@ -1,5 +1,5 @@
class a extends b{
c() {
class a extends b {
c() {
({ d: super[e] } = f);
}
}

View File

@ -1,2 +1,2 @@
(class a extends 1{
(class a extends 1 {
});

View File

@ -1,5 +1,5 @@
class a{
b() {
class a {
b() {
}
get b() {
}

View File

@ -1,5 +1,5 @@
class a{
b() {
class a {
b() {
new super.c;
}
}

View File

@ -1,6 +1,6 @@
class a{
b() {
class a {
b() {
}
static c() {
static c() {
}
}

View File

@ -1,4 +1,4 @@
class a{
class a {
constructor(){
}
constructor(){

View File

@ -1,2 +1,2 @@
(class extends a{
(class extends a {
});

View File

@ -1,4 +1,4 @@
class a{
static get() {
class a {
static get() {
}
}

View File

@ -1,6 +1,6 @@
function* a() {
(class{
[yield]() {
(class {
[yield]() {
}
});
}

View File

@ -1,4 +1,4 @@
class a extends b{
class a extends b {
get c() {
}
}

View File

@ -1,6 +1,6 @@
class a{
b() {
class a {
b() {
}
c() {
c() {
}
}

View File

@ -1,4 +1,4 @@
class a{
class a {
static set b(c) {
}
}

View File

@ -1,4 +1,4 @@
class a{
static() {
class a {
static() {
}
}

View File

@ -1,4 +1,4 @@
(class{
static() {
(class {
static() {
}
});

View File

@ -1,5 +1,5 @@
class a extends b{
c() {
class a extends b {
c() {
super.yield;
}
}

View File

@ -1,6 +1,6 @@
class a{
b() {
class a {
b() {
}
c() {
c() {
}
}

View File

@ -1,4 +1,4 @@
(class{
a() {
(class {
a() {
}
});

View File

@ -1,4 +1,4 @@
class a{
static [b]() {
class a {
static [b]() {
}
}

View File

@ -1,4 +1,4 @@
class a extends b{
class a extends b {
constructor(c = super()){
}
}

View File

@ -1,4 +1,4 @@
function* a() {
yield class{
yield class {
};
}

View File

@ -1,5 +1,5 @@
(class{
a(b) {
(class {
a(b) {
'use strict';
}
});

View File

@ -1,2 +1,2 @@
(class extends (a, b){
(class extends (a, b) {
});

View File

@ -1,4 +1,4 @@
(class{
(class {
set a(b) {
}
});

View File

@ -1,6 +1,6 @@
(class{
a() {
(class {
a() {
}
b() {
b() {
}
});

View File

@ -1,4 +1,4 @@
class a{
class a {
static get b() {
}
get b() {

View File

@ -1,4 +1,4 @@
class a{
class a {
static get b() {
}
static set b(c) {

View File

@ -1,5 +1,5 @@
(class{
a({ b } = {
(class {
a({ b } = {
b: 1
}) {
}

View File

@ -1,4 +1,4 @@
class a{
*static() {
class a {
*static() {
}
}

View File

@ -1,4 +1,4 @@
class a{
static() {
class a {
static() {
}
}

View File

@ -1,4 +1,4 @@
(class{
[1 + 2]() {
(class {
[1 + 2]() {
}
});

View File

@ -1,14 +1,14 @@
a = class{
static b() {
a = class {
static b() {
}
static get c() {
}
static set d(a) {
}
static() {
static() {
}
get() {
get() {
}
set() {
set() {
}
};

View File

@ -1,4 +1,4 @@
class a{
class a {
static get [b]() {
}
}

View File

@ -1,2 +1,2 @@
class a{
class a {
}

View File

@ -1,2 +1,2 @@
class a{
class a {
}

View File

@ -1,2 +1,2 @@
class a{
class a {
}

View File

@ -1,2 +1,2 @@
export default class{
export default class {
};

View File

@ -1,2 +1,2 @@
(class extends 1{
(class extends 1 {
});

View File

@ -1,4 +1,4 @@
class a{
static set(b) {
class a {
static set(b) {
}
}

View File

@ -1,2 +1,2 @@
class a extends 1{
class a extends 1 {
}

View File

@ -1,7 +1,7 @@
function a() {
class b{
class b {
}
var c = b;
var d = class e{
var d = class e {
};
}

Some files were not shown because too many files have changed in this diff Show More