Skip to content

Commit

Permalink
Merge pull request #65 from es-tooling/matvey
Browse files Browse the repository at this point in the history
feat: add concat-map as es-aggregate-error
  • Loading branch information
thepassle authored Jul 24, 2024
2 parents 714ceab + 97e39e0 commit 823049e
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 0 deletions.
25 changes: 25 additions & 0 deletions codemods/concat-map/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import jscodeshift from 'jscodeshift';
import { transformArrayMethod } 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: 'concat-map',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const dirty = transformArrayMethod('concat-map', 'flatMap', root, j);

return dirty ? root.toSource(options) : file.source;
},
};
}
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/concat-map/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var assert = require("assert");

var xs = [1, 2, 3, 4, 5, 6];
var ys = xs.flatMap(function (x) {
return x % 2 ? [x - 0.1, x, x + 0.1] : [];
});

assert.deepEqual(ys, [0.9, 1, 1.1, 2.9, 3, 3.1, 4.9, 5, 5.1]);
9 changes: 9 additions & 0 deletions test/fixtures/concat-map/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var concatMap = require("concat-map");
var assert = require("assert");

var xs = [1, 2, 3, 4, 5, 6];
var ys = concatMap(xs, function (x) {
return x % 2 ? [x - 0.1, x, x + 0.1] : [];
});

assert.deepEqual(ys, [0.9, 1, 1.1, 2.9, 3, 3.1, 4.9, 5, 5.1]);
8 changes: 8 additions & 0 deletions test/fixtures/concat-map/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var assert = require("assert");

var xs = [1, 2, 3, 4, 5, 6];
var ys = xs.flatMap(function (x) {
return x % 2 ? [x - 0.1, x, x + 0.1] : [];
});

assert.deepEqual(ys, [0.9, 1, 1.1, 2.9, 3, 3.1, 4.9, 5, 5.1]);
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 823049e

Please sign in to comment.