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

feat: add es-errors #67

Merged
merged 1 commit into from
Jul 27, 2024
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
49 changes: 49 additions & 0 deletions codemods/es-errors/index.js
Original file line number Diff line number Diff line change
@@ -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);
},
};
}
17 changes: 17 additions & 0 deletions test/fixtures/es-errors/case-1/after.js
Original file line number Diff line number Diff line change
@@ -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);
24 changes: 24 additions & 0 deletions test/fixtures/es-errors/case-1/before.js
Original file line number Diff line number Diff line change
@@ -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);
17 changes: 17 additions & 0 deletions test/fixtures/es-errors/case-1/result.js
Original file line number Diff line number Diff line change
@@ -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);
Loading