diff --git a/codemods/es-errors/index.js b/codemods/es-errors/index.js new file mode 100644 index 0000000..8362db2 --- /dev/null +++ b/codemods/es-errors/index.js @@ -0,0 +1,49 @@ +import jscodeshift from 'jscodeshift'; +import { removeImport } from '../shared.js'; + +/** + * @typedef {import('../../types.js').Codemod} Codemod + * @typedef {import('../../types.js').CodemodOptions} CodemodOptions + */ + +const moduleToErrorMap = { + 'es-errors': 'Error', + 'es-errors/eval': 'EvalError', + 'es-errors/range': 'RangeError', + 'es-errors/ref': 'ReferenceError', + 'es-errors/syntax': 'SyntaxError', + 'es-errors/type': 'TypeError', + 'es-errors/uri': 'URIError', +}; + +/** + * @param {CodemodOptions} [options] + * @returns {Codemod} + */ +export default function (options) { + return { + name: 'es-errors', + transform: ({ file }) => { + const j = jscodeshift; + const root = j(file.source); + let dirtyFlag = false; + + for (const [moduleName, errorName] of Object.entries(moduleToErrorMap)) { + const { identifier } = removeImport(moduleName, root, j); + + root + .find(j.NewExpression, { + callee: { + type: 'Identifier', + name: identifier, + }, + }) + .replaceWith(({ node }) => { + return j.newExpression(j.identifier(errorName), node.arguments); + }); + } + + return root.toSource(options); + }, + }; +} diff --git a/test/fixtures/es-errors/case-1/after.js b/test/fixtures/es-errors/case-1/after.js new file mode 100644 index 0000000..2cf4c37 --- /dev/null +++ b/test/fixtures/es-errors/case-1/after.js @@ -0,0 +1,17 @@ +const assert = require("assert"); + +const baseError = new Error(); +const evalError = new EvalError(); +const rangeError = new RangeError(); +const refError = new ReferenceError(); +const syntaxError = new SyntaxError(); +const typeError = new TypeError(); +const uriError = new URIError(); + +assert.equal(baseError instanceof Error, true); +assert.equal(evalError instanceof EvalError, true); +assert.equal(rangeError instanceof RangeError, true); +assert.equal(refError instanceof ReferenceError, true); +assert.equal(syntaxError instanceof SyntaxError, true); +assert.equal(typeError instanceof TypeError, true); +assert.equal(uriError instanceof URIError, true); diff --git a/test/fixtures/es-errors/case-1/before.js b/test/fixtures/es-errors/case-1/before.js new file mode 100644 index 0000000..94a9da4 --- /dev/null +++ b/test/fixtures/es-errors/case-1/before.js @@ -0,0 +1,24 @@ +const assert = require("assert"); +const Base = require("es-errors"); +const Eval = require("es-errors/eval"); +const Range = require("es-errors/range"); +const Ref = require("es-errors/ref"); +const Syntax = require("es-errors/syntax"); +const Type = require("es-errors/type"); +const URI = require("es-errors/uri"); + +const baseError = new Base(); +const evalError = new Eval(); +const rangeError = new Range(); +const refError = new Ref(); +const syntaxError = new Syntax(); +const typeError = new Type(); +const uriError = new URI(); + +assert.equal(baseError instanceof Error, true); +assert.equal(evalError instanceof EvalError, true); +assert.equal(rangeError instanceof RangeError, true); +assert.equal(refError instanceof ReferenceError, true); +assert.equal(syntaxError instanceof SyntaxError, true); +assert.equal(typeError instanceof TypeError, true); +assert.equal(uriError instanceof URIError, true); diff --git a/test/fixtures/es-errors/case-1/result.js b/test/fixtures/es-errors/case-1/result.js new file mode 100644 index 0000000..2cf4c37 --- /dev/null +++ b/test/fixtures/es-errors/case-1/result.js @@ -0,0 +1,17 @@ +const assert = require("assert"); + +const baseError = new Error(); +const evalError = new EvalError(); +const rangeError = new RangeError(); +const refError = new ReferenceError(); +const syntaxError = new SyntaxError(); +const typeError = new TypeError(); +const uriError = new URIError(); + +assert.equal(baseError instanceof Error, true); +assert.equal(evalError instanceof EvalError, true); +assert.equal(rangeError instanceof RangeError, true); +assert.equal(refError instanceof ReferenceError, true); +assert.equal(syntaxError instanceof SyntaxError, true); +assert.equal(typeError instanceof TypeError, true); +assert.equal(uriError instanceof URIError, true);