Skip to content

Commit

Permalink
feat(language-core): infer prop JSDoc from defineModel's leading co…
Browse files Browse the repository at this point in the history
…mments
  • Loading branch information
KazariEX committed Feb 25, 2025
1 parent 810123c commit 0a2f0c6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
13 changes: 13 additions & 0 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ function* generateSetupFunction(
ctx.scriptSetupGeneratedOffset = options.getGeneratedLength() - scriptSetupRanges.importSectionEndOffset;

let setupCodeModifies: [Code[], number, number][] = [];
for (const { comments } of scriptSetupRanges.defineProp) {
if (comments) {
setupCodeModifies.push([
[``],
comments.start,
comments.end,
]);
}
}
if (scriptSetupRanges.defineProps) {
const { name, statement, callExp, typeArg } = scriptSetupRanges.defineProps;
setupCodeModifies.push(...generateDefineWithType(
Expand Down Expand Up @@ -485,6 +494,10 @@ function* generateComponentProps(
for (const defineProp of scriptSetupRanges.defineProp) {
const [propName, localName] = getPropAndLocalName(scriptSetup, defineProp);

if (defineProp.comments) {
yield generateSfcBlockSection(scriptSetup, defineProp.comments.start, defineProp.comments.end, codeFeatures.all);
yield newLine;
}
if (defineProp.isModel && !defineProp.name) {
yield propName!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export function* generateElementProps(
: wrapWith(
prop.loc.start.offset,
prop.loc.start.offset + 'v-model'.length,
ctx.codeFeatures.verification,
ctx.codeFeatures.withoutHighlightAndCompletion,
propName
)
),
Expand Down
24 changes: 24 additions & 0 deletions packages/language-core/lib/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type DefineProp = {
defaultValue?: TextRange;
required?: boolean;
isModel?: boolean;
comments?: TextRange;
// used by component-meta
argNode?: ts.Expression;
};
Expand Down Expand Up @@ -206,6 +207,7 @@ export function parseScriptSetupRanges(
defaultValue,
required,
isModel: true,
comments: getCommentsRange(ts, node, parents, ast),
argNode: options,
});
}
Expand Down Expand Up @@ -283,6 +285,7 @@ export function parseScriptSetupRanges(
runtimeType,
defaultValue,
required,
comments: getCommentsRange(ts, node, parents, ast),
argNode: options,
});
}
Expand Down Expand Up @@ -589,3 +592,24 @@ function getStatementRange(
}
return statementRange;
}

function getCommentsRange(
ts: typeof import('typescript'),
node: ts.Node,
parents: ts.Node[],
ast: ts.SourceFile
) {
for (let i = parents.length - 1; i >= 0; i--) {
if (ts.isStatement(node)) {
break;
}
node = parents[i];
}
const comments = ts.getLeadingCommentRanges(ast.text, node.pos);
if (comments?.length) {
return {
start: comments[0].pos,
end: comments.at(-1)!.end,
};
}
}

0 comments on commit 0a2f0c6

Please sign in to comment.