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

Add support for node protocol require/import. #11

Merged
merged 1 commit into from
May 11, 2021
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
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