-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update the
.vue
file shim for Vue 3 (#5903)
- Loading branch information
1 parent
bedf5ba
commit 027386e
Showing
11 changed files
with
121 additions
and
32 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
5 changes: 5 additions & 0 deletions
5
packages/@vue/cli-plugin-typescript/codemods/__testfixtures__/shims-vue.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,5 @@ | ||
declare module '*.vue' { | ||
import { defineComponent } from 'vue'; | ||
const component: ReturnType<typeof defineComponent>; | ||
export default component; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/@vue/cli-plugin-typescript/codemods/__testfixtures__/shims-vue.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,5 @@ | ||
declare module '*.vue' { | ||
import { DefineComponent } from 'vue'; | ||
const component: DefineComponent; | ||
export default component; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/@vue/cli-plugin-typescript/codemods/__tests__/migrateComponentType.spec.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,5 @@ | ||
jest.autoMockOff() | ||
|
||
const { defineTest } = require('jscodeshift/dist/testUtils') | ||
|
||
defineTest(__dirname, 'migrateComponentType', null, 'shims-vue', { parser: 'ts' }) |
93 changes: 93 additions & 0 deletions
93
packages/@vue/cli-plugin-typescript/codemods/migrateComponentType.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,93 @@ | ||
// `shims-vue.d.ts` for Vue 3, generated by CLI 4.5.0-4.5.6, uses the following declaration: | ||
// `component: ReturnType<typeof defineComponent>` | ||
|
||
// So needs to update to: | ||
// `component: DefineComponent` | ||
|
||
module.exports = function migrateComponentType (file, api) { | ||
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
const useDoubleQuote = root.find(j.StringLiteral).some(({ node }) => node.extra.raw.startsWith('"')) | ||
|
||
const tsmodule = root.find(j.TSModuleDeclaration, { | ||
id: { | ||
value: '*.vue' | ||
} | ||
}) | ||
|
||
const componentDecl = tsmodule.find(j.VariableDeclarator, { | ||
id: { | ||
name: 'component', | ||
typeAnnotation: { | ||
typeAnnotation: { | ||
typeName: { | ||
name: 'ReturnType' | ||
}, | ||
typeParameters: { | ||
params: { | ||
0: { | ||
exprName: { | ||
name: 'defineComponent' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
if (componentDecl.length !== 1) { | ||
return file.source | ||
} | ||
|
||
// update the component type | ||
componentDecl.forEach(({ node }) => { | ||
node.id.typeAnnotation = j.tsTypeAnnotation( | ||
j.tsTypeReference(j.identifier('DefineComponent')) | ||
) | ||
}) | ||
|
||
// insert DefineComponent type import | ||
const importDeclFromVue = tsmodule.find(j.ImportDeclaration, { | ||
source: { | ||
value: 'vue' | ||
} | ||
}) | ||
importDeclFromVue | ||
.get(0) | ||
.node.specifiers.push(j.importSpecifier(j.identifier('DefineComponent'))) | ||
|
||
// remove defineComponent import if unused | ||
const defineComponentUsages = tsmodule | ||
.find(j.Identifier, { name: 'defineComponent' }) | ||
.filter((identifierPath) => { | ||
const parent = identifierPath.parent.node | ||
|
||
// Ignore the import specifier | ||
if ( | ||
j.ImportDefaultSpecifier.check(parent) || | ||
j.ImportSpecifier.check(parent) || | ||
j.ImportNamespaceSpecifier.check(parent) | ||
) { | ||
return false | ||
} | ||
}) | ||
if (defineComponentUsages.length === 0) { | ||
tsmodule | ||
.find(j.ImportSpecifier, { | ||
local: { | ||
name: 'defineComponent' | ||
} | ||
}) | ||
.remove() | ||
} | ||
|
||
return root.toSource({ | ||
lineTerminator: '\n', | ||
quote: useDoubleQuote ? 'double' : 'single' | ||
}) | ||
} | ||
|
||
module.exports.parser = 'ts' |
4 changes: 2 additions & 2 deletions
4
packages/@vue/cli-plugin-typescript/generator/template-vue3/src/shims-vue.d.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
declare module '*.vue' { | ||
import { defineComponent } from 'vue' | ||
const component: ReturnType<typeof defineComponent> | ||
import type { DefineComponent } from 'vue' | ||
const component: DefineComponent | ||
export default component | ||
} |
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