Skip to content

Commit 2f67acd

Browse files
committed
feat: add regexp-to-arkregex codemod
1 parent 8673066 commit 2f67acd

File tree

14 files changed

+162
-86
lines changed

14 files changed

+162
-86
lines changed

codemods/.gitkeep

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# @codemod/arkregex
2+
3+
Migrate `new RegExp()` constructor calls to [arkregex](https://arktype.io/docs/regex)'s type-safe `regex()` function.
4+
5+
## What it does
6+
7+
This codemod automatically transforms your code to use arkregex, a type-safe regex library that provides:
8+
9+
- Type inference for regex patterns
10+
- Compile-time validation
11+
- Better TypeScript integration
12+
13+
### Transformation Example
14+
15+
**Before:**
16+
17+
```typescript
18+
const pattern = new RegExp("\\d+", "g");
19+
const emailRegex = new RegExp("^[a-z]+@[a-z]+\\.[a-z]+$");
20+
```
21+
22+
**After:**
23+
24+
```typescript
25+
import { regex } from "arkregex";
26+
const pattern = regex("\\d+", "g");
27+
const emailRegex = regex("^[a-z]+@[a-z]+\\.[a-z]+$");
28+
```
29+
30+
## Installation
31+
32+
```bash
33+
# Run from registry
34+
codemod run @codemod/arkregex
35+
36+
# Or run locally
37+
codemod run -w workflow.yaml
38+
```
39+
40+
## Development
41+
42+
```bash
43+
# Test the transformation
44+
npm test
45+
46+
# Validate the workflow
47+
codemod validate -w workflow.yaml
48+
49+
# Publish to registry
50+
codemod login
51+
codemod publish
52+
```
53+
54+
## License
55+
56+
MIT
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
schema_version: "1.0"
2+
3+
name: "@codemod/regexp-to-arkregex"
4+
version: "0.1.0"
5+
description: "using arkregex instead of `new RegExp`"
6+
author: "Amirabbas Ghasemi <[email protected]>"
7+
license: "MIT"
8+
workflow: "workflow.yaml"
9+
repository: "https://github.com/codemod/module-replacement-codemods/tree/main/codemods/regexp-to-arkregex"
10+
11+
targets:
12+
languages: ["typescript"]
13+
14+
keywords: ["transformation", "migration", "arkregex"]
15+
16+
registry:
17+
access: "public"
18+
visibility: "public"
19+
20+
capabilities: []

codemods/regexp-to-arkregex/package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "jssg-codemod",
2+
"name": "@codemod/regexp-to-arkregex",
33
"version": "0.1.0",
4-
"description": "Transform legacy code patterns",
4+
"description": "using arkregex instead of `new RegExp`",
55
"type": "module",
66
"devDependencies": {
7-
"@codemod.com/jssg-types": "^1.0.3",
7+
"@codemod.com/jssg-types": "^1.0.9",
88
"typescript": "^5.8.3"
99
},
1010
"scripts": {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { type SgRoot, parse } from "codemod:ast-grep";
2+
import type TS from "codemod:ast-grep/langs/typescript";
3+
4+
async function transform(root: SgRoot<TS>): Promise<string> {
5+
const rootNode = root.root();
6+
7+
const regexExpressionsRule = rootNode.findAll({
8+
rule: {
9+
kind: "new_expression",
10+
has: {
11+
kind: "arguments",
12+
pattern: "$ARGS",
13+
follows: {
14+
kind: "identifier",
15+
regex: "RegExp",
16+
},
17+
},
18+
},
19+
});
20+
21+
const edits = [];
22+
for (let expression of regexExpressionsRule) {
23+
const argsMatch = expression.getMatch("ARGS");
24+
if (!argsMatch) continue;
25+
const args = argsMatch.text();
26+
edits.push(expression.replace(`regex${args}`));
27+
}
28+
29+
let newSource = rootNode.commitEdits(edits);
30+
31+
if (edits.length) {
32+
newSource = `import { regex } from "arkregex";\n${newSource}`;
33+
}
34+
35+
return newSource;
36+
}
37+
38+
export default transform;

codemods/sample-jssg-codemod/tests/fixtures/expected.js renamed to codemods/regexp-to-arkregex/tests/fixtures/expected.js

File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var oldVariable = "should be const";
2+
var anotherVar = 42;
3+
console.log("debug statement");
File renamed without changes.

0 commit comments

Comments
 (0)