Skip to content

Commit 742b719

Browse files
committed
Fix parsing of declare (#624)
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
1 parent 17043ea commit 742b719

File tree

8 files changed

+109
-17
lines changed

8 files changed

+109
-17
lines changed

ecmascript/parser/src/parser/stmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
653653

654654
Ok(VarDecl {
655655
span: span!(start),
656-
declare: self.ctx().in_declare,
656+
declare: false,
657657
kind,
658658
decls,
659659
})

ecmascript/parser/src/parser/typescript.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
653653

654654
Ok(TsEnumDecl {
655655
span: span!(start),
656-
// TODO(kdy1): Is this correct?
657-
declare: self.ctx().in_declare,
656+
declare: false,
658657
is_const,
659658
id,
660659
members,
@@ -706,7 +705,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
706705

707706
Ok(TsModuleDecl {
708707
span: span!(start),
709-
declare: self.ctx().in_declare,
708+
declare: false,
710709
id: TsModuleName::Ident(id),
711710
body: Some(body),
712711
global: false,
@@ -745,7 +744,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
745744

746745
Ok(TsModuleDecl {
747746
span: span!(start),
748-
declare: self.ctx().in_declare,
747+
declare: false,
749748
id,
750749
global,
751750
body,
@@ -928,7 +927,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
928927
};
929928
Ok(TsInterfaceDecl {
930929
span: span!(start),
931-
declare: self.ctx().in_declare,
930+
declare: false,
932931
id,
933932
type_params,
934933
extends,
@@ -945,7 +944,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
945944
let type_ann = self.expect_then_parse_ts_type(&tok!('='))?;
946945
expect!(';');
947946
Ok(TsTypeAliasDecl {
948-
declare: self.ctx().in_declare,
947+
declare: false,
949948
span: span!(start),
950949
id,
951950
type_params,
@@ -968,7 +967,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
968967
expect!(';');
969968
Ok(TsImportEqualsDecl {
970969
span: span!(start),
971-
declare: self.ctx().in_declare,
970+
declare: false,
972971
id,
973972
is_export,
974973
module_ref,
@@ -1906,7 +1905,7 @@ impl<'a, I: Tokens> Parser<'a, I> {
19061905
TsModuleDecl {
19071906
span: span!(start),
19081907
global,
1909-
declare: self.ctx().in_declare,
1908+
declare: false,
19101909
id,
19111910
body,
19121911
}
@@ -1990,14 +1989,17 @@ impl<'a, I: Tokens> Parser<'a, I> {
19901989
if is!("global") {
19911990
return p
19921991
.parse_ts_ambient_external_module_decl(start)
1993-
.map(From::from)
1992+
.map(Decl::from)
1993+
.map(make_decl_declare)
19941994
.map(Some);
19951995
} else if is!(IdentName) {
19961996
let value = match *cur!(true)? {
19971997
Token::Word(ref w) => w.clone().into(),
19981998
_ => unreachable!(),
19991999
};
2000-
return p.parse_ts_decl(start, decorators, value, /* next */ true);
2000+
return p
2001+
.parse_ts_decl(start, decorators, value, /* next */ true)
2002+
.map(|v| v.map(make_decl_declare));
20012003
}
20022004

20032005
Ok(None)
@@ -2303,3 +2305,18 @@ enum SignatureParsingMode {
23032305
TSCallSignatureDeclaration,
23042306
TSConstructSignatureDeclaration,
23052307
}
2308+
2309+
/// Mark as declare
2310+
fn make_decl_declare(mut decl: Decl) -> Decl {
2311+
match decl {
2312+
Decl::Class(ref mut c) => c.declare = true,
2313+
Decl::Fn(ref mut f) => f.declare = true,
2314+
Decl::Var(ref mut v) => v.declare = true,
2315+
Decl::TsInterface(ref mut i) => i.declare = true,
2316+
Decl::TsTypeAlias(ref mut a) => a.declare = true,
2317+
Decl::TsEnum(ref mut e) => e.declare = true,
2318+
Decl::TsModule(ref mut m) => m.declare = true,
2319+
}
2320+
2321+
decl
2322+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module "@dsherret/package" {
2+
namespace packageName {
3+
}
4+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"type": "Module",
3+
"span": {
4+
"start": 0,
5+
"end": 72,
6+
"ctxt": 0
7+
},
8+
"body": [
9+
{
10+
"type": "TsModuleDeclaration",
11+
"span": {
12+
"start": 0,
13+
"end": 72,
14+
"ctxt": 0
15+
},
16+
"declare": true,
17+
"global": false,
18+
"id": {
19+
"type": "StringLiteral",
20+
"span": {
21+
"start": 15,
22+
"end": 34,
23+
"ctxt": 0
24+
},
25+
"value": "@dsherret/package",
26+
"hasEscape": false
27+
},
28+
"body": {
29+
"type": "TsModuleBlock",
30+
"span": {
31+
"start": 35,
32+
"end": 72,
33+
"ctxt": 0
34+
},
35+
"body": [
36+
{
37+
"type": "TsModuleDeclaration",
38+
"span": {
39+
"start": 41,
40+
"end": 70,
41+
"ctxt": 0
42+
},
43+
"declare": false,
44+
"global": false,
45+
"id": {
46+
"type": "Identifier",
47+
"span": {
48+
"start": 51,
49+
"end": 62,
50+
"ctxt": 0
51+
},
52+
"value": "packageName",
53+
"typeAnnotation": null,
54+
"optional": false
55+
},
56+
"body": {
57+
"type": "TsModuleBlock",
58+
"span": {
59+
"start": 63,
60+
"end": 70,
61+
"ctxt": 0
62+
},
63+
"body": []
64+
}
65+
}
66+
]
67+
}
68+
}
69+
],
70+
"interpreter": null
71+
}

ecmascript/parser/tests/typescript/module-namespace/body-declare/input.ts.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"ctxt": 0
4343
},
4444
"kind": "const",
45-
"declare": true,
45+
"declare": false,
4646
"declarations": [
4747
{
4848
"type": "VariableDeclarator",

ecmascript/parser/tests/typescript/module-namespace/body-nested-declare/input.ts.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"end": 70,
4242
"ctxt": 0
4343
},
44-
"declare": true,
44+
"declare": false,
4545
"global": false,
4646
"id": {
4747
"type": "Identifier",
@@ -70,7 +70,7 @@
7070
"ctxt": 0
7171
},
7272
"kind": "const",
73-
"declare": true,
73+
"declare": false,
7474
"declarations": [
7575
{
7676
"type": "VariableDeclarator",

ecmascript/parser/tests/typescript/module-namespace/global-in-module/input.ts.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"end": 62,
4141
"ctxt": 0
4242
},
43-
"declare": true,
43+
"declare": false,
4444
"global": true,
4545
"id": {
4646
"type": "Identifier",
@@ -69,7 +69,7 @@
6969
"ctxt": 0
7070
},
7171
"kind": "var",
72-
"declare": true,
72+
"declare": false,
7373
"declarations": [
7474
{
7575
"type": "VariableDeclarator",

ecmascript/parser/tests/typescript/module-namespace/head-declare/input.ts.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"end": 24,
3434
"ctxt": 0
3535
},
36-
"declare": true,
36+
"declare": false,
3737
"global": false,
3838
"id": {
3939
"type": "Identifier",

0 commit comments

Comments
 (0)