Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added remx.js codemods #1313

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/codemods/remix/v2/migration-reciepe/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "remix-js/v2/migration-recipe",
"version": "1.0.0",
"engine": "recipe",
"private": false,
"names": [
"remix-js/v2/add-matches-param-and-array-return-to-meta-export",
"remix-js/v2/fetcher-submission-properties-flattening",
"remix-js/v2/nodejs-module-exports-server-build-directory-to-path",
"remix-js/v2/remix-1-add-meta-v1-wrapper",
"remix-js/v2/remix-1-replace-response-with-create-readable-stream",
"remix-js/v2/remix-1-use-route-error-codemod",
"remix-js/v2/remix-run-v1-meta-function-args-wrapper",
"remix-js/v2/rename-browser-build-directory",
"remix-js/v2/typescript-2-update-links-function-and-property-names",
"remix-js/v2/webpack-v2-migrate-dev-server-port"
],
"meta": {
"tags": [
"remix",
"migration"
],
"git": "https://github.com/codemod-com/codemod/packages/codemods/remix/v2/migration-receipe"
},
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx", "**/*.vue"]
}
15 changes: 15 additions & 0 deletions packages/codemods/remix/v2/migration-reciepe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
This recipe is a set of codemods that will help migrate to Remix.js v2.


The recipe includes the following codemods:

- remix-js/v2/add-matches-param-and-array-return-to-meta-export
- remix-js/v2/fetcher-submission-properties-flattening
- remix-js/v2/nodejs-module-exports-server-build-directory-to-path
- remix-js/v2/remix-1-add-meta-v1-wrapper
- remix-js/v2/remix-1-replace-response-with-create-readable-stream
- remix-js/v2/remix-1-use-route-error-codemod
- remix-js/v2/remix-run-v1-meta-function-args-wrapper
- remix-js/v2/rename-browser-build-directory
- remix-js/v2/typescript-2-update-links-function-and-property-names
- remix-js/v2/webpack-v2-migrate-dev-server-port
6 changes: 6 additions & 0 deletions packages/codemods/remix/v2/migration-reciepe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@codemod/remix-js-v2-migration-recipe",
"files": ["./README.md", "./.codemodrc.json"],
"type": "module",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "remix-js/v2/add-matches-param-and-array-return-to-meta-export",
"version": "1.0.0",
"engine": "jscodeshift",
"private": false,
"arguments": [],
"meta": {
"tags": [
"remix.js",
"v2",
"add-matches param",
"array-return-to-meta-export",
"input"
],
"git": "https://github.com/dfordp/remix-js-v2-add-matches-param-and-array-return-to-meta-export"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 dfordp

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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Instead of returning an object from meta, you will now return an array of descriptors and manage the merge yourself. This brings the meta API closer to links, and it allows for more flexibility and control over how meta tags are rendered.

### Before

```ts
export const meta: MetaFunction = ({ data }) => {
const title = 'My Page Title';

return {
charset: 'utf-8',
viewport: 'width=device-width,initial-scale=1',
title,
};
};
```

### After

```ts
export const meta: MetaFunction = ({ data, matches }) => {
const title = 'My Page Title';

return [{
charset: 'utf-8',
viewport: 'width=device-width,initial-scale=1',
title,
}, ];
};
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const meta: MetaFunction = ({ data }) => {
const title = 'My Page Title';

return {
charset: 'utf-8',
viewport: 'width=device-width,initial-scale=1',
title,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const meta: MetaFunction = ({ data, matches }) => {
const title = 'My Page Title';

return [{
charset: 'utf-8',
viewport: 'width=device-width,initial-scale=1',
title,
}, ];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*! @license
The MIT License (MIT)

Copyright (c) 2024 dfordp

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.ExportNamedDeclaration).forEach(path=>{const declaration=path.node.declaration;if(j.VariableDeclaration.check(declaration)){const declarator=declaration.declarations[0];if(j.VariableDeclarator.check(declarator)&&j.Identifier.check(declarator.id)&&declarator.id.name==="meta"){const init=declarator.init;if(j.FunctionExpression.check(init)||j.ArrowFunctionExpression.check(init)){const params=init.params;if(params.length===1&&j.ObjectPattern.check(params[0])){params[0].properties.push(j.property.from({kind:"init",key:j.identifier("matches"),value:j.identifier("matches"),shorthand:true}));dirtyFlag=true}root.find(j.BlockStatement).forEach(blockPath=>{const body=blockPath.node.body;body.forEach((statement,index)=>{if(j.ReturnStatement.check(statement)&&j.ObjectExpression.check(statement.argument)){body[index]=j.returnStatement(j.arrayExpression([statement.argument]));dirtyFlag=true}})})}}}});return dirtyFlag?root.toSource():undefined}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "add-matches-param-and-array-return-to-meta-export",
"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": "dfordp"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export default function transform(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

// Find the export const meta function
root.find(j.ExportNamedDeclaration).forEach(path => {
const declaration = path.node.declaration;
if (j.VariableDeclaration.check(declaration)) {
const declarator = declaration.declarations[0];
if (j.VariableDeclarator.check(declarator) && j.Identifier.check(declarator.id) && declarator.id.name === 'meta') {
const init = declarator.init;
if (j.FunctionExpression.check(init) || j.ArrowFunctionExpression.check(init)) {
const params = init.params;
if (params.length === 1 && j.ObjectPattern.check(params[0])) {
// Add matches to the function parameters
params[0].properties.push(j.property.from({
kind: 'init',
key: j.identifier('matches'),
value: j.identifier('matches'),
shorthand: true
}));
dirtyFlag = true;
}

// Change the return type to an array containing a single object
root.find(j.BlockStatement).forEach(blockPath => {
const body = blockPath.node.body;
body.forEach((statement, index) => {
if (j.ReturnStatement.check(statement) && j.ObjectExpression.check(statement.argument)) {
body[index] = j.returnStatement(
j.arrayExpression([statement.argument])
);
dirtyFlag = true;
}
});
});
}
}
}
});

return dirtyFlag ? root.toSource() : undefined;
}
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('add-matches-param-and-array-return-to-meta-export', () => {
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, ''),
);
});
});
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,17 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "remix-js/v2/fetcher-submission-properties-flattening",
"version": "1.0.0",
"engine": "jscodeshift",
"private": false,
"arguments": [],
"meta": {
"tags": [
"remix.js",
"v2",
"fetcher submission",
"properties flattening"
],
"git": "https://github.com/dfordp/remix-js-v2-fetcher-submission-properties-flattening"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 dfordp

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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Like useNavigation, useFetcher has flattened the submission and removed the type field.

### Before

```ts
import { useFetcher } from '@remix-run/react';

function SomeComponent() {
const fetcher = useFetcher();
fetcher.submission.formData;
fetcher.submission.formMethod;
fetcher.submission.formAction;
fetcher.type;
}
```

### After

```ts
import { useFetcher } from '@remix-run/react';

function SomeComponent() {
const fetcher = useFetcher();

// these keys are flattened
fetcher.formData;
fetcher.formMethod;
fetcher.formAction;

// this key is removed
fetcher.type;
}
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useFetcher } from '@remix-run/react';

function SomeComponent() {
const fetcher = useFetcher();
fetcher.submission.formData;
fetcher.submission.formMethod;
fetcher.submission.formAction;
fetcher.type;
}
Loading
Loading