Skip to content

Commit

Permalink
Add support for node protocol require/import.
Browse files Browse the repository at this point in the history
See for instance nodejs/node#38343.
  • Loading branch information
make-github-pseudonymous-again committed May 10, 2021
1 parent 8518d89 commit 7c0c9ba
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const isRequireAssert = (id, init) => {
return false;
}
const arg = init.get('arguments')[0];
return (arg.isLiteral() && (arg.equals('value', 'assert') || arg.equals('value', 'power-assert')));
return (arg.isLiteral() && (arg.equals('value', 'assert') || arg.equals('value', 'power-assert') || arg.equals('value', 'node:assert')));
};

const isRequireAssertStrict = (id, init) => {
Expand Down Expand Up @@ -63,7 +63,7 @@ module.exports = (babel) => {
},
ImportDeclaration (nodePath, pluginPass) {
const source = nodePath.get('source');
if (!(source.equals('value', 'assert') || source.equals('value', 'power-assert'))) {
if (!(source.equals('value', 'assert') || source.equals('value', 'power-assert') || source.equals('value', 'node:assert'))) {
return;
}
const firstSpecifier = nodePath.get('specifiers')[0];
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/cjs_node_protocol/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

function add(a, b) {
return a + b;
}
10 changes: 10 additions & 0 deletions test/fixtures/cjs_node_protocol/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

var assert = require('node:assert');

function add (a, b) {
assert(!isNaN(a));
assert.equal(typeof b, 'number');
assert.ok(!isNaN(b));
return a + b;
}
5 changes: 5 additions & 0 deletions test/fixtures/cjs_node_protocol_strictmode/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

function add(a, b) {
return a + b;
}
10 changes: 10 additions & 0 deletions test/fixtures/cjs_node_protocol_strictmode/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

var assert = require('node:assert').strict;

function add (a, b) {
assert(!isNaN(a));
assert.equal(typeof b, 'number');
assert.ok(!isNaN(b));
return a + b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

function add(a, b) {
return a + b;
}
3 changes: 3 additions & 0 deletions test/fixtures/esm_default_binding_node_protocol/expected.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function add(a, b) {
return a + b;
}
8 changes: 8 additions & 0 deletions test/fixtures/esm_default_binding_node_protocol/fixture.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import assert from 'node:assert';

function add (a, b) {
assert(!isNaN(a));
assert.equal(typeof b, 'number');
assert.ok(!isNaN(b));
return a + b;
}
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ describe('babel-plugin-unassert', () => {
testTransform('cjs_strictmode');
testTransform('cjs_singlevar');
testTransform('cjs_singlevar_strictmode');
testTransform('cjs_node_protocol');
testTransform('cjs_node_protocol_strictmode');
testTransform('cjs_powerassert');
testTransform('cjs_powerassert_strictmode');
testTransform('cjs_assignment');
testTransform('cjs_assignment_singlevar');
testTransform('cjs_assignment_strictmode');
testTransform('esm_default_binding', { sourceType: 'module' });
testTransform('esm_default_binding_node_protocol', { sourceType: 'module' });
testTransform('esm_default_binding_powerassert', { sourceType: 'module' });
testTransform('esm_namespace_import', { sourceType: 'module' });
testTransform('esm_import_specifier', { sourceType: 'module' });
Expand All @@ -52,12 +55,15 @@ describe('babel-plugin-unassert with presets', () => {
testTransform('cjs_strictmode', opt);
testTransform('cjs_singlevar', opt);
testTransform('cjs_singlevar_strictmode', opt);
testTransform('cjs_node_protocol', opt);
testTransform('cjs_node_protocol_strictmode', opt);
testTransform('cjs_powerassert', opt);
testTransform('cjs_powerassert_strictmode', opt);
testTransform('cjs_assignment', opt);
testTransform('cjs_assignment_singlevar', Object.assign({}, opt, { dialect: 'env' }));
testTransform('cjs_assignment_strictmode', Object.assign({}, opt, { dialect: 'env' }));
testTransform('esm_default_binding', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' }));
testTransform('esm_default_binding_node_protocol', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' }));
testTransform('esm_default_binding_powerassert', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' }));
testTransform('esm_namespace_import', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' }));
testTransform('esm_import_specifier', Object.assign({}, opt, { dialect: 'env', sourceType: 'module' }));
Expand Down

0 comments on commit 7c0c9ba

Please sign in to comment.