Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): Fix logic for excluding FileName from source maps #7900

Merged
merged 14 commits into from
Aug 31, 2023
8 changes: 4 additions & 4 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions crates/swc/tests/fixture/issues-5xxx/5272/1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -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
}
}
},
}
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Base } from "../b/base";
export class Foo extends Base {
bar() {
return 1 + this.foo();
}
}
16 changes: 16 additions & 0 deletions crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.map
Original file line number Diff line number Diff line change
@@ -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\";\r\nexport class Foo extends Base {\r\n bar() {\r\n return 1 + this.foo();\r\n }\r\n}"
],
"version": 3
}
20 changes: 20 additions & 0 deletions crates/swc/tests/fixture/issues-5xxx/5272/1/output/a.spec.map
Original file line number Diff line number Diff line change
@@ -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\";\r\n\r\ndescribe(\"a\", () => {\r\n it(\"does its thing\", () => {\r\n const a = new Foo();\r\n expect(a.bar()).toBe(3);\r\n expect(a.foo()).toBe(2);\r\n });\r\n});"
],
"version": 3
}
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 6 additions & 2 deletions crates/swc_common/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(..))
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/swc_ecma_transforms_module/src/umd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ impl Config {
.into_iter()
.map(|(k, v)| {
let parse = |s| {
let fm = cm
.new_source_file(FileName::Custom(format!("<umd-config-{}.js>", s)), s);
let fm = cm.new_source_file(
FileName::Internal(format!("<umd-config-{}.js>", s)),
s,
);

parse_file_as_expr(
&fm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ pub fn const_modules(
fn parse_option(cm: &SourceMap, name: &str, src: String) -> Arc<Expr> {
static CACHE: Lazy<DashMap<String, Arc<Expr>, ARandomState>> = Lazy::new(DashMap::default);

let fm = cm.new_source_file(FileName::Custom(format!("<const-module-{}.js>", name)), src);
let fm = cm.new_source_file(
FileName::Internal(format!("<const-module-{}.js>", name)),
src,
);
if let Some(expr) = CACHE.get(&**fm.src) {
return expr.clone();
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_react/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn parse_expr_for_jsx(
src: String,
top_level_mark: Mark,
) -> Arc<Box<Expr>> {
let fm = cm.new_source_file(FileName::Custom(format!("<jsx-config-{}.js>", name)), src);
let fm = cm.new_source_file(FileName::Internal(format!("<jsx-config-{}.js>", name)), src);

parse_file_as_expr(
&fm,
Expand Down
Loading