Skip to content

Commit

Permalink
Merge pull request #69 from filipre/clone-regexp
Browse files Browse the repository at this point in the history
`clone-regexp`
  • Loading branch information
thepassle authored Jul 31, 2024
2 parents 25c03af + 385438e commit 90fdb79
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 0 deletions.
44 changes: 44 additions & 0 deletions codemods/clone-regexp/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import jscodeshift from 'jscodeshift';
import { removeImport } 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: 'clone-regexp',
transform: ({ file }) => {
const j = jscodeshift;
const root = j(file.source);

const { identifier } = removeImport('clone-regexp', root, j);

return root
.find(j.CallExpression, {
callee: {
type: 'Identifier',
name: identifier,
},
})
.replaceWith((path) => {
const args = path.node.arguments;
const arg = args.length >= 1 ? [args[0]] : [];
const newRegExp = j.newExpression(j.identifier('RegExp'), arg);
if (args.length >= 2) {
console.warn(
'[WARNING] Options are being passed to `clone-regexp`. Please modify the new regular expression accordingly.',
);
newRegExp.comments = [j.commentBlock(' Todo ', false, true)];
}
return newRegExp;
})
.toSource({ quote: 'single' });
},
};
}
13 changes: 13 additions & 0 deletions test/fixtures/clone-regexp/case-1/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import assert from 'assert';

const regex = /[a-z]/gi;

// Argument is a regex
const clone1 = new RegExp(/[a-z]/gi);
assert.deepStrictEqual(regex, clone1);
assert.ok(regex !== clone1)

// Argument is an identifier
const clone2 = new RegExp(regex);
assert.deepStrictEqual(regex, clone2);
assert.ok(regex !== clone2);
14 changes: 14 additions & 0 deletions test/fixtures/clone-regexp/case-1/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import cloneRegexp from 'clone-regexp';
import assert from 'assert';

const regex = /[a-z]/gi;

// Argument is a regex
const clone1 = cloneRegexp(/[a-z]/gi);
assert.deepStrictEqual(regex, clone1);
assert.ok(regex !== clone1)

// Argument is an identifier
const clone2 = cloneRegexp(regex);
assert.deepStrictEqual(regex, clone2);
assert.ok(regex !== clone2);
13 changes: 13 additions & 0 deletions test/fixtures/clone-regexp/case-1/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import assert from 'assert';

const regex = /[a-z]/gi;

// Argument is a regex
const clone1 = new RegExp(/[a-z]/gi);
assert.deepStrictEqual(regex, clone1);
assert.ok(regex !== clone1)

// Argument is an identifier
const clone2 = new RegExp(regex);
assert.deepStrictEqual(regex, clone2);
assert.ok(regex !== clone2);
17 changes: 17 additions & 0 deletions test/fixtures/clone-regexp/case-2/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const regex = /[a-z]/gi;

// Argument is a regex
const clone1 = new RegExp(/[a-z]/gi)/* Todo */;

// Argument is an identifier
const clone2 = new RegExp(regex)/* Todo */;

// Both arguments are identifier
const opt = { ignoreCase: 1 === 1 };
const clone3 = new RegExp(regex)/* Todo */;

// Multiple cloneRegexp
const clone4 = f(new RegExp(regex)/* Todo */, new RegExp(regex)/* Todo */);

// Regex comes from a function
const clone5 = new RegExp(f(regex))/* Todo */;
19 changes: 19 additions & 0 deletions test/fixtures/clone-regexp/case-2/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import cloneRegexp from 'clone-regexp';

const regex = /[a-z]/gi;

// Argument is a regex
const clone1 = cloneRegexp(/[a-z]/gi, { global: false });

// Argument is an identifier
const clone2 = cloneRegexp(regex, { unicode: true });

// Both arguments are identifier
const opt = { ignoreCase: 1 === 1 };
const clone3 = cloneRegexp(regex, opt);

// Multiple cloneRegexp
const clone4 = f(cloneRegexp(regex, { sticky: true }), cloneRegexp(regex, { dotAll: true }));

// Regex comes from a function
const clone5 = cloneRegexp(f(regex), { multiline: true });
17 changes: 17 additions & 0 deletions test/fixtures/clone-regexp/case-2/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const regex = /[a-z]/gi;

// Argument is a regex
const clone1 = new RegExp(/[a-z]/gi)/* Todo */;

// Argument is an identifier
const clone2 = new RegExp(regex)/* Todo */;

// Both arguments are identifier
const opt = { ignoreCase: 1 === 1 };
const clone3 = new RegExp(regex)/* Todo */;

// Multiple cloneRegexp
const clone4 = f(new RegExp(regex)/* Todo */, new RegExp(regex)/* Todo */);

// Regex comes from a function
const clone5 = new RegExp(f(regex))/* Todo */;
1 change: 1 addition & 0 deletions test/fixtures/clone-regexp/case-3/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const clone1 = new RegExp();
3 changes: 3 additions & 0 deletions test/fixtures/clone-regexp/case-3/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import cloneRegexp from 'clone-regexp';

const clone1 = cloneRegexp();
1 change: 1 addition & 0 deletions test/fixtures/clone-regexp/case-3/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const clone1 = new RegExp();
1 change: 1 addition & 0 deletions test/fixtures/clone-regexp/case-4/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const clone1 = new RegExp();
3 changes: 3 additions & 0 deletions test/fixtures/clone-regexp/case-4/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import myCloneRegexp from 'clone-regexp';

const clone1 = myCloneRegexp();
1 change: 1 addition & 0 deletions test/fixtures/clone-regexp/case-4/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const clone1 = new RegExp();

0 comments on commit 90fdb79

Please sign in to comment.