Skip to content

Commit

Permalink
feat: don't ignore unknown prop error
Browse files Browse the repository at this point in the history
close #1077
  • Loading branch information
johnsoncodehk committed Jun 2, 2022
1 parent bc91a10 commit a734e21
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
45 changes: 38 additions & 7 deletions packages/vue-code-gen/src/generators/template.ts
Expand Up @@ -644,18 +644,21 @@ export function generate(

const varInstanceProps = `__VLS_${elementIndex++}`;

tsCodeGen.addText(`type ${varInstanceProps} = typeof ${varComponentInstance} extends { $props: infer Props } ? Props & Omit<__VLS_types.GlobalAttrs, keyof Props> & Record<string, unknown> : typeof ${tagResolves[node.tag].rawComponent} & Record<string, unknown>;\n`);
tsCodeGen.addText(`type ${varInstanceProps} = typeof ${varComponentInstance} extends { $props: infer Props } ? Props & Record<string, unknown> : typeof ${tagResolves[node.tag].rawComponent} & Record<string, unknown>;\n`);
tsCodeGen.addText(`const __VLS_${elementIndex++}: {\n`);
tsCodeGen.addText(`'${prop.arg.loc.source}': __VLS_types.FillingEventArg<\n`);
{
tsCodeGen.addText(`__VLS_types.FirstFunction<\n`);
{
tsCodeGen.addText(`__VLS_types.EmitEvent<typeof ${tagResolves[node.tag].rawComponent}, '${prop.arg.loc.source}'>,\n`);
{

const key_2 = camelize('on-' + prop.arg.loc.source); // onClickOutside
const key_3 = 'on' + prop.arg.loc.source[0].toUpperCase() + prop.arg.loc.source.substring(1); // onClick-outside
const key_2 = camelize('on-' + prop.arg.loc.source); // onClickOutside
const key_3 = 'on' + prop.arg.loc.source[0].toUpperCase() + prop.arg.loc.source.substring(1); // onClick-outside

{
tsCodeGen.addText(`__VLS_types.EmitEvent<typeof ${tagResolves[node.tag].rawComponent}, '${prop.arg.loc.source}'>,\n`);
}

{
tsCodeGen.addText(`${varInstanceProps}[`);
writeCodeWithQuotes(
key_2,
Expand All @@ -676,8 +679,10 @@ export function generate(
},
);
tsCodeGen.addText(`],\n`);
}

if (key_3 !== key_2) {
{
if (key_3 !== key_2 || true) {
tsCodeGen.addText(`${varInstanceProps}[`);
writeCodeWithQuotes(
key_3,
Expand All @@ -700,7 +705,33 @@ export function generate(
tsCodeGen.addText(`],\n`);
}
}
tsCodeGen.addText(`typeof ${varComponentInstance} extends { $emit: infer Emit } ? __VLS_types.EmitEvent2<Emit, '${prop.arg.loc.source}'> : unknown,\n`);

{
tsCodeGen.addText(`typeof ${varComponentInstance} extends { $emit: infer Emit } ? __VLS_types.EmitEvent2<Emit, '${prop.arg.loc.source}'> : unknown,\n`);
}

{
tsCodeGen.addText(`__VLS_types.GlobalAttrs[`);
writeCodeWithQuotes(
key_2,
[{ start: prop.arg.loc.start.offset, end: prop.arg.loc.end.offset }],
{
vueTag: 'template',
capabilities: capabilitiesSet.attrReference,
normalizeNewName(newName) {
return camelize('on-' + newName);
},
applyNewName(oldName, newName) {
const hName = hyphenate(newName);
if (hyphenate(newName).startsWith('on-')) {
return camelize(hName.slice('on-'.length));
}
return newName;
},
},
);
tsCodeGen.addText(`],\n`);
}
}
tsCodeGen.addText(`>\n`);
}
Expand Down
6 changes: 0 additions & 6 deletions packages/vue-typescript/src/typescriptRuntime.ts
Expand Up @@ -279,12 +279,6 @@ export function createTypeScriptRuntime(options: {
}
let tsScript = options.vueLsHost.getScriptSnapshot(fileName);
if (tsScript) {
if (basename === 'runtime-dom.d.ts') {
// allow arbitrary attributes
let tsScriptText = tsScript.getText(0, tsScript.getLength());
tsScriptText = tsScriptText.replace('type ReservedProps = {', 'type ReservedProps = { [name: string]: any');
tsScript = ts.ScriptSnapshot.fromString(tsScriptText);
}
scriptSnapshots.set(fileName.toLowerCase(), [version, tsScript]);
return tsScript;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vue-typescript/src/utils/localTypes.ts
Expand Up @@ -116,11 +116,12 @@ export type EmitEvent_3<E2, E> =
: E2 extends AnyArray<infer K> ? (E extends K ? (...args: any) => void : unknown) // emits: ['event-1', 'event-2']
: E extends keyof E2 ? ReturnVoid<E2[E]> // emits: { 'event-1': () => true, 'event-2': () => true }
: unknown
export type FirstFunction<F0 = void, F1 = void, F2 = void, F3 = void> =
export type FirstFunction<F0 = void, F1 = void, F2 = void, F3 = void, F4 = void> =
NonNullable<F0> extends (Function | AnyArray<Function>) ? F0 :
NonNullable<F1> extends (Function | AnyArray<Function>) ? F1 :
NonNullable<F2> extends (Function | AnyArray<Function>) ? F2 :
NonNullable<F3> extends (Function | AnyArray<Function>) ? F3 :
NonNullable<F4> extends (Function | AnyArray<Function>) ? F4 :
unknown;
export type GlobalAttrs = JSX.IntrinsicElements['div'];
export type PickComponents<T> = ComponentKeys<T> extends keyof T ? Pick<T, ComponentKeys<T>> : T;
Expand Down

0 comments on commit a734e21

Please sign in to comment.