From d7d6526260c7d5f150e75f7a18135be28eda0d33 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 3 May 2019 00:33:28 +0200 Subject: [PATCH] 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. PR-URL: https://github.com/nodejs/node/pull/27545 Reviewed-By: Richard Lau Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott Reviewed-By: Luigi Pinca --- 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/async_error_sync_esm.mjs | 1 - test/message/async_error_sync_esm.out | 2 +- 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 ++++ 29 files changed, 6 insertions(+), 35 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/async_error_sync_esm.mjs b/test/message/async_error_sync_esm.mjs index 86a901a1225572..f16256d7778b81 100644 --- a/test/message/async_error_sync_esm.mjs +++ b/test/message/async_error_sync_esm.mjs @@ -1,5 +1,4 @@ // Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ import '../common/index.mjs'; import four from '../fixtures/async-error.js'; diff --git a/test/message/async_error_sync_esm.out b/test/message/async_error_sync_esm.out index 99ce9ed0b21c2f..f34628ef44e52a 100644 --- a/test/message/async_error_sync_esm.out +++ b/test/message/async_error_sync_esm.out @@ -4,4 +4,4 @@ Error: test at two (*fixtures*async-error.js:17:9) at async three (*fixtures*async-error.js:20:3) at async four (*fixtures*async-error.js:24:3) - at async main (*message*async_error_sync_esm.mjs:8:5) + at async main (*message*async_error_sync_esm.mjs:7:5) 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