Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(volar/jsx-directive): add type to the functional template-ref (#674
- Loading branch information
1 parent
3227507
commit b3c9022
Showing
7 changed files
with
135 additions
and
60 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,5 @@ | ||
--- | ||
"@vue-macros/volar": patch | ||
--- | ||
|
||
add type to the functional template-ref |
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,53 @@ | ||
import { getText, isJsxExpression } from '../common' | ||
import { | ||
type JsxDirective, | ||
type TransformOptions, | ||
getOpeningElement, | ||
getTagName, | ||
} from './index' | ||
|
||
export function transformCtx( | ||
node: JsxDirective['node'], | ||
index: number, | ||
options: TransformOptions, | ||
) { | ||
const { ts, codes, sfc } = options | ||
|
||
const openingElement = getOpeningElement(node, options) | ||
if (!openingElement) return '' | ||
|
||
let props = '' | ||
for (const prop of openingElement.attributes.properties) { | ||
if (!ts.isJsxAttribute(prop)) continue | ||
const name = getText(prop.name, options) | ||
if (name.startsWith('v-')) continue | ||
|
||
const value = isJsxExpression(prop.initializer) | ||
? getText(prop.initializer.expression!, options) | ||
: 'true' | ||
props += `'${name}': ${value},` | ||
} | ||
|
||
const tagName = getTagName(node, { ...options, withTypes: true }) | ||
const ctxName = `__VLS_ctx${index}` | ||
if (!codes.toString().includes('function __VLS_getFunctionalComponentCtx')) { | ||
codes.push( | ||
`function __VLS_getFunctionalComponentCtx<T, K>(comp: T, compInstance: K): __VLS_PickNotAny< | ||
'__ctx' extends keyof __VLS_PickNotAny<K, {}> ? K extends { __ctx?: infer Ctx } ? Ctx : never : any | ||
, T extends (props: infer P, ctx: infer Ctx) => any ? Ctx & { props: P } : any>; | ||
`, | ||
) | ||
} | ||
|
||
const code = `const ${ctxName} = __VLS_getFunctionalComponentCtx(${tagName}, __VLS_asFunctionalComponent(${tagName})({${props}}));\n` | ||
if (sfc.scriptSetup?.generic) { | ||
const index = codes.findIndex((code) => | ||
code.includes('__VLS_setup = (async () => {'), | ||
) | ||
codes.splice(index + 1, 0, code) | ||
} else { | ||
codes.push(code) | ||
} | ||
|
||
return ctxName | ||
} |
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,38 @@ | ||
import { allCodeFeatures, replaceSourceRange } from '@vue/language-core' | ||
import { getStart, getText, isJsxExpression } from '../common' | ||
import type { JsxDirective, TransformOptions } from './index' | ||
|
||
export function transformRef( | ||
nodes: JsxDirective[], | ||
ctxMap: Map<JsxDirective['node'], string>, | ||
options: TransformOptions, | ||
) { | ||
const { codes, source, ts } = options | ||
|
||
for (const { node, attribute } of nodes) { | ||
if ( | ||
attribute.initializer && | ||
isJsxExpression(attribute.initializer) && | ||
attribute.initializer.expression && | ||
(ts.isFunctionExpression(attribute.initializer.expression) || | ||
ts.isArrowFunction(attribute.initializer.expression)) | ||
) { | ||
replaceSourceRange( | ||
codes, | ||
source, | ||
getStart(attribute, options), | ||
attribute.end, | ||
'{...({ ', | ||
['ref', source, getStart(attribute.name, options), allCodeFeatures], | ||
': ', | ||
[ | ||
getText(attribute.initializer.expression, options), | ||
source, | ||
getStart(attribute.initializer.expression, options), | ||
allCodeFeatures, | ||
], | ||
`} satisfies { ref: (e: Parameters<typeof ${ctxMap.get(node)}.expose>[0]) => any }) as any}`, | ||
) | ||
} | ||
} | ||
} |
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