Skip to content

Commit

Permalink
fix(language-core): generate script setup starting from last leading …
Browse files Browse the repository at this point in the history
…comment without `@ts-check` (#4900)
  • Loading branch information
KazariEX authored Nov 2, 2024
1 parent a7d6f68 commit 1a9eb26
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function* generateSetupFunction(
}
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);

let nextStart = scriptSetupRanges.importSectionEndOffset;
let nextStart = Math.max(scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.leadingCommentEndOffset);
for (const [codes, start, end] of setupCodeModifies) {
yield generateSfcBlockSection(scriptSetup, nextStart, start, codeFeatures.all);
for (const code of codes) {
Expand Down
8 changes: 7 additions & 1 deletion packages/language-core/lib/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type * as ts from 'typescript';
import { collectIdentifiers } from '../codegen/utils';
import type { TextRange, VueCompilerOptions } from '../types';

const tsCheckReg = /^\/\/\s*@ts-(?:no)?check($|\s)/;

export interface ScriptSetupRanges extends ReturnType<typeof parseScriptSetupRanges> { }

export function parseScriptSetupRanges(
Expand Down Expand Up @@ -66,9 +68,13 @@ export function parseScriptSetupRanges(
isModel?: boolean;
}[] = [];
const text = ast.text;
const leadingCommentEndOffset = ts.getLeadingCommentRanges(text, 0)?.reverse()[0].end ?? 0;
const importComponentNames = new Set<string>();

const leadingCommentRanges = ts.getLeadingCommentRanges(text, 0)?.reverse() ?? [];
const leadingCommentEndOffset = leadingCommentRanges.find(
range => tsCheckReg.test(text.slice(range.pos, range.end))
)?.end ?? 0;

let bindings = parseBindingRanges(ts, ast);

ts.forEachChild(ast, node => {
Expand Down
15 changes: 15 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#4899/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script setup>
// @ts-check
/**
* @import { PropType } from 'vue'
*/
// @ts-ignore
const a = 1;
defineProps({
foo: {
/** @type PropType<string> */
type: String
}
});
</script>

0 comments on commit 1a9eb26

Please sign in to comment.