Skip to content

Commit

Permalink
fix(common): Fix logic for excluding FileName from source maps (#7900)
Browse files Browse the repository at this point in the history
**Description:**

This PR also modifies React transform to use `FileName::Internal` and makes `SourceMapGenConfig` skips `FileName::Internal`.

**Related issue:**

 - Closes #5272
  • Loading branch information
kdy1 authored Aug 31, 2023
1 parent e5f7a9d commit aa64955
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 10 deletions.
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\";\nexport class Foo extends Base {\n bar() {\n return 1 + this.foo();\n }\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\";\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
}
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);
});
});
32 changes: 32 additions & 0 deletions crates/swc/tests/fixture/issues-5xxx/5272/1/output/source/a/a.ts
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
5 changes: 4 additions & 1 deletion crates/swc_ecma_transforms_optimization/src/const_modules.rs
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

1 comment on commit aa64955

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: aa64955 Previous: c714dd2 Ratio
es/full/bugs-1 280628 ns/iter (± 5331) 280569 ns/iter (± 2456) 1.00
es/full/minify/libraries/antd 1304215294 ns/iter (± 8869837) 1287545691 ns/iter (± 12038060) 1.01
es/full/minify/libraries/d3 273847872 ns/iter (± 3444517) 271112637 ns/iter (± 4971427) 1.01
es/full/minify/libraries/echarts 1042977928 ns/iter (± 3513275) 1026138777 ns/iter (± 4407801) 1.02
es/full/minify/libraries/jquery 83156038 ns/iter (± 181838) 83323898 ns/iter (± 87492) 1.00
es/full/minify/libraries/lodash 96792320 ns/iter (± 178328) 96771578 ns/iter (± 333612) 1.00
es/full/minify/libraries/moment 49279230 ns/iter (± 123532) 49516224 ns/iter (± 178763) 1.00
es/full/minify/libraries/react 17838421 ns/iter (± 131766) 17878526 ns/iter (± 45739) 1.00
es/full/minify/libraries/terser 215340552 ns/iter (± 913117) 215673514 ns/iter (± 916209) 1.00
es/full/minify/libraries/three 381908258 ns/iter (± 2231572) 380330150 ns/iter (± 843736) 1.00
es/full/minify/libraries/typescript 2609878206 ns/iter (± 9740386) 2594888248 ns/iter (± 13358142) 1.01
es/full/minify/libraries/victory 555183081 ns/iter (± 2989671) 556224352 ns/iter (± 3011580) 1.00
es/full/minify/libraries/vue 118084901 ns/iter (± 299658) 117981534 ns/iter (± 580107) 1.00
es/full/codegen/es3 34608 ns/iter (± 59) 33254 ns/iter (± 57) 1.04
es/full/codegen/es5 34501 ns/iter (± 61) 33246 ns/iter (± 132) 1.04
es/full/codegen/es2015 34850 ns/iter (± 1019) 33310 ns/iter (± 136) 1.05
es/full/codegen/es2016 34631 ns/iter (± 77) 33319 ns/iter (± 136) 1.04
es/full/codegen/es2017 34615 ns/iter (± 173) 33244 ns/iter (± 122) 1.04
es/full/codegen/es2018 34561 ns/iter (± 39) 33541 ns/iter (± 88) 1.03
es/full/codegen/es2019 34595 ns/iter (± 75) 33412 ns/iter (± 133) 1.04
es/full/codegen/es2020 34625 ns/iter (± 85) 33495 ns/iter (± 165) 1.03
es/full/all/es3 168254806 ns/iter (± 1301071) 169414785 ns/iter (± 1097111) 0.99
es/full/all/es5 159439700 ns/iter (± 1087981) 161252794 ns/iter (± 742131) 0.99
es/full/all/es2015 119757720 ns/iter (± 668073) 120578525 ns/iter (± 694718) 0.99
es/full/all/es2016 118609520 ns/iter (± 584228) 119333251 ns/iter (± 947487) 0.99
es/full/all/es2017 117321568 ns/iter (± 723892) 118899121 ns/iter (± 470567) 0.99
es/full/all/es2018 116235109 ns/iter (± 459972) 116405869 ns/iter (± 865652) 1.00
es/full/all/es2019 115619869 ns/iter (± 624074) 116222778 ns/iter (± 452146) 0.99
es/full/all/es2020 110738411 ns/iter (± 484225) 111838267 ns/iter (± 559620) 0.99
es/full/parser 483137 ns/iter (± 5017) 492766 ns/iter (± 4333) 0.98
es/full/base/fixer 19566 ns/iter (± 132) 19466 ns/iter (± 286) 1.01
es/full/base/resolver_and_hygiene 80521 ns/iter (± 179) 81350 ns/iter (± 181) 0.99
serialization of serde 292 ns/iter (± 0) 293 ns/iter (± 0) 1.00
css/minify/libraries/bootstrap 28632362 ns/iter (± 35847) 28950870 ns/iter (± 29351) 0.99
css/visitor/compare/clone 1623491 ns/iter (± 2246) 1613626 ns/iter (± 3225) 1.01
css/visitor/compare/visit_mut_span 1753361 ns/iter (± 5560) 1756795 ns/iter (± 6745) 1.00
css/visitor/compare/visit_mut_span_panic 1811776 ns/iter (± 7586) 1819507 ns/iter (± 2758) 1.00
css/visitor/compare/fold_span 2544639 ns/iter (± 6827) 2545710 ns/iter (± 5672) 1.00
css/visitor/compare/fold_span_panic 2756553 ns/iter (± 7741) 2732503 ns/iter (± 7928) 1.01
css/lexer/bootstrap_5_1_3 4517821 ns/iter (± 10224) 4482035 ns/iter (± 3678) 1.01
css/lexer/foundation_6_7_4 3787629 ns/iter (± 9823) 3772597 ns/iter (± 1368) 1.00
css/lexer/tailwind_3_1_1 720181 ns/iter (± 1835) 718741 ns/iter (± 430) 1.00
css/parser/bootstrap_5_1_3 19370990 ns/iter (± 43308) 18943710 ns/iter (± 30904) 1.02
css/parser/foundation_6_7_4 15367679 ns/iter (± 16171) 15208248 ns/iter (± 9294) 1.01
css/parser/tailwind_3_1_1 2964892 ns/iter (± 4517) 2956485 ns/iter (± 2735) 1.00
es/codegen/colors 732624 ns/iter (± 401990) 731820 ns/iter (± 399994) 1.00
es/codegen/large 3150171 ns/iter (± 1688483) 3142439 ns/iter (± 1668549) 1.00
es/codegen/with-parser/colors 44447 ns/iter (± 553) 44959 ns/iter (± 47) 0.99
es/codegen/with-parser/large 480275 ns/iter (± 2208) 481881 ns/iter (± 1089) 1.00
es/minify/libraries/antd 1159615366 ns/iter (± 12149059) 1120610405 ns/iter (± 10654513) 1.03
es/minify/libraries/d3 237999454 ns/iter (± 1254132) 236623868 ns/iter (± 521258) 1.01
es/minify/libraries/echarts 894669912 ns/iter (± 13732259) 882193973 ns/iter (± 5152319) 1.01
es/minify/libraries/jquery 72863573 ns/iter (± 237897) 72880340 ns/iter (± 102576) 1.00
es/minify/libraries/lodash 86755698 ns/iter (± 147892) 86365458 ns/iter (± 243704) 1.00
es/minify/libraries/moment 43270626 ns/iter (± 90890) 43272507 ns/iter (± 67877) 1.00
es/minify/libraries/react 15907124 ns/iter (± 48865) 15950955 ns/iter (± 50827) 1.00
es/minify/libraries/terser 184357685 ns/iter (± 478766) 184432306 ns/iter (± 631156) 1.00
es/minify/libraries/three 319156278 ns/iter (± 825059) 317328068 ns/iter (± 947800) 1.01
es/minify/libraries/typescript 2214282612 ns/iter (± 9205167) 2213918585 ns/iter (± 3386407) 1.00
es/minify/libraries/victory 463942297 ns/iter (± 1520798) 469431860 ns/iter (± 1256551) 0.99
es/minify/libraries/vue 105365415 ns/iter (± 168985) 104683259 ns/iter (± 171048) 1.01
es/visitor/compare/clone 1930916 ns/iter (± 6083) 1917501 ns/iter (± 5103) 1.01
es/visitor/compare/visit_mut_span 2273367 ns/iter (± 8639) 2262946 ns/iter (± 6830) 1.00
es/visitor/compare/visit_mut_span_panic 2310412 ns/iter (± 7754) 2290958 ns/iter (± 3836) 1.01
es/visitor/compare/fold_span 3298212 ns/iter (± 7375) 3313132 ns/iter (± 6678) 1.00
es/visitor/compare/fold_span_panic 3451946 ns/iter (± 5678) 3454101 ns/iter (± 6055) 1.00
es/lexer/colors 13105 ns/iter (± 42) 13132 ns/iter (± 86) 1.00
es/lexer/angular 6058310 ns/iter (± 6634) 6001923 ns/iter (± 5677) 1.01
es/lexer/backbone 785587 ns/iter (± 1502) 784906 ns/iter (± 4172) 1.00
es/lexer/jquery 4491766 ns/iter (± 6937) 4424869 ns/iter (± 3079) 1.02
es/lexer/jquery mobile 6845951 ns/iter (± 19240) 6794077 ns/iter (± 4369) 1.01
es/lexer/mootools 3533913 ns/iter (± 12819) 3504743 ns/iter (± 2770) 1.01
es/lexer/underscore 664242 ns/iter (± 1563) 651265 ns/iter (± 592) 1.02
es/lexer/three 21454814 ns/iter (± 37875) 21216119 ns/iter (± 22749) 1.01
es/lexer/yui 3807684 ns/iter (± 4623) 3812553 ns/iter (± 2386) 1.00
es/parser/colors 27235 ns/iter (± 180) 26629 ns/iter (± 34) 1.02
es/parser/angular 13405636 ns/iter (± 51758) 13270413 ns/iter (± 35429) 1.01
es/parser/backbone 1971400 ns/iter (± 7868) 1953768 ns/iter (± 7895) 1.01
es/parser/jquery 10812270 ns/iter (± 40807) 10694569 ns/iter (± 40931) 1.01
es/parser/jquery mobile 16449758 ns/iter (± 34066) 16388215 ns/iter (± 50339) 1.00
es/parser/mootools 8332841 ns/iter (± 18133) 8229827 ns/iter (± 19408) 1.01
es/parser/underscore 1707761 ns/iter (± 7232) 1685595 ns/iter (± 9168) 1.01
es/parser/three 46488151 ns/iter (± 104538) 46087519 ns/iter (± 108772) 1.01
es/parser/yui 8247970 ns/iter (± 25640) 8164867 ns/iter (± 24226) 1.01
es/preset-env/usage/builtin_type 139218 ns/iter (± 32783) 135554 ns/iter (± 32503) 1.03
es/preset-env/usage/property 16886 ns/iter (± 49) 16177 ns/iter (± 62) 1.04
es/resolver/typescript 90157207 ns/iter (± 1314261) 89528477 ns/iter (± 1111851) 1.01
es/fixer/typescript 63178593 ns/iter (± 716556) 62380048 ns/iter (± 677492) 1.01
es/hygiene/typescript 129450887 ns/iter (± 514825) 127745338 ns/iter (± 386996) 1.01
es/resolver_with_hygiene/typescript 241473030 ns/iter (± 819258) 239374852 ns/iter (± 954964) 1.01
es/visitor/base-perf/module_clone 60741 ns/iter (± 296) 59783 ns/iter (± 310) 1.02
es/visitor/base-perf/fold_empty 64127 ns/iter (± 268) 63828 ns/iter (± 254) 1.00
es/visitor/base-perf/fold_noop_impl_all 65109 ns/iter (± 242) 64511 ns/iter (± 235) 1.01
es/visitor/base-perf/fold_noop_impl_vec 65034 ns/iter (± 397) 64764 ns/iter (± 322) 1.00
es/visitor/base-perf/boxing_boxed_clone 57 ns/iter (± 0) 57 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed_clone 39 ns/iter (± 0) 40 ns/iter (± 0) 0.97
es/visitor/base-perf/boxing_boxed 106 ns/iter (± 0) 106 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed 75 ns/iter (± 0) 75 ns/iter (± 0) 1
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2540 ns/iter (± 11) 2485 ns/iter (± 14) 1.02
es/base/parallel/resolver/typescript 4219568887 ns/iter (± 282081759) 3527875316 ns/iter (± 267294222) 1.20
es/base/parallel/hygiene/typescript 1435281187 ns/iter (± 14686868) 1427183703 ns/iter (± 15496422) 1.01
misc/visitors/time-complexity/time 5 138 ns/iter (± 0) 118 ns/iter (± 0) 1.17
misc/visitors/time-complexity/time 10 372 ns/iter (± 14) 388 ns/iter (± 7) 0.96
misc/visitors/time-complexity/time 15 669 ns/iter (± 2) 659 ns/iter (± 5) 1.02
misc/visitors/time-complexity/time 20 907 ns/iter (± 4) 1041 ns/iter (± 47) 0.87
misc/visitors/time-complexity/time 40 3558 ns/iter (± 11) 4127 ns/iter (± 4) 0.86
misc/visitors/time-complexity/time 60 7224 ns/iter (± 25) 7254 ns/iter (± 36) 1.00
es/full-target/es2016 234726 ns/iter (± 1548) 227857 ns/iter (± 360) 1.03
es/full-target/es2017 226324 ns/iter (± 460) 218501 ns/iter (± 1061) 1.04
es/full-target/es2018 212973 ns/iter (± 635) 206220 ns/iter (± 577) 1.03
es2020_nullish_coalescing 70239 ns/iter (± 350) 69060 ns/iter (± 529) 1.02
es2020_optional_chaining 81812 ns/iter (± 274) 79645 ns/iter (± 195) 1.03
es2022_class_properties 116187 ns/iter (± 327) 113298 ns/iter (± 450) 1.03
es2018_object_rest_spread 75045 ns/iter (± 219) 73387 ns/iter (± 166) 1.02
es2019_optional_catch_binding 64598 ns/iter (± 139) 63070 ns/iter (± 223) 1.02
es2017_async_to_generator 63421 ns/iter (± 274) 62419 ns/iter (± 177) 1.02
es2016_exponentiation 69274 ns/iter (± 193) 64824 ns/iter (± 178) 1.07
es2015_arrow 72027 ns/iter (± 196) 70342 ns/iter (± 196) 1.02
es2015_block_scoped_fn 68394 ns/iter (± 231) 66884 ns/iter (± 235) 1.02
es2015_block_scoping 119572 ns/iter (± 773) 122795 ns/iter (± 763) 0.97

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.