Skip to content

Commit

Permalink
Merge branch 'codemod-com:main' into electron-v33
Browse files Browse the repository at this point in the history
  • Loading branch information
Yugal41735 authored Sep 20, 2024
2 parents a8a07f6 + 8466809 commit e6b5e8b
Show file tree
Hide file tree
Showing 343 changed files with 12,378 additions and 95 deletions.
30 changes: 18 additions & 12 deletions apps/docs/codemod-studio.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,30 @@ To use natural language descriptions, you can:

**Example:**

Before:
<video
autoPlay
loop
className="w-full aspect-video"
src="/images/codemod-studio/nld-usage.mp4"
></video>

<CodeGroup>

```tsx
const mapStateToProps = (state) => ({
a: selectA(state),
});
```tsx Before snippet
// In react 19, string refs are deprecated and replaced with callback refs.
<div ref='refName' />;
```

After:
```tsx After snippet
<div ref={(ref) => (this.refs.refName = ref)} />;
```

```tsx
// the codemod should work on any function argument which is named "state"
function mapStateToProps(state: State) {
const { data } = state;
return { data };
}
```tsx Output
<div ref={(ref) => (this.refs.refName = ref)} />;
```

</CodeGroup>

### Automated Test Case Generation

Codemod AI can generate new before and after examples automatically based on the existing test cases or a description from a migration guide. This feature allows you to create new before and after examples quickly and easily.
Expand Down
Binary file added apps/docs/images/codemod-studio/nld-usage.mp4
Binary file not shown.
12 changes: 12 additions & 0 deletions packages/codemods/jasmine/5.0/handling_env-execute/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "jasmine/v5/handling-env-execute-callbacks",
"version": "1.0.2",
"engine": "jscodeshift",
"private": false,
"arguments": [],
"meta": {
"tags": ["jasmine", "migration", "v5"],
"git": "https://github.com/codemod-com/codemod/pull/1319/files"
}
}
2 changes: 2 additions & 0 deletions packages/codemods/jasmine/5.0/handling_env-execute/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
9 changes: 9 additions & 0 deletions packages/codemods/jasmine/5.0/handling_env-execute/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 yugal41735

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 28 additions & 0 deletions packages/codemods/jasmine/5.0/handling_env-execute/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
This codemod migrates `Env.execute` callbacks to await.

## Example

### Before

```ts
try {
env.execute(null, function () {
console.log("Test suite finished.");
});
} catch (e) {
console.log("Failed to start the test suite.");
}

```

### After

```ts
try {
await env.execute();
} catch (e) {
console.log("Failed to start the test suite.");
}

```

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const toReplace = "hello";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const replacement = "hello";
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = function (fileInfo, api) {
const j = api.jscodeshift;
const root = j(fileInfo.source);

// Find all CallExpressions where env.execute is called with a callback
root.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: { name: 'env' },
property: { name: 'execute' }
},
arguments: (args) => args.length === 2 && j.FunctionExpression.check(args[1]),
}).forEach((path) => {
const callExpression = path.node;

// Replace the function expression argument with async/await syntax
callExpression.arguments = [];

// Wrap in an await expression
const awaitExpression = j.awaitExpression(callExpression);

// Replace the old statement with await env.execute()
j(path).replaceWith(awaitExpression);
});

return root.toSource();
};

23 changes: 23 additions & 0 deletions packages/codemods/jasmine/5.0/handling_env-execute/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "handling-env-execute",
"license": "MIT",
"devDependencies": {
"@types/node": "20.9.0",
"typescript": "^5.2.2",
"vitest": "^1.0.1",
"@codemod.com/codemod-utils": "*",
"jscodeshift": "^0.15.1",
"@types/jscodeshift": "^0.11.10"
},
"scripts": {
"test": "vitest run",
"test:watch": "vitest watch"
},
"files": [
"README.md",
".codemodrc.json",
"/dist/index.cjs"
],
"type": "module",
"author": "yugal41735"
}
29 changes: 29 additions & 0 deletions packages/codemods/jasmine/5.0/handling_env-execute/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { API, FileInfo } from 'jscodeshift';

export default function transformer(fileInfo: FileInfo, api: API) {
const j = api.jscodeshift;
const root = j(fileInfo.source);

// Find all CallExpressions where env.execute is called with a callback
root.find(j.CallExpression, {
callee: {
type: 'MemberExpression',
object: { name: 'env' },
property: { name: 'execute' }
},
arguments: (args: any[]) => args.length === 2 && j.FunctionExpression.check(args[1]),
}).forEach((path) => {
const callExpression = path.node;

// Replace the function expression argument with async/await syntax
callExpression.arguments = [];

// Wrap in an await expression
const awaitExpression = j.awaitExpression(callExpression);

// Replace the old statement with await env.execute()
j(path).replaceWith(awaitExpression);
});

return root.toSource();
}
40 changes: 40 additions & 0 deletions packages/codemods/jasmine/5.0/handling_env-execute/test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, it } from 'vitest';
import jscodeshift, { type API } from 'jscodeshift';
import transform from '../src/index.js';
import assert from 'node:assert';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';

