Skip to content

Commit

Permalink
fix: can't ignore variable unused report by _ in v-for
Browse files Browse the repository at this point in the history
close #878
  • Loading branch information
johnsoncodehk committed Jan 20, 2022
1 parent e4700d8 commit f368020
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 87 deletions.
10 changes: 6 additions & 4 deletions packages/vscode-vue-languageservice/src/utils/localTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type IsComponent_Strict<T> = IsConstructorComponent<T> extends true ? true : IsF
type ComponentKeys<T> = keyof { [K in keyof T as IsComponent_Loose<T[K]> extends true ? K : never]: any };
export type PickNotAny<A, B> = IsAny<A> extends true ? B : A;
type AnyArray<T = any> = T[] | readonly T[];
type ForableSource<T> = [
T extends { [Symbol.iterator](): IterableIterator<infer T1> } ? T1 : T[keyof T], // item
typeof Symbol.iterator extends keyof T ? number : T extends T ? keyof T : never, // key
typeof Symbol.iterator extends keyof T ? undefined : number, // index
][];
${camelCaseText};
export type GlobalComponents =
Expand All @@ -54,11 +59,8 @@ export type GlobalComponents =
| 'Teleport'
>;
export declare function getVforSourceType<T>(source: T): T extends number ? number[] : T extends string ? string[] : T;
export declare function getVforKeyType<T>(source: T): typeof Symbol.iterator extends keyof T ? number : T extends T ? keyof T : never; // use "T extends T" support for union
export declare function getVforIndexType<T>(source: T): typeof Symbol.iterator extends keyof T ? undefined : number;
export declare function getVforSourceType<T>(source: T): ForableSource<NonNullable<T extends number ? number[] : T extends string ? string[] : T>>;
export declare function getNameOption<T>(t?: T): T extends { name: infer N } ? N : undefined;
export declare function pickForItem<T>(source: T): T extends { [Symbol.iterator](): IterableIterator<infer T1> } ? T1 : T[keyof T];
export declare function directiveFunction<T>(dir: T):
T extends ObjectDirective<infer E, infer V> ? (value: V) => void
: T extends FunctionDirective<infer E, infer V> ? (value: V) => void
Expand Down
104 changes: 21 additions & 83 deletions packages/vue-code-gen/src/generators/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,108 +551,46 @@ export function generate(
}
else if (node.type === CompilerDOM.NodeTypes.FOR) {
// v-for
const source = node.parseResult.source;
const value = node.parseResult.value;
const key = node.parseResult.key;
const index = node.parseResult.index;

if (value
&& source.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
&& value.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {

let start_value = value.loc.start.offset;
let start_source = source.loc.start.offset;

const sourceVarName = `__VLS_${elementIndex++}`;
// const __VLS_100 = 123;
// const __VLS_100 = vmValue;
tsCodeGen.addText(`const ${sourceVarName} = __VLS_types.getVforSourceType(`);
const { source, value, key, index } = node.parseResult;
const leftExpressionRange = value ? { start: (value ?? key ?? index).loc.start.offset, end: (index ?? key ?? value).loc.end.offset } : undefined;
const leftExpressionText = leftExpressionRange ? node.loc.source.substring(leftExpressionRange.start - node.loc.start.offset, leftExpressionRange.end - node.loc.start.offset) : undefined;

tsCodeGen.addText(`for (const [`);
if (leftExpressionRange && leftExpressionText) {
writeCode(
source.content,
{
start: start_source,
end: start_source + source.content.length,
},
leftExpressionText,
leftExpressionRange,
SourceMaps.Mode.Offset,
{
vueTag: 'template',
capabilities: capabilitiesSet.all,
},
formatBrackets.round,
formatBrackets.square,
);
tsCodeGen.addText(`);\n`);
tsCodeGen.addText(`for (const __VLS_${elementIndex++} in `);
}
tsCodeGen.addText(`] of __VLS_types.getVforSourceType(`);
if (source.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
writeCode(
sourceVarName,
source.content,
{
start: source.loc.start.offset,
end: source.loc.end.offset,
},
SourceMaps.Mode.Totally,
{
vueTag: 'template',
capabilities: capabilitiesSet.diagnosticOnly,
},
);
tsCodeGen.addText(`) {\n`);

tsCodeGen.addText(`const `);
writeCode(
value.content,
{
start: start_value,
end: start_value + value.content.length,
end: source.loc.start.offset + source.content.length,
},
SourceMaps.Mode.Offset,
{
vueTag: 'template',
capabilities: capabilitiesSet.all,
},
formatBrackets.empty,
formatBrackets.round,
);
tsCodeGen.addText(` = __VLS_types.pickForItem(${sourceVarName});\n`);
}
tsCodeGen.addText(`)) {\n`);

if (key && key.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
let start_key = key.loc.start.offset;
tsCodeGen.addText(`const `);
writeCode(
key.content,
{
start: start_key,
end: start_key + key.content.length,
},
SourceMaps.Mode.Offset,
{
vueTag: 'template',
capabilities: capabilitiesSet.all,
},
formatBrackets.empty,
);
tsCodeGen.addText(` = __VLS_types.getVforKeyType(${sourceVarName});\n`);
}
if (index && index.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
let start_index = index.loc.start.offset;
tsCodeGen.addText(`const `);
writeCode(
index.content,
{
start: start_index,
end: start_index + index.content.length,
},
SourceMaps.Mode.Offset,
{
vueTag: 'template',
capabilities: capabilitiesSet.all,
},
formatBrackets.empty,
);
tsCodeGen.addText(` = __VLS_types.getVforIndexType(${sourceVarName});\n`);
}
for (const childNode of node.children) {
visitNode(childNode, parentEl);
}
tsCodeGen.addText('}\n');
for (const childNode of node.children) {
visitNode(childNode, parentEl);
}

tsCodeGen.addText('}\n');
}
else if (node.type === CompilerDOM.NodeTypes.TEXT) {
// not needed progress
Expand Down

0 comments on commit f368020

Please sign in to comment.