-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
138 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@vue-macros/volar': minor | ||
'@vue-macros/nuxt': minor | ||
--- | ||
|
||
add volar support of export props |
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 @@ | ||
module.exports = require('./dist/export-props').default |
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 |
---|---|---|
@@ -1,3 +1,22 @@ | ||
import type { FileRangeCapabilities } from '@volar/language-core' | ||
import type { Segment } from 'muggle-string' | ||
|
||
export function getVueLibraryName(vueVersion: number) { | ||
return vueVersion < 2.7 ? '@vue/runtime-dom' : 'vue' | ||
} | ||
|
||
export function addProps( | ||
content: Segment<FileRangeCapabilities>[], | ||
decl: Segment<FileRangeCapabilities>[], | ||
vueLibName: string | ||
) { | ||
const idx = content.indexOf('setup() {\n') | ||
if (idx === -1) return false | ||
content.splice(idx, 0, ...['props: ({} as ', ...decl, '),\n']) | ||
|
||
content.push( | ||
`type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;\n`, | ||
`type __VLS_TypePropsToRuntimeProps<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: import('${vueLibName}').PropType<__VLS_NonUndefinedable<T[K]>> } : { type: import('${vueLibName}').PropType<T[K]>, required: true } };\n` | ||
) | ||
return true | ||
} |
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,98 @@ | ||
import { FileKind, FileRangeCapabilities } from '@volar/language-core' | ||
import { addProps, getVueLibraryName } from './common' | ||
import type { Segment } from 'muggle-string' | ||
import type { | ||
Sfc, | ||
VueEmbeddedFile, | ||
VueLanguagePlugin, | ||
} from '@volar/vue-language-core' | ||
|
||
function transform({ | ||
file, | ||
sfc, | ||
ts, | ||
vueLibName, | ||
}: { | ||
file: VueEmbeddedFile | ||
sfc: Sfc | ||
ts: typeof import('typescript/lib/tsserverlibrary') | ||
vueLibName: string | ||
}) { | ||
const idx = file?.content.indexOf('const __VLS_setup = async () => {\n') | ||
|
||
const props: Record<string, boolean> = {} | ||
const sources: [offset: number, content: string][] = [] | ||
let source = file.content[idx + 1][0] | ||
let cursor = 0 | ||
for (const stmt of sfc.scriptSetupAst!.statements) { | ||
if (!ts.isVariableStatement(stmt)) continue | ||
const exportModifier = stmt.modifiers?.find( | ||
(m) => m.kind === ts.SyntaxKind.ExportKeyword | ||
) | ||
if (!exportModifier) continue | ||
|
||
const start = exportModifier.getFullStart() | ||
const end = exportModifier.getEnd() | ||
if (cursor > 0) { | ||
sources.push([cursor, source.slice(0, start - cursor)]) | ||
} | ||
source = source.slice(end - cursor) | ||
cursor = end | ||
|
||
for (const decl of stmt.declarationList.declarations) { | ||
if (!ts.isIdentifier(decl.name)) continue | ||
props[decl.name.text] = !!decl.initializer | ||
} | ||
} | ||
|
||
if (sources.length > 0) { | ||
sources.push([cursor, source]) | ||
file.content.splice( | ||
idx + 1, | ||
1, | ||
...sources.map(([offset, content]): Segment<FileRangeCapabilities> => { | ||
return [content, 'scriptSetup', offset, FileRangeCapabilities.full] | ||
}) | ||
) | ||
} | ||
|
||
addProps( | ||
file.content, | ||
[ | ||
`__VLS_TypePropsToRuntimeProps<{ | ||
${Object.entries(props) | ||
.map(([prop, optional]) => `${prop}${optional ? '?' : ''}: typeof ${prop}`) | ||
.join(',\n')} | ||
}>`, | ||
], | ||
vueLibName | ||
) | ||
} | ||
|
||
const plugin: VueLanguagePlugin = ({ | ||
modules: { typescript: ts }, | ||
vueCompilerOptions, | ||
}) => { | ||
return { | ||
name: 'vue-macros-export-props', | ||
version: 1, | ||
resolveEmbeddedFile(fileName, sfc, embeddedFile) { | ||
if ( | ||
embeddedFile.kind !== FileKind.TypeScriptHostFile || | ||
!sfc.scriptSetup || | ||
!sfc.scriptSetupAst | ||
) | ||
return | ||
|
||
const vueLibName = getVueLibraryName(vueCompilerOptions.target) | ||
|
||
transform({ | ||
file: embeddedFile, | ||
sfc, | ||
vueLibName, | ||
ts, | ||
}) | ||
}, | ||
} | ||
} | ||
export default plugin |
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 |
---|---|---|
|
@@ -3,5 +3,5 @@ import Child from './child.vue' | |
</script> | ||
|
||
<template> | ||
<Child foo="foo" bar="20" /> | ||
<Child foo="foo" /> | ||
</template> |
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