Skip to content

Commit

Permalink
Fix parsing of declare (#624)
Browse files Browse the repository at this point in the history
Previously all children in declare context were marked as `declare`. It's wrong, and I fixed to set declare: true only on the exact node
  • Loading branch information
kdy1 committed Feb 4, 2020
1 parent 17043ea commit 42373f9
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 17 deletions.
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

0 comments on commit 42373f9

Please sign in to comment.