Skip to content

Commit 93e068d

Browse files
committed
fix #2557: remove warning about top-level this
The warning is still available as a debug log message, which can be made visible with either `--log-level=debug` or `--log-override:this-is-undefined-in-esm=warning`.
1 parent be4e272 commit 93e068d

File tree

5 files changed

+51
-201
lines changed

5 files changed

+51
-201
lines changed

internal/bundler/bundler_default_test.go

Lines changed: 28 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,39 +1937,40 @@ func TestThisWithES6Syntax(t *testing.T) {
19371937
Mode: config.ModeBundle,
19381938
AbsOutputFile: "/out.js",
19391939
},
1940-
expectedScanLog: `es6-export-abstract-class.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1940+
debugLogs: true,
1941+
expectedScanLog: `es6-export-abstract-class.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19411942
es6-export-abstract-class.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1942-
es6-export-async-function.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1943+
es6-export-async-function.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19431944
es6-export-async-function.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1944-
es6-export-class.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1945+
es6-export-class.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19451946
es6-export-class.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1946-
es6-export-clause-from.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1947+
es6-export-clause-from.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19471948
es6-export-clause-from.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1948-
es6-export-clause.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1949+
es6-export-clause.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19491950
es6-export-clause.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1950-
es6-export-const-enum.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1951+
es6-export-const-enum.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19511952
es6-export-const-enum.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1952-
es6-export-default.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1953+
es6-export-default.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19531954
es6-export-default.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1954-
es6-export-enum.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1955+
es6-export-enum.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19551956
es6-export-enum.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1956-
es6-export-function.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1957+
es6-export-function.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19571958
es6-export-function.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1958-
es6-export-import-assign.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1959+
es6-export-import-assign.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19591960
es6-export-import-assign.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1960-
es6-export-module.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1961+
es6-export-module.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19611962
es6-export-module.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1962-
es6-export-namespace.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1963+
es6-export-namespace.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19631964
es6-export-namespace.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1964-
es6-export-star-as.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1965+
es6-export-star-as.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19651966
es6-export-star-as.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1966-
es6-export-star.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1967+
es6-export-star.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19671968
es6-export-star.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1968-
es6-export-variable.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1969+
es6-export-variable.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19691970
es6-export-variable.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
1970-
es6-expr-import-meta.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1971+
es6-expr-import-meta.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19711972
es6-expr-import-meta.js: NOTE: This file is considered to be an ECMAScript module because of the use of "import.meta" here:
1972-
es6-import-meta.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
1973+
es6-import-meta.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
19731974
es6-import-meta.js: NOTE: This file is considered to be an ECMAScript module because of the use of "import.meta" here:
19741975
`,
19751976
})
@@ -5121,9 +5122,10 @@ func TestJSXThisValueESM(t *testing.T) {
51215122
},
51225123
AbsOutputDir: "/out",
51235124
},
5124-
expectedScanLog: `factory.jsx: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
5125+
debugLogs: true,
5126+
expectedScanLog: `factory.jsx: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
51255127
factory.jsx: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
5126-
fragment.jsx: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
5128+
fragment.jsx: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
51275129
fragment.jsx: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
51285130
`,
51295131
})
@@ -5208,9 +5210,10 @@ func TestJSXThisPropertyESM(t *testing.T) {
52085210
},
52095211
AbsOutputDir: "/out",
52105212
},
5211-
expectedScanLog: `factory.jsx: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
5213+
debugLogs: true,
5214+
expectedScanLog: `factory.jsx: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
52125215
factory.jsx: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
5213-
fragment.jsx: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
5216+
fragment.jsx: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
52145217
fragment.jsx: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
52155218
`,
52165219
})
@@ -5486,8 +5489,11 @@ func TestThisUndefinedWarningESM(t *testing.T) {
54865489
Mode: config.ModeBundle,
54875490
AbsOutputDir: "/out",
54885491
},
5489-
expectedScanLog: `file1.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
5492+
debugLogs: true,
5493+
expectedScanLog: `file1.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
54905494
file1.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
5495+
node_modules/pkg/file2.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
5496+
node_modules/pkg/file2.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
54915497
`,
54925498
})
54935499
}
@@ -6736,67 +6742,6 @@ b.js: NOTE: Another definition of "x" comes from "b.js" here:
67366742
})
67376743
}
67386744

