From 7c0c9ba26dd1f76de03aa3ed83f7e56be58705bf Mon Sep 17 00:00:00 2001 From: make-github-pseudonymous-again <5165674+make-github-pseudonymous-again@users.noreply.github.com> Date: Mon, 10 May 2021 14:11:44 +0200 Subject: [PATCH] Add support for node protocol require/import. See for instance https://github.com/nodejs/node/issues/38343. --- index.js | 4 ++-- test/fixtures/cjs_node_protocol/expected.js | 5 +++++ test/fixtures/cjs_node_protocol/fixture.js | 10 ++++++++++ test/fixtures/cjs_node_protocol_strictmode/expected.js | 5 +++++ test/fixtures/cjs_node_protocol_strictmode/fixture.js | 10 ++++++++++ .../expected-presets-env.mjs | 5 +++++ .../esm_default_binding_node_protocol/expected.mjs | 3 +++ .../esm_default_binding_node_protocol/fixture.mjs | 8 ++++++++ test/test.js | 6 ++++++ 9 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/cjs_node_protocol/expected.js create mode 100644 test/fixtures/cjs_node_protocol/fixture.js create mode 100644 test/fixtures/cjs_node_protocol_strictmode/expected.js create mode 100644 test/fixtures/cjs_node_protocol_strictmode/fixture.js create mode 100644 test/fixtures/esm_default_binding_node_protocol/expected-presets-env.mjs create mode 100644 test/fixtures/esm_default_binding_node_protocol/expected.mjs create mode 100644 test/fixtures/esm_default_binding_node_protocol/fixture.mjs diff --git a/index.js b/index.js index fde9a74..f976289 100644 --- a/index.js +++ b/index.js @@ -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) => { @@ -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]; diff --git a/test/fixtures/cjs_node_protocol/expected.js b/test/fixtures/cjs_node_protocol/expected.js new file mode 100644 index 0000000..7e01055 --- /dev/null +++ b/test/fixtures/cjs_node_protocol/expected.js @@ -0,0 +1,5 @@ +'use strict'; + +function add(a, b) { + return a + b; +} diff --git a/test/fixtures/cjs_node_protocol/fixture.js b/test/fixtures/cjs_node_protocol/fixture.js new file mode 100644 index 0000000..72a023c --- /dev/null +++ b/test/fixtures/cjs_node_protocol/fixture.js @@ -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; +} diff --git a/test/fixtures/cjs_node_protocol_strictmode/expected.js b/test/fixtures/cjs_node_protocol_strictmode/expected.js new file mode 100644 index 0000000..7e01055 --- /dev/null +++ b/test/fixtures/cjs_node_protocol_strictmode/expected.js @@ -0,0 +1,5 @@ +'use strict'; + +function add(a, b) { + return a + b; +} diff --git a/test/fixtures/cjs_node_protocol_strictmode/fixture.js b/test/fixtures/cjs_node_protocol_strictmode/fixture.js new file mode 100644 index 0000000..45fa311 --- /dev/null +++ b/test/fixtures/cjs_node_protocol_strictmode/fixture.js @@ -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; +} diff --git a/test/fixtures/esm_default_binding_node_protocol/expected-presets-env.mjs b/test/fixtures/esm_default_binding_node_protocol/expected-presets-env.mjs new file mode 100644 index 0000000..3f2bb2b --- /dev/null +++ b/test/fixtures/esm_default_binding_node_protocol/expected-presets-env.mjs @@ -0,0 +1,5 @@ +"use strict"; + +function add(a, b) { + return a + b; +} diff --git a/test/fixtures/esm_default_binding_node_protocol/expected.mjs b/test/fixtures/esm_default_binding_node_protocol/expected.mjs new file mode 100644 index 0000000..86eb6d1 --- /dev/null +++ b/test/fixtures/esm_default_binding_node_protocol/expected.mjs @@ -0,0 +1,3 @@ +function add(a, b) { + return a + b; +} diff --git a/test/fixtures/esm_default_binding_node_protocol/fixture.mjs b/test/fixtures/esm_default_binding_node_protocol/fixture.mjs new file mode 100644 index 0000000..bfa9a04 --- /dev/null +++ b/test/fixtures/esm_default_binding_node_protocol/fixture.mjs @@ -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; +} diff --git a/test/test.js b/test/test.js index 877906e..77ff876 100644 --- a/test/test.js +++ b/test/test.js @@ -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' }); @@ -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' }));