const buildApi = (parser: string | undefined): API => ({
j: parser ? jscodeshift.withParser(parser) : jscodeshift,
jscodeshift: parser ? jscodeshift.withParser(parser) : jscodeshift,
stats: () => {
console.error(
'The stats function was called, which is not supported on purpose',
);
},
report: () => {
console.error(
'The report function was called, which is not supported on purpose',
);
},
});

describe('handling_env-execute', () => {
it('test #1', async () => {
const INPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture1.input.ts'), 'utf-8');
const OUTPUT = await readFile(join(__dirname, '..', '__testfixtures__/fixture1.output.ts'), 'utf-8');

const actualOutput = transform({
path: 'index.js',
source: INPUT,
},
buildApi('tsx'), {}
);

assert.deepEqual(
actualOutput?.replace(/W/gm, ''),
OUTPUT.replace(/W/gm, ''),
);
});
});
38 changes: 38 additions & 0 deletions packages/codemods/jasmine/5.0/handling_env-execute/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"compilerOptions": {
"module": "NodeNext",
"target": "ESNext",
"moduleResolution": "NodeNext",
"lib": [
"ESNext",
"DOM"
],
"skipLibCheck": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"isolatedModules": true,
"jsx": "react-jsx",
"useDefineForClassFields": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
"strict": true,
"strictNullChecks": true,
"incremental": true,
"noUncheckedIndexedAccess": true,
"noPropertyAccessFromIndexSignature": false,
"allowJs": true
},
"include": [
"./src/**/*.ts",
"./src/**/*.js",
"./test/**/*.ts",
"./test/**/*.js"
],
"exclude": ["node_modules", "./dist/**/*"],
"ts-node": {
"transpileOnly": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { configDefaults, defineConfig } from 'vitest/config';

export default defineConfig({
test: {
include: [...configDefaults.include, '**/test/*.ts'],
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "jasmine/v5/migration-recipe",
"version": "1.0.1",
"engine": "recipe",
"private": false,
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx", "**/*.vue"],
"applicability": {
"from": [["jasmine", ">=", "4.0.0"], ["jasmine", "<", "5.0.0"]],
"to": [["jasmine", ">=", "5.0.0"]]
},
"arguments": [],
"meta": {
"tags": ["migration", "jasmine", "v5"],
"git": "https://github.com/codemod-com/codemod/pull/1319/files"
},
"names": [
"jasmine/v5/handling-env-execute-callbacks",
"jasmine/v5/node-boot-removal"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This recipe is a set of codemods that will help migrate to jasmine v5 from jasmine 4.x .

The recipe includes the following codemods:


- jasmine/v5/handling-env-execute-callbacks
- jasmine/v5/node-boot-removal
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@codemod/jasmine-v5-migration-recipe",
"files": ["./README.md", "./.codemodrc.json"],
"type": "module",
"private": true
}
12 changes: 12 additions & 0 deletions packages/codemods/jasmine/5.0/node-boot-removal/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "jasmine/v5/node-boot-removal",
"version": "1.0.1",
"engine": "jscodeshift",
"private": false,
"arguments": [],
"meta": {
"tags": ["jasmine", "migration", "v5"],
"git": "https://github.com/codemod-com/codemod/pull/1319/files"
}
}
2 changes: 2 additions & 0 deletions packages/codemods/jasmine/5.0/node-boot-removal/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
9 changes: 9 additions & 0 deletions packages/codemods/jasmine/5.0/node-boot-removal/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 yugal41735

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 changes: 17 additions & 0 deletions packages/codemods/jasmine/5.0/node-boot-removal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
This codemod remove `node_boot.js` as it is no longer supported in jasmin 5.0


## Example

### Before

```ts
const boot = require('jasmine-core/node_boot.js');
```

### After

```ts
const boot = require('jasmine-core').boot;
```

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const boot = require('jasmine-core/node_boot.js');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const boot = require('jasmine-core').boot;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*! @license
The MIT License (MIT)
Copyright (c) 2024 yugal41735
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
"use strict";Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"default",{enumerable:true,get:function(){return transform}});function transform(file,api,options){const j=api.jscodeshift;const root=j(file.source);let dirtyFlag=false;root.find(j.VariableDeclarator,{init:{callee:{name:"require"},arguments:[{value:"jasmine-core/node_boot.js"}]}}).forEach(path=>{const requireCall=path.value.init;if(j.CallExpression.check(requireCall)&&requireCall.arguments.length===1){const arg=requireCall.arguments[0];if(j.Literal.check(arg)&&arg.value==="jasmine-core/node_boot.js"){path.value.init=j.memberExpression(j.callExpression(j.identifier("require"),[j.literal("jasmine-core")]),j.identifier("boot"));dirtyFlag=true}}});return dirtyFlag?root.toSource():undefined}
Loading

0 comments on commit e6b5e8b

Please sign in to comment.