Skip to content

Commit

Permalink
use getLeadingCommentRanges
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jul 13, 2023
1 parent 0b74569 commit 06c6432
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
9 changes: 4 additions & 5 deletions packages/vue-language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ export function generate(
emitsTypeArg: undefined,
emitsTypeNums: 0,
exposeRuntimeArg: undefined,
importSectionEndOffsetWithComment: 0,
importSectionEndOffsetWithoutComment: 0,
importSectionEndOffset: 0,
defineProps: undefined,
propsAssignName: undefined,
propsRuntimeArg: undefined,
Expand Down Expand Up @@ -268,7 +267,7 @@ export function generate(
return;

codes.push([
sfc.scriptSetup.content.substring(0, scriptSetupRanges.importSectionEndOffsetWithComment),
sfc.scriptSetup.content.substring(0, scriptSetupRanges.importSectionEndOffset),
'scriptSetup',
0,
FileRangeCapabilities.full,
Expand Down Expand Up @@ -481,9 +480,9 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
`.trim() + '\n');
}

const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffsetWithoutComment;
const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffset;

addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffsetWithoutComment);
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset);

if (scriptSetupRanges.propsTypeArg && scriptSetupRanges.withDefaultsArg) {
// fix https://github.com/vuejs/language-tools/issues/1187
Expand Down
16 changes: 10 additions & 6 deletions packages/vue-language-core/src/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ export function parseScriptSetupRanges(
) {

let foundNonImportExportNode = false;
let importSectionEndOffsetWithComment = 0;
let importSectionEndOffsetWithoutComment = 0;
let importSectionEndOffset = 0;
let withDefaultsArg: TextRange | undefined;
let propsAssignName: string | undefined;
let defineProps: TextRange | undefined;
Expand Down Expand Up @@ -45,16 +44,21 @@ export function parseScriptSetupRanges(
// fix https://github.com/vuejs/language-tools/issues/1223
&& !ts.isImportEqualsDeclaration(node)
) {
importSectionEndOffsetWithComment = node.getStart(ast, true);
importSectionEndOffsetWithoutComment = node.getFullStart();
const commentRagnes = ts.getLeadingCommentRanges(ast.getFullText(), node.getFullStart());
if (commentRagnes?.length) {
const commentRange = commentRagnes.sort((a, b) => a.pos - b.pos)[0];
importSectionEndOffset = commentRange.pos;
}
else {
importSectionEndOffset = node.getStart(ast);
}
foundNonImportExportNode = true;
}
});
ast.forEachChild(child => visitNode(child, ast));

return {
importSectionEndOffsetWithComment,
importSectionEndOffsetWithoutComment,
importSectionEndOffset,
bindings,
withDefaultsArg,
defineProps,
Expand Down

0 comments on commit 06c6432

Please sign in to comment.