From 5c065f351c65a8a713f9cc1fe048f91e71b75852 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Fri, 13 Oct 2023 16:04:53 -0700 Subject: [PATCH] Replace old helper with new --- lib/internal/modules/cjs/loader.js | 7 ++++--- lib/internal/modules/esm/translators.js | 10 +++++----- lib/internal/modules/helpers.js | 23 ----------------------- 3 files changed, 9 insertions(+), 31 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index b3b438372fe9ed..7c91ba49e2cc7b 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -90,6 +90,7 @@ const { makeContextifyScript, runScriptInThisContext, } = require('internal/vm'); +const { containsModuleSyntax } = internalBinding('contextify'); const assert = require('internal/assert'); const fs = require('fs'); @@ -104,7 +105,6 @@ const { const { getCjsConditions, initializeCjsConditions, - hasEsmSyntax, loadBuiltinModule, makeRequireFunction, normalizeReferrerURL, @@ -1315,7 +1315,7 @@ function wrapSafe(filename, content, cjsModuleInstance, codeCache) { } catch (err) { if (process.mainModule === cjsModuleInstance) { const { enrichCJSError } = require('internal/modules/esm/translators'); - enrichCJSError(err, content); + enrichCJSError(err, content, filename); } throw err; } @@ -1400,10 +1400,11 @@ Module._extensions['.js'] = function(module, filename) { const pkg = packageJsonReader.readPackageScope(filename) || { __proto__: null }; // Function require shouldn't be used in ES modules. if (pkg.data?.type === 'module') { + // This is an error path, because `require` of a `.js` file in a `"type": "module"` scope is not allowed. const parent = moduleParentCache.get(module); const parentPath = parent?.filename; const packageJsonPath = path.resolve(pkg.path, 'package.json'); - const usesEsm = hasEsmSyntax(content); + const usesEsm = containsModuleSyntax(content, filename); const err = new ERR_REQUIRE_ESM(filename, usesEsm, parentPath, packageJsonPath); // Attempt to reconstruct the parent require frame. diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index c6540f49811460..2c626c054a770e 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -30,11 +30,11 @@ function lazyTypes() { return _TYPES = require('internal/util/types'); } +const { containsModuleSyntax } = internalBinding('contextify'); const assert = require('internal/assert'); const { readFileSync } = require('fs'); const { dirname, extname, isAbsolute } = require('path'); const { - hasEsmSyntax, loadBuiltinModule, stripBOM, } = require('internal/modules/helpers'); @@ -166,11 +166,11 @@ translators.set('module', async function moduleStrategy(url, source, isMain) { * Provide a more informative error for CommonJS imports. * @param {Error | any} err * @param {string} [content] Content of the file, if known. - * @param {string} [filename] Useful only if `content` is unknown. + * @param {string} [filename] The filename of the erroring module. */ function enrichCJSError(err, content, filename) { if (err != null && ObjectGetPrototypeOf(err) === SyntaxErrorPrototype && - hasEsmSyntax(content || readFileSync(filename, 'utf-8'))) { + containsModuleSyntax(content, filename ?? '')) { // Emit the warning synchronously because we are in the middle of handling // a SyntaxError that will throw and likely terminate the process before an // asynchronous warning would be emitted. @@ -217,7 +217,7 @@ function loadCJSModule(module, source, url, filename) { importModuleDynamically, // importModuleDynamically ).function; } catch (err) { - enrichCJSError(err, source, url); + enrichCJSError(err, source, filename); throw err; } @@ -344,7 +344,7 @@ translators.set('commonjs', async function commonjsStrategy(url, source, assert(module === CJSModule._cache[filename]); CJSModule._load(filename); } catch (err) { - enrichCJSError(err, source, url); + enrichCJSError(err, source, filename); throw err; } } : loadCJSModule; diff --git a/lib/internal/modules/helpers.js b/lib/internal/modules/helpers.js index 7f2959cc469dc1..6b30a1d8c76d4b 100644 --- a/lib/internal/modules/helpers.js +++ b/lib/internal/modules/helpers.js @@ -3,7 +3,6 @@ const { ArrayPrototypeForEach, ArrayPrototypeJoin, - ArrayPrototypeSome, ObjectDefineProperty, ObjectPrototypeHasOwnProperty, SafeMap, @@ -299,32 +298,10 @@ function normalizeReferrerURL(referrer) { return new URL(referrer).href; } -/** - * For error messages only, check if ESM syntax is in use. - * @param {string} code - */ -function hasEsmSyntax(code) { - debug('Checking for ESM syntax'); - const parser = require('internal/deps/acorn/acorn/dist/acorn').Parser; - let root; - try { - root = parser.parse(code, { sourceType: 'module', ecmaVersion: 'latest' }); - } catch { - return false; - } - - return ArrayPrototypeSome(root.body, (stmt) => - stmt.type === 'ExportDefaultDeclaration' || - stmt.type === 'ExportNamedDeclaration' || - stmt.type === 'ImportDeclaration' || - stmt.type === 'ExportAllDeclaration'); -} - module.exports = { addBuiltinLibsToObject, getCjsConditions, initializeCjsConditions, - hasEsmSyntax, loadBuiltinModule, makeRequireFunction, normalizeReferrerURL,