Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Fix parsing of declare #624

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ecmascript/parser/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ impl<'a, I: Tokens> Parser<'a, I> {

Ok(VarDecl {
span: span!(start),
declare: self.ctx().in_declare,
declare: false,
kind,
decls,
})
Expand Down
37 changes: 27 additions & 10 deletions ecmascript/parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,7 @@ impl<'a, I: Tokens> Parser<'a, I> {

Ok(TsEnumDecl {
span: span!(start),
// TODO(kdy1): Is this correct?
declare: self.ctx().in_declare,
declare: false,
is_const,
id,
members,
Expand Down Expand Up @@ -706,7 +705,7 @@ impl<'a, I: Tokens> Parser<'a, I> {

Ok(TsModuleDecl {
span: span!(start),
declare: self.ctx().in_declare,
declare: false,
id: TsModuleName::Ident(id),
body: Some(body),
global: false,
Expand Down Expand Up @@ -745,7 +744,7 @@ impl<'a, I: Tokens> Parser<'a, I> {

Ok(TsModuleDecl {
span: span!(start),
declare: self.ctx().in_declare,
declare: false,
id,
global,
body,
Expand Down Expand Up @@ -928,7 +927,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
};
Ok(TsInterfaceDecl {
span: span!(start),
declare: self.ctx().in_declare,
declare: false,
id,
type_params,
extends,
Expand All @@ -945,7 +944,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
let type_ann = self.expect_then_parse_ts_type(&tok!('='))?;
expect!(';');
Ok(TsTypeAliasDecl {
declare: self.ctx().in_declare,
declare: false,
span: span!(start),
id,
type_params,
Expand All @@ -968,7 +967,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
expect!(';');
Ok(TsImportEqualsDecl {
span: span!(start),
declare: self.ctx().in_declare,
declare: false,
id,
is_export,
module_ref,
Expand Down Expand Up @@ -1906,7 +1905,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
TsModuleDecl {
span: span!(start),
global,
declare: self.ctx().in_declare,
declare: false,
id,
body,
}
Expand Down Expand Up @@ -1990,14 +1989,17 @@ impl<'a, I: Tokens> Parser<'a, I> {
if is!("global") {
return p
.parse_ts_ambient_external_module_decl(start)
.map(From::from)
.map(Decl::from)
.map(make_decl_declare)
.map(Some);
} else if is!(IdentName) {
let value = match *cur!(true)? {
Token::Word(ref w) => w.clone().into(),
_ => unreachable!(),
};
return p.parse_ts_decl(start, decorators, value, /* next */ true);
return p
.parse_ts_decl(start, decorators, value, /* next */ true)
.map(|v| v.map(make_decl_declare));
}

Ok(None)
Expand Down Expand Up @@ -2303,3 +2305,18 @@ enum SignatureParsingMode {
TSCallSignatureDeclaration,
TSConstructSignatureDeclaration,
}

/// Mark as declare
fn make_decl_declare(mut decl: Decl) -> Decl {
match decl {
Decl::Class(ref mut c) => c.declare = true,
Decl::Fn(ref mut f) => f.declare = true,
Decl::Var(ref mut v) => v.declare = true,
Decl::TsInterface(ref mut i) => i.declare = true,
Decl::TsTypeAlias(ref mut a) => a.declare = true,
Decl::TsEnum(ref mut e) => e.declare = true,
Decl::TsModule(ref mut m) => m.declare = true,
}

decl
}
4 changes: 4 additions & 0 deletions ecmascript/parser/tests/typescript/custom/issue-623/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "@dsherret/package" {
namespace packageName {
}
}
71 changes: 71 additions & 0 deletions ecmascript/parser/tests/typescript/custom/issue-623/input.ts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 72,
"ctxt": 0
},
"body": [
{
"type": "TsModuleDeclaration",
"span": {
"start": 0,
"end": 72,
"ctxt": 0
},
"declare": true,
"global": false,
"id": {
"type": "StringLiteral",
"span": {
"start": 15,
"end": 34,
"ctxt": 0
},
"value": "@dsherret/package",
"hasEscape": false
},
"body": {
"type": "TsModuleBlock",
"span": {
"start": 35,
"end": 72,
"ctxt": 0
},
"body": [
{
"type": "TsModuleDeclaration",
"span": {
"start": 41,
"end": 70,
"ctxt": 0
},
"declare": false,
"global": false,
"id": {
"type": "Identifier",
"span": {
"start": 51,
"end": 62,
"ctxt": 0
},
"value": "packageName",
"typeAnnotation": null,
"optional": false
},
"body": {
"type": "TsModuleBlock",
"span": {
"start": 63,
"end": 70,
"ctxt": 0
},
"body": []
}
}
]
}
}
],
"interpreter": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"ctxt": 0
},
"kind": "const",
"declare": true,
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"end": 70,
"ctxt": 0
},
"declare": true,
"declare": false,
"global": false,
"id": {
"type": "Identifier",
Expand Down Expand Up @@ -70,7 +70,7 @@
"ctxt": 0
},
"kind": "const",
"declare": true,
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"end": 62,
"ctxt": 0
},
"declare": true,
"declare": false,
"global": true,
"id": {
"type": "Identifier",
Expand Down Expand Up @@ -69,7 +69,7 @@
"ctxt": 0
},
"kind": "var",
"declare": true,
"declare": false,
"declarations": [
{
"type": "VariableDeclarator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"end": 24,
"ctxt": 0
},
"declare": true,
"declare": false,
"global": false,
"id": {
"type": "Identifier",
Expand Down