From aa6495519b9271cb21d380c0c5a35fe79d31ee14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 31 Aug 2023 22:23:11 +0900 Subject: [PATCH] fix(common): Fix logic for excluding `FileName` from source maps (#7900) **Description:** This PR also modifies React transform to use `FileName::Internal` and makes `SourceMapGenConfig` skips `FileName::Internal`. **Related issue:** - Closes #5272 --- crates/swc/src/lib.rs | 8 ++--- .../fixture/issues-5xxx/5272/1/input/.swcrc | 20 ++++++++++++ .../5272/1/input/source/a/a.spec.ts | 9 ++++++ .../issues-5xxx/5272/1/input/source/a/a.ts | 6 ++++ .../fixture/issues-5xxx/5272/1/output/a.map | 16 ++++++++++ .../issues-5xxx/5272/1/output/a.spec.map | 20 ++++++++++++ .../5272/1/output/source/a/a.spec.ts | 11 +++++++ .../issues-5xxx/5272/1/output/source/a/a.ts | 32 +++++++++++++++++++ crates/swc_common/src/source_map.rs | 8 +++-- .../src/umd/config.rs | 6 ++-- .../src/const_modules.rs | 5 ++- .../swc_ecma_transforms_react/src/jsx/mod.rs | 2 +- 12 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-5xxx/5272/1/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-5xxx/5272/1/input/source/a/a.spec.ts create mode 100644 crates/swc/tests/fixture/issues-5xxx/5272/1/input/source/a/a.ts create mode 100644 crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.map create mode 100644 crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.spec.map create mode 100644 crates/swc/tests/fixture/issues-5xxx/5272/1/output/source/a/a.spec.ts create mode 100644 crates/swc/tests/fixture/issues-5xxx/5272/1/output/source/a/a.ts diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index 0a29f12587e..6ef1ade4d10 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -681,10 +681,10 @@ impl SourceMapGenConfig for SwcSourceMapConfig<'_> { } fn skip(&self, f: &FileName) -> bool { - if let FileName::Custom(s) = f { - s.starts_with('<') - } else { - false + match f { + FileName::Internal(..) => true, + FileName::Custom(s) => s.starts_with('<'), + _ => false, } } } diff --git a/crates/swc/tests/fixture/issues-5xxx/5272/1/input/.swcrc b/crates/swc/tests/fixture/issues-5xxx/5272/1/input/.swcrc new file mode 100644 index 00000000000..39df3d29849 --- /dev/null +++ b/crates/swc/tests/fixture/issues-5xxx/5272/1/input/.swcrc @@ -0,0 +1,20 @@ +{ + "sourceMaps": true, + "module": { + "type": "commonjs", + "strict": false, + "strictMode": false + }, + "jsc": { + "target": "es5", + "parser": { + "syntax": "typescript", + "dynamicImport": true + }, + "transform": { + "hidden": { + "jest": true + } + } + }, +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-5xxx/5272/1/input/source/a/a.spec.ts b/crates/swc/tests/fixture/issues-5xxx/5272/1/input/source/a/a.spec.ts new file mode 100644 index 00000000000..3b3fa5e6b6f --- /dev/null +++ b/crates/swc/tests/fixture/issues-5xxx/5272/1/input/source/a/a.spec.ts @@ -0,0 +1,9 @@ +import { Foo } from "./a"; + +describe("a", () => { + it("does its thing", () => { + const a = new Foo(); + expect(a.bar()).toBe(3); + expect(a.foo()).toBe(2); + }); +}); \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-5xxx/5272/1/input/source/a/a.ts b/crates/swc/tests/fixture/issues-5xxx/5272/1/input/source/a/a.ts new file mode 100644 index 00000000000..4e741aeb16a --- /dev/null +++ b/crates/swc/tests/fixture/issues-5xxx/5272/1/input/source/a/a.ts @@ -0,0 +1,6 @@ +import { Base } from "../b/base"; +export class Foo extends Base { + bar() { + return 1 + this.foo(); + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.map b/crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.map new file mode 100644 index 00000000000..926b19ade3c --- /dev/null +++ b/crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.map @@ -0,0 +1,16 @@ +{ + "mappings": ";;;+BACaA;;;eAAAA;;;;;;;oBADQ;AACd,IAAA,AAAMA,oBAAN;;gBAAMA;iCAAAA;aAAAA;kCAAAA;;;oBAAAA;;YACTC,KAAAA;mBAAAA,SAAAA;gBACI,OAAO,IAAI,IAAI,CAACC,GAAG;YACvB;;;WAHSF;EAAYG,UAAI", + "names": [ + "Foo", + "bar", + "foo", + "Base" + ], + "sources": [ + "../../input/source/a/a.ts" + ], + "sourcesContent": [ + "import { Base } from \"../b/base\";\nexport class Foo extends Base {\n bar() {\n return 1 + this.foo();\n }\n}" + ], + "version": 3 +} diff --git a/crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.spec.map b/crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.spec.map new file mode 100644 index 00000000000..0326470833b --- /dev/null +++ b/crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.spec.map @@ -0,0 +1,20 @@ +{ + "mappings": ";;;iBAAoB;AAEpBA,SAAS,KAAK;IACVC,GAAG,kBAAkB;QACjB,IAAMC,IAAI,IAAIC,MAAG;QACjBC,OAAOF,EAAEG,GAAG,IAAIC,IAAI,CAAC;QACrBF,OAAOF,EAAEK,GAAG,IAAID,IAAI,CAAC;IACzB;AACJ", + "names": [ + "describe", + "it", + "a", + "Foo", + "expect", + "bar", + "toBe", + "foo" + ], + "sources": [ + "../../input/source/a/a.spec.ts" + ], + "sourcesContent": [ + "import { Foo } from \"./a\";\n\ndescribe(\"a\", () => {\n it(\"does its thing\", () => {\n const a = new Foo();\n expect(a.bar()).toBe(3);\n expect(a.foo()).toBe(2);\n });\n});" + ], + "version": 3 +} diff --git a/crates/swc/tests/fixture/issues-5xxx/5272/1/output/source/a/a.spec.ts b/crates/swc/tests/fixture/issues-5xxx/5272/1/output/source/a/a.spec.ts new file mode 100644 index 00000000000..ac3485a50d9 --- /dev/null +++ b/crates/swc/tests/fixture/issues-5xxx/5272/1/output/source/a/a.spec.ts @@ -0,0 +1,11 @@ +Object.defineProperty(exports, "__esModule", { + value: true +}); +var _a = require("./a"); +describe("a", function() { + it("does its thing", function() { + var a = new _a.Foo(); + expect(a.bar()).toBe(3); + expect(a.foo()).toBe(2); + }); +}); diff --git a/crates/swc/tests/fixture/issues-5xxx/5272/1/output/source/a/a.ts b/crates/swc/tests/fixture/issues-5xxx/5272/1/output/source/a/a.ts new file mode 100644 index 00000000000..19644b89285 --- /dev/null +++ b/crates/swc/tests/fixture/issues-5xxx/5272/1/output/source/a/a.ts @@ -0,0 +1,32 @@ +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "Foo", { + enumerable: true, + get: function() { + return Foo; + } +}); +var _class_call_check = require("@swc/helpers/_/_class_call_check"); +var _create_class = require("@swc/helpers/_/_create_class"); +var _inherits = require("@swc/helpers/_/_inherits"); +var _create_super = require("@swc/helpers/_/_create_super"); +var _base = require("../b/base"); +var Foo = /*#__PURE__*/ function(Base) { + "use strict"; + _inherits._(Foo, Base); + var _super = _create_super._(Foo); + function Foo() { + _class_call_check._(this, Foo); + return _super.apply(this, arguments); + } + _create_class._(Foo, [ + { + key: "bar", + value: function bar() { + return 1 + this.foo(); + } + } + ]); + return Foo; +}(_base.Base); diff --git a/crates/swc_common/src/source_map.rs b/crates/swc_common/src/source_map.rs index 1b8c97f8aa2..cc121bee11c 100644 --- a/crates/swc_common/src/source_map.rs +++ b/crates/swc_common/src/source_map.rs @@ -1266,6 +1266,9 @@ impl SourceMap { Some(ref f) if f.start_pos <= pos && pos < f.end_pos => f, _ => { f = self.lookup_source_file(pos); + if config.skip(&f.name) { + continue; + } src_id = builder.add_source(&config.file_name_to_source(&f.name)); inline_sources_content = config.inline_sources_content(&f.name); @@ -1447,8 +1450,9 @@ pub trait SourceMapGenConfig { true } - fn skip(&self, _f: &FileName) -> bool { - false + /// By default, we skip internal files. + fn skip(&self, f: &FileName) -> bool { + matches!(f, FileName::Internal(..)) } } diff --git a/crates/swc_ecma_transforms_module/src/umd/config.rs b/crates/swc_ecma_transforms_module/src/umd/config.rs index 3fb840eef0b..1564b7283bc 100644 --- a/crates/swc_ecma_transforms_module/src/umd/config.rs +++ b/crates/swc_ecma_transforms_module/src/umd/config.rs @@ -29,8 +29,10 @@ impl Config { .into_iter() .map(|(k, v)| { let parse = |s| { - let fm = cm - .new_source_file(FileName::Custom(format!("", s)), s); + let fm = cm.new_source_file( + FileName::Internal(format!("", s)), + s, + ); parse_file_as_expr( &fm, diff --git a/crates/swc_ecma_transforms_optimization/src/const_modules.rs b/crates/swc_ecma_transforms_optimization/src/const_modules.rs index 5e578e07c1f..ed71111e62c 100644 --- a/crates/swc_ecma_transforms_optimization/src/const_modules.rs +++ b/crates/swc_ecma_transforms_optimization/src/const_modules.rs @@ -46,7 +46,10 @@ pub fn const_modules( fn parse_option(cm: &SourceMap, name: &str, src: String) -> Arc { static CACHE: Lazy, ARandomState>> = Lazy::new(DashMap::default); - let fm = cm.new_source_file(FileName::Custom(format!("", name)), src); + let fm = cm.new_source_file( + FileName::Internal(format!("", name)), + src, + ); if let Some(expr) = CACHE.get(&**fm.src) { return expr.clone(); } diff --git a/crates/swc_ecma_transforms_react/src/jsx/mod.rs b/crates/swc_ecma_transforms_react/src/jsx/mod.rs index 66842c9c97e..ea53f552cda 100644 --- a/crates/swc_ecma_transforms_react/src/jsx/mod.rs +++ b/crates/swc_ecma_transforms_react/src/jsx/mod.rs @@ -118,7 +118,7 @@ pub fn parse_expr_for_jsx( src: String, top_level_mark: Mark, ) -> Arc> { - let fm = cm.new_source_file(FileName::Custom(format!("", name)), src); + let fm = cm.new_source_file(FileName::Internal(format!("", name)), src); parse_file_as_expr( &fm,