-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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: code migration for useAsyncData #26862
Draft
niklas-wortmann
wants to merge
3
commits into
nuxt:main
Choose a base branch
from
niklas-wortmann:useAsyncDataMigration
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,805
−3
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as fs from 'node:fs' | ||
import * as path from 'node:path' | ||
import type { SchematicContext } from '@wandeljs/core' | ||
import { Tree, UnitTestTree } from '@wandeljs/core' | ||
import update from './index' | ||
|
||
describe('Schematics Migration 4_0_0', () => { | ||
let tree: Tree | ||
|
||
beforeEach(() => { | ||
tree = new UnitTestTree(Tree.empty()) | ||
}) | ||
|
||
it('should convert basic scenario successfully', () => { | ||
const t = update(tree) | ||
const file = fs.readFileSync(path.resolve(__dirname, './snapshots/basic/before.vue')) | ||
tree.create('test.vue', file) | ||
t(tree, {} as SchematicContext) | ||
const content = tree.read('test.vue') | ||
expect(content?.toString()).toMatchFileSnapshot('./snapshots/basic/after.vue') | ||
}) | ||
|
||
it('should convert complex scenario successfully', () => { | ||
const t = update(tree) | ||
const file = fs.readFileSync(path.resolve(__dirname, './snapshots/complex/before.vue')) | ||
tree.create('test.vue', file) | ||
t(tree, {} as SchematicContext) | ||
const content = tree.read('test.vue') | ||
expect(content?.toString()).toMatchFileSnapshot('./snapshots/complex/after.vue') | ||
}) | ||
|
||
it('should not make any changes if not necessary', () => { | ||
const t = update(tree) | ||
const file = fs.readFileSync(path.resolve(__dirname, './snapshots/no-edit/before.vue')) | ||
tree.create('test.vue', file) | ||
t(tree, {} as SchematicContext) | ||
const content = tree.read('test.vue') | ||
expect(content?.toString()).toMatchFileSnapshot('./snapshots/no-edit/after.vue') | ||
}) | ||
}) |
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,94 @@ | ||
import type { Rule, Tree } from '@wandeljs/core' | ||
import type { Identifier, OptionalKind, VariableDeclarationStructure } from 'ts-morph' | ||
import { Node, Project, SyntaxKind, VariableDeclarationKind } from 'ts-morph' | ||
|
||
class ContentsStore { | ||
private _project: Project = null! | ||
|
||
collection: Array<{ path: string, content: string }> = [] | ||
|
||
get project () { | ||
if (!this._project) { | ||
this._project = new Project({ useInMemoryFileSystem: true, manipulationSettings: { | ||
usePrefixAndSuffixTextForRename: true, | ||
} }) | ||
} | ||
|
||
return this._project | ||
} | ||
|
||
track (path: string, content: string) { | ||
this.collection.push({ path, content }) | ||
this.project.createSourceFile(path, content, {}) | ||
} | ||
} | ||
|
||
export default function update (tree: Tree): Rule { | ||
return migrateUseAsyncData(tree) | ||
} | ||
|
||
function migrateUseAsyncData (host: Tree): Rule { | ||
return () => { | ||
const contentsStore = new ContentsStore() | ||
host.visit((file) => { | ||
if (file.includes('.vue')) { | ||
host.beginUpdate(file) | ||
const content = host.read(file)?.toString() | ||
if (content && content.includes('useAsyncData')) { | ||
contentsStore.track(file, content) | ||
} | ||
} | ||
}) | ||
|
||
for (const { path: sourcePath } of contentsStore.collection) { | ||
const sourceFile = contentsStore.project.getSourceFile(sourcePath) | ||
if (sourceFile) { | ||
let statusDeclaration: OptionalKind<VariableDeclarationStructure> | undefined = undefined | ||
sourceFile.forEachChild((node) => { | ||
if (Node.isVariableStatement(node)) { | ||
const structure = node.getStructure() | ||
if (!statusDeclaration) { | ||
statusDeclaration = structure.declarations.find(declaration => declaration.name.includes('status')) | ||
} | ||
const declaration = structure.declarations.find(declaration => declaration.initializer?.includes('useAsyncData') && declaration.name.includes('pending')) | ||
if (declaration) { | ||
const identifiers = node.getDescendantsOfKind(SyntaxKind.Identifier) | ||
const identifier = identifiers.reduce((acc, identifier) => { | ||
const name = identifier.getFullText().trim() | ||
if (name === 'pending') { | ||
return { ...acc, pending: identifier } | ||
} else if (name !== 'pending' && (identifier.compilerNode.parent as any).propertyName?.getFullText().trim() === 'pending') { | ||
return { ...acc, pendingAlias: identifier } | ||
} | ||
return { ...acc } | ||
}, { pending: undefined, pendingAlias: undefined } as { pending: undefined | Identifier, pendingAlias: undefined | Identifier }) | ||
const statusName = statusDeclaration ? 'dataStatus' : 'status' | ||
const startLineNumber = node.getStartLineNumber() + 1 | ||
const name = identifier.pendingAlias ? identifier.pendingAlias.getFullText().trim() : 'pending' | ||
if (identifier.pendingAlias && identifier.pending) { | ||
sourceFile.replaceText([identifier.pending.getPos(), identifier.pendingAlias.getEnd()], statusDeclaration ? 'status: dataStatus' : 'status') | ||
} else { | ||
identifier.pending?.replaceWithText('status') | ||
} | ||
sourceFile.insertVariableStatement(startLineNumber, { | ||
isExported: false, | ||
isDefaultExport: false, | ||
hasDeclareKeyword: false, | ||
declarationKind: VariableDeclarationKind.Const, | ||
declarations: [ | ||
{ | ||
name, | ||
initializer: `computed(() => ${statusName}.value === 'pending' || ${statusName}.value === 'idle')`, | ||
hasExclamationToken: false, | ||
}, | ||
], | ||
}) | ||
} | ||
} | ||
}) | ||
host.overwrite(sourcePath, sourceFile.getFullText()) | ||
} | ||
return host | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/nuxt/schematics/migrations/4_0_0/snapshots/basic/after.vue
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 @@ | ||
<script setup lang="ts"> | ||
const { data, status, error, refresh } = await useAsyncData(async () => { | ||
// do something | ||
}); | ||
const pending = computed(() => status.value === 'pending' || status.value === 'idle'); | ||
|
||
</script> | ||
|
||
<template> | ||
<div v-if="pending"> | ||
Waiting for data | ||
</div> | ||
<pre v-else> | ||
{{ data }} | ||
</pre> | ||
</template> | ||
|
||
<style scoped> | ||
:root { | ||
font-family: 'Inter var'; | ||
} | ||
</style> |
20 changes: 20 additions & 0 deletions
20
packages/nuxt/schematics/migrations/4_0_0/snapshots/basic/before.vue
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,20 @@ | ||
<script setup lang="ts"> | ||
const { data, pending, error, refresh } = await useAsyncData(async () => { | ||
// do something | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div v-if="pending"> | ||
Waiting for data | ||
</div> | ||
<pre v-else> | ||
{{ data }} | ||
</pre> | ||
</template> | ||
|
||
<style scoped> | ||
:root { | ||
font-family: 'Inter var'; | ||
} | ||
</style> |
24 changes: 24 additions & 0 deletions
24
packages/nuxt/schematics/migrations/4_0_0/snapshots/complex/after.vue
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,24 @@ | ||
<script setup lang="ts"> | ||
const pending = 'something-else' | ||
const status = 'something-else' | ||
const { data,status: dataStatus } = await useAsyncData(async () => { | ||
// do something | ||
}); | ||
const testididi = computed(() => dataStatus.value === 'pending' || dataStatus.value === 'idle'); | ||
|
||
</script> | ||
|
||
<template> | ||
<div v-if="testididi"> | ||
Waiting for data | ||
</div> | ||
<pre v-else> | ||
{{ data }} | ||
</pre> | ||
</template> | ||
|
||
<style scoped> | ||
:root { | ||
font-family: 'Inter var'; | ||
} | ||
</style> |
22 changes: 22 additions & 0 deletions
22
packages/nuxt/schematics/migrations/4_0_0/snapshots/complex/before.vue
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 @@ | ||
<script setup lang="ts"> | ||
const pending = 'something-else' | ||
const status = 'something-else' | ||
const { data, pending: testididi } = await useAsyncData(async () => { | ||
// do something | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div v-if="testididi"> | ||
Waiting for data | ||
</div> | ||
<pre v-else> | ||
{{ data }} | ||
</pre> | ||
</template> | ||
|
||
<style scoped> | ||
:root { | ||
font-family: 'Inter var'; | ||
} | ||
</style> |
18 changes: 18 additions & 0 deletions
18
packages/nuxt/schematics/migrations/4_0_0/snapshots/no-edit/after.vue
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,18 @@ | ||
<script setup lang="ts"> | ||
const pending = 'something-else' | ||
</script> | ||
|
||
<template> | ||
<div v-if="pending"> | ||
Waiting for data | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
:root { | ||
font-family: 'Inter var'; | ||
} | ||
</style> | ||
|
||
|
||
|
18 changes: 18 additions & 0 deletions
18
packages/nuxt/schematics/migrations/4_0_0/snapshots/no-edit/before.vue
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,18 @@ | ||
<script setup lang="ts"> | ||
const pending = 'something-else' | ||
</script> | ||
|
||
<template> | ||
<div v-if="pending"> | ||
Waiting for data | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
:root { | ||
font-family: 'Inter var'; | ||
} | ||
</style> | ||
|
||
|
||
|
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,10 @@ | ||
{ | ||
"$schema": "../../node_modules/@wandeljs/core/collection-schema.json", | ||
"schematics": { | ||
"nuxt-v4": { | ||
"description": "All you ever wanted from Nuxt 4", | ||
"version": "0.0.1", | ||
"factory": "./4_0_0/index" | ||
} | ||
} | ||
} |
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,26 @@ | ||
{ | ||
"name": "nuxt", | ||
"version": "0.0.0", | ||
"description": "A blank schematics", | ||
"keywords": [ | ||
"schematics" | ||
], | ||
"license": "MIT", | ||
"author": "", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.json", | ||
"test": "vitest ./**/*spec.ts" | ||
}, | ||
"dependencies": { | ||
"@wandeljs/core": "^0.1.9", | ||
"typescript": "~5.4.2", | ||
"ts-morph": "22.0.0" | ||
}, | ||
"devDependencies": { | ||
"@wandeljs/cli": "^0.1.9", | ||
"@types/node": "^18.18.0" | ||
}, | ||
"wandel": { | ||
"migrations": "./migrations/migration.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,23 @@ | ||
{ | ||
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. do we need a separate |
||
"compilerOptions": { | ||
"baseUrl": "tsconfig", | ||
"lib": ["es2018", "dom"], | ||
"declaration": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"noEmitOnError": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"noUnusedParameters": true, | ||
"noUnusedLocals": true, | ||
"skipDefaultLibCheck": true, | ||
"skipLibCheck": true, | ||
"sourceMap": true, | ||
"strictNullChecks": true, | ||
"target": "es6", | ||
"types": ["vitest/globals", "node"] | ||
}, | ||
"include": ["src/**/*", "migrations/**/*"], | ||
"exclude": ["src/*/files/**/*"] | ||
} |
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,8 @@ | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
exclude: [], | ||
globals: true, | ||
}, | ||
}) |
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.
Might also be worth handling the situation of: