GetterProp - Add type_ann (#557)

swc_ecma_ast:
 - add type_ann to GetterProp

swc_ecmascript_parser:
 - parse type annotation after a getter property
This commit is contained in:
David Sherret 2020-01-02 23:08:27 -05:00 committed by 강동윤
parent 1bcfcb741a
commit 5acf557e35
5 changed files with 187 additions and 3 deletions

View File

@ -5,6 +5,7 @@ use crate::{
lit::{Number, Str},
pat::Pat,
stmt::BlockStmt,
typescript::TsTypeAnn,
};
use swc_common::{ast_node, Span};
@ -52,6 +53,8 @@ pub struct AssignProp {
pub struct GetterProp {
pub span: Span,
pub key: PropName,
#[serde(default, rename = "typeAnnotation")]
pub type_ann: Option<TsTypeAnn>,
#[serde(default)]
pub body: Option<BlockStmt>,
}

View File

@ -256,7 +256,10 @@ impl<'a, I: Tokens> ParseObject<'a, Box<Expr>> for Parser<'a, I> {
)
.map(
|Function {
body, type_params, ..
body,
type_params,
return_type,
..
}| {
if type_params.is_some() {
self.emit_err(type_params.unwrap().span(), SyntaxError::TS1094);
@ -271,6 +274,7 @@ impl<'a, I: Tokens> ParseObject<'a, Box<Expr>> for Parser<'a, I> {
PropOrSpread::Prop(Box::new(Prop::Getter(GetterProp {
span: span!(start),
key,
type_ann: return_type,
body,
})))
},

View File

@ -0,0 +1,8 @@
const obj = {
get prop1(): string {
return "";
},
get prop2() {
return 5;
}
};

View File

@ -0,0 +1,164 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 110,
"ctxt": 0
},
"body": [
{
"type": "VariableDeclaration",
"span": {
"start": 0,
"end": 110,
"ctxt": 0
},
"kind": "const",
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
"span": {
"start": 6,
"end": 109,
"ctxt": 0
},
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 9,
"ctxt": 0
},
"value": "obj",
"typeAnnotation": null,
"optional": false
},
"init": {
"type": "ObjectExpression",
"span": {
"start": 12,
"end": 109,
"ctxt": 0
},
"properties": [
{
"type": "GetterProperty",
"span": {
"start": 18,
"end": 64,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 22,
"end": 27,
"ctxt": 0
},
"value": "prop1",
"typeAnnotation": null,
"optional": false
},
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 29,
"end": 37,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 31,
"end": 37,
"ctxt": 0
},
"kind": "string"
}
},
"body": {
"type": "BlockStatement",
"span": {
"start": 38,
"end": 64,
"ctxt": 0
},
"stmts": [
{
"type": "ReturnStatement",
"span": {
"start": 48,
"end": 58,
"ctxt": 0
},
"argument": {
"type": "StringLiteral",
"span": {
"start": 55,
"end": 57,
"ctxt": 0
},
"value": "",
"hasEscape": false
}
}
]
}
},
{
"type": "GetterProperty",
"span": {
"start": 70,
"end": 107,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 74,
"end": 79,
"ctxt": 0
},
"value": "prop2",
"typeAnnotation": null,
"optional": false
},
"typeAnnotation": null,
"body": {
"type": "BlockStatement",
"span": {
"start": 82,
"end": 107,
"ctxt": 0
},
"stmts": [
{
"type": "ReturnStatement",
"span": {
"start": 92,
"end": 101,
"ctxt": 0
},
"argument": {
"type": "NumericLiteral",
"span": {
"start": 99,
"end": 100,
"ctxt": 0
},
"value": 5.0
}
}
]
}
}
]
},
"definite": false
}
]
}
],
"interpreter": null
}

View File

@ -121,7 +121,12 @@ impl Fold<Expr> for ObjectLitFolder {
_ => None,
};
let (key, function) = match prop {
Prop::Getter(GetterProp { span, body, key }) => (
Prop::Getter(GetterProp {
span,
body,
key,
type_ann,
}) => (
key,
Function {
span,
@ -131,7 +136,7 @@ impl Fold<Expr> for ObjectLitFolder {
params: vec![],
decorators: Default::default(),
type_params: Default::default(),
return_type: Default::default(),
return_type: type_ann,
},
),
Prop::Setter(SetterProp {