Skip to content

Commit

Permalink
fix #2557: remove warning about top-level this
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
evanw committed Sep 18, 2022
1 parent be4e272 commit 93e068d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 201 deletions.
111 changes: 28 additions & 83 deletions internal/bundler/bundler_default_test.go
Expand Up @@ -1937,39 +1937,40 @@ func TestThisWithES6Syntax(t *testing.T) {
Mode: config.ModeBundle,
AbsOutputFile: "/out.js",
},
expectedScanLog: `es6-export-abstract-class.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
debugLogs: true,
expectedScanLog: `es6-export-abstract-class.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-abstract-class.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-async-function.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-async-function.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-async-function.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-class.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-class.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-class.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-clause-from.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-clause-from.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-clause-from.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-clause.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-clause.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-clause.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-const-enum.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-const-enum.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-const-enum.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-default.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-default.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-default.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-enum.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-enum.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-enum.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-function.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-function.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-function.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-import-assign.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-import-assign.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-import-assign.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-module.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-module.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-module.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-namespace.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-namespace.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-namespace.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-star-as.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-star-as.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-star-as.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-star.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-star.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-star.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-export-variable.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-variable.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-export-variable.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
es6-expr-import-meta.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-expr-import-meta.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-expr-import-meta.js: NOTE: This file is considered to be an ECMAScript module because of the use of "import.meta" here:
es6-import-meta.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-import-meta.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
es6-import-meta.js: NOTE: This file is considered to be an ECMAScript module because of the use of "import.meta" here:
`,
})
Expand Down Expand Up @@ -5121,9 +5122,10 @@ func TestJSXThisValueESM(t *testing.T) {
},
AbsOutputDir: "/out",
},
expectedScanLog: `factory.jsx: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
debugLogs: true,
expectedScanLog: `factory.jsx: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
factory.jsx: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
fragment.jsx: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
fragment.jsx: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
fragment.jsx: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
`,
})
Expand Down Expand Up @@ -5208,9 +5210,10 @@ func TestJSXThisPropertyESM(t *testing.T) {
},
AbsOutputDir: "/out",
},
expectedScanLog: `factory.jsx: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
debugLogs: true,
expectedScanLog: `factory.jsx: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
factory.jsx: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
fragment.jsx: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
fragment.jsx: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
fragment.jsx: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
`,
})
Expand Down Expand Up @@ -5486,8 +5489,11 @@ func TestThisUndefinedWarningESM(t *testing.T) {
Mode: config.ModeBundle,
AbsOutputDir: "/out",
},
expectedScanLog: `file1.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
debugLogs: true,
expectedScanLog: `file1.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
file1.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
node_modules/pkg/file2.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
node_modules/pkg/file2.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
`,
})
}
Expand Down Expand Up @@ -6736,67 +6742,6 @@ b.js: NOTE: Another definition of "x" comes from "b.js" here:
})
}

func TestThisIsUndefinedWarningBabelCompiledJSX(t *testing.T) {
loader_suite.expectBundled(t, bundled{
files: map[string]string{
"/no1.js": `
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
export var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this);
`,
"/no2.js": `
import { jsxDEV } from "react/jsx-dev-runtime";
export var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this);
`,

"/yes1.js": `
import { jsxDEV as _jsxDEV } from "react/jsx-runtime";
export var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this);
`,
"/yes2.js": `
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
export var Foo = () => _jsxDEV("div", {}, void 0, false, this, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 });
`,
"/yes3.js": `
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
export var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this, null);
`,
"/yes4.js": `
import { _jsxDEV } from "react/jsx-dev-runtime";
export var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, this);
`,

"/node_modules/react/jsx-runtime.js": `
export var jsxDEV
`,
"/node_modules/react/jsx-dev-runtime.js": `
export var jsxDEV, _jsxDEV
`,
},
entryPaths: []string{
"/no1.js",
"/no2.js",

"/yes1.js",
"/yes2.js",
"/yes3.js",
"/yes4.js",
},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
},
expectedScanLog: `yes1.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
yes1.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
yes2.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
yes2.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
yes3.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
yes3.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
yes4.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
yes4.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
`,
})
}

// See: https://github.com/evanw/esbuild/issues/2537
func TestNonDeterminismIssue2537(t *testing.T) {
loader_suite.expectBundled(t, bundled{
Expand Down
3 changes: 2 additions & 1 deletion internal/bundler/bundler_lower_test.go
Expand Up @@ -876,7 +876,8 @@ func TestLowerAsyncThis2016ES6(t *testing.T) {
UnsupportedJSFeatures: es(2016),
AbsOutputFile: "/out.js",
},
expectedScanLog: `entry.js: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
debugLogs: true,
expectedScanLog: `entry.js: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
entry.js: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
`,
})
Expand Down
7 changes: 4 additions & 3 deletions internal/bundler/bundler_ts_test.go
Expand Up @@ -2057,11 +2057,12 @@ func TestTSThisIsUndefinedWarning(t *testing.T) {
Mode: config.ModeBundle,
AbsOutputDir: "/out",
},
expectedScanLog: `warning1.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
debugLogs: true,
expectedScanLog: `warning1.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
warning1.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
warning2.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
warning2.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
warning2.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
warning3.ts: WARNING: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
warning3.ts: DEBUG: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
warning3.ts: NOTE: This file is considered to be an ECMAScript module because of the "export" keyword here:
`,
})
Expand Down
62 changes: 0 additions & 62 deletions internal/bundler/snapshots/snapshots_loader.txt
Expand Up @@ -1045,68 +1045,6 @@ export default {
_bar_: 1
};

================================================================================
TestThisIsUndefinedWarningBabelCompiledJSX
---------- /out/no1.js ----------
// node_modules/react/jsx-dev-runtime.js
var jsxDEV;

// no1.js
var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0);
export {
Foo
};

---------- /out/no2.js ----------
// node_modules/react/jsx-dev-runtime.js
var jsxDEV;

// no2.js
var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0);
export {
Foo
};

---------- /out/yes1.js ----------
// node_modules/react/jsx-runtime.js
var jsxDEV;

// yes1.js
var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0);
export {
Foo
};

---------- /out/yes2.js ----------
// node_modules/react/jsx-dev-runtime.js
var jsxDEV;

// yes2.js
var Foo = () => jsxDEV("div", {}, void 0, false, void 0, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 });
export {
Foo
};

---------- /out/yes3.js ----------
// node_modules/react/jsx-dev-runtime.js
var jsxDEV;

// yes3.js
var Foo = () => jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0, null);
export {
Foo
};

---------- /out/yes4.js ----------
// node_modules/react/jsx-dev-runtime.js
var _jsxDEV;

// yes4.js
var Foo = () => _jsxDEV("div", {}, void 0, false, { fileName: "Foo.tsx", lineNumber: 1, columnNumber: 23 }, void 0);
export {
Foo
};

================================================================================
TestToESMWrapperOmission
---------- /out/entry.js ----------
Expand Down

0 comments on commit 93e068d

Please sign in to comment.