Skip to content

Commit

Permalink
feat: add es-aggregate-error
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanru committed Jul 24, 2024
1 parent e81d35b commit 97e39e0
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
38 changes: 38 additions & 0 deletions codemods/es-aggregate-error/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import jscodeshift from 'jscodeshift';
import { removeImport, transformMathPolyfill } from '../shared.js';

/**
* @typedef {import('../../types.js').Codemod} Codemod
* @typedef {import('../../types.js').CodemodOptions} CodemodOptions
*/

/**
* @param {CodemodOptions} [options]
* @returns {Codemod}
*/
export default function (options) {
return {
name: 'es-aggregate-error',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const { identifier } = removeImport('es-aggregate-error', root, j);

const expressions = root.find(j.NewExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
});

if (!expressions.length) return file.source;

expressions.replaceWith(({ node }) =>
j.newExpression(j.identifier('AggregateError'), node.arguments),
);

return root.toSource(options);
},
};
}
8 changes: 8 additions & 0 deletions test/fixtures/es-aggregate-error/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var assert = require("assert");

var oneError = new RangeError("hi!");
var otherError = new EvalError("oops");
var error = new AggregateError([oneError, otherError], "this is two kinds of errors");

assert.deepEqual(error.errors, [oneError, otherError]);
assert.equal(error.message, "this is two kinds of errors");
12 changes: 12 additions & 0 deletions test/fixtures/es-aggregate-error/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var assert = require("assert");
var AggregateError = require("es-aggregate-error");

var oneError = new RangeError("hi!");
var otherError = new EvalError("oops");
var error = new AggregateError(
[oneError, otherError],
"this is two kinds of errors",
);

assert.deepEqual(error.errors, [oneError, otherError]);
assert.equal(error.message, "this is two kinds of errors");
8 changes: 8 additions & 0 deletions test/fixtures/es-aggregate-error/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var assert = require("assert");

var oneError = new RangeError("hi!");
var otherError = new EvalError("oops");
var error = new AggregateError([oneError, otherError], "this is two kinds of errors");

assert.deepEqual(error.errors, [oneError, otherError]);
assert.equal(error.message, "this is two kinds of errors");

0 comments on commit 97e39e0

Please sign in to comment.