0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -05:00

refactor(ext/node): remove several TODOs (#15452)

This commit is contained in:
Bartek Iwańczuk 2022-08-11 16:44:01 +02:00 committed by GitHub
parent 2164f6b1eb
commit 10ce2e9e80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -19,6 +19,7 @@
ObjectPrototypeHasOwnProperty, ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf, ObjectSetPrototypeOf,
ObjectKeys, ObjectKeys,
ObjectPrototype,
ObjectCreate, ObjectCreate,
SafeMap, SafeMap,
SafeWeakMap, SafeWeakMap,
@ -50,11 +51,6 @@
} }
} }
// TODO:
function isProxy() {
return false;
}
// TODO(bartlomieju): verify in other parts of this file that // TODO(bartlomieju): verify in other parts of this file that
// we have initialized the system before making APIs work // we have initialized the system before making APIs work
let cjsInitialized = false; let cjsInitialized = false;
@ -200,7 +196,6 @@
function getExportsForCircularRequire(module) { function getExportsForCircularRequire(module) {
if ( if (
module.exports && module.exports &&
!isProxy(module.exports) &&
ObjectGetPrototypeOf(module.exports) === ObjectPrototype && ObjectGetPrototypeOf(module.exports) === ObjectPrototype &&
// Exclude transpiled ES6 modules / TypeScript code because those may // Exclude transpiled ES6 modules / TypeScript code because those may
// employ unusual patterns for accessing 'module.exports'. That should // employ unusual patterns for accessing 'module.exports'. That should
@ -218,6 +213,13 @@
return module.exports; return module.exports;
} }
function emitCircularRequireWarning(prop) {
processGlobal.emitWarning(
`Accessing non-existent property '${String(prop)}' of module exports ` +
"inside circular dependency",
);
}
// A Proxy that can be used as the prototype of a module.exports object and // A Proxy that can be used as the prototype of a module.exports object and
// warns when non-existent properties are accessed. // warns when non-existent properties are accessed.
const CircularRequirePrototypeWarningProxy = new Proxy({}, { const CircularRequirePrototypeWarningProxy = new Proxy({}, {
@ -226,9 +228,7 @@
// of transpiled code to determine whether something comes from an // of transpiled code to determine whether something comes from an
// ES module, and is not used as a regular key of `module.exports`. // ES module, and is not used as a regular key of `module.exports`.
if (prop in target || prop === "__esModule") return target[prop]; if (prop in target || prop === "__esModule") return target[prop];
// TODO: emitCircularRequireWarning(prop);
// emitCircularRequireWarning(prop);
console.log("TODO: emitCircularRequireWarning");
return undefined; return undefined;
}, },
@ -238,9 +238,7 @@
) { ) {
return ObjectGetOwnPropertyDescriptor(target, prop); return ObjectGetOwnPropertyDescriptor(target, prop);
} }
// TODO: emitCircularRequireWarning(prop);
// emitCircularRequireWarning(prop);
console.log("TODO: emitCircularRequireWarning");
return undefined; return undefined;
}, },
}); });
@ -258,7 +256,6 @@
} }
const builtinModules = []; const builtinModules = [];
// TODO(bartlomieju): handle adding native modules
Module.builtinModules = builtinModules; Module.builtinModules = builtinModules;
Module._extensions = Object.create(null); Module._extensions = Object.create(null);
@ -298,10 +295,7 @@
if (curPath && stat(curPath) < 1) continue; if (curPath && stat(curPath) < 1) continue;
if (!absoluteRequest) { if (!absoluteRequest) {
const exportsResolved = false; const exportsResolved = resolveExports(curPath, request);
// TODO:
console.log("TODO: Module._findPath resolveExports");
// const exportsResolved = resolveExports(curPath, request);
if (exportsResolved) { if (exportsResolved) {
return exportsResolved; return exportsResolved;
} }
@ -447,7 +441,6 @@
} }
} else if ( } else if (
module.exports && module.exports &&
!isProxy(module.exports) &&
ObjectGetPrototypeOf(module.exports) === ObjectGetPrototypeOf(module.exports) ===
CircularRequirePrototypeWarningProxy CircularRequirePrototypeWarningProxy
) { ) {
@ -624,15 +617,33 @@
return `${Module.wrapper[0]}${script}${Module.wrapper[1]}`; return `${Module.wrapper[0]}${script}${Module.wrapper[1]}`;
}; };
function enrichCJSError(error) {
if (error instanceof SyntaxError) {
if (
error.message.includes(
"Cannot use import statement outside a module",
) ||
error.message.includes("Unexpected token 'export'")
) {
console.error(
'To load an ES module, set "type": "module" in the package.json or use ' +
"the .mjs extension.",
);
}
}
}
function wrapSafe( function wrapSafe(
filename, filename,
content, content,
_cjsModuleInstance, cjsModuleInstance,
) { ) {
const wrapper = Module.wrap(content); const wrapper = Module.wrap(content);
const [f, err] = core.evalContext(wrapper, filename); const [f, err] = core.evalContext(wrapper, filename);
if (err) { if (err) {
console.log("TODO: wrapSafe check if main module"); if (processGlobal.mainModule === cjsModuleInstance) {
enrichCJSError(err.thrown);
}
throw err.thrown; throw err.thrown;
} }
return f; return f;