Skip to content

Commit

Permalink
feat: add concat-map
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanru committed Jul 24, 2024
1 parent 714ceab commit e81d35b
Show file tree
Hide file tree
Showing 4 changed files with 50 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;
},
};
}
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]);

0 comments on commit e81d35b

Please sign in to comment.