-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from filipre/clone-regexp
`clone-regexp`
- Loading branch information
Showing
13 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const clone1 = new RegExp(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import cloneRegexp from 'clone-regexp'; | ||
|
||
const clone1 = cloneRegexp(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const clone1 = new RegExp(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const clone1 = new RegExp(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import myCloneRegexp from 'clone-regexp'; | ||
|
||
const clone1 = myCloneRegexp(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const clone1 = new RegExp(); |