From 9d042bec023419e22a412f22a32eb5ebb88d8d84 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 24 Apr 2019 23:47:35 +0200 Subject: [PATCH 1/3] tools: fix node-core/required-modules eslint rule Make the node-core/required-modules eslint rule smart enough to recognize that `import '../common/index.mjs'` in ESM files imports the mandatory 'common' module. --- test/es-module/test-esm-basic-imports.mjs | 1 - test/es-module/test-esm-cyclic-dynamic-import.mjs | 1 - test/es-module/test-esm-double-encoding.mjs | 1 - test/es-module/test-esm-encoded-path.mjs | 1 - test/es-module/test-esm-forbidden-globals.mjs | 1 - test/es-module/test-esm-import-meta.mjs | 2 -- test/es-module/test-esm-json-cache.mjs | 1 - test/es-module/test-esm-json.mjs | 2 -- test/es-module/test-esm-live-binding.mjs | 2 -- test/es-module/test-esm-loader-invalid-format.mjs | 1 - test/es-module/test-esm-loader-invalid-url.mjs | 2 -- .../test-esm-loader-missing-dynamic-instantiate-hook.mjs | 2 -- test/es-module/test-esm-main-lookup.mjs | 1 - test/es-module/test-esm-named-exports.mjs | 1 - test/es-module/test-esm-namespace.mjs | 2 -- test/es-module/test-esm-process.mjs | 1 - test/es-module/test-esm-require-cache.mjs | 1 - test/es-module/test-esm-shared-loader-dep.mjs | 1 - test/es-module/test-esm-shebang.mjs | 1 - test/es-module/test-esm-snapshot.mjs | 1 - test/es-module/test-esm-throw-undefined.mjs | 2 -- test/es-module/test-esm-type-flag.mjs | 1 - test/message/esm_display_syntax_error_import.mjs | 2 +- test/message/esm_display_syntax_error_import_module.mjs | 1 - test/message/esm_display_syntax_error_module.mjs | 1 - test/parallel/test-loaders-unknown-builtin-module.mjs | 1 - tools/eslint-rules/required-modules.js | 4 ++++ 27 files changed, 5 insertions(+), 33 deletions(-) diff --git a/test/es-module/test-esm-basic-imports.mjs b/test/es-module/test-esm-basic-imports.mjs index d9bb22be0a5f12..c21804d78b292b 100644 --- a/test/es-module/test-esm-basic-imports.mjs +++ b/test/es-module/test-esm-basic-imports.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import assert from 'assert'; import ok from '../fixtures/es-modules/test-esm-ok.mjs'; diff --git a/test/es-module/test-esm-cyclic-dynamic-import.mjs b/test/es-module/test-esm-cyclic-dynamic-import.mjs index a207efc73ecb0a..03d405baf3ce9c 100644 --- a/test/es-module/test-esm-cyclic-dynamic-import.mjs +++ b/test/es-module/test-esm-cyclic-dynamic-import.mjs @@ -1,4 +1,3 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import('./test-esm-cyclic-dynamic-import.mjs'); diff --git a/test/es-module/test-esm-double-encoding.mjs b/test/es-module/test-esm-double-encoding.mjs index 9366d4bd6bcc68..2edfc8add5d2db 100644 --- a/test/es-module/test-esm-double-encoding.mjs +++ b/test/es-module/test-esm-double-encoding.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; // Assert we can import files with `%` in their pathname. diff --git a/test/es-module/test-esm-encoded-path.mjs b/test/es-module/test-esm-encoded-path.mjs index 2cabfdacff3954..efb681ef0b4dfe 100644 --- a/test/es-module/test-esm-encoded-path.mjs +++ b/test/es-module/test-esm-encoded-path.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import assert from 'assert'; // ./test-esm-ok.mjs diff --git a/test/es-module/test-esm-forbidden-globals.mjs b/test/es-module/test-esm-forbidden-globals.mjs index cf110ff2900eff..b66f278a8a9bb8 100644 --- a/test/es-module/test-esm-forbidden-globals.mjs +++ b/test/es-module/test-esm-forbidden-globals.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; // eslint-disable-next-line no-undef diff --git a/test/es-module/test-esm-import-meta.mjs b/test/es-module/test-esm-import-meta.mjs index 4c34b337fb8914..54c14a44f5e281 100644 --- a/test/es-module/test-esm-import-meta.mjs +++ b/test/es-module/test-esm-import-meta.mjs @@ -1,6 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ - import '../common/index.mjs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-json-cache.mjs b/test/es-module/test-esm-json-cache.mjs index ecd27c5488c7dc..d1fee4f444c2c0 100644 --- a/test/es-module/test-esm-json-cache.mjs +++ b/test/es-module/test-esm-json-cache.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules --experimental-json-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import { strictEqual, deepStrictEqual } from 'assert'; diff --git a/test/es-module/test-esm-json.mjs b/test/es-module/test-esm-json.mjs index b140d031cae8be..3d246124a9bdae 100644 --- a/test/es-module/test-esm-json.mjs +++ b/test/es-module/test-esm-json.mjs @@ -1,6 +1,4 @@ // Flags: --experimental-modules --experimental-json-modules -/* eslint-disable node-core/required-modules */ - import '../common/index.mjs'; import { strictEqual } from 'assert'; diff --git a/test/es-module/test-esm-live-binding.mjs b/test/es-module/test-esm-live-binding.mjs index 880a6c389b422c..5858b13bb51861 100644 --- a/test/es-module/test-esm-live-binding.mjs +++ b/test/es-module/test-esm-live-binding.mjs @@ -1,6 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ - import '../common/index.mjs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-loader-invalid-format.mjs b/test/es-module/test-esm-loader-invalid-format.mjs index c3f3a874079fa7..e4e4e30f5cc2e8 100644 --- a/test/es-module/test-esm-loader-invalid-format.mjs +++ b/test/es-module/test-esm-loader-invalid-format.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-invalid-format.mjs -/* eslint-disable node-core/required-modules */ import { expectsError, mustCall } from '../common/index.mjs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-loader-invalid-url.mjs b/test/es-module/test-esm-loader-invalid-url.mjs index 9cf17b2478ef13..44bacf9347c840 100644 --- a/test/es-module/test-esm-loader-invalid-url.mjs +++ b/test/es-module/test-esm-loader-invalid-url.mjs @@ -1,6 +1,4 @@ // Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-invalid-url.mjs -/* eslint-disable node-core/required-modules */ - import { expectsError, mustCall } from '../common/index.mjs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-loader-missing-dynamic-instantiate-hook.mjs b/test/es-module/test-esm-loader-missing-dynamic-instantiate-hook.mjs index ab2da7adcedb52..50fbf0c83ecea9 100644 --- a/test/es-module/test-esm-loader-missing-dynamic-instantiate-hook.mjs +++ b/test/es-module/test-esm-loader-missing-dynamic-instantiate-hook.mjs @@ -1,6 +1,4 @@ // Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/missing-dynamic-instantiate-hook.mjs -/* eslint-disable node-core/required-modules */ - import { expectsError } from '../common/index.mjs'; import('test').catch(expectsError({ diff --git a/test/es-module/test-esm-main-lookup.mjs b/test/es-module/test-esm-main-lookup.mjs index 19c025beab9ea9..cbc6424dd2ff1c 100644 --- a/test/es-module/test-esm-main-lookup.mjs +++ b/test/es-module/test-esm-main-lookup.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-named-exports.mjs b/test/es-module/test-esm-named-exports.mjs index e235f598cb1d34..8b7c429b3d802d 100644 --- a/test/es-module/test-esm-named-exports.mjs +++ b/test/es-module/test-esm-named-exports.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import { readFile } from 'fs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-namespace.mjs b/test/es-module/test-esm-namespace.mjs index 38b7ef12d585fc..07c58cd2dc99c8 100644 --- a/test/es-module/test-esm-namespace.mjs +++ b/test/es-module/test-esm-namespace.mjs @@ -1,6 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ - import '../common/index.mjs'; import * as fs from 'fs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-process.mjs b/test/es-module/test-esm-process.mjs index 3a23573d33b8b1..a2b0d31f1efa27 100644 --- a/test/es-module/test-esm-process.mjs +++ b/test/es-module/test-esm-process.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import assert from 'assert'; import process from 'process'; diff --git a/test/es-module/test-esm-require-cache.mjs b/test/es-module/test-esm-require-cache.mjs index 09030e0578e8c5..8c126c39e0eefe 100644 --- a/test/es-module/test-esm-require-cache.mjs +++ b/test/es-module/test-esm-require-cache.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import { createRequire } from '../common/index.mjs'; import assert from 'assert'; // diff --git a/test/es-module/test-esm-shared-loader-dep.mjs b/test/es-module/test-esm-shared-loader-dep.mjs index b8953ab1ec1a72..00ba1ec31a226a 100644 --- a/test/es-module/test-esm-shared-loader-dep.mjs +++ b/test/es-module/test-esm-shared-loader-dep.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-shared-dep.mjs -/* eslint-disable node-core/required-modules */ import { createRequire } from '../common/index.mjs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-shebang.mjs b/test/es-module/test-esm-shebang.mjs index 486e04dadece61..1d22551bd7a4be 100644 --- a/test/es-module/test-esm-shebang.mjs +++ b/test/es-module/test-esm-shebang.mjs @@ -1,6 +1,5 @@ #! }]) // isn't js // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; const isJs = true; diff --git a/test/es-module/test-esm-snapshot.mjs b/test/es-module/test-esm-snapshot.mjs index 3997e24ed703c6..99767f10e2846e 100644 --- a/test/es-module/test-esm-snapshot.mjs +++ b/test/es-module/test-esm-snapshot.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import '../fixtures/es-modules/esm-snapshot-mutator.js'; import one from '../fixtures/es-modules/esm-snapshot.js'; diff --git a/test/es-module/test-esm-throw-undefined.mjs b/test/es-module/test-esm-throw-undefined.mjs index 97e917da5e8d44..4c091e61532640 100644 --- a/test/es-module/test-esm-throw-undefined.mjs +++ b/test/es-module/test-esm-throw-undefined.mjs @@ -1,6 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ - import '../common/index.mjs'; import assert from 'assert'; diff --git a/test/es-module/test-esm-type-flag.mjs b/test/es-module/test-esm-type-flag.mjs index 4c04ef03e121f7..e820c9ad67458f 100644 --- a/test/es-module/test-esm-type-flag.mjs +++ b/test/es-module/test-esm-type-flag.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import cjs from '../fixtures/baz.js'; import '../common/index.mjs'; import { message } from '../fixtures/es-modules/message.mjs'; diff --git a/test/message/esm_display_syntax_error_import.mjs b/test/message/esm_display_syntax_error_import.mjs index 12d10270e96854..4c41b292efd4d6 100644 --- a/test/message/esm_display_syntax_error_import.mjs +++ b/test/message/esm_display_syntax_error_import.mjs @@ -1,5 +1,5 @@ // Flags: --experimental-modules -/* eslint-disable no-unused-vars, node-core/required-modules */ +/* eslint-disable no-unused-vars */ import '../common/index.mjs'; import { foo, diff --git a/test/message/esm_display_syntax_error_import_module.mjs b/test/message/esm_display_syntax_error_import_module.mjs index a53bbbcd19243f..346b3489fd920d 100644 --- a/test/message/esm_display_syntax_error_import_module.mjs +++ b/test/message/esm_display_syntax_error_import_module.mjs @@ -1,4 +1,3 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import '../fixtures/es-module-loaders/syntax-error-import.mjs'; diff --git a/test/message/esm_display_syntax_error_module.mjs b/test/message/esm_display_syntax_error_module.mjs index 5905d2a95478c1..1f2b87ecd0cb1a 100644 --- a/test/message/esm_display_syntax_error_module.mjs +++ b/test/message/esm_display_syntax_error_module.mjs @@ -1,4 +1,3 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import '../fixtures/es-module-loaders/syntax-error.mjs'; diff --git a/test/parallel/test-loaders-unknown-builtin-module.mjs b/test/parallel/test-loaders-unknown-builtin-module.mjs index 5f47f191f54094..b7d815c812ce3f 100644 --- a/test/parallel/test-loaders-unknown-builtin-module.mjs +++ b/test/parallel/test-loaders-unknown-builtin-module.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs -/* eslint-disable node-core/required-modules */ import { expectsError, mustCall } from '../common/index.mjs'; import assert from 'assert'; diff --git a/tools/eslint-rules/required-modules.js b/tools/eslint-rules/required-modules.js index f22b14caac4183..64b3d748afa630 100644 --- a/tools/eslint-rules/required-modules.js +++ b/tools/eslint-rules/required-modules.js @@ -46,6 +46,10 @@ module.exports = function(context) { * @returns {undefined|String} required module name or undefined */ function getRequiredModuleName(str) { + if (str === '../common/index.mjs') { + return 'common'; + } + const value = path.basename(str); // Check if value is in required modules array From 9bdd95feb71ac3973885d44e8f341830ae016105 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 24 Apr 2019 23:07:13 +0200 Subject: [PATCH 2/3] doc: document globalThis TC39 stage 3 proposal Document `globalThis` from the TC39 stage 3 proposal and doc-deprecate `global`. It's been available since V8 7.0 and Node.js v11.0.0. Float a one-line patch on top of eslint for the moment to teach it about the new global. I'll be opening an upstream pull request in tandem with this change. Refs: https://github.com/tc39/proposal-global --- doc/api/globals.md | 12 ++++++++++++ test/parallel/test-global-this.js | 11 +++++++++++ test/parallel/test-global-this.mjs | 10 ++++++++++ .../eslint/node_modules/globals/globals.json | 1 + 4 files changed, 34 insertions(+) create mode 100644 test/parallel/test-global-this.js create mode 100644 test/parallel/test-global-this.mjs diff --git a/doc/api/globals.md b/doc/api/globals.md index 7d1cc415bf8dbf..9eb36d2dc465d6 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -81,6 +81,18 @@ This variable may appear to be global but is not. See [`exports`]. ## global + + + +> Stability: 0 - Deprecated: Use [`globalThis)`][] instead. + +* {Object} The global namespace object. + +## globalThis + diff --git a/test/parallel/test-global-this.js b/test/parallel/test-global-this.js new file mode 100644 index 00000000000000..10798a16acdaac --- /dev/null +++ b/test/parallel/test-global-this.js @@ -0,0 +1,11 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +assert.strictEqual(typeof globalThis, 'object'); +assert.deepStrictEqual( + Object.getOwnPropertyDescriptor(globalThis, 'globalThis'), + { configurable: true, enumerable: false, writable: true, value: globalThis }); +assert.strictEqual(globalThis, global); +assert.strictEqual(String(globalThis), '[object global]'); diff --git a/test/parallel/test-global-this.mjs b/test/parallel/test-global-this.mjs new file mode 100644 index 00000000000000..f1d904d7094f54 --- /dev/null +++ b/test/parallel/test-global-this.mjs @@ -0,0 +1,10 @@ +// Flags: --experimental-modules +import '../common/index.mjs'; +import assert from 'assert'; + +assert.strictEqual(typeof globalThis, 'object'); +assert.deepStrictEqual( + Object.getOwnPropertyDescriptor(globalThis, 'globalThis'), + { configurable: true, enumerable: false, writable: true, value: globalThis }); +assert.strictEqual(globalThis, global); +assert.strictEqual(String(globalThis), '[object global]'); diff --git a/tools/node_modules/eslint/node_modules/globals/globals.json b/tools/node_modules/eslint/node_modules/globals/globals.json index 65a4c7ec783dc4..c13f11f48a7a6a 100644 --- a/tools/node_modules/eslint/node_modules/globals/globals.json +++ b/tools/node_modules/eslint/node_modules/globals/globals.json @@ -1013,6 +1013,7 @@ "console": false, "exports": true, "global": false, + "globalThis": false, "Intl": false, "module": false, "process": false, From cacb5f29b3b791bd050b9591609a6767591664b6 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 25 Apr 2019 10:20:01 +0200 Subject: [PATCH 3/3] squash! s/REPLACEME/v11.0.0/ --- doc/api/globals.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/globals.md b/doc/api/globals.md index 9eb36d2dc465d6..44cfcd9261fd65 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -92,7 +92,7 @@ deprecated: REPLACEME ## globalThis