File tree Expand file tree Collapse file tree 8 files changed +158
-0
lines changed
recipes/testing-another-codemod Expand file tree Collapse file tree 8 files changed +158
-0
lines changed Original file line number Diff line number Diff line change 1+ # Dependencies
2+ node_modules /
3+ npm-debug.log *
4+ yarn-debug.log *
5+ yarn-error.log *
6+
7+ # Build artifacts
8+ target /
9+ dist /
10+ build /
11+
12+ # Temporary files
13+ * .tmp
14+ * .temp
15+ .cache /
16+
17+ # Environment files
18+ .env
19+ .env.local
20+
21+ # IDE files
22+ .vscode /
23+ .idea /
24+ * .swp
25+ * .swo
26+
27+ # OS files
28+ .DS_Store
29+ Thumbs.db
30+
31+ # Package bundles
32+ * .tar.gz
33+ * .tgz
Original file line number Diff line number Diff line change 1+ # testing-another-codemod
2+
3+ Transform legacy code patterns
4+
5+ ## Installation
6+
7+ ``` bash
8+ # Install from registry
9+ codemod run testing-another-codemod
10+
11+ # Or run locally
12+ codemod run -w workflow.yaml
13+ ```
14+
15+ ## Usage
16+
17+ This codemod transforms typescript code by:
18+
19+ - Converting ` var ` declarations to ` const ` /` let `
20+ - Removing debug statements
21+ - Modernizing syntax patterns
22+
23+ ## Development
24+
25+ ``` bash
26+ # Test the transformation
27+ npm test
28+
29+ # Validate the workflow
30+ codemod validate -w workflow.yaml
31+
32+ # Publish to registry
33+ codemod login
34+ codemod publish
35+ ```
36+
37+ ## License
38+
39+ MIT
Original file line number Diff line number Diff line change 1+ schema_version : " 1.0"
2+
3+ name : " testing-another-codemod"
4+ version : " 0.1.0"
5+ description : " Transform legacy code patterns"
6+ author :
" Author <[email protected] >" 7+ license : " MIT"
8+ workflow : " workflow.yaml"
9+ category : " migration"
10+
11+ targets :
12+ languages : ["typescript"]
13+
14+ keywords : ["transformation", "migration"]
15+
16+ registry :
17+ access : " public"
18+ visibility : " public"
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " testing-another-codemod" ,
3+ "version" : " 0.1.0" ,
4+ "description" : " Transform legacy code patterns" ,
5+ "type" : " module" ,
6+ "devDependencies" : {
7+ "@codemod.com/jssg-types" : " ^1.0.3" ,
8+ "typescript" : " ^5.8.3"
9+ },
10+ "scripts" : {
11+ "test" : " npx codemod@latest jssg test -l typescript ./scripts/codemod.ts" ,
12+ "check-types" : " npx tsc --noEmit"
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ import type { SgRoot } 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 nodes = rootNode.findAll({
8+ rule: {
9+ pattern: "var $VAR = $VALUE",
10+ },
11+ });
12+
13+ const edits = nodes.map((node) => {
14+ const varName = node.getMatch("VAR")?.text();
15+ const value = node.getMatch("VALUE")?.text();
16+ return node.replace(`const ${varName} = ${value}`);
17+ });
18+
19+ const newSource = rootNode.commitEdits(edits);
20+ return newSource;
21+ }
22+
23+ export default transform;
Original file line number Diff line number Diff line change 1+ const oldVariable = "should be const"
2+ const anotherVar = 42
3+ console . log ( "debug statement" ) ;
Original file line number Diff line number Diff line change 1+ {
2+ "compilerOptions" : {
3+ "module" : " NodeNext" ,
4+ "moduleResolution" : " NodeNext" ,
5+ "types" : [" @codemod.com/jssg-types" ],
6+ "allowImportingTsExtensions" : true ,
7+ "noEmit" : true ,
8+ "verbatimModuleSyntax" : true ,
9+ "erasableSyntaxOnly" : true ,
10+ "strict" : true ,
11+ "strictNullChecks" : true ,
12+ "noImplicitReturns" : true ,
13+ "noFallthroughCasesInSwitch" : true ,
14+ "noUncheckedIndexedAccess" : true
15+ },
16+ "exclude" : [" tests" ]
17+ }
Original file line number Diff line number Diff line change 1+ version : " 1"
2+
3+ nodes :
4+ - id : apply-transforms
5+ name : Apply AST Transformations
6+ type : automatic
7+ steps :
8+ - name : " Scan typescript files and apply fixes"
9+ js-ast-grep :
10+ js_file : scripts/codemod.ts
11+ language : " typescript"
You can’t perform that action at this time.
0 commit comments