Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Sep 26, 2023
1 parent c3a86aa commit 82a4aaa
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 168 deletions.
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function getFormatOfExtensionlessFile(url) {
return 'wasm';
}
} finally {
if (fd) { closeSync(fd) };
if (fd) { closeSync(fd); }
}

return 'module';
Expand Down
17 changes: 9 additions & 8 deletions lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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';
}
}
Expand All @@ -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);
}
Expand Down
1 change: 0 additions & 1 deletion lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
45 changes: 23 additions & 22 deletions test/es-module/test-esm-type-flag-errors.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
116 changes: 59 additions & 57 deletions test/es-module/test-esm-type-flag-loose-files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Loading

0 comments on commit 82a4aaa

Please sign in to comment.