From 5118000d55ae4e3a1597d69e6cb15a5bbc64e915 Mon Sep 17 00:00:00 2001 From: Mohab Sameh Date: Wed, 3 Sep 2025 00:10:26 +0300 Subject: [PATCH 1/2] add codemod --- testing-another-codemod/.gitignore | 33 ++++++++++++++++ testing-another-codemod/README.md | 39 +++++++++++++++++++ testing-another-codemod/codemod.yaml | 18 +++++++++ testing-another-codemod/package.json | 14 +++++++ testing-another-codemod/scripts/codemod.ts | 23 +++++++++++ .../tests/fixtures/expected.js | 3 ++ testing-another-codemod/tsconfig.json | 17 ++++++++ testing-another-codemod/workflow.yaml | 11 ++++++ 8 files changed, 158 insertions(+) create mode 100644 testing-another-codemod/.gitignore create mode 100644 testing-another-codemod/README.md create mode 100644 testing-another-codemod/codemod.yaml create mode 100644 testing-another-codemod/package.json create mode 100644 testing-another-codemod/scripts/codemod.ts create mode 100644 testing-another-codemod/tests/fixtures/expected.js create mode 100644 testing-another-codemod/tsconfig.json create mode 100644 testing-another-codemod/workflow.yaml diff --git a/testing-another-codemod/.gitignore b/testing-another-codemod/.gitignore new file mode 100644 index 0000000..78174f4 --- /dev/null +++ b/testing-another-codemod/.gitignore @@ -0,0 +1,33 @@ +# Dependencies +node_modules/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Build artifacts +target/ +dist/ +build/ + +# Temporary files +*.tmp +*.temp +.cache/ + +# Environment files +.env +.env.local + +# IDE files +.vscode/ +.idea/ +*.swp +*.swo + +# OS files +.DS_Store +Thumbs.db + +# Package bundles +*.tar.gz +*.tgz \ No newline at end of file diff --git a/testing-another-codemod/README.md b/testing-another-codemod/README.md new file mode 100644 index 0000000..69ee8ed --- /dev/null +++ b/testing-another-codemod/README.md @@ -0,0 +1,39 @@ +# testing-another-codemod + +Transform legacy code patterns + +## Installation + +```bash +# Install from registry +codemod run testing-another-codemod + +# Or run locally +codemod run -w workflow.yaml +``` + +## Usage + +This codemod transforms typescript code by: + +- Converting `var` declarations to `const`/`let` +- Removing debug statements +- Modernizing syntax patterns + +## Development + +```bash +# Test the transformation +npm test + +# Validate the workflow +codemod validate -w workflow.yaml + +# Publish to registry +codemod login +codemod publish +``` + +## License + +MIT \ No newline at end of file diff --git a/testing-another-codemod/codemod.yaml b/testing-another-codemod/codemod.yaml new file mode 100644 index 0000000..1bb1feb --- /dev/null +++ b/testing-another-codemod/codemod.yaml @@ -0,0 +1,18 @@ +schema_version: "1.0" + +name: "testing-another-codemod" +version: "0.1.0" +description: "Transform legacy code patterns" +author: "Author " +license: "MIT" +workflow: "workflow.yaml" +category: "migration" + +targets: + languages: ["typescript"] + +keywords: ["transformation", "migration"] + +registry: + access: "public" + visibility: "public" diff --git a/testing-another-codemod/package.json b/testing-another-codemod/package.json new file mode 100644 index 0000000..34a0184 --- /dev/null +++ b/testing-another-codemod/package.json @@ -0,0 +1,14 @@ +{ + "name": "testing-another-codemod", + "version": "0.1.0", + "description": "Transform legacy code patterns", + "type": "module", + "devDependencies": { + "@codemod.com/jssg-types": "^1.0.3", + "typescript": "^5.8.3" + }, + "scripts": { + "test": "npx codemod@latest jssg test -l typescript ./scripts/codemod.ts", + "check-types": "npx tsc --noEmit" + } +} diff --git a/testing-another-codemod/scripts/codemod.ts b/testing-another-codemod/scripts/codemod.ts new file mode 100644 index 0000000..85c69c3 --- /dev/null +++ b/testing-another-codemod/scripts/codemod.ts @@ -0,0 +1,23 @@ +import type { SgRoot } from "codemod:ast-grep"; +import type TS from "codemod:ast-grep/langs/typescript"; + +async function transform(root: SgRoot): Promise { + const rootNode = root.root(); + + const nodes = rootNode.findAll({ + rule: { + pattern: "var $VAR = $VALUE", + }, + }); + + const edits = nodes.map((node) => { + const varName = node.getMatch("VAR")?.text(); + const value = node.getMatch("VALUE")?.text(); + return node.replace(`const ${varName} = ${value}`); + }); + + const newSource = rootNode.commitEdits(edits); + return newSource; +} + +export default transform; diff --git a/testing-another-codemod/tests/fixtures/expected.js b/testing-another-codemod/tests/fixtures/expected.js new file mode 100644 index 0000000..0eb1048 --- /dev/null +++ b/testing-another-codemod/tests/fixtures/expected.js @@ -0,0 +1,3 @@ +const oldVariable = "should be const" +const anotherVar = 42 +console.log("debug statement"); \ No newline at end of file diff --git a/testing-another-codemod/tsconfig.json b/testing-another-codemod/tsconfig.json new file mode 100644 index 0000000..469fc5a --- /dev/null +++ b/testing-another-codemod/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "module": "NodeNext", + "moduleResolution": "NodeNext", + "types": ["@codemod.com/jssg-types"], + "allowImportingTsExtensions": true, + "noEmit": true, + "verbatimModuleSyntax": true, + "erasableSyntaxOnly": true, + "strict": true, + "strictNullChecks": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedIndexedAccess": true + }, + "exclude": ["tests"] +} diff --git a/testing-another-codemod/workflow.yaml b/testing-another-codemod/workflow.yaml new file mode 100644 index 0000000..50a4933 --- /dev/null +++ b/testing-another-codemod/workflow.yaml @@ -0,0 +1,11 @@ +version: "1" + +nodes: + - id: apply-transforms + name: Apply AST Transformations + type: automatic + steps: + - name: "Scan typescript files and apply fixes" + js-ast-grep: + js_file: scripts/codemod.ts + language: "typescript" From 5afbe1823b923e09db31c13cc2e9540c510f734f Mon Sep 17 00:00:00 2001 From: Mohab Sameh Date: Wed, 3 Sep 2025 00:13:09 +0300 Subject: [PATCH 2/2] move files --- .../testing-another-codemod}/.gitignore | 0 .../testing-another-codemod}/README.md | 0 .../testing-another-codemod}/codemod.yaml | 0 .../testing-another-codemod}/package.json | 0 .../testing-another-codemod}/scripts/codemod.ts | 0 .../testing-another-codemod}/tests/fixtures/expected.js | 0 .../testing-another-codemod}/tsconfig.json | 0 .../testing-another-codemod}/workflow.yaml | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename {testing-another-codemod => recipes/testing-another-codemod}/.gitignore (100%) rename {testing-another-codemod => recipes/testing-another-codemod}/README.md (100%) rename {testing-another-codemod => recipes/testing-another-codemod}/codemod.yaml (100%) rename {testing-another-codemod => recipes/testing-another-codemod}/package.json (100%) rename {testing-another-codemod => recipes/testing-another-codemod}/scripts/codemod.ts (100%) rename {testing-another-codemod => recipes/testing-another-codemod}/tests/fixtures/expected.js (100%) rename {testing-another-codemod => recipes/testing-another-codemod}/tsconfig.json (100%) rename {testing-another-codemod => recipes/testing-another-codemod}/workflow.yaml (100%) diff --git a/testing-another-codemod/.gitignore b/recipes/testing-another-codemod/.gitignore similarity index 100% rename from testing-another-codemod/.gitignore rename to recipes/testing-another-codemod/.gitignore diff --git a/testing-another-codemod/README.md b/recipes/testing-another-codemod/README.md similarity index 100% rename from testing-another-codemod/README.md rename to recipes/testing-another-codemod/README.md diff --git a/testing-another-codemod/codemod.yaml b/recipes/testing-another-codemod/codemod.yaml similarity index 100% rename from testing-another-codemod/codemod.yaml rename to recipes/testing-another-codemod/codemod.yaml diff --git a/testing-another-codemod/package.json b/recipes/testing-another-codemod/package.json similarity index 100% rename from testing-another-codemod/package.json rename to recipes/testing-another-codemod/package.json diff --git a/testing-another-codemod/scripts/codemod.ts b/recipes/testing-another-codemod/scripts/codemod.ts similarity index 100% rename from testing-another-codemod/scripts/codemod.ts rename to recipes/testing-another-codemod/scripts/codemod.ts diff --git a/testing-another-codemod/tests/fixtures/expected.js b/recipes/testing-another-codemod/tests/fixtures/expected.js similarity index 100% rename from testing-another-codemod/tests/fixtures/expected.js rename to recipes/testing-another-codemod/tests/fixtures/expected.js diff --git a/testing-another-codemod/tsconfig.json b/recipes/testing-another-codemod/tsconfig.json similarity index 100% rename from testing-another-codemod/tsconfig.json rename to recipes/testing-another-codemod/tsconfig.json diff --git a/testing-another-codemod/workflow.yaml b/recipes/testing-another-codemod/workflow.yaml similarity index 100% rename from testing-another-codemod/workflow.yaml rename to recipes/testing-another-codemod/workflow.yaml