Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support node v18 #1045

Merged
merged 1 commit into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
});
});
});
});