From 42373f975bb964f4aaf4c1ad777fc0946b1ee4f5 Mon Sep 17 00:00:00 2001 From: kdy1 Date: Tue, 4 Feb 2020 02:12:55 +0000 Subject: [PATCH] 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 --- ecmascript/parser/src/parser/stmt.rs | 2 +- ecmascript/parser/src/parser/typescript.rs | 37 +++++++--- .../typescript/custom/issue-623/input.ts | 4 ++ .../typescript/custom/issue-623/input.ts.json | 71 +++++++++++++++++++ .../body-declare/input.ts.json | 2 +- .../body-nested-declare/input.ts.json | 4 +- .../global-in-module/input.ts.json | 4 +- .../head-declare/input.ts.json | 2 +- 8 files changed, 109 insertions(+), 17 deletions(-) create mode 100644 ecmascript/parser/tests/typescript/custom/issue-623/input.ts create mode 100644 ecmascript/parser/tests/typescript/custom/issue-623/input.ts.json diff --git a/ecmascript/parser/src/parser/stmt.rs b/ecmascript/parser/src/parser/stmt.rs index f872bbd4258c..d2749a1845e5 100644 --- a/ecmascript/parser/src/parser/stmt.rs +++ b/ecmascript/parser/src/parser/stmt.rs @@ -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, }) diff --git a/ecmascript/parser/src/parser/typescript.rs b/ecmascript/parser/src/parser/typescript.rs index dea17e6ec573..5e9080388464 100644 --- a/ecmascript/parser/src/parser/typescript.rs +++ b/ecmascript/parser/src/parser/typescript.rs @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, } @@ -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) @@ -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 +} diff --git a/ecmascript/parser/tests/typescript/custom/issue-623/input.ts b/ecmascript/parser/tests/typescript/custom/issue-623/input.ts new file mode 100644 index 000000000000..df6845bd8c5c --- /dev/null +++ b/ecmascript/parser/tests/typescript/custom/issue-623/input.ts @@ -0,0 +1,4 @@ +declare module "@dsherret/package" { + namespace packageName { + } +} \ No newline at end of file diff --git a/ecmascript/parser/tests/typescript/custom/issue-623/input.ts.json b/ecmascript/parser/tests/typescript/custom/issue-623/input.ts.json new file mode 100644 index 000000000000..9162e2b0c57f --- /dev/null +++ b/ecmascript/parser/tests/typescript/custom/issue-623/input.ts.json @@ -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 +} diff --git a/ecmascript/parser/tests/typescript/module-namespace/body-declare/input.ts.json b/ecmascript/parser/tests/typescript/module-namespace/body-declare/input.ts.json index 51040770a9f2..28431f34ed60 100644 --- a/ecmascript/parser/tests/typescript/module-namespace/body-declare/input.ts.json +++ b/ecmascript/parser/tests/typescript/module-namespace/body-declare/input.ts.json @@ -42,7 +42,7 @@ "ctxt": 0 }, "kind": "const", - "declare": true, + "declare": false, "declarations": [ { "type": "VariableDeclarator", diff --git a/ecmascript/parser/tests/typescript/module-namespace/body-nested-declare/input.ts.json b/ecmascript/parser/tests/typescript/module-namespace/body-nested-declare/input.ts.json index bbabe17ee74f..6f9e800cb0e8 100644 --- a/ecmascript/parser/tests/typescript/module-namespace/body-nested-declare/input.ts.json +++ b/ecmascript/parser/tests/typescript/module-namespace/body-nested-declare/input.ts.json @@ -41,7 +41,7 @@ "end": 70, "ctxt": 0 }, - "declare": true, + "declare": false, "global": false, "id": { "type": "Identifier", @@ -70,7 +70,7 @@ "ctxt": 0 }, "kind": "const", - "declare": true, + "declare": false, "declarations": [ { "type": "VariableDeclarator", diff --git a/ecmascript/parser/tests/typescript/module-namespace/global-in-module/input.ts.json b/ecmascript/parser/tests/typescript/module-namespace/global-in-module/input.ts.json index b7ff245fcc67..199dc48a768c 100644 --- a/ecmascript/parser/tests/typescript/module-namespace/global-in-module/input.ts.json +++ b/ecmascript/parser/tests/typescript/module-namespace/global-in-module/input.ts.json @@ -40,7 +40,7 @@ "end": 62, "ctxt": 0 }, - "declare": true, + "declare": false, "global": true, "id": { "type": "Identifier", @@ -69,7 +69,7 @@ "ctxt": 0 }, "kind": "var", - "declare": true, + "declare": false, "declarations": [ { "type": "VariableDeclarator", diff --git a/ecmascript/parser/tests/typescript/module-namespace/head-declare/input.ts.json b/ecmascript/parser/tests/typescript/module-namespace/head-declare/input.ts.json index 6d5491be0b72..bfd78c102585 100644 --- a/ecmascript/parser/tests/typescript/module-namespace/head-declare/input.ts.json +++ b/ecmascript/parser/tests/typescript/module-namespace/head-declare/input.ts.json @@ -33,7 +33,7 @@ "end": 24, "ctxt": 0 }, - "declare": true, + "declare": false, "global": false, "id": { "type": "Identifier",