6739-
func TestThisIsUndefinedWarningBabelCompiledJSX(t *testing.T) {
6740-
loader_suite.expectBundled(t, bundled{
6741-
files: map[string]string{
6742-
"/no1.js": `
6743-
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
6744-
export var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this);
6745-
`,
6746-
"/no2.js": `
6747-
import { jsxDEV } from "react/jsx-dev-runtime";
6748-
export var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this);
6749-
`,
6750-
6751-
"/yes1.js": `
6752-
import { jsxDEV as _jsxDEV } from "react/jsx-runtime";
6753-
export var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this);
6754-
`,
6755-
"/yes2.js": `
6756-
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
6757-
export var Foo = () => _jsxDEV("div", {}, void 0, false, this, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 });
6758-
`,
6759-
"/yes3.js": `
6760-
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
6761-
export var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this, null);
6762-
`,
6763-
"/yes4.js": `
6764-
import { _jsxDEV } from "react/jsx-dev-runtime";
6765-
export var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this);
6766-
`,
6767-
6768-
"/node_modules/react/jsx-runtime.js": `
6769-
export var jsxDEV
6770-
`,
6771-
"/node_modules/react/jsx-dev-runtime.js": `
6772-
export var jsxDEV, _jsxDEV
6773-
`,
6774-
},
6775-
entryPaths: []string{
6776-
"/no1.js",
6777-
"/no2.js",
6778-
6779-
"/yes1.js",
6780-
"/yes2.js",
6781-
"/yes3.js",
6782-
"/yes4.js",
6783-
},
6784-
options: config.Options{
6785-
Mode: config.ModeBundle,
6786-
AbsOutputDir: "/out",
6787-
},
6788-
expectedScanLog: `yes1.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
6789-
yes1.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
6790-
yes2.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
6791-
yes2.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
6792-
yes3.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
6793-
yes3.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
6794-
yes4.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
6795-
yes4.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
6796-
`,
6797-
})
6798-
}
6799-
68006745
// See: https://github.com/evanw/esbuild/issues/2537
68016746
func TestNonDeterminismIssue2537(t *testing.T) {
68026747
loader_suite.expectBundled(t, bundled{

internal/bundler/bundler_lower_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,8 @@ func TestLowerAsyncThis2016ES6(t *testing.T) {
876876
UnsupportedJSFeatures: es(2016),
877877
AbsOutputFile: "/out.js",
878878
},
879-
expectedScanLog: `entry.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
879+
debugLogs: true,
880+
expectedScanLog: `entry.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
880881
entry.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
881882
`,
882883
})

internal/bundler/bundler_ts_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,11 +2057,12 @@ func TestTSThisIsUndefinedWarning(t *testing.T) {
20572057
Mode: config.ModeBundle,
20582058
AbsOutputDir: "/out",
20592059
},
2060-
expectedScanLog: `warning1.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
2060+
debugLogs: true,
2061+
expectedScanLog: `warning1.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
20612062
warning1.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
2062-
warning2.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
2063+
warning2.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
20632064
warning2.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
2064-
warning3.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
2065+
warning3.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
20652066
warning3.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
20662067
`,
20672068
})

internal/bundler/snapshots/snapshots_loader.txt

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,68 +1045,6 @@ export default {
10451045
_bar_: 1
10461046
};
10471047

1048-
================================================================================
1049-
TestThisIsUndefinedWarningBabelCompiledJSX
1050-
---------- /out/no1.js ----------
1051-
// node_modules/react/jsx-dev-runtime.js
1052-
var jsxDEV;
1053-
1054-
// no1.js
1055-
var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0);
1056-
export {
1057-
Foo
1058-
};
1059-
1060-
---------- /out/no2.js ----------
1061-
// node_modules/react/jsx-dev-runtime.js
1062-
var jsxDEV;
1063-
1064-
// no2.js
1065-
var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0);
1066-
export {
1067-
Foo
1068-
};
1069-
1070-
---------- /out/yes1.js ----------
1071-
// node_modules/react/jsx-runtime.js
1072-
var jsxDEV;
1073-
1074-
// yes1.js
1075-
var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0);
1076-
export {
1077-
Foo
1078-
};
1079-
1080-
---------- /out/yes2.js ----------
1081-
// node_modules/react/jsx-dev-runtime.js
1082-
var jsxDEV;
1083-
1084-
// yes2.js
1085-
var Foo = () => jsxDEV("div", {}, void 0, false, void 0, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 });
1086-
export {
1087-
Foo
1088-
};
1089-
1090-
---------- /out/yes3.js ----------
1091-
// node_modules/react/jsx-dev-runtime.js
1092-
var jsxDEV;
1093-
1094-
// yes3.js
1095-
var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0, null);
1096-
export {
1097-
Foo
1098-
};
1099-
1100-
---------- /out/yes4.js ----------
1101-
// node_modules/react/jsx-dev-runtime.js
1102-
var _jsxDEV;
1103-
1104-
// yes4.js
1105-
var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0);
1106-
export {
1107-
Foo
1108-
};
1109-
11101048
================================================================================
11111049
TestToESMWrapperOmission
11121050
---------- /out/entry.js ----------

0 commit comments

Comments
 (0)