Skip to content

Commit 1b74d13

Browse files
authored
feat(language-core): introduce strictVModel option (#5229)
1 parent d782f70 commit 1b74d13

File tree

10 files changed

+77
-31
lines changed

10 files changed

+77
-31
lines changed

packages/language-core/lib/codegen/template/elementEvents.ts

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ export function* generateElementEvents(
2424
for (const prop of node.props) {
2525
if (
2626
prop.type === CompilerDOM.NodeTypes.DIRECTIVE
27-
&& prop.name === 'on'
28-
&& prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
29-
&& prop.arg.isStatic
27+
&& (
28+
prop.name === 'on'
29+
&& (prop.arg?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic)
30+
|| options.vueCompilerOptions.strictVModel
31+
&& prop.name === 'model'
32+
&& (!prop.arg || prop.arg.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION && prop.arg.isStatic)
33+
)
3034
) {
3135
ctx.currentComponent!.used = true;
3236
if (!emitVar) {
@@ -37,21 +41,31 @@ export function* generateElementEvents(
3741
yield `let ${eventsVar}!: __VLS_NormalizeEmits<typeof ${emitVar}>${endOfLine}`;
3842
yield `let ${propsVar}!: __VLS_FunctionalComponentProps<typeof ${componentFunctionalVar}, typeof ${componentVNodeVar}>${endOfLine}`;
3943
}
40-
let source = prop.arg.loc.source;
41-
let start = prop.arg.loc.start.offset;
42-
let propPrefix = 'on';
44+
let source = prop.arg?.loc.source ?? 'model-value';
45+
let start = prop.arg?.loc.start.offset;
46+
let propPrefix = 'on-';
4347
let emitPrefix = '';
44-
if (source.startsWith('vue:')) {
48+
if (prop.name === 'model') {
49+
propPrefix = 'onUpdate:';
50+
emitPrefix = 'update:';
51+
}
52+
else if (source.startsWith('vue:')) {
4553
source = source.slice('vue:'.length);
46-
start = start + 'vue:'.length;
47-
propPrefix = 'onVnode';
54+
start = start! + 'vue:'.length;
55+
propPrefix = 'onVnode-';
4856
emitPrefix = 'vnode-';
4957
}
50-
yield `const ${ctx.getInternalVariable()}: __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${eventsVar}, '${camelize(propPrefix + '-' + source)}', '${emitPrefix}${source}', '${camelize(emitPrefix + source)}'> = {${newLine}`;
51-
yield* generateEventArg(ctx, source, start, propPrefix);
52-
yield `: `;
53-
yield* generateEventExpression(options, ctx, prop);
54-
yield `}${endOfLine}`;
58+
yield `(): __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${eventsVar}, '${camelize(propPrefix + source)}', '${emitPrefix + source}', '${camelize(emitPrefix + source)}'> => ({${newLine}`;
59+
if (prop.name === 'on') {
60+
yield* generateEventArg(ctx, source, start!, propPrefix.slice(0, -1));
61+
yield `: `;
62+
yield* generateEventExpression(options, ctx, prop);
63+
}
64+
else {
65+
yield `'${camelize(propPrefix + source)}': `;
66+
yield* generateModelEventExpression(options, ctx, prop);
67+
}
68+
yield `})${endOfLine}`;
5569
}
5670
}
5771
}
@@ -156,6 +170,31 @@ export function* generateEventExpression(
156170
}
157171
}
158172

173+
export function* generateModelEventExpression(
174+
options: TemplateCodegenOptions,
175+
ctx: TemplateCodegenContext,
176+
prop: CompilerDOM.DirectiveNode
177+
): Generator<Code> {
178+
if (prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
179+
yield `(...[$event]) => {${newLine}`;
180+
yield* ctx.generateConditionGuards();
181+
yield* generateInterpolation(
182+
options,
183+
ctx,
184+
'template',
185+
ctx.codeFeatures.verification,
186+
prop.exp.content,
187+
prop.exp.loc.start.offset,
188+
prop.exp.loc
189+
);
190+
yield ` = $event${endOfLine}`;
191+
yield `}`;
192+
}
193+
else {
194+
yield `() => {}`;
195+
}
196+
}
197+
159198
export function isCompoundExpression(ts: typeof import('typescript'), ast: ts.SourceFile) {
160199
let result = true;
161200
if (ast.statements.length === 0) {

packages/language-core/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface VueCompilerOptions {
2929
vitePressExtensions: string[];
3030
petiteVueExtensions: string[];
3131
jsxSlots: boolean;
32+
strictVModel: boolean;
3233
checkUnknownProps: boolean;
3334
checkUnknownEvents: boolean;
3435
checkUnknownDirectives: boolean;

packages/language-core/lib/utils/ts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export function getDefaultCompilerOptions(target = 99, lib = 'vue', strictTempla
267267
vitePressExtensions: [],
268268
petiteVueExtensions: [],
269269
jsxSlots: false,
270+
strictVModel: strictTemplates,
270271
checkUnknownProps: strictTemplates,
271272
checkUnknownEvents: strictTemplates,
272273
checkUnknownDirectives: strictTemplates,

packages/language-core/schemas/vue-tsconfig.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
"default": false,
4646
"markdownDescription": "Strict props, component type-checking in templates."
4747
},
48+
"strictVModel": {
49+
"type": "boolean",
50+
"default": false,
51+
"markdownDescription": "Strict `v-model` type-checking. If not set, uses the 'strictTemplates' value."
52+
},
4853
"checkUnknownProps": {
4954
"type": "boolean",
5055
"default": false,

test-workspace/tsc/passedFixtures/vue2/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"../vue3/#4822",
3030
"../vue3/#4826",
3131
"../vue3/#4828",
32-
"../vue3/#5225",
3332
"../vue3/attrs",
3433
"../vue3/components",
3534
"../vue3/defineEmits",

test-workspace/tsc/passedFixtures/vue2_strictTemplate/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"exclude": [
1212
"../vue3_strictTemplate/#3140",
1313
"../vue3_strictTemplate/#3718",
14+
"../vue3_strictTemplate/defineModel",
1415
"../vue3_strictTemplate/intrinsicProps",
1516
]
1617
}

test-workspace/tsc/passedFixtures/vue3/#5225/comp.vue

Lines changed: 0 additions & 3 deletions
This file was deleted.

test-workspace/tsc/passedFixtures/vue3/#5225/main.vue

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script setup lang="ts">
2+
defineModel<number>('foo');
3+
defineModel<number>('bar', { required: true });
4+
</script>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script setup lang="ts">
2+
import Comp from './comp.vue';
3+
4+
let foo!: number;
5+
let bar!: number | undefined;
6+
</script>
7+
8+
<template>
9+
<!-- @vue-expect-error -->
10+
<Comp :bar="0" v-model:foo="foo" />
11+
<Comp v-if="bar" v-model:bar="bar" />
12+
</template>

0 commit comments

Comments
 (0)