Skip to content

Commit

Permalink
Fix bugs (#949)
Browse files Browse the repository at this point in the history
swc_ecma_parser:
 - Allow `in` in class properties (#944)
 - Make `delete` with optional chaining valid (#947)

swc_ecma_transforms:
 - Add a `typescript_class_properties` pass (#930)
  • Loading branch information
kdy1 authored Aug 9, 2020
1 parent 26f4909 commit 1315d58
Show file tree
Hide file tree
Showing 13 changed files with 519 additions and 91 deletions.
2 changes: 1 addition & 1 deletion ecmascript/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "swc_ecma_parser"
version = "0.33.1"
version = "0.33.2"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git"
Expand Down
1 change: 1 addition & 0 deletions ecmascript/parser/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ impl Error {

Eof => "Unexpected eof".into(),

TS2703 => "The operand of a delete operator must be a property reference.".into(),
// TODO:
_ => format!("{:?}", kind).into(),
};
Expand Down
1 change: 1 addition & 0 deletions ecmascript/parser/src/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ impl<'a, I: Tokens> Parser<I> {
let ctx = Context {
in_class_prop: true,
in_method: false,
include_in_expr: true,
..self.ctx()
};
self.with_ctx(ctx).parse_with(|p| {
Expand Down
7 changes: 6 additions & 1 deletion ecmascript/parser/src/parser/expr/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,13 @@ impl<'a, I: Tokens> Parser<I> {
_ => e,
}
}
match *arg {
match &*arg {
Expr::Member(..) => {}
Expr::OptChain(e)
if match &*e.expr {
Expr::Member(..) => true,
_ => false,
} => {}
_ => self.emit_err(unwrap_paren(&arg).span(), SyntaxError::TS2703),
}
}
Expand Down
3 changes: 3 additions & 0 deletions ecmascript/parser/tests/typescript/issue-944/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CTest {
myFunc = () => "key" in {};
}
109 changes: 109 additions & 0 deletions ecmascript/parser/tests/typescript/issue-944/input.ts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 47,
"ctxt": 0
},
"body": [
{
"type": "ClassDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 6,
"end": 11,
"ctxt": 0
},
"value": "CTest",
"typeAnnotation": null,
"optional": false
},
"declare": false,
"span": {
"start": 0,
"end": 47,
"ctxt": 0
},
"decorators": [],
"body": [
{
"type": "ClassProperty",
"span": {
"start": 18,
"end": 45,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 18,
"end": 24,
"ctxt": 0
},
"value": "myFunc",
"typeAnnotation": null,
"optional": false
},
"value": {
"type": "ArrowFunctionExpression",
"span": {
"start": 27,
"end": 44,
"ctxt": 0
},
"params": [],
"body": {
"type": "BinaryExpression",
"span": {
"start": 33,
"end": 44,
"ctxt": 0
},
"operator": "in",
"left": {
"type": "StringLiteral",
"span": {
"start": 33,
"end": 38,
"ctxt": 0
},
"value": "key",
"hasEscape": false
},
"right": {
"type": "ObjectExpression",
"span": {
"start": 42,
"end": 44,
"ctxt": 0
},
"properties": []
}
},
"async": false,
"generator": false,
"typeParameters": null,
"returnType": null
},
"typeAnnotation": null,
"isStatic": false,
"decorators": [],
"computed": false,
"accessibility": null,
"isAbstract": false,
"isOptional": false,
"readonly": false,
"declare": false,
"definite": false
}
],
"superClass": null,
"isAbstract": false,
"typeParams": null,
"superTypeParams": null,
"implements": []
}
],
"interpreter": null
}
1 change: 1 addition & 0 deletions ecmascript/parser/tests/typescript/issue-947/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete obj?.myProp;
72 changes: 72 additions & 0 deletions ecmascript/parser/tests/typescript/issue-947/input.ts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 19,
"ctxt": 0
},
"body": [
{
"type": "ExpressionStatement",
"span": {
"start": 0,
"end": 19,
"ctxt": 0
},
"expression": {
"type": "UnaryExpression",
"span": {
"start": 0,
"end": 18,
"ctxt": 0
},
"operator": "delete",
"argument": {
"type": "OptionalChainingExpression",
"span": {
"start": 7,
"end": 18,
"ctxt": 0
},
"questionDotToken": {
"start": 10,
"end": 11,
"ctxt": 0
},
"expr": {
"type": "MemberExpression",
"span": {
"start": 7,
"end": 18,
"ctxt": 0
},
"object": {
"type": "Identifier",
"span": {
"start": 7,
"end": 10,
"ctxt": 0
},
"value": "obj",
"typeAnnotation": null,
"optional": false
},
"property": {
"type": "Identifier",
"span": {
"start": 12,
"end": 18,
"ctxt": 0
},
"value": "myProp",
"typeAnnotation": null,
"optional": false
},
"computed": false
}
}
}
}
],
"interpreter": null
}
73 changes: 73 additions & 0 deletions ecmascript/parser/tests/typescript/v4/issue-941/input.ts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 30,
"ctxt": 0
},
"body": [
{
"type": "TryStatement",
"span": {
"start": 0,
"end": 30,
"ctxt": 0
},
"block": {
"type": "BlockStatement",
"span": {
"start": 4,
"end": 7,
"ctxt": 0
},
"stmts": []
},
"handler": {
"type": "CatchClause",
"span": {
"start": 8,
"end": 30,
"ctxt": 0
},
"param": {
"type": "Identifier",
"span": {
"start": 15,
"end": 16,
"ctxt": 0
},
"value": "e",
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 16,
"end": 25,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 18,
"end": 25,
"ctxt": 0
},
"kind": "unknown"
}
},
"optional": false
},
"body": {
"type": "BlockStatement",
"span": {
"start": 27,
"end": 30,
"ctxt": 0
},
"stmts": []
}
},
"finalizer": null
}
],
"interpreter": null
}
3 changes: 2 additions & 1 deletion ecmascript/transforms/src/compat/es2020.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use self::{
class_properties::class_properties, nullish_coalescing::nullish_coalescing,
class_properties::{class_properties, typescript_class_properties},
nullish_coalescing::nullish_coalescing,
opt_chaining::optional_chaining,
};

Expand Down
Loading

0 comments on commit 1315d58

Please sign in to comment.