From d5f68a8aefb38e85994ccfdc8c942067d7e9c172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 18:17:57 +0900 Subject: [PATCH 01/56] Add a test for #1477 --- tests/issue-1477/case1/input/index.js | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tests/issue-1477/case1/input/index.js diff --git a/tests/issue-1477/case1/input/index.js b/tests/issue-1477/case1/input/index.js new file mode 100644 index 000000000000..3c5341897011 --- /dev/null +++ b/tests/issue-1477/case1/input/index.js @@ -0,0 +1,3 @@ +async function f(a, b) { + const [{ a: a_ = 1 }] = JSON.parse(b) +} \ No newline at end of file From 69c76061abc0c494b3a7b733cfe1713cc60f8af8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 18:19:22 +0900 Subject: [PATCH 02/56] Bump version (related to other prs) --- ecmascript/transforms/react/Cargo.toml | 2 +- ecmascript/transforms/typescript/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ecmascript/transforms/react/Cargo.toml b/ecmascript/transforms/react/Cargo.toml index 6893d67cc55c..083f44a5de0a 100644 --- a/ecmascript/transforms/react/Cargo.toml +++ b/ecmascript/transforms/react/Cargo.toml @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"] license = "Apache-2.0/MIT" name = "swc_ecma_transforms_react" repository = "https://github.com/swc-project/swc.git" -version = "0.12.0" +version = "0.12.1" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/ecmascript/transforms/typescript/Cargo.toml b/ecmascript/transforms/typescript/Cargo.toml index e30b6e812780..1bf95ff9009b 100644 --- a/ecmascript/transforms/typescript/Cargo.toml +++ b/ecmascript/transforms/typescript/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" license = "Apache-2.0/MIT" name = "swc_ecma_transforms_typescript" repository = "https://github.com/swc-project/swc.git" -version = "0.12.0" +version = "0.12.1" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] From e6210f0b9f499905e1c48638703a652073552781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 18:21:29 +0900 Subject: [PATCH 03/56] Fix lints --- ecmascript/transforms/react/src/refresh/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecmascript/transforms/react/src/refresh/mod.rs b/ecmascript/transforms/react/src/refresh/mod.rs index ec66d1986221..58c2fa050ba3 100644 --- a/ecmascript/transforms/react/src/refresh/mod.rs +++ b/ecmascript/transforms/react/src/refresh/mod.rs @@ -673,7 +673,7 @@ impl Fold for Refresh { Expr::Arrow(func) } } - Expr::Call(mut call) => Expr::Call(call), + Expr::Call(call) => Expr::Call(call), _ => n, } } From 975c3e3f2c4f694df8b7404f5943d805d598200e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 18:22:56 +0900 Subject: [PATCH 04/56] Move the test for #1477 --- tests/{ => fixture}/issue-1477/case1/input/index.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{ => fixture}/issue-1477/case1/input/index.js (100%) diff --git a/tests/issue-1477/case1/input/index.js b/tests/fixture/issue-1477/case1/input/index.js similarity index 100% rename from tests/issue-1477/case1/input/index.js rename to tests/fixture/issue-1477/case1/input/index.js From 3a33b18882864fc735783bfc8d29dbd01b555fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 20:32:14 +0900 Subject: [PATCH 05/56] Add a test for #1477 --- .../compat/tests/es2015_destructuring.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ecmascript/transforms/compat/tests/es2015_destructuring.rs b/ecmascript/transforms/compat/tests/es2015_destructuring.rs index eedca6fbf510..2fea18841cff 100644 --- a/ecmascript/transforms/compat/tests/es2015_destructuring.rs +++ b/ecmascript/transforms/compat/tests/es2015_destructuring.rs @@ -1488,3 +1488,18 @@ test!( "var ref; foo((ref = [1, 2], a = ref[0], b = ref[1], ref));" ); + +test!( + syntax(), + |_| tr(), + issue_1477_1, + " + const [ { a: a_ = 1 } ] = b + ", + " + const _b = b, + _b2 = _slicedToArray(_b, 1), + _b2$0$a = _b2[0].a, + a_ = _b2$0$a === void 0 ? 1 : _b2$0$a; + " +); From af847a73d6f93735f4b6e7ebb6c88767b0dfd81c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 20:34:10 +0900 Subject: [PATCH 06/56] One more --- .../compat/tests/es2015_destructuring.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ecmascript/transforms/compat/tests/es2015_destructuring.rs b/ecmascript/transforms/compat/tests/es2015_destructuring.rs index 2fea18841cff..53673ac30b0a 100644 --- a/ecmascript/transforms/compat/tests/es2015_destructuring.rs +++ b/ecmascript/transforms/compat/tests/es2015_destructuring.rs @@ -1503,3 +1503,18 @@ test!( a_ = _b2$0$a === void 0 ? 1 : _b2$0$a; " ); + +test!( + syntax(), + |_| tr(), + issue_1477_2, + " + async function f(a, b) { + const [ { a: a_ = 1 } ] = JSON.parse(b) + } + ", + " + var _b, _b$0$a; + _b = _slicedToArray(b, 1), _b$0$a = _b[0].a, a_ = _b$0$a === void 0 ? 1 : _b$0$a; + " +); From 20df84e53d98e623070e868e65186665d8e81ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 20:38:41 +0900 Subject: [PATCH 07/56] Add a test for #1477 --- .../compat/tests/es2015_destructuring.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ecmascript/transforms/compat/tests/es2015_destructuring.rs b/ecmascript/transforms/compat/tests/es2015_destructuring.rs index 53673ac30b0a..7be1a4c68e83 100644 --- a/ecmascript/transforms/compat/tests/es2015_destructuring.rs +++ b/ecmascript/transforms/compat/tests/es2015_destructuring.rs @@ -1518,3 +1518,18 @@ test!( _b = _slicedToArray(b, 1), _b$0$a = _b[0].a, a_ = _b$0$a === void 0 ? 1 : _b$0$a; " ); + +test!( + syntax(), + |_| tr(), + issue_1477_3, + " + const [ a = 1 ] = b + ", + " + var _b = b, + _b2 = _slicedToArray(_b, 1), + _b2$ = _b2[0], + a = _b2$ === void 0 ? 1 : _b2$; + " +); From 9d2312da838cf81cef5ee3182c810229d744e338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 20:42:59 +0900 Subject: [PATCH 08/56] Update test refs for #1477 --- .../transforms/compat/tests/es2015_destructuring.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ecmascript/transforms/compat/tests/es2015_destructuring.rs b/ecmascript/transforms/compat/tests/es2015_destructuring.rs index 7be1a4c68e83..85000d56bb53 100644 --- a/ecmascript/transforms/compat/tests/es2015_destructuring.rs +++ b/ecmascript/transforms/compat/tests/es2015_destructuring.rs @@ -1497,10 +1497,7 @@ test!( const [ { a: a_ = 1 } ] = b ", " - const _b = b, - _b2 = _slicedToArray(_b, 1), - _b2$0$a = _b2[0].a, - a_ = _b2$0$a === void 0 ? 1 : _b2$0$a; + const ref = b[0], tmp = ref.a, a_ = tmp === void 0 ? 1 : tmp; " ); @@ -1514,8 +1511,9 @@ test!( } ", " - var _b, _b$0$a; - _b = _slicedToArray(b, 1), _b$0$a = _b[0].a, a_ = _b$0$a === void 0 ? 1 : _b$0$a; + async function f(a, b) { + const ref = JSON.parse(b), ref1 = ref[0], tmp = ref1.a, a_ = tmp === void 0 ? 1 : tmp; + } " ); From eff5f5b766547798345adfbc99069cd3249e90be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 20:49:56 +0900 Subject: [PATCH 09/56] Add a test for #1477 --- .../compat/tests/es2015_destructuring.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ecmascript/transforms/compat/tests/es2015_destructuring.rs b/ecmascript/transforms/compat/tests/es2015_destructuring.rs index 85000d56bb53..ccc002030213 100644 --- a/ecmascript/transforms/compat/tests/es2015_destructuring.rs +++ b/ecmascript/transforms/compat/tests/es2015_destructuring.rs @@ -1531,3 +1531,21 @@ test!( a = _b2$ === void 0 ? 1 : _b2$; " ); + +test!( + syntax(), + |_| tr(), + issue_1477_4, + " + [ a = 1 ] = b + ", + " + var _b = b; + + var _b2 = _slicedToArray(_b, 1); + + var _b2$ = _b2[0]; + a = _b2$ === void 0 ? 1 : _b2$; + _b; + " +); From c8d0c483b6e66ceac3c88e86ddb85ce3abc13551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:02:21 +0900 Subject: [PATCH 10/56] Fix #1477 --- .../compat/src/es2015/destructuring.rs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ecmascript/transforms/compat/src/es2015/destructuring.rs b/ecmascript/transforms/compat/src/es2015/destructuring.rs index 831f9d02378f..8b47b9ce4803 100644 --- a/ecmascript/transforms/compat/src/es2015/destructuring.rs +++ b/ecmascript/transforms/compat/src/es2015/destructuring.rs @@ -807,7 +807,30 @@ impl Fold for AssignFolder { exprs, }) } - Pat::Assign(pat) => unimplemented!("assignment pattern {:?}", pat), + Pat::Assign(pat) => { + let ref_ident = make_ref_ident(self.c, &mut self.vars, None); + + let mut exprs = vec![]; + + exprs.push(Box::new(Expr::Assign(AssignExpr { + span, + left: PatOrExpr::Pat(Box::new(Pat::Ident(ref_ident.clone().into()))), + op: op!("="), + right: pat.right, + }))); + + exprs.push(Box::new(Expr::Assign(AssignExpr { + span, + left: PatOrExpr::Pat(pat.left), + op: op!("="), + right: Box::new(Expr::Ident(ref_ident)), + }))); + + Expr::Seq(SeqExpr { + span: DUMMY_SP, + exprs, + }) + } Pat::Rest(pat) => unimplemented!("rest pattern {:?}", pat), Pat::Invalid(..) => unreachable!(), From b372b38f59bddf60e2e654c48296b2c64c40b9db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:02:43 +0900 Subject: [PATCH 11/56] Update test refs for #1477 --- .../fixture/issue-1477/case1/output/index.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/fixture/issue-1477/case1/output/index.js diff --git a/tests/fixture/issue-1477/case1/output/index.js b/tests/fixture/issue-1477/case1/output/index.js new file mode 100644 index 000000000000..6b4b297437ba --- /dev/null +++ b/tests/fixture/issue-1477/case1/output/index.js @@ -0,0 +1,51 @@ +function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { + try { + var info = gen[key](arg); + var value = info.value; + } catch (error) { + reject(error); + return; + } + if (info.done) { + resolve(value); + } else { + Promise.resolve(value).then(_next, _throw); + } +} +function _asyncToGenerator(fn) { + return function() { + var self = this, args = arguments; + return new Promise(function(resolve, reject) { + var gen = fn.apply(self, args); + function _next(value) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); + } + function _throw(err) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); + } + _next(undefined); + }); + }; +} +var regeneratorRuntime = require("regenerator-runtime"); +var _marked = regeneratorRuntime.mark(_f); +function _f() { + _f = _asyncToGenerator(regeneratorRuntime.mark(function _callee(a, b) { + var a_; + return regeneratorRuntime.wrap(function _callee$(_ctx) { + var ref; + while(1)switch(_ctx.prev = _ctx.next){ + case 0: + var ref1, ref2; + ref1 = JSON.parse(b), ref2 = ref1[0], ref = 1, a_ = ref, ref1; + case 1: + case "end": + return _ctx.stop(); + } + }, _callee); + })); + return _f.apply(this, arguments); +} +function f(a, b) { + return _f.apply(this, arguments); +} From e468546c983a1db95bacc4e131a8f77c8f44d6f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:03:34 +0900 Subject: [PATCH 12/56] Update test refs --- .../compat/tests/es2015_destructuring.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/ecmascript/transforms/compat/tests/es2015_destructuring.rs b/ecmascript/transforms/compat/tests/es2015_destructuring.rs index ccc002030213..3f4e5e084293 100644 --- a/ecmascript/transforms/compat/tests/es2015_destructuring.rs +++ b/ecmascript/transforms/compat/tests/es2015_destructuring.rs @@ -1525,10 +1525,7 @@ test!( const [ a = 1 ] = b ", " - var _b = b, - _b2 = _slicedToArray(_b, 1), - _b2$ = _b2[0], - a = _b2$ === void 0 ? 1 : _b2$; + const tmp = b[0], a = tmp === void 0 ? 1 : tmp; " ); @@ -1540,12 +1537,7 @@ test!( [ a = 1 ] = b ", " - var _b = b; - - var _b2 = _slicedToArray(_b, 1); - - var _b2$ = _b2[0]; - a = _b2$ === void 0 ? 1 : _b2$; - _b; + var ref, ref1; + ref = b, ref1 = ref[0], a = ref1 === void 0 ? 1 : ref1, ref; " ); From 4a2643e552ab7908a96799a688e92bef9483d43b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:05:21 +0900 Subject: [PATCH 13/56] Add a test for #1449 --- tests/fixture/issue-1449/case1/index.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tests/fixture/issue-1449/case1/index.js diff --git a/tests/fixture/issue-1449/case1/index.js b/tests/fixture/issue-1449/case1/index.js new file mode 100644 index 000000000000..fd37a6c96fe2 --- /dev/null +++ b/tests/fixture/issue-1449/case1/index.js @@ -0,0 +1,5 @@ +async function foo() { + const { + bar: { } = {} + } = baz; +} \ No newline at end of file From 5c7dfbabbbc63d175d4aac7e7339abd035c0de6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:07:50 +0900 Subject: [PATCH 14/56] Move --- tests/fixture/issue-1449/case1/{ => input}/index.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/fixture/issue-1449/case1/{ => input}/index.js (100%) diff --git a/tests/fixture/issue-1449/case1/index.js b/tests/fixture/issue-1449/case1/input/index.js similarity index 100% rename from tests/fixture/issue-1449/case1/index.js rename to tests/fixture/issue-1449/case1/input/index.js From e7ab44e170003e99b6e41b8d25b375ccfff1f1a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:08:32 +0900 Subject: [PATCH 15/56] Update test refs --- .../fixture/issue-1449/case1/output/index.js | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/fixture/issue-1449/case1/output/index.js diff --git a/tests/fixture/issue-1449/case1/output/index.js b/tests/fixture/issue-1449/case1/output/index.js new file mode 100644 index 000000000000..fe3124ee7cac --- /dev/null +++ b/tests/fixture/issue-1449/case1/output/index.js @@ -0,0 +1,52 @@ +function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { + try { + var info = gen[key](arg); + var value = info.value; + } catch (error) { + reject(error); + return; + } + if (info.done) { + resolve(value); + } else { + Promise.resolve(value).then(_next, _throw); + } +} +function _asyncToGenerator(fn) { + return function() { + var self = this, args = arguments; + return new Promise(function(resolve, reject) { + var gen = fn.apply(self, args); + function _next(value) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); + } + function _throw(err) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); + } + _next(undefined); + }); + }; +} +var regeneratorRuntime = require("regenerator-runtime"); +var _marked = regeneratorRuntime.mark(_foo); +function _foo() { + _foo = _asyncToGenerator(regeneratorRuntime.mark(function _callee() { + return regeneratorRuntime.wrap(function _callee$(_ctx) { + var ref; + while(1)switch(_ctx.prev = _ctx.next){ + case 0: + var ref1; + var ref2; + ref1 = baz, ref = { + }, ref2 = ref, ref1; + case 1: + case "end": + return _ctx.stop(); + } + }, _callee); + })); + return _foo.apply(this, arguments); +} +function foo() { + return _foo.apply(this, arguments); +} From e5082dc6761bbdaf7b11463b0167cf5218d98b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:09:41 +0900 Subject: [PATCH 16/56] Add a test for #1448 --- tests/issue-1448/case1/input/.swcrc | 12 ++++++++++++ tests/issue-1448/case1/input/index.ts | 2 ++ 2 files changed, 14 insertions(+) create mode 100644 tests/issue-1448/case1/input/.swcrc create mode 100644 tests/issue-1448/case1/input/index.ts diff --git a/tests/issue-1448/case1/input/.swcrc b/tests/issue-1448/case1/input/.swcrc new file mode 100644 index 000000000000..13bc04c6fd16 --- /dev/null +++ b/tests/issue-1448/case1/input/.swcrc @@ -0,0 +1,12 @@ +{ + "env": { + "coreJs": 3 + }, + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": true + }, + "loose": true + } +} diff --git a/tests/issue-1448/case1/input/index.ts b/tests/issue-1448/case1/input/index.ts new file mode 100644 index 000000000000..e325eccfc42d --- /dev/null +++ b/tests/issue-1448/case1/input/index.ts @@ -0,0 +1,2 @@ +import F = require("yaml") +console.log(F) \ No newline at end of file From a79ed66d9d292217d59c083086a7b8a429f1a7c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:10:47 +0900 Subject: [PATCH 17/56] Add a test for #1448 --- ecmascript/transforms/typescript/tests/strip.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ecmascript/transforms/typescript/tests/strip.rs b/ecmascript/transforms/typescript/tests/strip.rs index 7befdc3944ce..9915a4d0226e 100644 --- a/ecmascript/transforms/typescript/tests/strip.rs +++ b/ecmascript/transforms/typescript/tests/strip.rs @@ -3880,3 +3880,15 @@ to!( import { Test } from 'test'; " ); + +to!( + issue_1448_1, + " + import F = require('yaml') + console.log(F) + ", + " + import F from 'yaml'; + console.log(F) + " +); From 19e018fc7991c96d47f0f6334b5a9f5dad4262e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:33:13 +0900 Subject: [PATCH 18/56] Fix #1448 --- ecmascript/transforms/typescript/src/strip.rs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ecmascript/transforms/typescript/src/strip.rs b/ecmascript/transforms/typescript/src/strip.rs index 73cc2d1f5e8b..c67488628109 100644 --- a/ecmascript/transforms/typescript/src/strip.rs +++ b/ecmascript/transforms/typescript/src/strip.rs @@ -7,6 +7,7 @@ use swc_common::{util::move_map::MoveMap, Span, Spanned, SyntaxContext, DUMMY_SP use swc_ecma_ast::*; use swc_ecma_transforms_base::ext::MapWithMut; use swc_ecma_utils::private_ident; +use swc_ecma_utils::quote_ident; use swc_ecma_utils::var::VarCollector; use swc_ecma_utils::ExprFactory; use swc_ecma_utils::{constructor::inject_after_super, default_constructor}; @@ -1660,6 +1661,33 @@ impl VisitMut for Strip { continue } + ModuleItem::ModuleDecl(ModuleDecl::TsImportEquals(TsImportEqualsDecl { + span, + declare: false, + is_export: false, + id, + module_ref: + TsModuleRef::TsExternalModuleRef(TsExternalModuleRef { span: _, expr }), + })) => { + let default = VarDeclarator { + span: DUMMY_SP, + name: Pat::Ident(id.into()), + init: Some(Box::new(Expr::Call(CallExpr { + span: DUMMY_SP, + callee: quote_ident!("require").as_callee(), + args: vec![expr.as_arg()], + type_args: None, + }))), + definite: false, + }; + stmts.push(ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl { + span, + kind: VarDeclKind::Const, + declare: false, + decls: vec![default], + })))) + } + // Always strip type only import / exports ModuleItem::Stmt(Stmt::Empty(..)) | ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl { From 9061a32320a1a9f8f1f854df0f11d0500ae63677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:33:22 +0900 Subject: [PATCH 19/56] Update test refsx --- ecmascript/transforms/typescript/tests/strip.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecmascript/transforms/typescript/tests/strip.rs b/ecmascript/transforms/typescript/tests/strip.rs index 9915a4d0226e..8705f57eb016 100644 --- a/ecmascript/transforms/typescript/tests/strip.rs +++ b/ecmascript/transforms/typescript/tests/strip.rs @@ -3888,7 +3888,7 @@ to!( console.log(F) ", " - import F from 'yaml'; + const F = require('yaml'); console.log(F) " ); From e77debd81c809cb4e72dbd1a0584f102f4452489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:35:31 +0900 Subject: [PATCH 20/56] Add a test for #1455 --- tests/fixture/issue-1455/case1/input/.swcrc | 15 +++++++++++++++ tests/fixture/issue-1455/case1/input/index.ts | 10 ++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/fixture/issue-1455/case1/input/.swcrc create mode 100644 tests/fixture/issue-1455/case1/input/index.ts diff --git a/tests/fixture/issue-1455/case1/input/.swcrc b/tests/fixture/issue-1455/case1/input/.swcrc new file mode 100644 index 000000000000..45a6553f67ac --- /dev/null +++ b/tests/fixture/issue-1455/case1/input/.swcrc @@ -0,0 +1,15 @@ +{ + "exclude": [".*.js$", ".*.map$", ".*.test.ts$"], + "jsc": { + "target": "es2016", + "parser": { + "syntax": "typescript", + "tsx": false, + "decorators": true + } + }, + "minify": false, + "module": { + "type": "commonjs" + } +} diff --git a/tests/fixture/issue-1455/case1/input/index.ts b/tests/fixture/issue-1455/case1/input/index.ts new file mode 100644 index 000000000000..545f76e635b1 --- /dev/null +++ b/tests/fixture/issue-1455/case1/input/index.ts @@ -0,0 +1,10 @@ +const SampleData = typedModel("SampleVideo", VideosSchema, undefined, undefined, { + byPlatform: async function (platform: string) { + const result = await this.find({ platform: { $eq: platform } }); + return result; + }, +}); + +SampleData.byPlatform("youtube").then((res) => { + console.info(res); +}); \ No newline at end of file From ab8073d51e1ebf7c65425b7c7886fd0b86ce4eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:45:18 +0900 Subject: [PATCH 21/56] Add a test for #1455 --- tests/fixture/issue-1455/case2/input/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/fixture/issue-1455/case2/input/index.ts diff --git a/tests/fixture/issue-1455/case2/input/index.ts b/tests/fixture/issue-1455/case2/input/index.ts new file mode 100644 index 000000000000..fd2d85618fa1 --- /dev/null +++ b/tests/fixture/issue-1455/case2/input/index.ts @@ -0,0 +1,11 @@ +const obj = { + find({ platform }) { + return { platform } + }, + byPlatform: async function (platform) { + const result = await this.find({ platform: { $eq: platform } }); + return result; + }, +}; + +console.log(obj.byPlatform('foo')); \ No newline at end of file From e56c0bba63d0942d4c413aed8e275af75cf50012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:47:20 +0900 Subject: [PATCH 22/56] One more --- tests/fixture/issue-1455/case3/input/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 tests/fixture/issue-1455/case3/input/index.ts diff --git a/tests/fixture/issue-1455/case3/input/index.ts b/tests/fixture/issue-1455/case3/input/index.ts new file mode 100644 index 000000000000..b60561307d67 --- /dev/null +++ b/tests/fixture/issue-1455/case3/input/index.ts @@ -0,0 +1,11 @@ +const obj = { + find({ platform }) { + return { platform } + }, + byPlatform: function (platform) { + const result = await this.find({ platform: { $eq: platform } }); + return result; + }, +}; + +console.log(obj.byPlatform('foo')); \ No newline at end of file From 66a13a2c5bb8362399035d88c3116c2369e039b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:50:55 +0900 Subject: [PATCH 23/56] Update test refs --- .../fixture/issue-1455/case2/output/index.ts | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/fixture/issue-1455/case2/output/index.ts diff --git a/tests/fixture/issue-1455/case2/output/index.ts b/tests/fixture/issue-1455/case2/output/index.ts new file mode 100644 index 000000000000..f323853e0da0 --- /dev/null +++ b/tests/fixture/issue-1455/case2/output/index.ts @@ -0,0 +1,71 @@ +function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { + try { + var info = gen[key](arg); + var value = info.value; + } catch (error) { + reject(error); + return; + } + if (info.done) { + resolve(value); + } else { + Promise.resolve(value).then(_next, _throw); + } +} +function _asyncToGenerator(fn) { + return function () { + var self = this, args = arguments; + return new Promise(function (resolve, reject) { + var gen = fn.apply(self, args); + function _next(value) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); + } + function _throw(err) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); + } + _next(undefined); + }); + }; +} +var regeneratorRuntime = require("regenerator-runtime"); +var obj = { + find: function (param) { + var platform = param.platform; + return { + platform: platform + }; + }, + byPlatform: function (platform) { + var _byPlatform = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(platform) { + var result; + return regeneratorRuntime.wrap(function _callee$(_context) { + while (1) { + switch (_context.prev = _context.next) { + case 0: + _context.next = 2; + return this.find({ + platform: { + $eq: platform + } + }); + + case 2: + result = _context.sent; + return _context.abrupt("return", result); + + case 4: + case "end": + return _context.stop(); + } + } + }, _callee, this); + })); + + function byPlatform(_x) { + return _byPlatform.apply(this, arguments); + } + + return byPlatform; + }() +}; +console.log(obj.byPlatform('foo')); From 1600308d1e281bca42ad9cf8babbeaf5b380cfb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:52:51 +0900 Subject: [PATCH 24/56] Update a test for #1455 --- tests/fixture/issue-1455/case2/input/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixture/issue-1455/case2/input/index.ts b/tests/fixture/issue-1455/case2/input/index.ts index fd2d85618fa1..e3808e6d5d8b 100644 --- a/tests/fixture/issue-1455/case2/input/index.ts +++ b/tests/fixture/issue-1455/case2/input/index.ts @@ -8,4 +8,4 @@ const obj = { }, }; -console.log(obj.byPlatform('foo')); \ No newline at end of file +obj.byPlatform('foo').then(v => console.log(v)) From 8ad835068216f85545f24e6d979bc2e2b36ecb20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:53:06 +0900 Subject: [PATCH 25/56] Update test refs --- .../fixture/issue-1455/case2/output/index.ts | 49 +++++++++---------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/tests/fixture/issue-1455/case2/output/index.ts b/tests/fixture/issue-1455/case2/output/index.ts index f323853e0da0..fa0b543ebeba 100644 --- a/tests/fixture/issue-1455/case2/output/index.ts +++ b/tests/fixture/issue-1455/case2/output/index.ts @@ -36,36 +36,31 @@ var obj = { }; }, byPlatform: function (platform) { - var _byPlatform = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(platform) { + var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(platform) { var result; - return regeneratorRuntime.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - _context.next = 2; - return this.find({ - platform: { - $eq: platform - } - }); - - case 2: - result = _context.sent; - return _context.abrupt("return", result); - - case 4: - case "end": - return _context.stop(); - } + return regeneratorRuntime.wrap(function _callee$(_ctx) { + while (1) switch (_ctx.prev = _ctx.next) { + case 0: + _ctx.next = 2; + return this.find({ + platform: { + $eq: platform + } + }); + case 2: + result = _ctx.sent; + return _ctx.abrupt("return", result); + case 4: + case "end": + return _ctx.stop(); } }, _callee, this); })); - - function byPlatform(_x) { - return _byPlatform.apply(this, arguments); - } - - return byPlatform; + return function () { + return _ref.apply(this, arguments); + }; }() }; -console.log(obj.byPlatform('foo')); +obj.byPlatform('foo').then(function (v) { + return console.log(v); +}); From f995bda67ea2ea0f9c1ea68930c4d0d3affc38e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 21:59:24 +0900 Subject: [PATCH 26/56] Fix #1455? --- .../compat/src/es2017/async_to_generator.rs | 63 +++++++++++++------ 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/ecmascript/transforms/compat/src/es2017/async_to_generator.rs b/ecmascript/transforms/compat/src/es2017/async_to_generator.rs index df71ed0bf1cf..0d8f4038c1ed 100644 --- a/ecmascript/transforms/compat/src/es2017/async_to_generator.rs +++ b/ecmascript/transforms/compat/src/es2017/async_to_generator.rs @@ -42,6 +42,7 @@ pub fn async_to_generator() -> impl Fold { struct AsyncToGenerator; struct Actual { + in_object_prop: bool, extra_stmts: Vec, } @@ -70,6 +71,7 @@ impl AsyncToGenerator { for stmt in stmts { let mut actual = Actual { + in_object_prop: false, extra_stmts: vec![], }; let stmt = stmt.fold_with(&mut actual); @@ -98,10 +100,13 @@ impl Fold for Actual { let mut folder = MethodFolder { vars: vec![] }; m.function.params.clear(); let function = m.function.fold_children_with(&mut folder); - let expr = make_fn_ref(FnExpr { - ident: None, - function, - }); + let expr = make_fn_ref( + FnExpr { + ident: None, + function, + }, + self.in_object_prop, + ); let hoisted_super = if folder.vars.is_empty() { None @@ -259,12 +264,12 @@ impl Fold for Actual { }; if !used_this { - return make_fn_ref(fn_expr); + return make_fn_ref(fn_expr, false); } return Expr::Call(CallExpr { span, - callee: make_fn_ref(fn_expr) + callee: make_fn_ref(fn_expr, false) .make_member(quote_ident!("bind")) .as_callee(), args: vec![ThisExpr { span: DUMMY_SP }.as_arg()], @@ -324,6 +329,18 @@ impl Fold for Actual { declare: false, } } + + fn fold_prop(&mut self, n: Prop) -> Prop { + let old = self.in_object_prop; + self.in_object_prop = true; + + let n = n.fold_children_with(self); + + self.in_object_prop = old; + + n + } + fn fold_method_prop(&mut self, prop: MethodProp) -> MethodProp { let prop = prop.fold_children_with(self); @@ -332,13 +349,16 @@ impl Fold for Actual { } let params = prop.function.params; - let fn_ref = make_fn_ref(FnExpr { - ident: None, - function: Function { - params: vec![], - ..prop.function + let fn_ref = make_fn_ref( + FnExpr { + ident: None, + function: Function { + params: vec![], + ..prop.function + }, }, - }); + true, + ); let fn_ref = Expr::Call(CallExpr { span: DUMMY_SP, callee: fn_ref.as_callee(), @@ -660,7 +680,7 @@ impl Actual { .fold_children_with(self); } - return make_fn_ref(callee); + return make_fn_ref(callee, self.in_object_prop); } fn fold_fn(&mut self, raw_ident: Option, f: Function, is_decl: bool) -> Function { if f.body.is_none() { @@ -693,10 +713,13 @@ impl Actual { let ident = raw_ident.clone().unwrap_or_else(|| quote_ident!("ref")); let real_fn_ident = private_ident!(ident.span, format!("_{}", ident.sym)); - let right = make_fn_ref(FnExpr { - ident: None, - function: f, - }); + let right = make_fn_ref( + FnExpr { + ident: None, + function: f, + }, + self.in_object_prop, + ); if is_decl { let real_fn = FnDecl { @@ -817,7 +840,7 @@ impl Actual { /// Creates /// /// `_asyncToGenerator(function*() {})` from `async function() {}`; -fn make_fn_ref(mut expr: FnExpr) -> Expr { +fn make_fn_ref(mut expr: FnExpr, should_not_bind_this: bool) -> Expr { struct AwaitToYield; macro_rules! noop { @@ -859,8 +882,8 @@ fn make_fn_ref(mut expr: FnExpr) -> Expr { let span = expr.span(); - let contains_this = contains_this_expr(&expr.function.body); - let expr = if contains_this { + let should_bind_this = !should_not_bind_this && contains_this_expr(&expr.function.body); + let expr = if should_bind_this { Expr::Call(CallExpr { span: DUMMY_SP, callee: expr.make_member(quote_ident!("bind")).as_callee(), From f4463ab93ef382ade6e536f27cd8128bcd4c0e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 22:01:12 +0900 Subject: [PATCH 27/56] Update test refs --- tests/fixture/issue-1455/case2/output/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/fixture/issue-1455/case2/output/index.ts b/tests/fixture/issue-1455/case2/output/index.ts index fa0b543ebeba..c3bbf5944db6 100644 --- a/tests/fixture/issue-1455/case2/output/index.ts +++ b/tests/fixture/issue-1455/case2/output/index.ts @@ -13,9 +13,9 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { } } function _asyncToGenerator(fn) { - return function () { + return function() { var self = this, args = arguments; - return new Promise(function (resolve, reject) { + return new Promise(function(resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); @@ -29,17 +29,17 @@ function _asyncToGenerator(fn) { } var regeneratorRuntime = require("regenerator-runtime"); var obj = { - find: function (param) { + find: function(param) { var platform = param.platform; return { platform: platform }; }, - byPlatform: function (platform) { + byPlatform: function(platform) { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(platform) { var result; return regeneratorRuntime.wrap(function _callee$(_ctx) { - while (1) switch (_ctx.prev = _ctx.next) { + while(1)switch(_ctx.prev = _ctx.next){ case 0: _ctx.next = 2; return this.find({ @@ -56,11 +56,11 @@ var obj = { } }, _callee, this); })); - return function () { + return function() { return _ref.apply(this, arguments); }; }() }; -obj.byPlatform('foo').then(function (v) { +obj.byPlatform('foo').then(function(v) { return console.log(v); }); From 33f2d01971904e02f1335f4c4dede8a04d0d9410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 22:01:21 +0900 Subject: [PATCH 28/56] Remove wrong test for #1455 --- tests/fixture/issue-1455/case3/input/index.ts | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 tests/fixture/issue-1455/case3/input/index.ts diff --git a/tests/fixture/issue-1455/case3/input/index.ts b/tests/fixture/issue-1455/case3/input/index.ts deleted file mode 100644 index b60561307d67..000000000000 --- a/tests/fixture/issue-1455/case3/input/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -const obj = { - find({ platform }) { - return { platform } - }, - byPlatform: function (platform) { - const result = await this.find({ platform: { $eq: platform } }); - return result; - }, -}; - -console.log(obj.byPlatform('foo')); \ No newline at end of file From 5ba0b04caadd804d94a34b1eb3f5a2bc5ee2970f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 22:01:32 +0900 Subject: [PATCH 29/56] Update test refs --- .../fixture/issue-1455/case1/output/index.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/fixture/issue-1455/case1/output/index.ts diff --git a/tests/fixture/issue-1455/case1/output/index.ts b/tests/fixture/issue-1455/case1/output/index.ts new file mode 100644 index 000000000000..d0b9ef67dd96 --- /dev/null +++ b/tests/fixture/issue-1455/case1/output/index.ts @@ -0,0 +1,48 @@ +"use strict"; +function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { + try { + var info = gen[key](arg); + var value = info.value; + } catch (error) { + reject(error); + return; + } + if (info.done) { + resolve(value); + } else { + Promise.resolve(value).then(_next, _throw); + } +} +function _asyncToGenerator(fn) { + return function() { + var self = this, args = arguments; + return new Promise(function(resolve, reject) { + var gen = fn.apply(self, args); + function _next(value) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); + } + function _throw(err) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); + } + _next(undefined); + }); + }; +} +const SampleData = typedModel("SampleVideo", VideosSchema, undefined, undefined, { + byPlatform: function(platform) { + var _ref = _asyncToGenerator(function*(platform) { + const result = yield this.find({ + platform: { + $eq: platform + } + }); + return result; + }); + return function() { + return _ref.apply(this, arguments); + }; + }() +}); +SampleData.byPlatform("youtube").then((res)=>{ + console.info(res); +}); From f72220eca606eb7a9bb7ed5a7a80ffe23fb19f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 1 Apr 2021 22:04:56 +0900 Subject: [PATCH 30/56] Add a test for #1455 --- .../compat/tests/es2017_async_to_generator.rs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/ecmascript/transforms/compat/tests/es2017_async_to_generator.rs b/ecmascript/transforms/compat/tests/es2017_async_to_generator.rs index d3a5f105929c..58bbb038c216 100644 --- a/ecmascript/transforms/compat/tests/es2017_async_to_generator.rs +++ b/ecmascript/transforms/compat/tests/es2017_async_to_generator.rs @@ -2334,3 +2334,46 @@ test_exec!( expect(a.foo()).resolves.toEqual('a1') " ); + +test!( + Syntax::default(), + |_| async_to_generator(), + issue_1455_1, + " + const obj = { + find({ platform }) { + return { platform } + }, + byPlatform: async function (platform) { + const result = await this.find({ platform: { $eq: platform } }); + return result; + }, + }; + + obj.byPlatform('foo').then(v => console.log(v)) + ", + " + const obj = { + find ({ platform }) { + return { + platform + }; + }, + byPlatform: function(platform) { + var _ref = _asyncToGenerator(function*(platform) { + const result = yield this.find({ + platform: { + $eq: platform + } + }); + return result; + }); + return function() { + return _ref.apply(this, arguments); + }; + }() + }; + obj.byPlatform('foo').then((v)=>console.log(v) + ); + " +); From 1394c6fceb9459156d4dbedd846d48f0a533a272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:29:07 +0900 Subject: [PATCH 31/56] WIP --- ecmascript/parser/tests/comments.rs | 87 +++++++++++++++++++ .../tests/comments/stmts/block/input.js | 3 + 2 files changed, 90 insertions(+) create mode 100644 ecmascript/parser/tests/comments.rs create mode 100644 ecmascript/parser/tests/comments/stmts/block/input.js diff --git a/ecmascript/parser/tests/comments.rs b/ecmascript/parser/tests/comments.rs new file mode 100644 index 000000000000..92039a994c53 --- /dev/null +++ b/ecmascript/parser/tests/comments.rs @@ -0,0 +1,87 @@ +use std::cell::RefCell; +use std::path::PathBuf; +use std::rc::Rc; +use swc_common::comments::SingleThreadedComments; +use swc_common::input::SourceFileInput; +use swc_ecma_ast::*; +use swc_ecma_parser::lexer::Lexer; +use swc_ecma_parser::EsConfig; +use swc_ecma_parser::Parser; +use swc_ecma_parser::Syntax; +use swc_ecma_parser::TsConfig; +use testing::fixture; +use testing::run_test2; + +#[fixture("comments/**/input.js")] +#[fixture("comments/**/input.ts")] +#[fixture("comments/**/input.tsx")] +fn test(input: PathBuf) { + let ext = input.extension().unwrap(); + let ext = ext.to_string_lossy(); + + let output_file_name = format!("output.{}", ext); + let output_file = input.with_file_name(&output_file_name); + + run_test2(false, |cm, handler| { + // + let fm = cm.load_file(&input).unwrap(); + let syntax = match &*ext { + "js" => Syntax::Es(EsConfig { + jsx: false, + num_sep: true, + class_private_props: true, + class_private_methods: true, + class_props: true, + fn_bind: false, + decorators: true, + decorators_before_export: false, + export_default_from: true, + export_namespace_from: true, + dynamic_import: true, + nullish_coalescing: true, + optional_chaining: true, + import_meta: true, + top_level_await: true, + import_assertions: true, + }), + "ts" | "tsx" => Syntax::Typescript(TsConfig { + tsx: ext == "tsx", + decorators: true, + dynamic_import: true, + no_early_errors: true, + import_assertions: true, + ..Default::default() + }), + _ => { + unreachable!() + } + }; + + let comments = SingleThreadedComments::default(); + + let lexer = Lexer::new( + syntax, + EsVersion::latest(), + SourceFileInput::from(&*fm), + Some(&comments), + ); + let mut parser = Parser::new_from(lexer); + + let module = parser.parse_module(); + let module = match module { + Ok(v) => v, + Err(err) => { + err.into_diagnostic(&handler).emit(); + return Err(()); + } + }; + + let (leading, trailing) = comments.take_all(); + + let leading = RefCell::into_inner(Rc::try_unwrap(leading).unwrap()); + let trailing = RefCell::into_inner(Rc::try_unwrap(trailing).unwrap()); + + Ok(()) + }) + .unwrap() +} diff --git a/ecmascript/parser/tests/comments/stmts/block/input.js b/ecmascript/parser/tests/comments/stmts/block/input.js new file mode 100644 index 000000000000..3f4890200f3e --- /dev/null +++ b/ecmascript/parser/tests/comments/stmts/block/input.js @@ -0,0 +1,3 @@ +while (1) { + /* test */ +} \ No newline at end of file From 9e1d9ea0714fb6c2548d60d3d20d597625826207 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:38:46 +0900 Subject: [PATCH 32/56] Implement comment test suitre --- ecmascript/parser/tests/comments.rs | 183 +++++++++++++++++++--------- 1 file changed, 125 insertions(+), 58 deletions(-) diff --git a/ecmascript/parser/tests/comments.rs b/ecmascript/parser/tests/comments.rs index 92039a994c53..879e90dc30ef 100644 --- a/ecmascript/parser/tests/comments.rs +++ b/ecmascript/parser/tests/comments.rs @@ -1,16 +1,21 @@ -use std::cell::RefCell; use std::path::PathBuf; -use std::rc::Rc; use swc_common::comments::SingleThreadedComments; +use swc_common::errors::DiagnosticBuilder; +use swc_common::errors::Handler; use swc_common::input::SourceFileInput; +use swc_common::Span; +use swc_common::DUMMY_SP; use swc_ecma_ast::*; use swc_ecma_parser::lexer::Lexer; use swc_ecma_parser::EsConfig; use swc_ecma_parser::Parser; use swc_ecma_parser::Syntax; use swc_ecma_parser::TsConfig; +use swc_ecma_visit::Node; +use swc_ecma_visit::Visit; +use swc_ecma_visit::VisitWith; use testing::fixture; -use testing::run_test2; +use testing::Tester; #[fixture("comments/**/input.js")] #[fixture("comments/**/input.ts")] @@ -22,66 +27,128 @@ fn test(input: PathBuf) { let output_file_name = format!("output.{}", ext); let output_file = input.with_file_name(&output_file_name); - run_test2(false, |cm, handler| { - // - let fm = cm.load_file(&input).unwrap(); - let syntax = match &*ext { - "js" => Syntax::Es(EsConfig { - jsx: false, - num_sep: true, - class_private_props: true, - class_private_methods: true, - class_props: true, - fn_bind: false, - decorators: true, - decorators_before_export: false, - export_default_from: true, - export_namespace_from: true, - dynamic_import: true, - nullish_coalescing: true, - optional_chaining: true, - import_meta: true, - top_level_await: true, - import_assertions: true, - }), - "ts" | "tsx" => Syntax::Typescript(TsConfig { - tsx: ext == "tsx", - decorators: true, - dynamic_import: true, - no_early_errors: true, - import_assertions: true, - ..Default::default() - }), - _ => { - unreachable!() - } - }; + let output = Tester::new() + .print_errors(|cm, handler| -> Result<(), _> { + // + let fm = cm.load_file(&input).unwrap(); + let syntax = match &*ext { + "js" => Syntax::Es(EsConfig { + jsx: false, + num_sep: true, + class_private_props: true, + class_private_methods: true, + class_props: true, + fn_bind: false, + decorators: true, + decorators_before_export: false, + export_default_from: true, + export_namespace_from: true, + dynamic_import: true, + nullish_coalescing: true, + optional_chaining: true, + import_meta: true, + top_level_await: true, + import_assertions: true, + }), + "ts" | "tsx" => Syntax::Typescript(TsConfig { + tsx: ext == "tsx", + decorators: true, + dynamic_import: true, + no_early_errors: true, + import_assertions: true, + ..Default::default() + }), + _ => { + unreachable!() + } + }; + + let comments = SingleThreadedComments::default(); + + let lexer = Lexer::new( + syntax, + EsVersion::latest(), + SourceFileInput::from(&*fm), + Some(&comments), + ); + let mut parser = Parser::new_from(lexer); - let comments = SingleThreadedComments::default(); + let module = parser.parse_module(); + let module = match module { + Ok(v) => v, + Err(err) => { + panic!("{:?}", err) + } + }; - let lexer = Lexer::new( - syntax, - EsVersion::latest(), - SourceFileInput::from(&*fm), - Some(&comments), - ); - let mut parser = Parser::new_from(lexer); + module.visit_with( + &Invalid { span: DUMMY_SP }, + &mut CommentPrinter { + handler: &handler, + comments, + }, + ); + + Err(()) + }) + .unwrap_err(); + + output.compare_to_file(&output_file).unwrap(); +} - let module = parser.parse_module(); - let module = match module { - Ok(v) => v, - Err(err) => { - err.into_diagnostic(&handler).emit(); - return Err(()); +struct CommentPrinter<'a> { + handler: &'a Handler, + comments: SingleThreadedComments, +} + +impl Visit for CommentPrinter<'_> { + fn visit_span(&mut self, n: &Span, _: &dyn Node) { + self.comments.with_leading(n.lo, |comments| { + for c in comments { + DiagnosticBuilder::new( + &self.handler, + swc_common::errors::Level::Note, + "Leading (lo)", + ) + .span_note(Span::new(n.lo, n.lo, Default::default()), &c.text) + .emit(); } - }; + }); - let (leading, trailing) = comments.take_all(); + self.comments.with_trailing(n.lo, |comments| { + for c in comments { + DiagnosticBuilder::new( + &self.handler, + swc_common::errors::Level::Note, + "Trailing (lo)", + ) + .span_note(Span::new(n.lo, n.lo, Default::default()), &c.text) + .emit(); + } + }); - let leading = RefCell::into_inner(Rc::try_unwrap(leading).unwrap()); - let trailing = RefCell::into_inner(Rc::try_unwrap(trailing).unwrap()); + self.comments.with_leading(n.hi, |comments| { + for c in comments { + DiagnosticBuilder::new( + &self.handler, + swc_common::errors::Level::Note, + "Leading (hi)", + ) + .span_note(Span::new(n.hi, n.hi, Default::default()), &c.text) + .emit(); + } + }); - Ok(()) - }) - .unwrap() + self.comments.with_trailing(n.hi, |comments| { + for c in comments { + DiagnosticBuilder::new( + &self.handler, + swc_common::errors::Level::Note, + "Trailing (hi)", + ) + .span_note(Span::new(n.hi, n.hi, Default::default()), &c.text) + .emit(); + } + }); + } } From b0809173d0f1a1170b5c6bc6c1141e30f446af2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:40:26 +0900 Subject: [PATCH 33/56] fixup --- ecmascript/parser/tests/comments.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ecmascript/parser/tests/comments.rs b/ecmascript/parser/tests/comments.rs index 879e90dc30ef..443050ae0900 100644 --- a/ecmascript/parser/tests/comments.rs +++ b/ecmascript/parser/tests/comments.rs @@ -24,8 +24,7 @@ fn test(input: PathBuf) { let ext = input.extension().unwrap(); let ext = ext.to_string_lossy(); - let output_file_name = format!("output.{}", ext); - let output_file = input.with_file_name(&output_file_name); + let output_file = PathBuf::from(format!("{}.stderr", input.display())); let output = Tester::new() .print_errors(|cm, handler| -> Result<(), _> { From e6defd5177aac28eb09c264e160dc92320da3757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:41:44 +0900 Subject: [PATCH 34/56] Add a test --- ecmascript/parser/tests/comments/stmts/switch/input.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 ecmascript/parser/tests/comments/stmts/switch/input.js diff --git a/ecmascript/parser/tests/comments/stmts/switch/input.js b/ecmascript/parser/tests/comments/stmts/switch/input.js new file mode 100644 index 000000000000..d580a2e0d675 --- /dev/null +++ b/ecmascript/parser/tests/comments/stmts/switch/input.js @@ -0,0 +1,5 @@ +switch (1) { + case 2: + 3; + // 4 +} \ No newline at end of file From 2a390b5abc0dd019112acab9168618831ac40b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:43:39 +0900 Subject: [PATCH 35/56] Add a test --- .../parser/tests/comments/exprs/array/blockStmt/input.js | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 ecmascript/parser/tests/comments/exprs/array/blockStmt/input.js diff --git a/ecmascript/parser/tests/comments/exprs/array/blockStmt/input.js b/ecmascript/parser/tests/comments/exprs/array/blockStmt/input.js new file mode 100644 index 000000000000..cb7f7240033c --- /dev/null +++ b/ecmascript/parser/tests/comments/exprs/array/blockStmt/input.js @@ -0,0 +1,3 @@ +React.useEffect(() => { + // @refresh reset +}); \ No newline at end of file From a4775210619b54008694210116be21d57e5389c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:44:52 +0900 Subject: [PATCH 36/56] Improve tester --- ecmascript/parser/tests/comments.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ecmascript/parser/tests/comments.rs b/ecmascript/parser/tests/comments.rs index 443050ae0900..0ad30ed024d6 100644 --- a/ecmascript/parser/tests/comments.rs +++ b/ecmascript/parser/tests/comments.rs @@ -91,6 +91,9 @@ fn test(input: PathBuf) { Err(()) }) .unwrap_err(); + if output.trim().is_empty() { + panic!("Comments have incorrect position") + } output.compare_to_file(&output_file).unwrap(); } From 2b3babd230efffb1254dd465cdef5c73eed282fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:47:29 +0900 Subject: [PATCH 37/56] Add a test for span --- ecmascript/parser/tests/span/js/expr/arrow-2.js | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 ecmascript/parser/tests/span/js/expr/arrow-2.js diff --git a/ecmascript/parser/tests/span/js/expr/arrow-2.js b/ecmascript/parser/tests/span/js/expr/arrow-2.js new file mode 100644 index 000000000000..cb7f7240033c --- /dev/null +++ b/ecmascript/parser/tests/span/js/expr/arrow-2.js @@ -0,0 +1,3 @@ +React.useEffect(() => { + // @refresh reset +}); \ No newline at end of file From 05d338f3555fcb157fcad2bc7360d02ec212dc25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:49:32 +0900 Subject: [PATCH 38/56] Update test refs --- .../tests/span/js/expr/arrow-2.js.spans | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 ecmascript/parser/tests/span/js/expr/arrow-2.js.spans diff --git a/ecmascript/parser/tests/span/js/expr/arrow-2.js.spans b/ecmascript/parser/tests/span/js/expr/arrow-2.js.spans new file mode 100644 index 000000000000..29f3d66149a2 --- /dev/null +++ b/ecmascript/parser/tests/span/js/expr/arrow-2.js.spans @@ -0,0 +1,141 @@ +warning: Module + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | / React.useEffect(() => { +2 | | // @refresh reset +3 | | }); + | |___^ + +warning: ModuleItem + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | / React.useEffect(() => { +2 | | // @refresh reset +3 | | }); + | |___^ + +warning: Stmt + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | / React.useEffect(() => { +2 | | // @refresh reset +3 | | }); + | |___^ + +warning: ExprStmt + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | / React.useEffect(() => { +2 | | // @refresh reset +3 | | }); + | |___^ + +warning: Expr + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | / React.useEffect(() => { +2 | | // @refresh reset +3 | | }); + | |__^ + +warning: CallExpr + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | / React.useEffect(() => { +2 | | // @refresh reset +3 | | }); + | |__^ + +warning: ExprOrSuper + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | React.useEffect(() => { + | ^^^^^^^^^^^^^^^ + +warning: Expr + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | React.useEffect(() => { + | ^^^^^^^^^^^^^^^ + +warning: MemberExpr + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | React.useEffect(() => { + | ^^^^^^^^^^^^^^^ + +warning: ExprOrSuper + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | React.useEffect(() => { + | ^^^^^ + +warning: Expr + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | React.useEffect(() => { + | ^^^^^ + +warning: Ident + --> $DIR/tests/span/js/expr/arrow-2.js:1:1 + | +1 | React.useEffect(() => { + | ^^^^^ + +warning: Expr + --> $DIR/tests/span/js/expr/arrow-2.js:1:7 + | +1 | React.useEffect(() => { + | ^^^^^^^^^ + +warning: Ident + --> $DIR/tests/span/js/expr/arrow-2.js:1:7 + | +1 | React.useEffect(() => { + | ^^^^^^^^^ + +warning: ExprOrSpread + --> $DIR/tests/span/js/expr/arrow-2.js:1:17 + | +1 | React.useEffect(() => { + | _________________^ +2 | | // @refresh reset +3 | | }); + | |_^ + +warning: Expr + --> $DIR/tests/span/js/expr/arrow-2.js:1:17 + | +1 | React.useEffect(() => { + | _________________^ +2 | | // @refresh reset +3 | | }); + | |_^ + +warning: ArrowExpr + --> $DIR/tests/span/js/expr/arrow-2.js:1:17 + | +1 | React.useEffect(() => { + | _________________^ +2 | | // @refresh reset +3 | | }); + | |_^ + +warning: BlockStmtOrExpr + --> $DIR/tests/span/js/expr/arrow-2.js:1:23 + | +1 | React.useEffect(() => { + | _______________________^ +2 | | // @refresh reset +3 | | }); + | |_^ + +warning: BlockStmt + --> $DIR/tests/span/js/expr/arrow-2.js:1:23 + | +1 | React.useEffect(() => { + | _______________________^ +2 | | // @refresh reset +3 | | }); + | |_^ + From 9ecf9853a6d1f890c636edf6243d1ea299880985 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:51:33 +0900 Subject: [PATCH 39/56] Fix #1530 --- ecmascript/parser/src/lexer/state.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ecmascript/parser/src/lexer/state.rs b/ecmascript/parser/src/lexer/state.rs index 34ff99a78f6b..1fd92187b957 100644 --- a/ecmascript/parser/src/lexer/state.rs +++ b/ecmascript/parser/src/lexer/state.rs @@ -278,13 +278,8 @@ impl<'a, I: Input> Iterator for Lexer<'a, I> { .borrow() .is_empty() { - let comment_pos = match token { - tok!('}') | tok!('`') => start + BytePos(1), - _ => start, - }; - self.comments.as_ref().unwrap().add_leading_comments( - comment_pos, + start, mem::replace( &mut *self .leading_comments_buffer From 9df37b5dfb2f9d5f681f7f62dd7564d25d806534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:52:10 +0900 Subject: [PATCH 40/56] Update test refs --- ecmascript/parser/tests/comments.rs | 15 +++++++++++---- .../exprs/array/blockStmt/input.js.stderr | 8 ++++++++ .../tests/comments/stmts/block/input.js.stderr | 8 ++++++++ .../tests/comments/stmts/switch/input.js.stderr | 8 ++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 ecmascript/parser/tests/comments/exprs/array/blockStmt/input.js.stderr create mode 100644 ecmascript/parser/tests/comments/stmts/block/input.js.stderr create mode 100644 ecmascript/parser/tests/comments/stmts/switch/input.js.stderr diff --git a/ecmascript/parser/tests/comments.rs b/ecmascript/parser/tests/comments.rs index 0ad30ed024d6..43b0c31d9850 100644 --- a/ecmascript/parser/tests/comments.rs +++ b/ecmascript/parser/tests/comments.rs @@ -3,6 +3,7 @@ use swc_common::comments::SingleThreadedComments; use swc_common::errors::DiagnosticBuilder; use swc_common::errors::Handler; use swc_common::input::SourceFileInput; +use swc_common::BytePos; use swc_common::Span; use swc_common::DUMMY_SP; use swc_ecma_ast::*; @@ -129,26 +130,32 @@ impl Visit for CommentPrinter<'_> { } }); - self.comments.with_leading(n.hi, |comments| { + self.comments.with_leading(n.hi - BytePos(1), |comments| { for c in comments { DiagnosticBuilder::new( &self.handler, swc_common::errors::Level::Note, "Leading (hi)", ) - .span_note(Span::new(n.hi, n.hi, Default::default()), &c.text) + .span_note( + Span::new(n.hi - BytePos(1), n.hi - BytePos(1), Default::default()), + &c.text, + ) .emit(); } }); - self.comments.with_trailing(n.hi, |comments| { + self.comments.with_trailing(n.hi - BytePos(1), |comments| { for c in comments { DiagnosticBuilder::new( &self.handler, swc_common::errors::Level::Note, "Trailing (hi)", ) - .span_note(Span::new(n.hi, n.hi, Default::default()), &c.text) + .span_note( + Span::new(n.hi - BytePos(1), n.hi - BytePos(1), Default::default()), + &c.text, + ) .emit(); } }); diff --git a/ecmascript/parser/tests/comments/exprs/array/blockStmt/input.js.stderr b/ecmascript/parser/tests/comments/exprs/array/blockStmt/input.js.stderr new file mode 100644 index 000000000000..63d66befd04f --- /dev/null +++ b/ecmascript/parser/tests/comments/exprs/array/blockStmt/input.js.stderr @@ -0,0 +1,8 @@ +note: Leading (hi) + | +note: @refresh reset + --> $DIR/tests/comments/exprs/array/blockStmt/input.js:3:1 + | +3 | }); + | ^ + diff --git a/ecmascript/parser/tests/comments/stmts/block/input.js.stderr b/ecmascript/parser/tests/comments/stmts/block/input.js.stderr new file mode 100644 index 000000000000..f6c28b5b2cd1 --- /dev/null +++ b/ecmascript/parser/tests/comments/stmts/block/input.js.stderr @@ -0,0 +1,8 @@ +note: Leading (hi) + | +note: test + --> $DIR/tests/comments/stmts/block/input.js:3:1 + | +3 | } + | ^ + diff --git a/ecmascript/parser/tests/comments/stmts/switch/input.js.stderr b/ecmascript/parser/tests/comments/stmts/switch/input.js.stderr new file mode 100644 index 000000000000..ecd2eb169f4a --- /dev/null +++ b/ecmascript/parser/tests/comments/stmts/switch/input.js.stderr @@ -0,0 +1,8 @@ +note: Leading (hi) + | +note: 4 + --> $DIR/tests/comments/stmts/switch/input.js:5:1 + | +5 | } + | ^ + From 2a11d1d850dd31cba1e864c8f5cad7395a2b4750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:52:17 +0900 Subject: [PATCH 41/56] Bump version --- ecmascript/parser/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecmascript/parser/Cargo.toml b/ecmascript/parser/Cargo.toml index 735011122aad..a0afd453d305 100644 --- a/ecmascript/parser/Cargo.toml +++ b/ecmascript/parser/Cargo.toml @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"] license = "Apache-2.0/MIT" name = "swc_ecma_parser" repository = "https://github.com/swc-project/swc.git" -version = "0.52.1" +version = "0.52.2" [features] default = [] From 3adc4135d4f5ce2c9d52e0ef3e685dc80019948c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:53:14 +0900 Subject: [PATCH 42/56] fixup --- ecmascript/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ecmascript/Cargo.toml b/ecmascript/Cargo.toml index ae448a918e11..51b6f1c8b2b9 100644 --- a/ecmascript/Cargo.toml +++ b/ecmascript/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" license = "Apache-2.0/MIT" name = "swc_ecmascript" repository = "https://github.com/swc-project/swc.git" -version = "0.29.0" +version = "0.29.1" [package.metadata.docs.rs] all-features = true @@ -30,7 +30,7 @@ typescript = ["swc_ecma_transforms/typescript"] swc_ecma_ast = {version = "0.42.0", path = "./ast"} swc_ecma_codegen = {version = "0.50.0", path = "./codegen", optional = true} swc_ecma_dep_graph = {version = "0.20.0", path = "./dep-graph", optional = true} -swc_ecma_parser = {version = "0.52.0", path = "./parser", optional = true} +swc_ecma_parser = {version = "0.52.1", path = "./parser", optional = true} swc_ecma_transforms = {version = "0.43.0", path = "./transforms", optional = true} swc_ecma_utils = {version = "0.33.0", path = "./utils", optional = true} swc_ecma_visit = {version = "0.28.0", path = "./visit", optional = true} From 884ef15a44e7198e23d8f8c7db1bf2fff1bf2140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:56:11 +0900 Subject: [PATCH 43/56] WIP: Fix codegen --- ecmascript/codegen/src/comments.rs | 11 ++++++++++- ecmascript/codegen/src/decl.rs | 6 +++--- ecmascript/codegen/src/lib.rs | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ecmascript/codegen/src/comments.rs b/ecmascript/codegen/src/comments.rs index be972c33e747..f7b7ae7707b4 100644 --- a/ecmascript/codegen/src/comments.rs +++ b/ecmascript/codegen/src/comments.rs @@ -39,11 +39,16 @@ impl<'a> Emitter<'a> { &mut self, pos: BytePos, prefix_space: bool, + is_hi: bool, ) -> Result { if pos == BytePos(0) { return Ok(()); } + if is_hi { + pos = pos - BytePos(1) + } + let comments = match self.comments { Some(ref comments) => comments, None => return Ok(()), @@ -54,11 +59,15 @@ impl<'a> Emitter<'a> { write_comments!(self, prefix_space, &cmts) } - pub(super) fn emit_leading_comments_of_pos(&mut self, pos: BytePos) -> Result { + pub(super) fn emit_leading_comments_of_pos(&mut self, mut pos: BytePos, is_hi: bool) -> Result { if pos == BytePos(0) { return Ok(()); } + if is_hi { + pos = pos - BytePos(1) + } + let comments = match self.comments { Some(ref comments) => comments, None => return Ok(()), diff --git a/ecmascript/codegen/src/decl.rs b/ecmascript/codegen/src/decl.rs index c2301693b9d2..c1bcad49bd09 100644 --- a/ecmascript/codegen/src/decl.rs +++ b/ecmascript/codegen/src/decl.rs @@ -23,7 +23,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_class_decl(&mut self, node: &ClassDecl) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; if node.declare { keyword!("declare"); @@ -44,7 +44,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_fn_decl(&mut self, node: &FnDecl) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; if node.declare { keyword!("declare"); @@ -90,7 +90,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_var_declarator(&mut self, node: &VarDeclarator) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.name); diff --git a/ecmascript/codegen/src/lib.rs b/ecmascript/codegen/src/lib.rs index 0a25bc737916..28ed64ff634d 100644 --- a/ecmascript/codegen/src/lib.rs +++ b/ecmascript/codegen/src/lib.rs @@ -1669,7 +1669,7 @@ impl<'a> Emitter<'a> { && previous_sibling.span().hi() != parent_node.hi() && emit_trailing_comments { - self.emit_leading_comments_of_pos(previous_sibling.span().hi())?; + self.emit_leading_comments_of_pos(previous_sibling.span().hi(), true)?; } } } From 4b99645e5a1681a22506e686a2fee1053e09b957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 16:59:52 +0900 Subject: [PATCH 44/56] fixup --- ecmascript/codegen/src/decl.rs | 2 +- ecmascript/codegen/src/lib.rs | 138 ++++++++++++++++----------------- 2 files changed, 70 insertions(+), 70 deletions(-) diff --git a/ecmascript/codegen/src/decl.rs b/ecmascript/codegen/src/decl.rs index c1bcad49bd09..e9cbe0a5200f 100644 --- a/ecmascript/codegen/src/decl.rs +++ b/ecmascript/codegen/src/decl.rs @@ -71,7 +71,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_var_decl(&mut self, node: &VarDecl) -> Result { - self.emit_leading_comments_of_pos(node.span.lo())?; + self.emit_leading_comments_of_pos(node.span.lo(), false)?; if node.declare { keyword!("declare"); diff --git a/ecmascript/codegen/src/lib.rs b/ecmascript/codegen/src/lib.rs index 28ed64ff634d..d831af8eff23 100644 --- a/ecmascript/codegen/src/lib.rs +++ b/ecmascript/codegen/src/lib.rs @@ -99,7 +99,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_module_decl(&mut self, node: &ModuleDecl) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; match *node { ModuleDecl::Import(ref d) => emit!(d), @@ -134,7 +134,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_export_default_decl(&mut self, node: &ExportDefaultDecl) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("export"); space!(); @@ -150,7 +150,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_import(&mut self, node: &ImportDecl) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("import"); space!(); @@ -238,7 +238,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_namespace_export_specifier(&mut self, node: &ExportNamespaceSpecifier) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("*"); formatting_space!(); @@ -249,7 +249,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_named_export_specifier(&mut self, node: &ExportNamedSpecifier) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; if let Some(ref exported) = node.exported { emit!(node.orig); @@ -264,7 +264,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_named_export(&mut self, node: &NamedExport) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; struct Specifiers<'a> { has_namespace_spec: bool, @@ -335,7 +335,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_export_all(&mut self, node: &ExportAll) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("export"); space!(); @@ -349,7 +349,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_lit(&mut self, node: &Lit) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; match *node { Lit::Bool(Bool { value, span }) => { @@ -381,7 +381,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_str_lit(&mut self, node: &Str) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; let (single_quote, value) = match node.kind { StrKind::Normal { contains_quote } => { @@ -422,7 +422,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_num_lit(&mut self, num: &Number) -> Result { - self.emit_leading_comments_of_pos(num.span().lo())?; + self.emit_leading_comments_of_pos(num.span().lo(), false)?; // Handle infinity if num.value.is_infinite() { @@ -441,7 +441,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_big_lit(&mut self, v: &BigInt) -> Result { - self.emit_leading_comments_of_pos(v.span.lo())?; + self.emit_leading_comments_of_pos(v.span.lo(), false)?; self.wr.write_lit(v.span, &v.value.to_string())?; self.wr.write_lit(v.span, "n")?; @@ -528,7 +528,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_opt_chain(&mut self, n: &OptChainExpr) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; match *n.expr { Expr::Member(ref e) => { @@ -564,7 +564,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_call_expr(&mut self, node: &CallExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.callee); @@ -575,7 +575,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_new_expr(&mut self, node: &NewExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("new"); space!(); @@ -594,7 +594,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_member_expr(&mut self, node: &MemberExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.obj); @@ -646,7 +646,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_arrow_expr(&mut self, node: &ArrowExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; let space = !self.cfg.minify || match node.params.as_slice() { @@ -689,7 +689,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_meta_prop_expr(&mut self, node: &MetaPropExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.meta); punct!("."); @@ -698,7 +698,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_seq_expr(&mut self, node: &SeqExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; let mut first = true; //TODO: Indention @@ -716,7 +716,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_assign_expr(&mut self, node: &AssignExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.left); formatting_space!(); @@ -764,7 +764,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_bin_expr(&mut self, node: &BinExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; { let mut left = Some(node); @@ -798,7 +798,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_decorator(&mut self, node: &Decorator) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("@"); emit!(node.expr); @@ -807,7 +807,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_class_expr(&mut self, node: &ClassExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; for dec in &node.class.decorators { emit!(dec); @@ -857,7 +857,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_private_method(&mut self, n: &PrivateMethod) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.is_static { keyword!("static"); @@ -894,7 +894,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_bool(&mut self, n: &Bool) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.value { keyword!(n.span, "true") @@ -905,7 +905,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_class_method(&mut self, n: &ClassMethod) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; self.emit_accesibility(n.accessibility)?; @@ -967,7 +967,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_private_prop(&mut self, n: &PrivateProp) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; self.emit_list(n.span, Some(&n.decorators), ListFormat::Decorators)?; @@ -1004,7 +1004,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_class_prop(&mut self, n: &ClassProp) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.accessibility != Some(Accessibility::Public) { self.emit_accesibility(n.accessibility)?; @@ -1066,7 +1066,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_class_constructor(&mut self, n: &Constructor) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; self.emit_accesibility(n.accessibility)?; @@ -1102,7 +1102,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_cond_expr(&mut self, node: &CondExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.test); formatting_space!(); @@ -1117,7 +1117,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_fn_expr(&mut self, node: &FnExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; if node.function.is_async { keyword!("async"); @@ -1178,7 +1178,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_this_expr(&mut self, node: &ThisExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("this"); } @@ -1187,7 +1187,7 @@ impl<'a> Emitter<'a> { fn emit_tpl_lit(&mut self, node: &Tpl) -> Result { debug_assert!(node.quasis.len() == node.exprs.len() + 1); - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("`"); let i = 0; @@ -1207,7 +1207,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_tagged_tpl_lit(&mut self, node: &TaggedTpl) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.tag); emit!(node.type_params); @@ -1223,7 +1223,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_unary_expr(&mut self, node: &UnaryExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; let need_formatting_space = match node.op { op!("typeof") | op!("void") | op!("delete") => { @@ -1247,7 +1247,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_update_expr(&mut self, node: &UpdateExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; if node.prefix { operator!(node.op.as_str()); @@ -1261,7 +1261,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_yield_expr(&mut self, node: &YieldExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("yield"); if node.delegate { @@ -1289,7 +1289,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_expr_or_spread(&mut self, node: &ExprOrSpread) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; if node.spread.is_some() { punct!("..."); @@ -1300,7 +1300,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_await_expr(&mut self, node: &AwaitExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("await"); @@ -1311,7 +1311,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_array_lit(&mut self, node: &ArrayLit) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("["); self.emit_list( @@ -1324,7 +1324,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_object_lit(&mut self, node: &ObjectLit) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("{"); if !self.cfg.minify { @@ -1355,7 +1355,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_kv_prop(&mut self, node: &KeyValueProp) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.key); punct!(":"); @@ -1365,7 +1365,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_assign_prop(&mut self, node: &AssignProp) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.key); punct!("="); @@ -1374,7 +1374,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_getter_prop(&mut self, node: &GetterProp) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("get"); space!(); @@ -1388,7 +1388,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_setter_prop(&mut self, node: &SetterProp) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("set"); space!(); @@ -1404,7 +1404,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_method_prop(&mut self, node: &MethodProp) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; if node.function.is_async { keyword!("async"); @@ -1423,7 +1423,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_paren_expr(&mut self, node: &ParenExpr) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("("); emit!(node.expr); @@ -1432,7 +1432,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_private_name(&mut self, n: &PrivateName) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("#"); emit!(n.id) @@ -1713,7 +1713,7 @@ impl<'a> Emitter<'a> { impl<'a> Emitter<'a> { #[emitter] fn emit_param(&mut self, node: &Param) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; self.emit_list(node.span, Some(&node.decorators), ListFormat::Decorators)?; @@ -1735,7 +1735,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_rest_pat(&mut self, node: &RestPat) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("..."); emit!(node.arg); @@ -1757,7 +1757,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_spread_element(&mut self, node: &SpreadElement) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("..."); emit!(node.expr) @@ -1773,7 +1773,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_array_pat(&mut self, node: &ArrayPat) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("["); self.emit_list( @@ -1795,7 +1795,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_assign_pat(&mut self, node: &AssignPat) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.left); formatting_space!(); @@ -1806,7 +1806,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_object_pat(&mut self, node: &ObjectPat) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("{"); self.emit_list( @@ -1837,7 +1837,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_object_kv_pat(&mut self, node: &KeyValuePatProp) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.key); punct!(":"); @@ -1848,7 +1848,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_object_assign_pat(&mut self, node: &AssignPatProp) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; emit!(node.key); formatting_space!(); @@ -1911,7 +1911,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_block_stmt(&mut self, node: &BlockStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("{"); self.emit_list( @@ -1927,14 +1927,14 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_empty_stmt(&mut self, node: &EmptyStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; semi!(); } #[emitter] fn emit_debugger_stmt(&mut self, node: &DebuggerStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("debugger"); formatting_semi!(); @@ -2011,7 +2011,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_if_stmt(&mut self, node: &IfStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("if"); @@ -2044,7 +2044,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_switch_stmt(&mut self, node: &SwitchStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("switch"); @@ -2059,7 +2059,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_catch_clause(&mut self, node: &CatchClause) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("catch"); formatting_space!(); @@ -2077,7 +2077,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_switch_case(&mut self, node: &SwitchCase) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; if let Some(ref test) = node.test { keyword!("case"); @@ -2109,7 +2109,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_throw_stmt(&mut self, node: &ThrowStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("throw"); space!(); @@ -2119,7 +2119,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_try_stmt(&mut self, node: &TryStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("try"); formatting_space!(); @@ -2140,7 +2140,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_while_stmt(&mut self, node: &WhileStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("while"); @@ -2153,7 +2153,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_do_while_stmt(&mut self, node: &DoWhileStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("do"); if node.body.starts_with_alpha_num() { @@ -2174,7 +2174,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_for_stmt(&mut self, node: &ForStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("for"); punct!("("); @@ -2190,7 +2190,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_for_in_stmt(&mut self, node: &ForInStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("for"); punct!("("); @@ -2206,7 +2206,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_for_of_stmt(&mut self, node: &ForOfStmt) -> Result { - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; keyword!("for"); if node.await_token.is_some() { From 1bf47253e9e68a26ee8b5d3a1cdf4c564bd13d00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:02:26 +0900 Subject: [PATCH 45/56] Fix codegen --- ecmascript/codegen/src/lib.rs | 30 ++++--- ecmascript/codegen/src/typescript.rs | 118 +++++++++++++-------------- 2 files changed, 76 insertions(+), 72 deletions(-) diff --git a/ecmascript/codegen/src/lib.rs b/ecmascript/codegen/src/lib.rs index d831af8eff23..b96565c7d6d1 100644 --- a/ecmascript/codegen/src/lib.rs +++ b/ecmascript/codegen/src/lib.rs @@ -557,7 +557,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_invalid(&mut self, n: &Invalid) -> Result { - self.emit_leading_comments_of_pos(n.span.lo())?; + self.emit_leading_comments_of_pos(n.span.lo(), false)?; self.wr.write_str_lit(n.span, "")?; } @@ -605,12 +605,12 @@ impl<'a> Emitter<'a> { } else { if self.needs_2dots_for_property_access(&node.obj) { if node.prop.span().lo() >= BytePos(2) { - self.emit_leading_comments_of_pos(node.prop.span().lo() - BytePos(2))?; + self.emit_leading_comments_of_pos(node.prop.span().lo() - BytePos(2), false)?; } punct!("."); } if node.prop.span().lo() >= BytePos(1) { - self.emit_leading_comments_of_pos(node.prop.span().lo() - BytePos(1))?; + self.emit_leading_comments_of_pos(node.prop.span().lo() - BytePos(1), false)?; } punct!("."); emit!(node.prop); @@ -1457,7 +1457,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ident(&mut self, ident: &Ident) -> Result { // TODO: Use write_symbol when ident is a symbol. - self.emit_leading_comments_of_pos(ident.span.lo())?; + self.emit_leading_comments_of_pos(ident.span.lo(), false)?; // TODO: span self.wr.write_symbol(ident.span, &ident.sym)?; @@ -1515,6 +1515,7 @@ impl<'a> Emitter<'a> { parent_node.lo() }, true, + false, )?; } } @@ -1576,7 +1577,7 @@ impl<'a> Emitter<'a> { if format.contains(ListFormat::DelimitersMask) && previous_sibling.span().hi() != parent_node.hi() { - self.emit_leading_comments_of_pos(previous_sibling.span().hi())?; + self.emit_leading_comments_of_pos(previous_sibling.span().hi(), true)?; } self.write_delim(format)?; @@ -1612,7 +1613,7 @@ impl<'a> Emitter<'a> { // Emit this child. if should_emit_intervening_comments { let comment_range = child.comment_range(); - self.emit_trailing_comments_of_pos(comment_range.hi(), false)?; + self.emit_trailing_comments_of_pos(comment_range.hi(), false, true)?; } else { should_emit_intervening_comments = may_emit_intervening_comments; } @@ -1696,11 +1697,14 @@ impl<'a> Emitter<'a> { if format.contains(ListFormat::BracketsMask) { if is_empty { - self.emit_leading_comments_of_pos({ - //TODO: children.hi() + self.emit_leading_comments_of_pos( + { + //TODO: children.hi() - parent_node.hi() - })?; // Emit leading comments within empty lists + parent_node.hi() + }, + true, + )?; // Emit leading comments within empty lists } self.wr.write_punct(format.closing_bracket())?; } @@ -1896,7 +1900,7 @@ impl<'a> Emitter<'a> { Stmt::ForOf(ref e) => emit!(e), Stmt::Decl(ref e) => emit!(e), } - self.emit_trailing_comments_of_pos(node.span().hi(), true)?; + self.emit_trailing_comments_of_pos(node.span().hi(), true, true)?; if !self.cfg.minify { self.wr.write_line()?; @@ -1920,7 +1924,7 @@ impl<'a> Emitter<'a> { ListFormat::MultiLineBlockStatements, )?; - self.emit_leading_comments_of_pos(node.span().hi())?; + self.emit_leading_comments_of_pos(node.span().hi(), true)?; punct!("}"); } @@ -1954,7 +1958,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_return_stmt(&mut self, node: &ReturnStmt) -> Result { - self.emit_leading_comments_of_pos(node.span.lo())?; + self.emit_leading_comments_of_pos(node.span.lo(), false)?; keyword!("return"); if let Some(ref arg) = node.arg { diff --git a/ecmascript/codegen/src/typescript.rs b/ecmascript/codegen/src/typescript.rs index ce8cd6ac2da0..c536bae7b4b9 100644 --- a/ecmascript/codegen/src/typescript.rs +++ b/ecmascript/codegen/src/typescript.rs @@ -15,7 +15,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_array_type(&mut self, n: &TsArrayType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.elem_type); punct!("["); @@ -24,7 +24,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_as_expr(&mut self, n: &TsAsExpr) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.expr); @@ -37,7 +37,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_call_signature_decl(&mut self, n: &TsCallSignatureDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.type_params); @@ -56,7 +56,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_cond_type(&mut self, n: &TsConditionalType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.check_type); space!(); @@ -80,7 +80,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_constructor_signature_decl(&mut self, n: &TsConstructSignatureDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; keyword!("constructor"); @@ -91,7 +91,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_constructor_type(&mut self, n: &TsConstructorType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.is_abstract { keyword!("abstract"); @@ -117,7 +117,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_entity_name(&mut self, n: &TsEntityName) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; match n { TsEntityName::TsQualifiedName(n) => { @@ -129,7 +129,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_enum_decl(&mut self, n: &TsEnumDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.declare { keyword!("declare"); @@ -156,7 +156,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_enum_member(&mut self, n: &TsEnumMember) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.id); @@ -178,7 +178,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_export_assignment(&mut self, n: &TsExportAssignment) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; keyword!("export"); formatting_space!(); @@ -189,7 +189,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_expr_with_type_args(&mut self, n: &TsExprWithTypeArgs) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.expr); @@ -198,14 +198,14 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_external_module_ref(&mut self, n: &TsExternalModuleRef) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; unimplemented!("emit_ts_external_module_ref") } #[emitter] fn emit_ts_fn_or_constructor_type(&mut self, n: &TsFnOrConstructorType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; match n { TsFnOrConstructorType::TsFnType(n) => emit!(n), @@ -225,7 +225,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_fn_type(&mut self, n: &TsFnType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.type_params); @@ -242,7 +242,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_import_equals_decl(&mut self, n: &TsImportEqualsDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.is_export { keyword!("export"); @@ -259,7 +259,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_index_signature(&mut self, n: &TsIndexSignature) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("["); self.emit_list(n.span, Some(&n.params), ListFormat::Parameters)?; @@ -274,7 +274,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_index_accessed_type(&mut self, n: &TsIndexedAccessType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.obj_type); @@ -285,7 +285,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_infer_type(&mut self, n: &TsInferType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; keyword!("infer"); space!(); @@ -294,7 +294,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_interface_body(&mut self, n: &TsInterfaceBody) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("{"); @@ -305,7 +305,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_interface_decl(&mut self, n: &TsInterfaceDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.declare { keyword!("declare"); @@ -338,7 +338,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_intersection_type(&mut self, n: &TsIntersectionType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; self.emit_list( n.span, @@ -349,7 +349,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_keyword_type(&mut self, n: &TsKeywordType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; match n.kind { TsKeywordTypeKind::TsAnyKeyword => keyword!(n.span, "any"), @@ -383,7 +383,7 @@ impl<'a> Emitter<'a> { fn emit_ts_tpl_lit(&mut self, node: &TsTplLitType) -> Result { debug_assert!(node.quasis.len() == node.types.len() + 1); - self.emit_leading_comments_of_pos(node.span().lo())?; + self.emit_leading_comments_of_pos(node.span().lo(), false)?; punct!("`"); let i = 0; @@ -403,14 +403,14 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_lit_type(&mut self, n: &TsLitType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.lit); } #[emitter] fn emit_ts_mapped_type(&mut self, n: &TsMappedType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("{"); self.wr.write_line()?; @@ -485,7 +485,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_method_signature(&mut self, n: &TsMethodSignature) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.readonly { keyword!("readonly"); @@ -521,12 +521,12 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_module_block(&mut self, n: &TsModuleBlock) -> Result { self.emit_list(n.span, Some(&n.body), ListFormat::SourceFileStatements)?; - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; } #[emitter] fn emit_ts_module_decl(&mut self, n: &TsModuleDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.declare { keyword!("declare"); @@ -553,14 +553,14 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_module_ref(&mut self, n: &TsModuleRef) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; unimplemented!("emit_ts_module_ref") } #[emitter] fn emit_ts_ns_body(&mut self, n: &TsNamespaceBody) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("{"); self.wr.increase_indent()?; @@ -574,14 +574,14 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_ns_decl(&mut self, n: &TsNamespaceDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; unimplemented!("emit_ts_ns_decl") } #[emitter] fn emit_ts_ns_export_decl(&mut self, n: &TsNamespaceExportDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; keyword!("export"); space!(); @@ -592,7 +592,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_non_null_expr(&mut self, n: &TsNonNullExpr) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.expr); punct!("!") @@ -600,7 +600,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_optional_type(&mut self, n: &TsOptionalType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.type_ann); punct!("?"); @@ -608,7 +608,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_param_prop(&mut self, n: &TsParamProp) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; self.emit_accesibility(n.accessibility)?; @@ -622,7 +622,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_param_prop_param(&mut self, n: &TsParamPropParam) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; match n { TsParamPropParam::Ident(n) => emit!(n), @@ -632,7 +632,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_paren_type(&mut self, n: &TsParenthesizedType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("("); emit!(n.type_ann); @@ -641,7 +641,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_property_signature(&mut self, n: &TsPropertySignature) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.readonly { keyword!("readonly"); @@ -682,7 +682,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_qualified_name(&mut self, n: &TsQualifiedName) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.left); punct!("."); @@ -691,7 +691,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_rest_type(&mut self, n: &TsRestType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("..."); emit!(n.type_ann); @@ -699,7 +699,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_signature_decl(&mut self, n: &TsSignatureDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; match n { TsSignatureDecl::TsCallSignatureDecl(n) => { @@ -722,14 +722,14 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_this_type(&mut self, n: &TsThisType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; keyword!(n.span, "this"); } #[emitter] fn emit_ts_this_type_or_ident(&mut self, n: &TsThisTypeOrIdent) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; match n { TsThisTypeOrIdent::TsThisType(n) => emit!(n), @@ -739,7 +739,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_tuple_type(&mut self, n: &TsTupleType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("["); self.emit_list(n.span, Some(&n.elem_types), ListFormat::TupleTypeElements)?; @@ -748,7 +748,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_tuple_element(&mut self, n: &TsTupleElement) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if let Some(label) = &n.label { emit!(label); @@ -787,7 +787,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_import_type(&mut self, n: &TsImportType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; keyword!("import"); punct!("("); @@ -808,7 +808,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_alias_decl(&mut self, n: &TsTypeAliasDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.declare { keyword!("declare"); @@ -836,14 +836,14 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_ann(&mut self, n: &TsTypeAnn) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.type_ann) } #[emitter] fn emit_ts_type_assertion(&mut self, n: &TsTypeAssertion) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("<"); emit!(n.type_ann); @@ -853,7 +853,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_const_assertion(&mut self, n: &TsConstAssertion) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.expr); @@ -913,7 +913,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_lit(&mut self, n: &TsTypeLit) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("{"); self.emit_list( @@ -926,7 +926,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_operator(&mut self, n: &TsTypeOperator) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; match n.op { TsTypeOperatorOp::KeyOf => keyword!("keyof"), @@ -939,7 +939,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_param(&mut self, n: &TsTypeParam) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.name); @@ -960,7 +960,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_param_decl(&mut self, n: &TsTypeParamDecl) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("<"); @@ -971,7 +971,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_param_instantiation(&mut self, n: &TsTypeParamInstantiation) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; punct!("<"); self.emit_list(n.span, Some(&n.params), ListFormat::TypeParameters)?; @@ -981,7 +981,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_predicate(&mut self, n: &TsTypePredicate) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; if n.asserts { keyword!("asserts"); @@ -1000,7 +1000,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_query(&mut self, n: &TsTypeQuery) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; keyword!("typeof"); space!(); @@ -1017,7 +1017,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_type_ref(&mut self, n: &TsTypeRef) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; emit!(n.type_name); @@ -1038,7 +1038,7 @@ impl<'a> Emitter<'a> { #[emitter] fn emit_ts_union_type(&mut self, n: &TsUnionType) -> Result { - self.emit_leading_comments_of_pos(n.span().lo())?; + self.emit_leading_comments_of_pos(n.span().lo(), false)?; self.emit_list(n.span, Some(&n.types), ListFormat::UnionTypeConstituents)?; } From 946227e87e68d295728400d10abc9169eff25844 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:02:38 +0900 Subject: [PATCH 46/56] fixup --- ecmascript/codegen/src/comments.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecmascript/codegen/src/comments.rs b/ecmascript/codegen/src/comments.rs index f7b7ae7707b4..fe4faf1976bd 100644 --- a/ecmascript/codegen/src/comments.rs +++ b/ecmascript/codegen/src/comments.rs @@ -37,7 +37,7 @@ macro_rules! write_comments { impl<'a> Emitter<'a> { pub(super) fn emit_trailing_comments_of_pos( &mut self, - pos: BytePos, + mut pos: BytePos, prefix_space: bool, is_hi: bool, ) -> Result { From ddb96cca2df974ee2deb9c3c60f7f67e5a78e75f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:04:28 +0900 Subject: [PATCH 47/56] Fix fast refresh --- ecmascript/transforms/react/src/refresh/mod.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ecmascript/transforms/react/src/refresh/mod.rs b/ecmascript/transforms/react/src/refresh/mod.rs index 58c2fa050ba3..8402d6cdf51a 100644 --- a/ecmascript/transforms/react/src/refresh/mod.rs +++ b/ecmascript/transforms/react/src/refresh/mod.rs @@ -6,6 +6,7 @@ use once_cell::sync::Lazy; use regex::Regex; use swc_atoms::JsWord; +use swc_common::BytePos; use swc_common::{ comments::Comments, comments::CommentsExt, sync::Lrc, SourceMap, Span, Spanned, DUMMY_SP, }; @@ -486,11 +487,13 @@ where let mut should_refresh = self.should_refresh; if let Some(comments) = &self.comments { - comments.with_leading(n.hi, |comments| { - if comments.iter().any(|c| c.text.contains("@refresh reset")) { - should_refresh = true - } - }); + if n.hi != BytePos(0) { + comments.with_leading(n.hi - BytePos(1), |comments| { + if comments.iter().any(|c| c.text.contains("@refresh reset")) { + should_refresh = true + } + }); + } comments.with_trailing(n.lo, |comments| { if comments.iter().any(|c| c.text.contains("@refresh reset")) { From afdd681304711b8d026816dcc404548e93b2ed1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:05:50 +0900 Subject: [PATCH 48/56] Update test refs --- .../fixture/codegen/escape/case-1/output/index.js | 2 +- .../fixture/codegen/escape/case-2/output/index.js | 2 +- tests/fixture/issue-1333/case2/output/index.js | 14 +------------- tests/fixture/issue-1341/case1/output/index.ts | 2 +- tests/fixture/issue-1490/full/output/index.js | 2 +- 5 files changed, 5 insertions(+), 17 deletions(-) diff --git a/tests/fixture/codegen/escape/case-1/output/index.js b/tests/fixture/codegen/escape/case-1/output/index.js index ca06baba5421..fef6ecaf3659 100644 --- a/tests/fixture/codegen/escape/case-1/output/index.js +++ b/tests/fixture/codegen/escape/case-1/output/index.js @@ -3,5 +3,5 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; -var _default = /* glsl */ "\nvoid main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}\n"; +var _default = "\nvoid main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}\n"; exports.default = _default; diff --git a/tests/fixture/codegen/escape/case-2/output/index.js b/tests/fixture/codegen/escape/case-2/output/index.js index d25d5cd76d8b..8eea8c77f35c 100644 --- a/tests/fixture/codegen/escape/case-2/output/index.js +++ b/tests/fixture/codegen/escape/case-2/output/index.js @@ -3,5 +3,5 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; -var _default = /* glsl */ "\n// Analytical approximation of the DFG LUT, one half of the\n// split-sum approximation used in indirect specular lighting.\n// via 'environmentBRDF' from \"Physically Based Shading on Mobile\"\n// https://www.unrealengine.com/blog/physically-based-shading-on-mobile - environmentBRDF for GGX on mobile\nvec2 integrateSpecularBRDF( const in float dotNV, const in float roughness ) {\n\tconst vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );\n\tconst vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );\n\tvec4 r = roughness * c0 + c1;\n\tfloat a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;\n\treturn vec2( -1.04, 1.04 ) * a004 + r.zw;\n}\nfloat punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {\n#if defined ( PHYSICALLY_CORRECT_LIGHTS )\n\t// based upon Frostbite 3 Moving to Physically-based Rendering\n\t// page 32, equation 26: E[window1]\n\t// https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf\n\t// this is intended to be used on spot and point lights who are represented as luminous intensity\n\t// but who must be converted to luminous irradiance for surface lighting calculation\n\tfloat distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );\n\tif( cutoffDistance > 0.0 ) {\n\t\tdistanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );\n\t}\n\treturn distanceFalloff;\n#else\n\tif( cutoffDistance > 0.0 && decayExponent > 0.0 ) {\n\t\treturn pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );\n\t}\n\treturn 1.0;\n#endif\n}\nvec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {\n\treturn RECIPROCAL_PI * diffuseColor;\n} // validated\nvec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {\n\t// Original approximation by Christophe Schlick '94\n\t// float fresnel = pow( 1.0 - dotLH, 5.0 );\n\t// Optimized variant (presented by Epic at SIGGRAPH '13)\n\t// https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf\n\tfloat fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );\n\treturn ( 1.0 - specularColor ) * fresnel + specularColor;\n} // validated\nvec3 F_Schlick_RoughnessDependent( const in vec3 F0, const in float dotNV, const in float roughness ) {\n\t// See F_Schlick\n\tfloat fresnel = exp2( ( -5.55473 * dotNV - 6.98316 ) * dotNV );\n\tvec3 Fr = max( vec3( 1.0 - roughness ), F0 ) - F0;\n\treturn Fr * fresnel + F0;\n}\n// Microfacet Models for Refraction through Rough Surfaces - equation (34)\n// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html\n// alpha is \"roughness squared\" in Disney\u2019s reparameterization\nfloat G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {\n\t// geometry term (normalized) = G(l)\u22c5G(v) / 4(n\u22c5l)(n\u22c5v)\n\t// also see #12151\n\tfloat a2 = pow2( alpha );\n\tfloat gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\tfloat gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\treturn 1.0 / ( gl * gv );\n} // validated\n// Moving Frostbite to Physically Based Rendering 3.0 - page 12, listing 2\n// https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf\nfloat G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {\n\tfloat a2 = pow2( alpha );\n\t// dotNL and dotNV are explicitly swapped. This is not a mistake.\n\tfloat gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\tfloat gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\treturn 0.5 / max( gv + gl, EPSILON );\n}\n// Microfacet Models for Refraction through Rough Surfaces - equation (33)\n// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html\n// alpha is \"roughness squared\" in Disney\u2019s reparameterization\nfloat D_GGX( const in float alpha, const in float dotNH ) {\n\tfloat a2 = pow2( alpha );\n\tfloat denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0; // avoid alpha = 0 with dotNH = 1\n\treturn RECIPROCAL_PI * a2 / pow2( denom );\n}\n// GGX Distribution, Schlick Fresnel, GGX-Smith Visibility\nvec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat alpha = pow2( roughness ); // UE4's roughness\n\tvec3 halfDir = normalize( incidentLight.direction + viewDir );\n\tfloat dotNL = saturate( dot( normal, incidentLight.direction ) );\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tfloat dotNH = saturate( dot( normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );\n\tfloat D = D_GGX( alpha, dotNH );\n\treturn F * ( G * D );\n} // validated\n// Rect Area Light\n// Real-Time Polygonal-Light Shading with Linearly Transformed Cosines\n// by Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt\n// code: https://github.com/selfshadow/ltc_code/\nvec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {\n\tconst float LUT_SIZE = 64.0;\n\tconst float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n\tconst float LUT_BIAS = 0.5 / LUT_SIZE;\n\tfloat dotNV = saturate( dot( N, V ) );\n\t// texture parameterized by sqrt( GGX alpha ) and sqrt( 1 - cos( theta ) )\n\tvec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );\n\tuv = uv * LUT_SCALE + LUT_BIAS;\n\treturn uv;\n}\nfloat LTC_ClippedSphereFormFactor( const in vec3 f ) {\n\t// Real-Time Area Lighting: a Journey from Research to Production (p.102)\n\t// An approximation of the form factor of a horizon-clipped rectangle.\n\tfloat l = length( f );\n\treturn max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );\n}\nvec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {\n\tfloat x = dot( v1, v2 );\n\tfloat y = abs( x );\n\t// rational polynomial approximation to theta / sin( theta ) / 2PI\n\tfloat a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;\n\tfloat b = 3.4175940 + ( 4.1616724 + y ) * y;\n\tfloat v = a / b;\n\tfloat theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;\n\treturn cross( v1, v2 ) * theta_sintheta;\n}\nvec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {\n\t// bail if point is on back side of plane of light\n\t// assumes ccw winding order of light vertices\n\tvec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];\n\tvec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];\n\tvec3 lightNormal = cross( v1, v2 );\n\tif( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );\n\t// construct orthonormal basis around N\n\tvec3 T1, T2;\n\tT1 = normalize( V - N * dot( V, N ) );\n\tT2 = - cross( N, T1 ); // negated from paper; possibly due to a different handedness of world coordinate system\n\t// compute transform\n\tmat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );\n\t// transform rect\n\tvec3 coords[ 4 ];\n\tcoords[ 0 ] = mat * ( rectCoords[ 0 ] - P );\n\tcoords[ 1 ] = mat * ( rectCoords[ 1 ] - P );\n\tcoords[ 2 ] = mat * ( rectCoords[ 2 ] - P );\n\tcoords[ 3 ] = mat * ( rectCoords[ 3 ] - P );\n\t// project rect onto sphere\n\tcoords[ 0 ] = normalize( coords[ 0 ] );\n\tcoords[ 1 ] = normalize( coords[ 1 ] );\n\tcoords[ 2 ] = normalize( coords[ 2 ] );\n\tcoords[ 3 ] = normalize( coords[ 3 ] );\n\t// calculate vector form factor\n\tvec3 vectorFormFactor = vec3( 0.0 );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );\n\t// adjust for horizon clipping\n\tfloat result = LTC_ClippedSphereFormFactor( vectorFormFactor );\n/*\n\t// alternate method of adjusting for horizon clipping (see referece)\n\t// refactoring required\n\tfloat len = length( vectorFormFactor );\n\tfloat z = vectorFormFactor.z / len;\n\tconst float LUT_SIZE = 64.0;\n\tconst float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n\tconst float LUT_BIAS = 0.5 / LUT_SIZE;\n\t// tabulated horizon-clipped sphere, apparently...\n\tvec2 uv = vec2( z * 0.5 + 0.5, len );\n\tuv = uv * LUT_SCALE + LUT_BIAS;\n\tfloat scale = texture2D( ltc_2, uv ).w;\n\tfloat result = len * scale;\n*/\n\treturn vec3( result );\n}\n// End Rect Area Light\n// ref: https://www.unrealengine.com/blog/physically-based-shading-on-mobile - environmentBRDF for GGX on mobile\nvec3 BRDF_Specular_GGX_Environment( const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\treturn specularColor * brdf.x + brdf.y;\n} // validated\n// Fdez-Ag\xfcera's \"Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting\"\n// Approximates multiscattering in order to preserve energy.\n// http://www.jcgt.org/published/0008/01/03/\nvoid BRDF_Specular_Multiscattering_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) {\n\tfloat dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\tvec3 F = F_Schlick_RoughnessDependent( specularColor, dotNV, roughness );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\tvec3 FssEss = F * brdf.x + brdf.y;\n\tfloat Ess = brdf.x + brdf.y;\n\tfloat Ems = 1.0 - Ess;\n\tvec3 Favg = specularColor + ( 1.0 - specularColor ) * 0.047619; // 1/21\n\tvec3 Fms = FssEss * Favg / ( 1.0 - Ems * Favg );\n\tsingleScatter += FssEss;\n\tmultiScatter += Fms * Ems;\n}\nfloat G_BlinnPhong_Implicit( /* const in float dotNL, const in float dotNV */ ) {\n\t// geometry term is (n dot l)(n dot v) / 4(n dot l)(n dot v)\n\treturn 0.25;\n}\nfloat D_BlinnPhong( const in float shininess, const in float dotNH ) {\n\treturn RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );\n}\nvec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {\n\tvec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );\n\t//float dotNL = saturate( dot( geometry.normal, incidentLight.direction ) );\n\t//float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\tfloat dotNH = saturate( dot( geometry.normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ );\n\tfloat D = D_BlinnPhong( shininess, dotNH );\n\treturn F * ( G * D );\n} // validated\n// source: http://simonstechblog.blogspot.ca/2011/12/microfacet-brdf.html\nfloat GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\treturn ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 );\n}\nfloat BlinnExponentToGGXRoughness( const in float blinnExponent ) {\n\treturn sqrt( 2.0 / ( blinnExponent + 2.0 ) );\n}\n#if defined( USE_SHEEN )\n// https://github.com/google/filament/blob/master/shaders/src/brdf.fs#L94\nfloat D_Charlie(float roughness, float NoH) {\n\t// Estevez and Kulla 2017, \"Production Friendly Microfacet Sheen BRDF\"\n\tfloat invAlpha = 1.0 / roughness;\n\tfloat cos2h = NoH * NoH;\n\tfloat sin2h = max(1.0 - cos2h, 0.0078125); // 2^(-14/2), so sin2h^2 > 0 in fp16\n\treturn (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI);\n}\n// https://github.com/google/filament/blob/master/shaders/src/brdf.fs#L136\nfloat V_Neubelt(float NoV, float NoL) {\n\t// Neubelt and Pettineo 2013, \"Crafting a Next-gen Material Pipeline for The Order: 1886\"\n\treturn saturate(1.0 / (4.0 * (NoL + NoV - NoL * NoV)));\n}\nvec3 BRDF_Specular_Sheen( const in float roughness, const in vec3 L, const in GeometricContext geometry, vec3 specularColor ) {\n\tvec3 N = geometry.normal;\n\tvec3 V = geometry.viewDir;\n\tvec3 H = normalize( V + L );\n\tfloat dotNH = saturate( dot( N, H ) );\n\treturn specularColor * D_Charlie( roughness, dotNH ) * V_Neubelt( dot(N, V), dot(N, L) );\n}\n#endif\n"; +var _default = "\n// Analytical approximation of the DFG LUT, one half of the\n// split-sum approximation used in indirect specular lighting.\n// via 'environmentBRDF' from \"Physically Based Shading on Mobile\"\n// https://www.unrealengine.com/blog/physically-based-shading-on-mobile - environmentBRDF for GGX on mobile\nvec2 integrateSpecularBRDF( const in float dotNV, const in float roughness ) {\n\tconst vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );\n\tconst vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );\n\tvec4 r = roughness * c0 + c1;\n\tfloat a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;\n\treturn vec2( -1.04, 1.04 ) * a004 + r.zw;\n}\nfloat punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {\n#if defined ( PHYSICALLY_CORRECT_LIGHTS )\n\t// based upon Frostbite 3 Moving to Physically-based Rendering\n\t// page 32, equation 26: E[window1]\n\t// https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf\n\t// this is intended to be used on spot and point lights who are represented as luminous intensity\n\t// but who must be converted to luminous irradiance for surface lighting calculation\n\tfloat distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );\n\tif( cutoffDistance > 0.0 ) {\n\t\tdistanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );\n\t}\n\treturn distanceFalloff;\n#else\n\tif( cutoffDistance > 0.0 && decayExponent > 0.0 ) {\n\t\treturn pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );\n\t}\n\treturn 1.0;\n#endif\n}\nvec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {\n\treturn RECIPROCAL_PI * diffuseColor;\n} // validated\nvec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {\n\t// Original approximation by Christophe Schlick '94\n\t// float fresnel = pow( 1.0 - dotLH, 5.0 );\n\t// Optimized variant (presented by Epic at SIGGRAPH '13)\n\t// https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf\n\tfloat fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );\n\treturn ( 1.0 - specularColor ) * fresnel + specularColor;\n} // validated\nvec3 F_Schlick_RoughnessDependent( const in vec3 F0, const in float dotNV, const in float roughness ) {\n\t// See F_Schlick\n\tfloat fresnel = exp2( ( -5.55473 * dotNV - 6.98316 ) * dotNV );\n\tvec3 Fr = max( vec3( 1.0 - roughness ), F0 ) - F0;\n\treturn Fr * fresnel + F0;\n}\n// Microfacet Models for Refraction through Rough Surfaces - equation (34)\n// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html\n// alpha is \"roughness squared\" in Disney\u2019s reparameterization\nfloat G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {\n\t// geometry term (normalized) = G(l)\u22c5G(v) / 4(n\u22c5l)(n\u22c5v)\n\t// also see #12151\n\tfloat a2 = pow2( alpha );\n\tfloat gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\tfloat gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\treturn 1.0 / ( gl * gv );\n} // validated\n// Moving Frostbite to Physically Based Rendering 3.0 - page 12, listing 2\n// https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf\nfloat G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {\n\tfloat a2 = pow2( alpha );\n\t// dotNL and dotNV are explicitly swapped. This is not a mistake.\n\tfloat gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\tfloat gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\treturn 0.5 / max( gv + gl, EPSILON );\n}\n// Microfacet Models for Refraction through Rough Surfaces - equation (33)\n// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html\n// alpha is \"roughness squared\" in Disney\u2019s reparameterization\nfloat D_GGX( const in float alpha, const in float dotNH ) {\n\tfloat a2 = pow2( alpha );\n\tfloat denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0; // avoid alpha = 0 with dotNH = 1\n\treturn RECIPROCAL_PI * a2 / pow2( denom );\n}\n// GGX Distribution, Schlick Fresnel, GGX-Smith Visibility\nvec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat alpha = pow2( roughness ); // UE4's roughness\n\tvec3 halfDir = normalize( incidentLight.direction + viewDir );\n\tfloat dotNL = saturate( dot( normal, incidentLight.direction ) );\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tfloat dotNH = saturate( dot( normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );\n\tfloat D = D_GGX( alpha, dotNH );\n\treturn F * ( G * D );\n} // validated\n// Rect Area Light\n// Real-Time Polygonal-Light Shading with Linearly Transformed Cosines\n// by Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt\n// code: https://github.com/selfshadow/ltc_code/\nvec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {\n\tconst float LUT_SIZE = 64.0;\n\tconst float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n\tconst float LUT_BIAS = 0.5 / LUT_SIZE;\n\tfloat dotNV = saturate( dot( N, V ) );\n\t// texture parameterized by sqrt( GGX alpha ) and sqrt( 1 - cos( theta ) )\n\tvec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );\n\tuv = uv * LUT_SCALE + LUT_BIAS;\n\treturn uv;\n}\nfloat LTC_ClippedSphereFormFactor( const in vec3 f ) {\n\t// Real-Time Area Lighting: a Journey from Research to Production (p.102)\n\t// An approximation of the form factor of a horizon-clipped rectangle.\n\tfloat l = length( f );\n\treturn max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );\n}\nvec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {\n\tfloat x = dot( v1, v2 );\n\tfloat y = abs( x );\n\t// rational polynomial approximation to theta / sin( theta ) / 2PI\n\tfloat a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;\n\tfloat b = 3.4175940 + ( 4.1616724 + y ) * y;\n\tfloat v = a / b;\n\tfloat theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;\n\treturn cross( v1, v2 ) * theta_sintheta;\n}\nvec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {\n\t// bail if point is on back side of plane of light\n\t// assumes ccw winding order of light vertices\n\tvec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];\n\tvec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];\n\tvec3 lightNormal = cross( v1, v2 );\n\tif( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );\n\t// construct orthonormal basis around N\n\tvec3 T1, T2;\n\tT1 = normalize( V - N * dot( V, N ) );\n\tT2 = - cross( N, T1 ); // negated from paper; possibly due to a different handedness of world coordinate system\n\t// compute transform\n\tmat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );\n\t// transform rect\n\tvec3 coords[ 4 ];\n\tcoords[ 0 ] = mat * ( rectCoords[ 0 ] - P );\n\tcoords[ 1 ] = mat * ( rectCoords[ 1 ] - P );\n\tcoords[ 2 ] = mat * ( rectCoords[ 2 ] - P );\n\tcoords[ 3 ] = mat * ( rectCoords[ 3 ] - P );\n\t// project rect onto sphere\n\tcoords[ 0 ] = normalize( coords[ 0 ] );\n\tcoords[ 1 ] = normalize( coords[ 1 ] );\n\tcoords[ 2 ] = normalize( coords[ 2 ] );\n\tcoords[ 3 ] = normalize( coords[ 3 ] );\n\t// calculate vector form factor\n\tvec3 vectorFormFactor = vec3( 0.0 );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );\n\t// adjust for horizon clipping\n\tfloat result = LTC_ClippedSphereFormFactor( vectorFormFactor );\n/*\n\t// alternate method of adjusting for horizon clipping (see referece)\n\t// refactoring required\n\tfloat len = length( vectorFormFactor );\n\tfloat z = vectorFormFactor.z / len;\n\tconst float LUT_SIZE = 64.0;\n\tconst float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n\tconst float LUT_BIAS = 0.5 / LUT_SIZE;\n\t// tabulated horizon-clipped sphere, apparently...\n\tvec2 uv = vec2( z * 0.5 + 0.5, len );\n\tuv = uv * LUT_SCALE + LUT_BIAS;\n\tfloat scale = texture2D( ltc_2, uv ).w;\n\tfloat result = len * scale;\n*/\n\treturn vec3( result );\n}\n// End Rect Area Light\n// ref: https://www.unrealengine.com/blog/physically-based-shading-on-mobile - environmentBRDF for GGX on mobile\nvec3 BRDF_Specular_GGX_Environment( const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\treturn specularColor * brdf.x + brdf.y;\n} // validated\n// Fdez-Ag\xfcera's \"Multiple-Scattering Microfacet Model for Real-Time Image Based Lighting\"\n// Approximates multiscattering in order to preserve energy.\n// http://www.jcgt.org/published/0008/01/03/\nvoid BRDF_Specular_Multiscattering_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) {\n\tfloat dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\tvec3 F = F_Schlick_RoughnessDependent( specularColor, dotNV, roughness );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\tvec3 FssEss = F * brdf.x + brdf.y;\n\tfloat Ess = brdf.x + brdf.y;\n\tfloat Ems = 1.0 - Ess;\n\tvec3 Favg = specularColor + ( 1.0 - specularColor ) * 0.047619; // 1/21\n\tvec3 Fms = FssEss * Favg / ( 1.0 - Ems * Favg );\n\tsingleScatter += FssEss;\n\tmultiScatter += Fms * Ems;\n}\nfloat G_BlinnPhong_Implicit( /* const in float dotNL, const in float dotNV */ ) {\n\t// geometry term is (n dot l)(n dot v) / 4(n dot l)(n dot v)\n\treturn 0.25;\n}\nfloat D_BlinnPhong( const in float shininess, const in float dotNH ) {\n\treturn RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );\n}\nvec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {\n\tvec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );\n\t//float dotNL = saturate( dot( geometry.normal, incidentLight.direction ) );\n\t//float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\tfloat dotNH = saturate( dot( geometry.normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ );\n\tfloat D = D_BlinnPhong( shininess, dotNH );\n\treturn F * ( G * D );\n} // validated\n// source: http://simonstechblog.blogspot.ca/2011/12/microfacet-brdf.html\nfloat GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\treturn ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 );\n}\nfloat BlinnExponentToGGXRoughness( const in float blinnExponent ) {\n\treturn sqrt( 2.0 / ( blinnExponent + 2.0 ) );\n}\n#if defined( USE_SHEEN )\n// https://github.com/google/filament/blob/master/shaders/src/brdf.fs#L94\nfloat D_Charlie(float roughness, float NoH) {\n\t// Estevez and Kulla 2017, \"Production Friendly Microfacet Sheen BRDF\"\n\tfloat invAlpha = 1.0 / roughness;\n\tfloat cos2h = NoH * NoH;\n\tfloat sin2h = max(1.0 - cos2h, 0.0078125); // 2^(-14/2), so sin2h^2 > 0 in fp16\n\treturn (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI);\n}\n// https://github.com/google/filament/blob/master/shaders/src/brdf.fs#L136\nfloat V_Neubelt(float NoV, float NoL) {\n\t// Neubelt and Pettineo 2013, \"Crafting a Next-gen Material Pipeline for The Order: 1886\"\n\treturn saturate(1.0 / (4.0 * (NoL + NoV - NoL * NoV)));\n}\nvec3 BRDF_Specular_Sheen( const in float roughness, const in vec3 L, const in GeometricContext geometry, vec3 specularColor ) {\n\tvec3 N = geometry.normal;\n\tvec3 V = geometry.viewDir;\n\tvec3 H = normalize( V + L );\n\tfloat dotNH = saturate( dot( N, H ) );\n\treturn specularColor * D_Charlie( roughness, dotNH ) * V_Neubelt( dot(N, V), dot(N, L) );\n}\n#endif\n"; exports.default = _default; diff --git a/tests/fixture/issue-1333/case2/output/index.js b/tests/fixture/issue-1333/case2/output/index.js index 070cb8786fa4..1172bdd316c0 100644 --- a/tests/fixture/issue-1333/case2/output/index.js +++ b/tests/fixture/issue-1333/case2/output/index.js @@ -426,19 +426,7 @@ class Shard extends _utils.Emitter { })); _classPrivateFieldSet(this, _presenceBucket, new _utils.Bucket(5, 60000)); } -} /** - * @typedef {Object} DiscordPacket - * @property {number} [op] - * @property {*} [d] - * @property {number | null} [s] - * @property {string} [t] - */ /** - * @typedef {Object} ShardDestroyOptions - * @property {boolean} [reset=false] Whether to reset the shard. - * @property {boolean} [emit=true] Whether to emit the "destroyed" event. - * @property {boolean} [log=false] Whether to emit a debug log. - * @property {number} [code=1000] The code to use. - */ +} exports.Shard = Shard; var _serialization2 = new WeakMap(); var _compression2 = new WeakMap(); diff --git a/tests/fixture/issue-1341/case1/output/index.ts b/tests/fixture/issue-1341/case1/output/index.ts index e12d3b32c6d5..4e3c680898e9 100644 --- a/tests/fixture/issue-1341/case1/output/index.ts +++ b/tests/fixture/issue-1341/case1/output/index.ts @@ -34,7 +34,7 @@ class A { try { return yield _asyncToGenerator((function*(x) { return x + this.val; - }).bind(this)).bind(this)('a'); // this is undefined + }).bind(this)).bind(this)('a'); // return await Promise.all(['a', 'b'].map(async (x) => x + this.val)); // this is undefined } catch (e) { throw e; diff --git a/tests/fixture/issue-1490/full/output/index.js b/tests/fixture/issue-1490/full/output/index.js index 9b5b376e8cdd..b2277fcad278 100644 --- a/tests/fixture/issue-1490/full/output/index.js +++ b/tests/fixture/issue-1490/full/output/index.js @@ -180,7 +180,7 @@ var ColouredCanvasElement = function(CanvasElement) { { key: "createFacets", value: function createFacets(hidden) { - hidden = _get(_getPrototypeOf(ColouredCanvasElement.prototype), "createFacets", this).call(this, hidden); /// + hidden = _get(_getPrototypeOf(ColouredCanvasElement.prototype), "createFacets", this).call(this, hidden); } } ]); From ca8645f9759a007acbc4b9038e3c7d535136c686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:08:46 +0900 Subject: [PATCH 49/56] Make test for #532 fixture --- .../issue-532/block}/input.js | 0 tests/projects.rs | 21 ------------------- 2 files changed, 21 deletions(-) rename tests/{projects/issue-532-block => fixture/issue-532/block}/input.js (100%) diff --git a/tests/projects/issue-532-block/input.js b/tests/fixture/issue-532/block/input.js similarity index 100% rename from tests/projects/issue-532-block/input.js rename to tests/fixture/issue-532/block/input.js diff --git a/tests/projects.rs b/tests/projects.rs index c4f4e9b51701..3392c419db97 100644 --- a/tests/projects.rs +++ b/tests/projects.rs @@ -337,27 +337,6 @@ fn project_env() { project("tests/projects/env/"); } -#[test] -fn issue_532() { - let f = file("tests/projects/issue-532-block/input.js") - .unwrap() - .replace(" ", ""); - let f = f.trim(); - - println!("{}", f); - - assert_eq!( - f, - "\ -/*pre:1*/test(); -test(123/*post:3*/); -test(/*pre:4*/123); -test(/*pre:5*/123/*post:6*/); -test(/*pre:7*/123,/*pre:8*/456); -test(123/*post:9*/,456/*post:10*/);" - ) -} - #[test] fn issue_602() { let f = file("tests/projects/issue-602/input.js").unwrap(); From 3a18115e94cf198366329e4377f11042f095fcd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:10:55 +0900 Subject: [PATCH 50/56] Bump version --- ecmascript/codegen/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecmascript/codegen/Cargo.toml b/ecmascript/codegen/Cargo.toml index 794de0554b50..713284b8358b 100644 --- a/ecmascript/codegen/Cargo.toml +++ b/ecmascript/codegen/Cargo.toml @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"] license = "Apache-2.0/MIT" name = "swc_ecma_codegen" repository = "https://github.com/swc-project/swc.git" -version = "0.50.2" +version = "0.50.3" [dependencies] bitflags = "1" From b05d604381873a5e25f4e2fd25be3d269cf4bd40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:11:39 +0900 Subject: [PATCH 51/56] fixup --- tests/fixture/issue-532/block/{input.js => input/index.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/fixture/issue-532/block/{input.js => input/index.js} (100%) diff --git a/tests/fixture/issue-532/block/input.js b/tests/fixture/issue-532/block/input/index.js similarity index 100% rename from tests/fixture/issue-532/block/input.js rename to tests/fixture/issue-532/block/input/index.js From 494963d23f21098b7026df7c70f76a4e7ee7986f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:15:56 +0900 Subject: [PATCH 52/56] Add a test --- ecmascript/parser/tests/comments/exprs/call/simple/input.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 ecmascript/parser/tests/comments/exprs/call/simple/input.js diff --git a/ecmascript/parser/tests/comments/exprs/call/simple/input.js b/ecmascript/parser/tests/comments/exprs/call/simple/input.js new file mode 100644 index 000000000000..155fbc792194 --- /dev/null +++ b/ecmascript/parser/tests/comments/exprs/call/simple/input.js @@ -0,0 +1 @@ +test(123/*post: 9*/, 456/*post: 10*/); From 7a21149f9e17912f641ac2ee67631e89eafa7594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:19:06 +0900 Subject: [PATCH 53/56] Fix coment bug --- ecmascript/parser/tests/comments.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ecmascript/parser/tests/comments.rs b/ecmascript/parser/tests/comments.rs index 43b0c31d9850..41a731a9ed32 100644 --- a/ecmascript/parser/tests/comments.rs +++ b/ecmascript/parser/tests/comments.rs @@ -145,17 +145,14 @@ impl Visit for CommentPrinter<'_> { } }); - self.comments.with_trailing(n.hi - BytePos(1), |comments| { + self.comments.with_trailing(n.hi, |comments| { for c in comments { DiagnosticBuilder::new( &self.handler, swc_common::errors::Level::Note, "Trailing (hi)", ) - .span_note( - Span::new(n.hi - BytePos(1), n.hi - BytePos(1), Default::default()), - &c.text, - ) + .span_note(Span::new(n.hi, n.hi, Default::default()), &c.text) .emit(); } }); From f5fb742cb69640be11183830be85882d03d46c47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:19:29 +0900 Subject: [PATCH 54/56] Update test refs --- .../comments/exprs/call/simple/input.js.stderr | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ecmascript/parser/tests/comments/exprs/call/simple/input.js.stderr diff --git a/ecmascript/parser/tests/comments/exprs/call/simple/input.js.stderr b/ecmascript/parser/tests/comments/exprs/call/simple/input.js.stderr new file mode 100644 index 000000000000..753305986b7d --- /dev/null +++ b/ecmascript/parser/tests/comments/exprs/call/simple/input.js.stderr @@ -0,0 +1,16 @@ +note: Trailing (hi) + | +note: post: 9 + --> $DIR/tests/comments/exprs/call/simple/input.js:1:9 + | +1 | test(123/*post: 9*/, 456/*post: 10*/); + | ^ + +note: Trailing (hi) + | +note: post: 10 + --> $DIR/tests/comments/exprs/call/simple/input.js:1:25 + | +1 | test(123/*post: 9*/, 456/*post: 10*/); + | ^ + From 992d0777bf4070f64cd060c2331dfa5e53391e20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:19:49 +0900 Subject: [PATCH 55/56] Fix codegen --- ecmascript/codegen/src/comments.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ecmascript/codegen/src/comments.rs b/ecmascript/codegen/src/comments.rs index fe4faf1976bd..46a50fb424b4 100644 --- a/ecmascript/codegen/src/comments.rs +++ b/ecmascript/codegen/src/comments.rs @@ -37,18 +37,14 @@ macro_rules! write_comments { impl<'a> Emitter<'a> { pub(super) fn emit_trailing_comments_of_pos( &mut self, - mut pos: BytePos, + pos: BytePos, prefix_space: bool, - is_hi: bool, + _is_hi: bool, ) -> Result { if pos == BytePos(0) { return Ok(()); } - if is_hi { - pos = pos - BytePos(1) - } - let comments = match self.comments { Some(ref comments) => comments, None => return Ok(()), From 822dcd1b4c10e9f080e8fdaef6417ffd40db8107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 2 Apr 2021 17:21:02 +0900 Subject: [PATCH 56/56] Update test refs --- tests/fixture/issue-1333/case2/output/index.js | 14 +++++++++++++- tests/fixture/issue-1341/case1/output/index.ts | 2 +- tests/fixture/issue-1490/full/output/index.js | 2 +- tests/fixture/issue-532/block/output/index.js | 6 ++++++ 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/fixture/issue-532/block/output/index.js diff --git a/tests/fixture/issue-1333/case2/output/index.js b/tests/fixture/issue-1333/case2/output/index.js index 1172bdd316c0..070cb8786fa4 100644 --- a/tests/fixture/issue-1333/case2/output/index.js +++ b/tests/fixture/issue-1333/case2/output/index.js @@ -426,7 +426,19 @@ class Shard extends _utils.Emitter { })); _classPrivateFieldSet(this, _presenceBucket, new _utils.Bucket(5, 60000)); } -} +} /** + * @typedef {Object} DiscordPacket + * @property {number} [op] + * @property {*} [d] + * @property {number | null} [s] + * @property {string} [t] + */ /** + * @typedef {Object} ShardDestroyOptions + * @property {boolean} [reset=false] Whether to reset the shard. + * @property {boolean} [emit=true] Whether to emit the "destroyed" event. + * @property {boolean} [log=false] Whether to emit a debug log. + * @property {number} [code=1000] The code to use. + */ exports.Shard = Shard; var _serialization2 = new WeakMap(); var _compression2 = new WeakMap(); diff --git a/tests/fixture/issue-1341/case1/output/index.ts b/tests/fixture/issue-1341/case1/output/index.ts index 4e3c680898e9..e12d3b32c6d5 100644 --- a/tests/fixture/issue-1341/case1/output/index.ts +++ b/tests/fixture/issue-1341/case1/output/index.ts @@ -34,7 +34,7 @@ class A { try { return yield _asyncToGenerator((function*(x) { return x + this.val; - }).bind(this)).bind(this)('a'); + }).bind(this)).bind(this)('a'); // this is undefined // return await Promise.all(['a', 'b'].map(async (x) => x + this.val)); // this is undefined } catch (e) { throw e; diff --git a/tests/fixture/issue-1490/full/output/index.js b/tests/fixture/issue-1490/full/output/index.js index b2277fcad278..9b5b376e8cdd 100644 --- a/tests/fixture/issue-1490/full/output/index.js +++ b/tests/fixture/issue-1490/full/output/index.js @@ -180,7 +180,7 @@ var ColouredCanvasElement = function(CanvasElement) { { key: "createFacets", value: function createFacets(hidden) { - hidden = _get(_getPrototypeOf(ColouredCanvasElement.prototype), "createFacets", this).call(this, hidden); + hidden = _get(_getPrototypeOf(ColouredCanvasElement.prototype), "createFacets", this).call(this, hidden); /// } } ]); diff --git a/tests/fixture/issue-532/block/output/index.js b/tests/fixture/issue-532/block/output/index.js new file mode 100644 index 000000000000..3484b2370387 --- /dev/null +++ b/tests/fixture/issue-532/block/output/index.js @@ -0,0 +1,6 @@ +/*pre: 1*/ test(); +test(123/*post: 3*/ ); +test(/*pre: 4*/ 123); +test(/*pre: 5*/ 123/*post: 6*/ ); +test(/*pre: 7*/ 123, /*pre: 8*/ 456); +test(123/*post: 9*/ , 456/*post: 10*/ );