From 82a4aaad34523b1e80371d547b436592ebf0aebc Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Sun, 24 Sep 2023 14:02:31 -0700 Subject: [PATCH] Lint --- lib/internal/modules/esm/formats.js | 2 +- lib/internal/modules/esm/get_format.js | 17 +- lib/internal/modules/esm/resolve.js | 1 - test/es-module/test-esm-type-flag-errors.mjs | 45 ++--- .../test-esm-type-flag-loose-files.mjs | 116 ++++++------ .../test-esm-type-flag-package-scopes.mjs | 170 ++++++++++-------- .../test-esm-type-flag-string-input.mjs | 3 +- 7 files changed, 186 insertions(+), 168 deletions(-) diff --git a/lib/internal/modules/esm/formats.js b/lib/internal/modules/esm/formats.js index d92e82af74a4b4..b5b59e2d9dc14b 100644 --- a/lib/internal/modules/esm/formats.js +++ b/lib/internal/modules/esm/formats.js @@ -56,7 +56,7 @@ function getFormatOfExtensionlessFile(url) { return 'wasm'; } } finally { - if (fd) { closeSync(fd) }; + if (fd) { closeSync(fd); } } return 'module'; diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 17801b7af10b6b..8b2908876ebdfa 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -20,7 +20,8 @@ const { const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); const typeFlag = getOptionValue('--experimental-type'); -const defaultType = typeFlag === 'module' ? 'module' : 'commonjs'; // ! This is where we flip the default to ES modules someday +// The next line is where we flip the default to ES modules someday. +const defaultType = typeFlag === 'module' ? 'module' : 'commonjs'; const { getPackageType, getPackageScopeConfig } = require('internal/modules/esm/resolve'); const { fileURLToPath } = require('internal/url'); const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; @@ -73,7 +74,8 @@ function extname(url) { /** * Determine whether the given file URL is under a `node_modules` folder. - * This function assumes that the input has already been verified to be a `file:` URL, and is a file rather than a folder. + * This function assumes that the input has already been verified to be a `file:` URL, + * and is a file rather than a folder. * @param {URL} url */ function underNodeModules(url) { @@ -110,13 +112,13 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) { return 'commonjs'; } // If package type is `module`, fall through to the error case below - } else { // defaultType === 'module' + } else { // Else defaultType === 'module' if (underNodeModules(url)) { // Exception for package scopes under `node_modules` return 'commonjs'; } if (packageType === 'none' || packageType === 'module') { return getFormatOfExtensionlessFile(url); - } // else packageType === 'commonjs' + } // Else packageType === 'commonjs' return 'commonjs'; } } @@ -133,10 +135,9 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) { const fileBasename = basename(filepath); const relativePath = StringPrototypeSlice(relative(config.pjsonPath, filepath), 1); suggestion = 'Loading extensionless files is not supported inside of "type":"module" package.json contexts ' + - `without --experimental-type=module. The package.json file ${config.pjsonPath} caused this ` + - `"type":"module" context. Try changing ${filepath} to have a file extension. Note the "bin" ` + - 'field of package.json can point to a file with an extension, for example ' + - `{"type":"module","bin":{"${fileBasename}":"${relativePath}.js"}}`; + `without --experimental-type=module. The package.json file ${config.pjsonPath} caused this "type":"module" ` + + `context. Try changing ${filepath} to have a file extension. Note the "bin" field of package.json can point ` + + `to a file with an extension, for example {"type":"module","bin":{"${fileBasename}":"${relativePath}.js"}}`; } throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath, suggestion); } diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index acba2adcaee7a8..5aea5ca5460199 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -35,7 +35,6 @@ const preserveSymlinks = getOptionValue('--preserve-symlinks'); const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); -const typeFlag = getOptionValue('--experimental-type'); const inputTypeFlag = getOptionValue('--input-type'); const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url'); const { getCWDURL } = require('internal/util'); diff --git a/test/es-module/test-esm-type-flag-errors.mjs b/test/es-module/test-esm-type-flag-errors.mjs index 90cd30abd42f60..270b54c8ab93ff 100644 --- a/test/es-module/test-esm-type-flag-errors.mjs +++ b/test/es-module/test-esm-type-flag-errors.mjs @@ -3,28 +3,29 @@ import * as fixtures from '../common/fixtures.mjs'; import { describe, it } from 'node:test'; import { match, strictEqual } from 'node:assert'; -describe('--experimental-type=module should not affect the interpretation of files with unknown extensions', { concurrency: true }, () => { - it('should error on an entry point with an unknown extension', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - fixtures.path('es-modules/package-type-module/extension.unknown'), - ]); +describe('--experimental-type=module should not affect the interpretation of files with unknown extensions', + { concurrency: true }, () => { + it('should error on an entry point with an unknown extension', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + fixtures.path('es-modules/package-type-module/extension.unknown'), + ]); - match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); - strictEqual(stdout, ''); - strictEqual(code, 1); - strictEqual(signal, null); - }); + match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); + strictEqual(stdout, ''); + strictEqual(code, 1); + strictEqual(signal, null); + }); - it('should error on an import with an unknown extension', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - fixtures.path('es-modules/package-type-module/imports-unknownext.mjs'), - ]); + it('should error on an import with an unknown extension', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + fixtures.path('es-modules/package-type-module/imports-unknownext.mjs'), + ]); - match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); - strictEqual(stdout, ''); - strictEqual(code, 1); - strictEqual(signal, null); - }); -}); + match(stderr, /ERR_UNKNOWN_FILE_EXTENSION/); + strictEqual(stdout, ''); + strictEqual(code, 1); + strictEqual(signal, null); + }); + }); diff --git a/test/es-module/test-esm-type-flag-loose-files.mjs b/test/es-module/test-esm-type-flag-loose-files.mjs index 468e6338c5b197..19b9613d55a517 100644 --- a/test/es-module/test-esm-type-flag-loose-files.mjs +++ b/test/es-module/test-esm-type-flag-loose-files.mjs @@ -4,70 +4,72 @@ import * as fixtures from '../common/fixtures.mjs'; import { describe, it } from 'node:test'; import { strictEqual } from 'node:assert'; -describe('the type flag should change the interpretation of certain files outside of any package scope', { concurrency: true }, () => { - it('should run as ESM a .js file that is outside of any package scope', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - fixtures.path('es-modules/loose.js'), - ]); +describe('the type flag should change the interpretation of certain files outside of any package scope', + { concurrency: true }, () => { + it('should run as ESM a .js file that is outside of any package scope', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + fixtures.path('es-modules/loose.js'), + ]); - strictEqual(stderr, ''); - strictEqual(stdout, 'executed\n'); - strictEqual(code, 0); - strictEqual(signal, null); - }); + strictEqual(stderr, ''); + strictEqual(stdout, 'executed\n'); + strictEqual(code, 0); + strictEqual(signal, null); + }); - it('should run as ESM an extensionless JavaScript file that is outside of any package scope', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - fixtures.path('es-modules/noext-esm'), - ]); + it('should run as ESM an extensionless JavaScript file that is outside of any package scope', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + fixtures.path('es-modules/noext-esm'), + ]); - strictEqual(stderr, ''); - strictEqual(stdout, 'executed\n'); - strictEqual(code, 0); - strictEqual(signal, null); - }); + strictEqual(stderr, ''); + strictEqual(stdout, 'executed\n'); + strictEqual(code, 0); + strictEqual(signal, null); + }); - it('should run as Wasm an extensionless Wasm file that is outside of any package scope', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - '--experimental-wasm-modules', - '--no-warnings', - fixtures.path('es-modules/noext-wasm'), - ]); + it('should run as Wasm an extensionless Wasm file that is outside of any package scope', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + '--experimental-wasm-modules', + '--no-warnings', + fixtures.path('es-modules/noext-wasm'), + ]); - strictEqual(stderr, ''); - strictEqual(stdout, ''); - strictEqual(code, 0); - strictEqual(signal, null); - }); + strictEqual(stderr, ''); + strictEqual(stdout, ''); + strictEqual(code, 0); + strictEqual(signal, null); + }); - it('should import as ESM a .js file that is outside of any package scope', async () => { - const { default: defaultExport } = await import(fixtures.fileURL('es-modules/loose.js')); - strictEqual(defaultExport, 'module'); - }); + it('should import as ESM a .js file that is outside of any package scope', async () => { + const { default: defaultExport } = await import(fixtures.fileURL('es-modules/loose.js')); + strictEqual(defaultExport, 'module'); + }); - it('should import as ESM an extensionless JavaScript file that is outside of any package scope', async () => { - const { default: defaultExport } = await import(fixtures.fileURL('es-modules/noext-esm')); - strictEqual(defaultExport, 'module'); - }); + it('should import as ESM an extensionless JavaScript file that is outside of any package scope', + async () => { + const { default: defaultExport } = await import(fixtures.fileURL('es-modules/noext-esm')); + strictEqual(defaultExport, 'module'); + }); - it('should import as Wasm an extensionless Wasm file that is outside of any package scope', async () => { - const { add } = await import(fixtures.fileURL('es-modules/noext-wasm')); - strictEqual(add(1, 2), 3); - }); + it('should import as Wasm an extensionless Wasm file that is outside of any package scope', async () => { + const { add } = await import(fixtures.fileURL('es-modules/noext-wasm')); + strictEqual(add(1, 2), 3); + }); - it('should check as ESM input passed via --check', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - '--check', - fixtures.path('es-modules/loose.js'), - ]); + it('should check as ESM input passed via --check', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + '--check', + fixtures.path('es-modules/loose.js'), + ]); - strictEqual(stderr, ''); - strictEqual(stdout, ''); - strictEqual(code, 0); - strictEqual(signal, null); - }); -}); + strictEqual(stderr, ''); + strictEqual(stdout, ''); + strictEqual(code, 0); + strictEqual(signal, null); + }); + }); diff --git a/test/es-module/test-esm-type-flag-package-scopes.mjs b/test/es-module/test-esm-type-flag-package-scopes.mjs index 90b5f1ad2c5047..989d6561f52bab 100644 --- a/test/es-module/test-esm-type-flag-package-scopes.mjs +++ b/test/es-module/test-esm-type-flag-package-scopes.mjs @@ -4,58 +4,63 @@ import * as fixtures from '../common/fixtures.mjs'; import { describe, it } from 'node:test'; import { strictEqual } from 'node:assert'; -describe('the type flag should change the interpretation of certain files within a "type": "module" package scope', { concurrency: true }, () => { - it('should run as ESM an extensionless JavaScript file within a "type": "module" scope', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - fixtures.path('es-modules/package-type-module/noext-esm'), - ]); - - strictEqual(stderr, ''); - strictEqual(stdout, 'executed\n'); - strictEqual(code, 0); - strictEqual(signal, null); - }); - - it('should import an extensionless JavaScript file within a "type": "module" scope', async () => { - const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-type-module/noext-esm')); - strictEqual(defaultExport, 'module'); - }); - - it('should run as Wasm an extensionless Wasm file within a "type": "module" scope', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - '--experimental-wasm-modules', - '--no-warnings', - fixtures.path('es-modules/package-type-module/noext-wasm'), - ]); - - strictEqual(stderr, ''); - strictEqual(stdout, ''); - strictEqual(code, 0); - strictEqual(signal, null); - }); - - it('should import as Wasm an extensionless Wasm file within a "type": "module" scope', async () => { - const { add } = await import(fixtures.fileURL('es-modules/package-type-module/noext-wasm')); - strictEqual(add(1, 2), 3); - }); -}); - -describe('the type flag should change the interpretation of certain files within a package scope that lacks a "type" field and is not under node_modules', { concurrency: true }, () => { - it('should run as ESM a .js file within package scope that has no defined "type" and is not under node_modules', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - fixtures.path('es-modules/package-without-type/module.js'), - ]); - - strictEqual(stderr, ''); - strictEqual(stdout, 'executed\n'); - strictEqual(code, 0); - strictEqual(signal, null); - }); - - it('should run as ESM an extensionless JavaScript file within a package scope that has no defined "type" and is not under node_modules', async () => { +describe('the type flag should change the interpretation of certain files within a "type": "module" package scope', + { concurrency: true }, () => { + it('should run as ESM an extensionless JavaScript file within a "type": "module" scope', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + fixtures.path('es-modules/package-type-module/noext-esm'), + ]); + + strictEqual(stderr, ''); + strictEqual(stdout, 'executed\n'); + strictEqual(code, 0); + strictEqual(signal, null); + }); + + it('should import an extensionless JavaScript file within a "type": "module" scope', async () => { + const { default: defaultExport } = + await import(fixtures.fileURL('es-modules/package-type-module/noext-esm')); + strictEqual(defaultExport, 'module'); + }); + + it('should run as Wasm an extensionless Wasm file within a "type": "module" scope', async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + '--experimental-wasm-modules', + '--no-warnings', + fixtures.path('es-modules/package-type-module/noext-wasm'), + ]); + + strictEqual(stderr, ''); + strictEqual(stdout, ''); + strictEqual(code, 0); + strictEqual(signal, null); + }); + + it('should import as Wasm an extensionless Wasm file within a "type": "module" scope', async () => { + const { add } = await import(fixtures.fileURL('es-modules/package-type-module/noext-wasm')); + strictEqual(add(1, 2), 3); + }); + }); + +describe(`the type flag should change the interpretation of certain files within a package scope that lacks a +"type" field and is not under node_modules`, { concurrency: true }, () => { + it('should run as ESM a .js file within package scope that has no defined "type" and is not under node_modules', + async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + fixtures.path('es-modules/package-without-type/module.js'), + ]); + + strictEqual(stderr, ''); + strictEqual(stdout, 'executed\n'); + strictEqual(code, 0); + strictEqual(signal, null); + }); + + it(`should run as ESM an extensionless JavaScript file within a package scope that has no defined "type" and is not +under node_modules`, async () => { const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ '--experimental-type=module', fixtures.path('es-modules/package-without-type/noext-esm'), @@ -67,7 +72,8 @@ describe('the type flag should change the interpretation of certain files within strictEqual(signal, null); }); - it('should run as Wasm an extensionless Wasm file within a package scope that has no defined "type" and is not under node_modules', async () => { + it(`should run as Wasm an extensionless Wasm file within a package scope that has no defined "type" and is not under + node_modules`, async () => { const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ '--experimental-type=module', '--experimental-wasm-modules', @@ -81,41 +87,49 @@ describe('the type flag should change the interpretation of certain files within strictEqual(signal, null); }); - it('should import as ESM a .js file within package scope that has no defined "type" and is not under node_modules', async () => { - const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-without-type/module.js')); - strictEqual(defaultExport, 'module'); - }); + it('should import as ESM a .js file within package scope that has no defined "type" and is not under node_modules', + async () => { + const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-without-type/module.js')); + strictEqual(defaultExport, 'module'); + }); - it('should import as ESM an extensionless JavaScript file within a package scope that has no defined "type" and is not under node_modules', async () => { + it(`should import as ESM an extensionless JavaScript file within a package scope that has no defined "type" and is + not under node_modules`, async () => { const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-without-type/noext-esm')); strictEqual(defaultExport, 'module'); }); - it('should import as Wasm an extensionless Wasm file within a package scope that has no defined "type" and is not under node_modules', async () => { + it(`should import as Wasm an extensionless Wasm file within a package scope that has no defined "type" and is not + under node_modules`, async () => { const { add } = await import(fixtures.fileURL('es-modules/noext-wasm')); strictEqual(add(1, 2), 3); }); }); -describe('the type flag should NOT change the interpretation of certain files within a package scope that lacks a "type" field and is under node_modules', { concurrency: true }, () => { - it('should run as CommonJS a .js file within package scope that has no defined "type" and is under node_modules', async () => { - const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ - '--experimental-type=module', - fixtures.path('es-modules/package-type-module/node_modules/dep-with-package-json/run.js'), - ]); - - strictEqual(stderr, ''); - strictEqual(stdout, 'executed\n'); - strictEqual(code, 0); - strictEqual(signal, null); - }); - - it('should import as CommonJS a .js file within a package scope that has no defined "type" and is under node_modules', async () => { - const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-type-module/node_modules/dep-with-package-json/run.js')); +describe(`the type flag should NOT change the interpretation of certain files within a package scope that lacks a +"type" field and is under node_modules`, { concurrency: true }, () => { + it('should run as CommonJS a .js file within package scope that has no defined "type" and is under node_modules', + async () => { + const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ + '--experimental-type=module', + fixtures.path('es-modules/package-type-module/node_modules/dep-with-package-json/run.js'), + ]); + + strictEqual(stderr, ''); + strictEqual(stdout, 'executed\n'); + strictEqual(code, 0); + strictEqual(signal, null); + }); + + it(`should import as CommonJS a .js file within a package scope that has no defined "type" and is under + node_modules`, async () => { + const { default: defaultExport } = + await import(fixtures.fileURL('es-modules/package-type-module/node_modules/dep-with-package-json/run.js')); strictEqual(defaultExport, 42); }); - it('should run as CommonJS an extensionless JavaScript file within a package scope that has no defined "type" and is under node_modules', async () => { + it(`should run as CommonJS an extensionless JavaScript file within a package scope that has no defined "type" and is + under node_modules`, async () => { const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ '--experimental-type=module', fixtures.path('es-modules/package-type-module/node_modules/dep-with-package-json/noext-cjs'), @@ -127,8 +141,10 @@ describe('the type flag should NOT change the interpretation of certain files wi strictEqual(signal, null); }); - it('should import as CommonJS an extensionless JavaScript file within a package scope that has no defined "type" and is under node_modules', async () => { - const { default: defaultExport } = await import(fixtures.fileURL('es-modules/package-type-module/node_modules/dep-with-package-json/noext-cjs')); + it(`should import as CommonJS an extensionless JavaScript file within a package scope that has no defined "type" and + is under node_modules`, async () => { + const { default: defaultExport } = + await import(fixtures.fileURL('es-modules/package-type-module/node_modules/dep-with-package-json/noext-cjs')); strictEqual(defaultExport, 42); }); }); diff --git a/test/es-module/test-esm-type-flag-string-input.mjs b/test/es-module/test-esm-type-flag-string-input.mjs index cfdcab30217401..5895bdc549e259 100644 --- a/test/es-module/test-esm-type-flag-string-input.mjs +++ b/test/es-module/test-esm-type-flag-string-input.mjs @@ -1,5 +1,4 @@ import { spawnPromisified } from '../common/index.mjs'; -import * as fixtures from '../common/fixtures.mjs'; import { spawn } from 'node:child_process'; import { describe, it } from 'node:test'; import { strictEqual, match } from 'node:assert'; @@ -9,7 +8,7 @@ describe('the type flag should change the interpretation of string input', { con const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [ '--experimental-type=module', '--eval', - `import "data:text/javascript,console.log(42)"`, + 'import "data:text/javascript,console.log(42)"', ]); strictEqual(stderr, '');