forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move next-codemod to Next.js monorepo (vercel#15536)
- Loading branch information
1 parent
f4433ce
commit 62031ff
Showing
99 changed files
with
2,565 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.d.ts | ||
*.js | ||
*.js.map | ||
!transforms/__tests__/**/*.js | ||
!transforms/__testfixtures__/**/*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# Next.js Codemod | ||
|
||
This repository contains Codemod transformations to help upgrade Next.js codebases. | ||
|
||
## v9 | ||
|
||
### `name-default-component` | ||
|
||
Transforms anonymous components into named components to make sure they work with [Fast Refresh](https://nextjs.org/blog/next-9-4#fast-refresh). | ||
|
||
For example | ||
|
||
```jsx | ||
// my-component.js | ||
export default function () { | ||
return <div>Hello World</div> | ||
} | ||
``` | ||
|
||
Transforms into: | ||
|
||
```jsx | ||
// my-component.js | ||
export default function MyComponent() { | ||
return <div>Hello World</div> | ||
} | ||
``` | ||
|
||
The component will have a camel cased name based on the name of the file, and it also works with arrow functions. | ||
|
||
#### Usage | ||
|
||
Go to your project | ||
|
||
``` | ||
cd path-to-your-project/ | ||
``` | ||
|
||
Download the codemod: | ||
|
||
``` | ||
curl -L https://github.com/zeit/next-codemod/archive/master.tar.gz | tar -xz --strip=2 next-codemod-master/transforms/name-default-component.js | ||
``` | ||
|
||
Run the transformation: | ||
|
||
``` | ||
npx jscodeshift -t ./name-default-component.js components/**/*.js | ||
``` | ||
|
||
TypeScript files can use this codemod too: | ||
|
||
``` | ||
npx jscodeshift -t ./name-default-component.js --parser=tsx components/**/*.tsx | ||
``` | ||
|
||
If you have components in multiple folders, change the path to `**/*.js` and add `--ignore-pattern="**/node_modules/**"`. | ||
|
||
After the transformation is done the `name-default-component.js` file in the root of your project can be removed. | ||
|
||
### `withamp-to-config` | ||
|
||
Transforms the `withAmp` HOC into Next.js 9 page configuration. | ||
|
||
For example: | ||
|
||
```js | ||
// Before | ||
import { withAmp } from 'next/amp' | ||
|
||
function Home() { | ||
return <h1>My AMP Page</h1> | ||
} | ||
|
||
export default withAmp(Home) | ||
``` | ||
|
||
```js | ||
// After | ||
export default function Home() { | ||
return <h1>My AMP Page</h1> | ||
} | ||
|
||
export const config = { | ||
amp: true, | ||
} | ||
``` | ||
|
||
#### Usage | ||
|
||
Go to your project | ||
|
||
``` | ||
cd path-to-your-project/ | ||
``` | ||
|
||
Download the codemod: | ||
|
||
``` | ||
curl -L https://github.com/zeit/next-codemod/archive/master.tar.gz | tar -xz --strip=2 next-codemod-master/transforms/withamp-to-config.js | ||
``` | ||
|
||
Run the transformation: | ||
|
||
``` | ||
npx jscodeshift -t ./withamp-to-config.js pages/**/*.js | ||
``` | ||
|
||
After the transformation is done the `withamp-to-config.js` file in the root of your project can be removed. | ||
|
||
## v6 | ||
|
||
### `url-to-withrouter` | ||
|
||
Tranforms the deprecated automatically injected `url` property on top level pages to using `withRouter` and the `router` property it injects. Read more here: [err.sh/next.js/url-deprecated](https://err.sh/next.js/url-deprecated) | ||
|
||
For example: | ||
|
||
```js | ||
// From | ||
import React from 'react' | ||
export default class extends React.Component { | ||
render() { | ||
const { pathname } = this.props.url | ||
return <div>Current pathname: {pathname}</div> | ||
} | ||
} | ||
``` | ||
|
||
```js | ||
// To | ||
import React from 'react' | ||
import { withRouter } from 'next/router' | ||
export default withRouter( | ||
class extends React.Component { | ||
render() { | ||
const { pathname } = this.props.router | ||
return <div>Current pathname: {pathname}</div> | ||
} | ||
} | ||
) | ||
``` | ||
|
||
This is just one case. All the cases that are transformed (and tested) can be found in the [`__testfixtures__` directory](./transforms/__testfixtures__/url-to-withrouter). | ||
|
||
#### Usage | ||
|
||
Go to your project | ||
|
||
``` | ||
cd path-to-your-project/ | ||
``` | ||
|
||
Download the codemod: | ||
|
||
``` | ||
curl -L https://github.com/zeit/next-codemod/archive/master.tar.gz | tar -xz --strip=2 next-codemod-master/transforms/url-to-withrouter.js | ||
``` | ||
|
||
Run the transformation: | ||
|
||
``` | ||
npx jscodeshift -t ./url-to-withrouter.js pages/**/*.js | ||
``` | ||
|
||
After the transformation is done the `url-to-withrouter.js` file in the root of your project can be removed. | ||
|
||
## Authors | ||
|
||
- Tim Neutkens ([@timneutkens](https://twitter.com/timneutkens)) – [ZEIT](https://zeit.co) | ||
- Joe Haddad ([@timer150](https://twitter.com/timer150)) - [ZEIT](https://zeit.co) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 Vercel, Inc. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@next/codemod", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
"dependencies": { | ||
"jscodeshift": "^0.6.4" | ||
}, | ||
"scripts": { | ||
"prepublish": "tsc -d -p tsconfig.json", | ||
"build": "tsc -d -w -p tsconfig.json", | ||
"test": "jest" | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...-codemod/transforms/__testfixtures__/name-default-component/1-starts-with-number.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => <div>Anonymous function</div>; |
Empty file.
11 changes: 11 additions & 0 deletions
11
.../next-codemod/transforms/__testfixtures__/name-default-component/existing-name-2.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class ExistingName2Input { | ||
render() {} | ||
} | ||
|
||
class nested { | ||
render() { | ||
const ExistingName2InputComponent = null; | ||
} | ||
} | ||
|
||
export default () => <div>Anonymous function</div>; |
13 changes: 13 additions & 0 deletions
13
...next-codemod/transforms/__testfixtures__/name-default-component/existing-name-2.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class ExistingName2Input { | ||
render() {} | ||
} | ||
|
||
class nested { | ||
render() { | ||
const ExistingName2InputComponent = null; | ||
} | ||
} | ||
|
||
const ExistingName2InputComponent = () => <div>Anonymous function</div>; | ||
|
||
export default ExistingName2InputComponent; |
7 changes: 7 additions & 0 deletions
7
.../next-codemod/transforms/__testfixtures__/name-default-component/existing-name-3.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function ExistingName3Input() {} | ||
|
||
function nested() { | ||
const ExistingName3InputComponent = null; | ||
} | ||
|
||
export default () => <div>Anonymous function</div>; |
9 changes: 9 additions & 0 deletions
9
...next-codemod/transforms/__testfixtures__/name-default-component/existing-name-3.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function ExistingName3Input() {} | ||
|
||
function nested() { | ||
const ExistingName3InputComponent = null; | ||
} | ||
|
||
const ExistingName3InputComponent = () => <div>Anonymous function</div>; | ||
|
||
export default ExistingName3InputComponent; |
4 changes: 4 additions & 0 deletions
4
...-codemod/transforms/__testfixtures__/name-default-component/existing-name-ignore.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const ExistingNameIgnoreInput = null; | ||
const ExistingNameIgnoreInputComponent = null; | ||
|
||
export default () => <div>Anonymous function</div>; |
Empty file.
7 changes: 7 additions & 0 deletions
7
...es/next-codemod/transforms/__testfixtures__/name-default-component/existing-name.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const ExistingNameInput = null; | ||
|
||
function nested() { | ||
const ExistingNameInputComponent = null; | ||
} | ||
|
||
export default () => <div>Anonymous function</div>; |
9 changes: 9 additions & 0 deletions
9
...s/next-codemod/transforms/__testfixtures__/name-default-component/existing-name.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const ExistingNameInput = null; | ||
|
||
function nested() { | ||
const ExistingNameInputComponent = null; | ||
} | ||
|
||
const ExistingNameInputComponent = () => <div>Anonymous function</div>; | ||
|
||
export default ExistingNameInputComponent; |
1 change: 1 addition & 0 deletions
1
...-codemod/transforms/__testfixtures__/name-default-component/function-component-2.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => <div>Anonymous function</div>; |
2 changes: 2 additions & 0 deletions
2
...codemod/transforms/__testfixtures__/name-default-component/function-component-2.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const FunctionComponent2Input = () => <div>Anonymous function</div>; | ||
export default FunctionComponent2Input; |
7 changes: 7 additions & 0 deletions
7
...mod/transforms/__testfixtures__/name-default-component/function-component-ignore.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default () => { | ||
const x = 'y'; | ||
if (true) { | ||
return ''; | ||
} | ||
return null; | ||
}; |
Empty file.
7 changes: 7 additions & 0 deletions
7
...xt-codemod/transforms/__testfixtures__/name-default-component/function-component.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default () => { | ||
const x = 'y'; | ||
if (true) { | ||
return <div>Anonymous function</div>; | ||
} | ||
return null; | ||
}; |
9 changes: 9 additions & 0 deletions
9
...t-codemod/transforms/__testfixtures__/name-default-component/function-component.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const FunctionComponentInput = () => { | ||
const x = 'y'; | ||
if (true) { | ||
return <div>Anonymous function</div>; | ||
} | ||
return null; | ||
}; | ||
|
||
export default FunctionComponentInput; |
7 changes: 7 additions & 0 deletions
7
...od/transforms/__testfixtures__/name-default-component/function-expression-ignore.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Name() { | ||
const x = 'y'; | ||
if (true) { | ||
return <div>Anonymous function</div>; | ||
} | ||
return null; | ||
} |
Empty file.
7 changes: 7 additions & 0 deletions
7
...t-codemod/transforms/__testfixtures__/name-default-component/function-expression.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function () { | ||
const x = 'y'; | ||
if (true) { | ||
return <div>Anonymous function</div>; | ||
} | ||
return null; | ||
} |
7 changes: 7 additions & 0 deletions
7
...-codemod/transforms/__testfixtures__/name-default-component/function-expression.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function FunctionExpressionInput() { | ||
const x = 'y'; | ||
if (true) { | ||
return <div>Anonymous function</div>; | ||
} | ||
return null; | ||
} |
1 change: 1 addition & 0 deletions
1
...ext-codemod/transforms/__testfixtures__/name-default-component/[email protected]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default () => <div>Anonymous function</div>; |
Empty file.
7 changes: 7 additions & 0 deletions
7
...t-codemod/transforms/__testfixtures__/url-to-withrouter/already-using-withrouter.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {withRouter} from 'next/router' | ||
|
||
export default withRouter(class extends React.Component { | ||
render() { | ||
const test = this.props.url | ||
} | ||
}) |
7 changes: 7 additions & 0 deletions
7
...-codemod/transforms/__testfixtures__/url-to-withrouter/already-using-withrouter.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {withRouter} from 'next/router' | ||
|
||
export default withRouter(class extends React.Component { | ||
render() { | ||
const test = this.props.router | ||
} | ||
}) |
3 changes: 3 additions & 0 deletions
3
...t-codemod/transforms/__testfixtures__/url-to-withrouter/arrow-function-component.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default withAppContainer(withAuth(props => { | ||
const test = props.url | ||
})) |
Oops, something went wrong.