Skip to content

Commit

Permalink
Built-in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 10, 2023
1 parent 027ce82 commit 09f54c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
24 changes: 18 additions & 6 deletions lib/serialize/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

// Exports
module.exports = {traceGlobal};
module.exports = {traceGlobal, createImportOrRequireNode};

// Modules
const {isObject, isSymbol, isNumber} = require('is-it-type'),
Expand Down Expand Up @@ -132,21 +132,33 @@ function serializeGlobalTopLevel(record) {
}
registerSerializer(GLOBAL_TOP_LEVEL_TYPE, serializeGlobalTopLevel);

// eslint-disable-next-line jsdoc/require-throws, jsdoc/require-returns-check
/**
* Serialize built-in module e.g. `require('path')`.
* @this {Object} Serializer
* @param {Object} record - Record
* @param {Object} record.extra - Extra props object
* @param {string} record.extra.name - Name of global
* @param {string} record.extra.name - Name of built-in module
* @returns {Object} - AST node
*/
function serializeBuiltInModule(record) { // eslint-disable-line no-unused-vars
// TODO
throw new Error('Built-in modules not supported');
function serializeBuiltInModule(record) {
return this.createImportOrRequireNode(t.stringLiteral(record.extra.name), record.varNode);
}
registerSerializer(GLOBAL_MODULE_TYPE, serializeBuiltInModule);

/**
* Create an `import` statement or `require()` expression to import a module.
* @this {Object} Serializer
* @param {Object} filePathStrNode -
* @param {Object} varNode - Identifier
* @returns {Object} - AST node
*/
function createImportOrRequireNode(filePathStrNode, varNode) {
if (this.options.format === 'esm') {
return t.importDeclaration([t.importDefaultSpecifier(varNode)], filePathStrNode);
}
return t.callExpression(t.identifier('require'), [filePathStrNode]);
}

/**
* Serialize global property (e.g. `Object.create`, `Object.prototype.toString`)
* or property of a built-in module (e.g. `require('path').join`).
Expand Down
10 changes: 5 additions & 5 deletions test/builtInModules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const pathModule = require('path'), // eslint-disable-line import/order

// Tests

describe.skip('Built-in modules', () => {
describe('Built-in modules', () => {
describe('top level', () => {
describe('single occurance', () => {
itSerializesEqual('JS format', {
Expand All @@ -32,7 +32,7 @@ describe.skip('Built-in modules', () => {
validate: res => expect(res).toBe(pathModule)
});

itSerializesEqual('ESM format', {
itSerializesEqual.skip('ESM format', {
in: () => pathModule,
format: 'esm',
minify: true,
Expand Down Expand Up @@ -72,7 +72,7 @@ describe.skip('Built-in modules', () => {
}
});

itSerializesEqual('ESM format', {
itSerializesEqual.skip('ESM format', {
in: () => ({path: pathModule, path2: pathModule, path3: pathModule}),
format: 'esm',
minify: true,
Expand Down Expand Up @@ -126,7 +126,7 @@ describe.skip('Built-in modules', () => {
validate: res => expect(res).toBe(pathJoin)
});

itSerializesEqual('ESM format', {
itSerializesEqual.skip('ESM format', {
in: () => pathJoin,
format: 'esm',
minify: true,
Expand Down Expand Up @@ -170,7 +170,7 @@ describe.skip('Built-in modules', () => {
}
});

itSerializesEqual('ESM format', {
itSerializesEqual.skip('ESM format', {
in: () => ({pathJoin, pathJoin2: pathJoin, pathJoin3: pathJoin}),
format: 'esm',
minify: true,
Expand Down
4 changes: 2 additions & 2 deletions test/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('Globals', () => {
});
});

describe.skip('getter + setters', () => {
describe('getter + setters', () => {
itSerializesEqual('getter', {
in: () => Object.getOwnPropertyDescriptor(fs, 'promises').get,
out: 'Object.getOwnPropertyDescriptor(require("fs"),"promises").get',
Expand All @@ -130,7 +130,7 @@ describe('Globals', () => {
});
});

describe.skip('values behind getters', () => {
describe('values behind getters', () => {
itSerializesEqual('fs.ReadStream', {
in: () => fs.ReadStream,
out: 'require("fs").ReadStream',
Expand Down

0 comments on commit 09f54c5

Please sign in to comment.