function transform(file, { j }, options) { const root = j(file.source); let { identifier } = removeImport('is-nan', root, j); root .find(j.CallExpression, { callee: { name: identifier } }) .replaceWith(({ node }) => { return j.callExpression( j.memberExpression(j.identifier('Number'), j.identifier('isNaN')), node.arguments ); }); return root.toSource(options); } function removeImport(name, root, j) { // Find the import or require statement for 'is-boolean-object' const importDeclaration = root.find(j.ImportDeclaration, { source: { value: name } }); const requireDeclaration = root .find(j.CallExpression, { callee: { name: 'require' }, arguments: [ { value: name } ] }) .closest(j.VariableDeclarator); // Require statements without declarations like `Object.is = require("object-is");` const requireAssignment = root.find(j.AssignmentExpression, { operator: '=', right: { callee: { name: 'require' }, arguments: [ { value: name } ] } }); // Side effect requires statements like `require("error-cause/auto");` const sideEffectRequireExpression = root.find(j.ExpressionStatement, { expression: { callee: { name: 'require' }, arguments: [ { value: name } ] } }); // Return the identifier name, e.g. 'fn' in `import { fn } from 'is-boolean-object'` // or `var fn = require('is-boolean-object')` const identifier = importDeclaration.paths().length > 0 ? importDeclaration.get().node.specifiers[0].local.name : requireDeclaration.paths().length > 0 ? requireDeclaration.find(j.Identifier).get().node.name : requireAssignment.paths().length > 0 ? requireAssignment.find(j.Identifier).get().node.name : null; importDeclaration.remove(); requireDeclaration.remove(); requireAssignment.remove(); sideEffectRequireExpression.remove(); const dirtyFlag = importDeclaration.length > 0 || requireDeclaration.length > 0 || requireAssignment.length > 0 || sideEffectRequireExpression.length > 0; return { identifier, dirtyFlag }; } export default transform;
Input
Number.isNaN = require('is-nan'); var assert = require('assert'); assert.notOk(Number.isNaN(undefined)); assert.notOk(Number.isNaN(null)); assert.notOk(Number.isNaN(false)); assert.notOk(Number.isNaN(true)); assert.notOk(Number.isNaN(0)); assert.notOk(Number.isNaN(42)); assert.notOk(Number.isNaN(Infinity)); assert.notOk(Number.isNaN(-Infinity)); assert.notOk(Number.isNaN('foo')); assert.notOk(Number.isNaN(function () {})); assert.notOk(Number.isNaN([])); assert.notOk(Number.isNaN({})); assert.ok(Number.isNaN(NaN)); { var isNaN = require('is-nan'); var assert = require('assert'); assert.notOk(isNaN(undefined)); assert.notOk(isNaN(null)); assert.notOk(isNaN(false)); assert.notOk(isNaN(true)); assert.notOk(isNaN(0)); assert.notOk(isNaN(42)); assert.notOk(isNaN(Infinity)); assert.notOk(isNaN(-Infinity)); assert.notOk(isNaN('foo')); assert.notOk(isNaN(function () {})); assert.notOk(isNaN([])); assert.notOk(isNaN({})); assert.ok(isNaN(NaN)); }
Output
loading
Read-only