Skip to content

Commit

Permalink
Fix error message when serializing require.resolve or `require.reso…
Browse files Browse the repository at this point in the history
…lve.paths` [fix]
  • Loading branch information
overlookmotel committed Dec 9, 2023
1 parent 51daff3 commit 9faaedd
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ module.exports = (filename, module, require, nextBlockId, prefixNum) => {
// Record `module` + `require`
globals.set(module, {type: COMMON_JS_MODULE, parent: null, key: 'module'});
specialFunctions.set(require, {type: 'require', path: filename});
specialFunctions.set(require.resolve, {type: 'require', path: filename});
specialFunctions.set(require.resolve.paths, {type: 'require', path: filename});

// Create local tracker function with additional properties and methods specific to the file
const blockIdCounter = {nextBlockId};
Expand Down
2 changes: 2 additions & 0 deletions lib/init/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ function createWrappedRequire(require, filename) {

// Record `require`
specialFunctions.set(wrappedRequire, {type: 'require', path: filename});
specialFunctions.set(wrappedRequire.resolve, {type: 'require', path: filename});
specialFunctions.set(wrappedRequire.resolve.paths, {type: 'require', path: filename});

// Return wrapped `require`
return wrappedRequire;
Expand Down
114 changes: 114 additions & 0 deletions test/commonjs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,117 @@ describe('`require`', () => {
});
});
});

describe('`require.resolve`', () => {
describe('from same file', () => {
it('cannot be serialized directly', () => {
expect(() => serialize(require.resolve)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});

it('cannot be serialized in function scope', () => {
const {resolve} = require;
expect(() => serialize(() => resolve)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});
});

describe('from another file', () => {
it('cannot be serialized directly', () => {
withFixtures(
'module.exports = require.resolve;',
(otherResolve) => {
expect(() => serialize(otherResolve)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
}
);
});

it('cannot be serialized in function scope', () => {
withFixtures(
'module.exports = require.resolve;',
(otherResolve) => {
expect(() => serialize(() => otherResolve)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
}
);
});
});

describe('created by `Module.createRequire()`', () => {
it('cannot be serialized directly', () => {
const otherResolve = Module.createRequire(__filename).resolve;
expect(() => serialize(otherResolve)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});

it('cannot be serialized in function scope', () => {
const otherResolve = Module.createRequire(__filename).resolve;
expect(() => serialize(() => otherResolve)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});
});
});

describe('`require.resolve.paths`', () => {
describe('from same file', () => {
it('cannot be serialized directly', () => {
expect(() => serialize(require.resolve.paths)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});

it('cannot be serialized in function scope', () => {
const {paths} = require.resolve;
expect(() => serialize(() => paths)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});
});

describe('from another file', () => {
it('cannot be serialized directly', () => {
withFixtures(
'module.exports = require.resolve.paths;',
(otherPaths) => {
expect(() => serialize(otherPaths)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
}
);
});

it('cannot be serialized in function scope', () => {
withFixtures(
'module.exports = require.resolve.paths;',
(otherPaths) => {
expect(() => serialize(() => otherPaths)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
}
);
});
});

describe('created by `Module.createRequire()`', () => {
it('cannot be serialized directly', () => {
const otherPaths = Module.createRequire(__filename).resolve.paths;
expect(() => serialize(otherPaths)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});

it('cannot be serialized in function scope', () => {
const otherPaths = Module.createRequire(__filename).resolve.paths;
expect(() => serialize(() => otherPaths)).toThrowWithMessage(
Error, /^Cannot serialize `require` or `import` \(in /
);
});
});
});

0 comments on commit 9faaedd

Please sign in to comment.