-
Notifications
You must be signed in to change notification settings - Fork 36
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
feat(codemod): UseSession Hook Update ( nextAuth - migration to v4) #1159
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/codemods/nextAuth/useSession-array-to-Object-destrucutring/.codemodrc.json
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,15 @@ | ||
{ | ||
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json", | ||
"name": "nextAuth/v4/useSession-array-to-object-destructuring", | ||
"version": "1.0.0", | ||
"engine": "jscodeshift", | ||
"private": false, | ||
"arguments": [], | ||
"meta": { | ||
"tags": ["nextAuth"], | ||
"git": "https://github.com/codemod-com/codemod" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets update the |
||
}, | ||
"applicability": { | ||
"from": [["nextAuth", "<", "v4"]] | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/codemods/nextAuth/useSession-array-to-Object-destrucutring/.gitignore
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 @@ | ||
node_modules | ||
dist |
9 changes: 9 additions & 0 deletions
9
packages/codemods/nextAuth/useSession-array-to-Object-destrucutring/LICENSE
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 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2024 Codemod 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. |
15 changes: 15 additions & 0 deletions
15
packages/codemods/nextAuth/useSession-array-to-Object-destrucutring/README.md
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,15 @@ | ||
The `useSession` hook has been updated to return an object. This allows you to test states much more cleanly with the new status option. | ||
|
||
### Before | ||
|
||
```ts | ||
const [session, loading] = useSession(); | ||
``` | ||
|
||
### After | ||
|
||
```ts | ||
const { data: session, status } = useSession(); | ||
const loading = status === 'loading'; | ||
``` | ||
|
1 change: 1 addition & 0 deletions
1
...mods/nextAuth/useSession-array-to-Object-destrucutring/__testfixtures__/fixture1.input.ts
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 @@ | ||
const [session, loading] = useSession(); |
2 changes: 2 additions & 0 deletions
2
...ods/nextAuth/useSession-array-to-Object-destrucutring/__testfixtures__/fixture1.output.ts
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 { data: session, status } = useSession(); | ||
const loading = status === 'loading'; |
22 changes: 22 additions & 0 deletions
22
packages/codemods/nextAuth/useSession-array-to-Object-destrucutring/package.json
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,22 @@ | ||
{ | ||
"name": "nextjs-use-session-array-to-object-destructuring", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "20.9.0", | ||
"typescript": "^5.2.2", | ||
"vitest": "^1.0.1", | ||
"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": "manishjha-04" | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/codemods/nextAuth/useSession-array-to-Object-destrucutring/src/index.ts
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,89 @@ | ||
import type { API, FileInfo, Options } from 'jscodeshift'; | ||
|
||
function transform( | ||
file: FileInfo, | ||
api: API, | ||
options: Options, | ||
): string | undefined { | ||
const j = api.jscodeshift; | ||
const root = j(file.source); | ||
|
||
// Find all variable declarations | ||
root.find(j.VariableDeclaration).forEach((path) => { | ||
// Iterate over each declarator in the declaration | ||
path.node.declarations.forEach((declarator) => { | ||
// Check if the declarator is of type VariableDeclarator | ||
if (j.VariableDeclarator.check(declarator)) { | ||
// Check if the declarator id is an array pattern | ||
if (j.ArrayPattern.check(declarator.id)) { | ||
const elements = declarator.id.elements; | ||
|
||
// Ensure the array pattern has exactly two elements | ||
if ( | ||
elements.length === 2 && | ||
j.Identifier.check(elements[0]) && | ||
j.Identifier.check(elements[1]) | ||
) { | ||
const sessionVar = elements[0].name; | ||
const loadingVar = elements[1].name; | ||
|
||
// Check if the initializer is a call expression to useSession | ||
if ( | ||
declarator.init && | ||
j.CallExpression.check(declarator.init) && | ||
j.Identifier.check(declarator.init.callee) && | ||
declarator.init.callee.name === 'useSession' | ||
) { | ||
// Replace the array destructuring with object destructuring | ||
declarator.id = j.objectPattern([ | ||
j.property( | ||
'init', | ||
j.identifier('data'), | ||
j.identifier(sessionVar), | ||
), | ||
j.property( | ||
'init', | ||
j.identifier('status'), | ||
j.identifier('status'), | ||
), | ||
]); | ||
|
||
// Add a new variable declaration for loading | ||
const loadingDeclaration = j.variableDeclaration( | ||
'const', | ||
[ | ||
j.variableDeclarator( | ||
j.identifier(loadingVar), | ||
j.binaryExpression( | ||
'===', | ||
j.identifier('status'), | ||
j.literal('loading'), | ||
), | ||
), | ||
], | ||
); | ||
|
||
// Insert the new loading declaration after the current declaration | ||
j(path).insertAfter(loadingDeclaration); | ||
|
||
// Ensure the status property is not renamed | ||
declarator.id.properties.forEach((property) => { | ||
if ( | ||
j.Property.check(property) && | ||
j.Identifier.check(property.key) && | ||
property.key.name === 'status' | ||
) { | ||
property.shorthand = true; | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
return root.toSource(options); | ||
} | ||
|
||
export default transform; |
40 changes: 40 additions & 0 deletions
40
packages/codemods/nextAuth/useSession-array-to-Object-destrucutring/test/test.ts
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,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('nextjs/use-session-array-to-object-destructuring', () => { | ||
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, ''), | ||
); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
packages/codemods/nextAuth/useSession-array-to-Object-destrucutring/tsconfig.json
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,34 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "NodeNext", | ||
"target": "ESNext", | ||
"moduleResolution": "NodeNext", | ||
"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 | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/codemods/nextAuth/useSession-array-to-Object-destrucutring/vitest.config.ts
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 { configDefaults, defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
include: [...configDefaults.include, '**/test/*.ts'], | ||
}, | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets change the name to
nextAuth/4/useSession-array-to-object-destructuring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure✌️