fix(es/parser): Allow negative bigint literal in types (#4938)

This commit is contained in:
magic-akari 2022-06-11 12:15:21 +08:00 committed by GitHub
parent 1266e00868
commit 11db191af5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 249 additions and 2 deletions

View File

@ -2058,8 +2058,8 @@ impl<I: Tokens> Parser<I> {
bump!(self);
if !matches!(*cur!(self, true)?, Token::Num { .. }) {
unexpected!(self, "a numeric literal")
if !matches!(*cur!(self, true)?, Token::Num { .. } | Token::BigInt { .. }) {
unexpected!(self, "numeric literal or bigint literal")
}
let lit = self.parse_lit()?;
@ -2082,6 +2082,24 @@ impl<I: Tokens> Parser<I> {
raw: Some(new_raw.into()),
})
}
Lit::BigInt(BigInt { span, value, raw }) => {
let mut new_raw = String::from("-");
match raw {
Some(raw) => {
new_raw.push_str(&raw);
}
_ => {
write!(new_raw, "{}", value).unwrap();
}
};
TsLit::BigInt(BigInt {
span,
value: -value,
raw: Some(new_raw.into()),
})
}
_ => unreachable!(),
};

View File

@ -0,0 +1,6 @@
type Test = -1n | 1n;
class Test {
sign(): -1n | 1n {
}
}

View File

@ -0,0 +1,223 @@
{
"type": "Script",
"span": {
"start": 1,
"end": 67,
"ctxt": 0
},
"body": [
{
"type": "TsTypeAliasDeclaration",
"span": {
"start": 1,
"end": 22,
"ctxt": 0
},
"declare": false,
"id": {
"type": "Identifier",
"span": {
"start": 6,
"end": 10,
"ctxt": 0
},
"value": "Test",
"optional": false
},
"typeParams": null,
"typeAnnotation": {
"type": "TsUnionType",
"span": {
"start": 13,
"end": 21,
"ctxt": 0
},
"types": [
{
"type": "TsLiteralType",
"span": {
"start": 13,
"end": 16,
"ctxt": 0
},
"literal": {
"type": "BigIntLiteral",
"span": {
"start": 14,
"end": 16,
"ctxt": 0
},
"value": [
-1,
[
1
]
],
"raw": "-1n"
}
},
{
"type": "TsLiteralType",
"span": {
"start": 19,
"end": 21,
"ctxt": 0
},
"literal": {
"type": "BigIntLiteral",
"span": {
"start": 19,
"end": 21,
"ctxt": 0
},
"value": [
1,
[
1
]
],
"raw": "1n"
}
}
]
}
},
{
"type": "ClassDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 30,
"end": 34,
"ctxt": 0
},
"value": "Test",
"optional": false
},
"declare": false,
"span": {
"start": 24,
"end": 67,
"ctxt": 0
},
"decorators": [],
"body": [
{
"type": "ClassMethod",
"span": {
"start": 41,
"end": 65,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 41,
"end": 45,
"ctxt": 0
},
"value": "sign",
"optional": false
},
"function": {
"params": [],
"decorators": [],
"span": {
"start": 41,
"end": 65,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 58,
"end": 65,
"ctxt": 0
},
"stmts": []
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": {
"type": "TsTypeAnnotation",
"span": {
"start": 47,
"end": 57,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsUnionType",
"span": {
"start": 49,
"end": 57,
"ctxt": 0
},
"types": [
{
"type": "TsLiteralType",
"span": {
"start": 49,
"end": 52,
"ctxt": 0
},
"literal": {
"type": "BigIntLiteral",
"span": {
"start": 50,
"end": 52,
"ctxt": 0
},
"value": [
-1,
[
1
]
],
"raw": "-1n"
}
},
{
"type": "TsLiteralType",
"span": {
"start": 55,
"end": 57,
"ctxt": 0
},
"literal": {
"type": "BigIntLiteral",
"span": {
"start": 55,
"end": 57,
"ctxt": 0
},
"value": [
1,
[
1
]
],
"raw": "1n"
}
}
]
}
}
},
"kind": "method",
"isStatic": false,
"accessibility": null,
"isAbstract": false,
"isOptional": false,
"isOverride": false
}
],
"superClass": null,
"isAbstract": false,
"typeParams": null,
"superTypeParams": null,
"implements": []
}
],
"interpreter": null
}