Skip to content

Commit

Permalink
Support node v18
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Apr 24, 2022
1 parent 08bf84c commit e4e4fdb
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/modules/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const Path = require('path');

const ESLint = require('eslint');
const ESLintParser = require('@babel/eslint-parser');
const SourceMap = require('source-map');
const SourceMapSupport = require('source-map-support');

const Eslintrc = require('../linter/.eslintrc');
const SourceMap = require('../source-map');
const Transform = require('./transform');

const internals = {
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const Path = require('path');

const Handlebars = require('handlebars');
const Hoek = require('@hapi/hoek');
const SourceMap = require('source-map');
const SourceMapSupport = require('source-map-support');

const SourceMap = require('../source-map');

const internals = {};

Expand Down
12 changes: 12 additions & 0 deletions lib/source-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const Utils = require('./utils');

// An unfortunate quirk of source-maps is that it detects
// whether it's a node or browser environment based on the
// presence of fetch(), which is now a global in node v18.
const stash = Utils.stashProperty(globalThis, 'fetch');

module.exports = require('source-map');

Utils.restoreProperty(globalThis, 'fetch', stash);
15 changes: 15 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@ exports.position = function (err) {
column: parseInt(match[2], 10)
};
};

exports.stashProperty = (obj, property) => {

const descriptor = Object.getOwnPropertyDescriptor(obj, property);
delete obj[property];

return descriptor;
};

exports.restoreProperty = (obj, property, descriptor) => {

if (descriptor) {
Object.defineProperty(obj, property, descriptor);
}
};
60 changes: 60 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,64 @@ describe('Utils', () => {
expect(Utils.position({ stack: '' })).to.equal({});
});
});

describe('stashProperty() and restoreProperty()', () => {

it('stashes and restores an existing property', () => {

const obj = { x: 1 };

expect(obj).to.contain('x');

const stash = Utils.stashProperty(obj, 'x');

expect(obj).to.not.contain('x');

Utils.restoreProperty(obj, 'x', stash);

expect(obj).to.contain('x');
expect(obj.x).to.equal(1);
});

it('stashes and restores a non-existing property', () => {

const obj = { x: 1 };

expect(obj).to.not.contain('y');

const stash = Utils.stashProperty(obj, 'y');

expect(obj).to.not.contain('y');

Utils.restoreProperty(obj, 'y', stash);

expect(obj).to.not.contain('y');
});

it('preserves descriptor', () => {

const obj = {};
Object.defineProperty(obj, 'x', {
value: 1,
enumerable: false,
configurable: true
});

expect(obj).to.contain('x');

const stash = Utils.stashProperty(obj, 'x');

expect(obj).to.not.contain('x');

Utils.restoreProperty(obj, 'x', stash);

expect(obj).to.contain('x');

expect(Object.getOwnPropertyDescriptor(obj, 'x')).to.contain({
value: 1,
enumerable: false,
configurable: true
});
});
});
});

0 comments on commit e4e4fdb

Please sign in to comment.