From ce01b8a9b7249a961e02ce7c4bb1c9ead8f314b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 18 Sep 2021 16:52:08 +0900 Subject: [PATCH] fix(es): Fix bugs (#2256) swc_ecma_codegen: - Fix source map of string literals. (#2253) swc_ecma_transforms_typescript: - Allow using same name for a module with function. (#2243) --- Cargo.lock | 6 +- ecmascript/codegen/Cargo.toml | 2 +- ecmascript/codegen/src/lib.rs | 8 +- ecmascript/transforms/typescript/Cargo.toml | 2 +- ecmascript/transforms/typescript/src/strip.rs | 5 +- .../{.issue-2243 => issue-2243}/input.ts | 4 +- .../tests/fixture/issue-2243/output.js | 33 ++++++++ package.json | 2 +- .../fixture/deno-10014/case1/output/index.map | 2 +- tests/fixture/issue-2232/case2/input/index.ts | 9 +++ .../fixture/issue-2232/case2/output/index.ts | 79 +++++++++++++++++++ wasm/Cargo.toml | 2 +- 12 files changed, 140 insertions(+), 14 deletions(-) rename ecmascript/transforms/typescript/tests/fixture/{.issue-2243 => issue-2243}/input.ts (89%) create mode 100644 ecmascript/transforms/typescript/tests/fixture/issue-2243/output.js create mode 100644 tests/fixture/issue-2232/case2/input/index.ts create mode 100644 tests/fixture/issue-2232/case2/output/index.ts diff --git a/Cargo.lock b/Cargo.lock index 37f4c255141..9eea22b8a25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2557,7 +2557,7 @@ dependencies = [ [[package]] name = "swc_ecma_codegen" -version = "0.70.3" +version = "0.70.4" dependencies = [ "bitflags", "num-bigint", @@ -2922,7 +2922,7 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_typescript" -version = "0.40.3" +version = "0.40.4" dependencies = [ "fxhash", "serde", @@ -3401,7 +3401,7 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] name = "wasm" -version = "1.2.88" +version = "1.2.89" dependencies = [ "anyhow", "console_error_panic_hook", diff --git a/ecmascript/codegen/Cargo.toml b/ecmascript/codegen/Cargo.toml index 3f0e9e3c835..9bf1acc8118 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.70.3" +version = "0.70.4" [dependencies] bitflags = "1" diff --git a/ecmascript/codegen/src/lib.rs b/ecmascript/codegen/src/lib.rs index 52208983adc..0e7375033ae 100644 --- a/ecmascript/codegen/src/lib.rs +++ b/ecmascript/codegen/src/lib.rs @@ -434,12 +434,12 @@ impl<'a> Emitter<'a> { }; if single_quote { - punct!("'"); - self.wr.write_str_lit(node.span, &value)?; + punct!(node.span, "'"); + self.wr.write_str_lit(DUMMY_SP, &value)?; punct!("'"); } else { - punct!("\""); - self.wr.write_str_lit(node.span, &value)?; + punct!(node.span, "\""); + self.wr.write_str_lit(DUMMY_SP, &value)?; punct!("\""); } } diff --git a/ecmascript/transforms/typescript/Cargo.toml b/ecmascript/transforms/typescript/Cargo.toml index f0a2dc3084e..dfe26d101aa 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.40.3" +version = "0.40.4" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/ecmascript/transforms/typescript/src/strip.rs b/ecmascript/transforms/typescript/src/strip.rs index ea8d27f4b12..e0dc2d6a0eb 100644 --- a/ecmascript/transforms/typescript/src/strip.rs +++ b/ecmascript/transforms/typescript/src/strip.rs @@ -1181,7 +1181,10 @@ impl Visit for Strip { self.decl_names.insert(class.ident.to_id()); class.class.visit_with(class, self); } - Decl::Fn(f) => f.function.visit_with(f, self), + Decl::Fn(f) => { + self.decl_names.insert(f.ident.to_id()); + f.function.visit_with(f, self) + } Decl::Var(ref var) => { for decl in &var.decls { self.in_var_pat = true; diff --git a/ecmascript/transforms/typescript/tests/fixture/.issue-2243/input.ts b/ecmascript/transforms/typescript/tests/fixture/issue-2243/input.ts similarity index 89% rename from ecmascript/transforms/typescript/tests/fixture/.issue-2243/input.ts rename to ecmascript/transforms/typescript/tests/fixture/issue-2243/input.ts index f104c86bcb3..21066747cbf 100644 --- a/ecmascript/transforms/typescript/tests/fixture/.issue-2243/input.ts +++ b/ecmascript/transforms/typescript/tests/fixture/issue-2243/input.ts @@ -1,3 +1,5 @@ +export type Colors = 0.0 | 1.0 | 2.0; + export function Colors(member: Colors.KeyType): Colors { return Colors.ValueFor(member); } @@ -17,7 +19,7 @@ export module Colors { return ValueMap[member]?.value; } - export function LabelFor( + export async function LabelFor( member: KeyType ): Promise { return ValueMap[member]?.label; diff --git a/ecmascript/transforms/typescript/tests/fixture/issue-2243/output.js b/ecmascript/transforms/typescript/tests/fixture/issue-2243/output.js new file mode 100644 index 00000000000..c3cf7e25811 --- /dev/null +++ b/ecmascript/transforms/typescript/tests/fixture/issue-2243/output.js @@ -0,0 +1,33 @@ +export function Colors(member) { + return Colors.ValueFor(member); +} +(function(Colors) { + Colors.ValueMap = { + Red: { + value: 0, + label: "Red" + }, + Blue: { + value: 1, + label: "Blue" + }, + Green: { + value: 2, + label: "Green" + } + }; + Colors.Values = [ + 0, + 1, + 2 + ]; + function ValueFor(member) { + return ValueMap[member]?.value; + } + Colors.ValueFor = ValueFor; + async function LabelFor(member) { + return ValueMap[member]?.label; + } + Colors.LabelFor = LabelFor; +})(Colors || (Colors = { +})); diff --git a/package.json b/package.json index e9434f196d2..c1677404f61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@swc/core", - "version": "1.2.88", + "version": "1.2.89", "description": "Super-fast alternative for babel", "homepage": "https://swc.rs", "main": "./index.js", diff --git a/tests/fixture/deno-10014/case1/output/index.map b/tests/fixture/deno-10014/case1/output/index.map index 2f64d837645..893a7e86081 100644 --- a/tests/fixture/deno-10014/case1/output/index.map +++ b/tests/fixture/deno-10014/case1/output/index.map @@ -1,5 +1,5 @@ { - "mappings": "QAAO,WAAa;SAEX,CAAC,GAAG,CAAC;IACV,CAAC;AACL,CAAC;SAEQ,CAAC,CAAC,KAAS,EAAE,CAAC;QAAZ,CAAC,GAAD,KAAS,cAAL,KAAK,GAAT,KAAS;IAChB,EAAE,EAAE,CAAC,EAAE,CAAC;QACJ,KAAK,CAAC,GAAG,CAAC,KAAK,EAAC,KAAO;IAC3B,CAAC;IACD,CAAC,EAAE,CAAC;AACR,CAAC;AAED,CAAC", + "mappings": "OAAO,CAAa;SAEX,CAAC,GAAG,CAAC;IACV,CAAC;AACL,CAAC;SAEQ,CAAC,CAAC,KAAS,EAAE,CAAC;QAAZ,CAAC,GAAD,KAAS,cAAL,KAAK,GAAT,KAAS;IAChB,EAAE,EAAE,CAAC,EAAE,CAAC;QACJ,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAO;IAC3B,CAAC;IACD,CAAC,EAAE,CAAC;AACR,CAAC;AAED,CAAC", "names": [ "a", "t", diff --git a/tests/fixture/issue-2232/case2/input/index.ts b/tests/fixture/issue-2232/case2/input/index.ts new file mode 100644 index 00000000000..72d65c69f23 --- /dev/null +++ b/tests/fixture/issue-2232/case2/input/index.ts @@ -0,0 +1,9 @@ +const resolver = { + async sendSomeMessage( + _parent: unknown, + { input: { toNumber, messageBody, ...all } }: SendSomeMessageInput, + { dataSources }: Context, + ): Promise { } +} + +export default resolver diff --git a/tests/fixture/issue-2232/case2/output/index.ts b/tests/fixture/issue-2232/case2/output/index.ts new file mode 100644 index 00000000000..801e9797f2e --- /dev/null +++ b/tests/fixture/issue-2232/case2/output/index.ts @@ -0,0 +1,79 @@ +import regeneratorRuntime from "regenerator-runtime"; +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); + }); + }; +} +function _objectWithoutProperties(source, excluded) { + if (source == null) return { + }; + var target = _objectWithoutPropertiesLoose(source, excluded); + var key, i; + if (Object.getOwnPropertySymbols) { + var sourceSymbolKeys = Object.getOwnPropertySymbols(source); + for(i = 0; i < sourceSymbolKeys.length; i++){ + key = sourceSymbolKeys[i]; + if (excluded.indexOf(key) >= 0) continue; + if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; + target[key] = source[key]; + } + } + return target; +} +function _objectWithoutPropertiesLoose(source, excluded) { + if (source == null) return { + }; + var target = { + }; + var sourceKeys = Object.keys(source); + var key, i; + for(i = 0; i < sourceKeys.length; i++){ + key = sourceKeys[i]; + if (excluded.indexOf(key) >= 0) continue; + target[key] = source[key]; + } + return target; +} +var resolver = { + sendSomeMessage: function(_parent, _param, _param1) { + return _asyncToGenerator(regeneratorRuntime.mark(function _callee() { + var toNumber, messageBody, all, dataSources; + return regeneratorRuntime.wrap(function _callee$(_ctx) { + var ref; + while(1)switch(_ctx.prev = _ctx.next){ + case 0: + var ref1, ref2; + ref1 = _param, ref = ref1.input, toNumber = ref.toNumber, messageBody = ref.messageBody, ref, ref1, all = _objectWithoutProperties(_param.input, ["toNumber", "messageBody"]), ref2 = _param1, dataSources = ref2.dataSources, ref2; + case 1: + case "end": + return _ctx.stop(); + } + }, _callee); + }))(); + } +}; +export default resolver; diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index a0c1b43a965..d984ddd001f 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -6,7 +6,7 @@ license = "Apache-2.0 AND MIT" name = "wasm" publish = false repository = "https://github.com/swc-project/swc.git" -version = "1.2.88" +version = "1.2.89" [lib] crate-type = ["cdylib"]