Skip to content

Commit

Permalink
fix(language-core): should error when invalid syntax at script end (#…
Browse files Browse the repository at this point in the history
…4692)

Co-authored-by: Johnson Chu <[email protected]>
  • Loading branch information
KazariEX and johnsoncodehk authored Oct 23, 2024
1 parent 32aa8a8 commit acddb02
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 7 deletions.
17 changes: 12 additions & 5 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { Code, Sfc, VueCodeInformation, VueCompilerOptions } from '../../ty
import { endOfLine, generateSfcBlockSection, newLine } from '../common';
import { generateGlobalTypes } from '../globalTypes';
import type { TemplateCodegenContext } from '../template/context';
import { createScriptCodegenContext, ScriptCodegenContext } from './context';
import { generateComponentSelf } from './componentSelf';
import { createScriptCodegenContext, ScriptCodegenContext } from './context';
import { generateScriptSetup, generateScriptSetupImports } from './scriptSetup';
import { generateSrc } from './src';
import { generateStyleModulesType } from './styleModulesType';
Expand Down Expand Up @@ -79,6 +79,7 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
}
else {
yield generateSfcBlockSection(options.sfc.script, 0, options.sfc.script.content.length, codeFeatures.all);
yield* generateScriptSectionPartiallyEnding(options.sfc.script.name, options.sfc.script.content.length, '#3632/both.vue');
yield* generateScriptSetup(options, ctx, options.sfc.scriptSetup, options.scriptSetupRanges);
}
}
Expand Down Expand Up @@ -131,12 +132,12 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
yield* generateScriptSetup(options, ctx, options.sfc.scriptSetup, options.scriptSetupRanges);
}

yield `;`;
if (options.sfc.script) {
yield* generateScriptSectionPartiallyEnding(options.sfc.script.name, options.sfc.script.content.length, '#3632/script.vue');
}
if (options.sfc.scriptSetup) {
// #4569
yield ['', 'scriptSetup', options.sfc.scriptSetup.content.length, codeFeatures.verification];
yield* generateScriptSectionPartiallyEnding(options.sfc.scriptSetup.name, options.sfc.scriptSetup.content.length, '#4569/main.vue');
}
yield newLine;

if (!ctx.generatedTemplate) {
yield `function __VLS_template() {${newLine}`;
Expand All @@ -163,6 +164,12 @@ export function* generateScript(options: ScriptCodegenOptions): Generator<Code,
return ctx;
}

export function* generateScriptSectionPartiallyEnding(source: string, end: number, mark: string): Generator<Code> {
yield `;`;
yield ['', source, end, codeFeatures.verification];
yield `/* PartiallyEnd: ${mark} */${newLine}`;
}

function* generateDefineProp(
options: ScriptCodegenOptions,
scriptSetup: NonNullable<Sfc['scriptSetup']>
Expand Down
6 changes: 4 additions & 2 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
import type { Code, Sfc, TextRange } from '../../types';
import { endOfLine, generateSfcBlockSection, newLine } from '../common';
import { generateComponent, generateEmitsOption } from './component';
import type { ScriptCodegenContext } from './context';
import { ScriptCodegenOptions, codeFeatures } from './index';
import { generateComponentSelf } from './componentSelf';
import type { ScriptCodegenContext } from './context';
import { ScriptCodegenOptions, codeFeatures, generateScriptSectionPartiallyEnding } from './index';
import { generateTemplate } from './template';

export function* generateScriptSetupImports(
Expand Down Expand Up @@ -278,6 +278,8 @@ function* generateSetupFunction(
yield generateSfcBlockSection(scriptSetup, scriptSetupRanges.importSectionEndOffset, scriptSetup.content.length, codeFeatures.all);
}

yield* generateScriptSectionPartiallyEnding(scriptSetup.name, scriptSetup.content.length, '#3632/scriptSetup.vue');

if (scriptSetupRanges.props.define?.typeArg && scriptSetupRanges.props.withDefaults?.arg) {
// fix https://github.com/vuejs/language-tools/issues/1187
yield `const __VLS_withDefaultsArg = (function <T>(t: T) { return t })(`;
Expand Down
8 changes: 8 additions & 0 deletions packages/tsc/tests/typecheck.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe(`vue-tsc`, () => {
getTscOutput('stable')
).toMatchInlineSnapshot(`
[
"test-workspace/tsc/failureFixtures/#3632/both.vue(3,1): error TS1109: Expression expected.",
"test-workspace/tsc/failureFixtures/#3632/both.vue(7,1): error TS1109: Expression expected.",
"test-workspace/tsc/failureFixtures/#3632/script.vue(3,1): error TS1109: Expression expected.",
"test-workspace/tsc/failureFixtures/#3632/scriptSetup.vue(3,1): error TS1109: Expression expected.",
"test-workspace/tsc/failureFixtures/directives/main.vue(4,6): error TS2339: Property 'notExist' does not exist on type 'CreateComponentPublicInstanceWithMixins<ToResolvedProps<{}, {}>, { exist: typeof exist; }, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 18 more ..., {}>'.",
"test-workspace/tsc/failureFixtures/directives/main.vue(9,6): error TS2339: Property 'notExist' does not exist on type 'CreateComponentPublicInstanceWithMixins<ToResolvedProps<{}, {}>, { exist: typeof exist; }, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 18 more ..., {}>'.",
"test-workspace/tsc/failureFixtures/directives/main.vue(12,2): error TS2578: Unused '@ts-expect-error' directive.",
Expand All @@ -24,6 +28,10 @@ describe(`vue-tsc`, () => {
getTscOutput('next')
).toMatchInlineSnapshot(`
[
"test-workspace/tsc/failureFixtures/#3632/both.vue(3,1): error TS1109: Expression expected.",
"test-workspace/tsc/failureFixtures/#3632/both.vue(7,1): error TS1109: Expression expected.",
"test-workspace/tsc/failureFixtures/#3632/script.vue(3,1): error TS1109: Expression expected.",
"test-workspace/tsc/failureFixtures/#3632/scriptSetup.vue(3,1): error TS1109: Expression expected.",
"test-workspace/tsc/failureFixtures/directives/main.vue(4,6): error TS2339: Property 'notExist' does not exist on type 'CreateComponentPublicInstanceWithMixins<ToResolvedProps<{}, {}>, { exist: typeof exist; }, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 18 more ..., {}>'.",
"test-workspace/tsc/failureFixtures/directives/main.vue(9,6): error TS2339: Property 'notExist' does not exist on type 'CreateComponentPublicInstanceWithMixins<ToResolvedProps<{}, {}>, { exist: typeof exist; }, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, ... 18 more ..., {}>'.",
"test-workspace/tsc/failureFixtures/directives/main.vue(12,2): error TS2578: Unused '@ts-expect-error' directive.",
Expand Down
7 changes: 7 additions & 0 deletions test-workspace/tsc/failureFixtures/#3632/both.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="ts">
const foo =
</script>
<script lang="ts" setup>
const bar =
</script>
3 changes: 3 additions & 0 deletions test-workspace/tsc/failureFixtures/#3632/script.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script lang="ts">
const foo =
</script>
3 changes: 3 additions & 0 deletions test-workspace/tsc/failureFixtures/#3632/scriptSetup.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script lang="ts" setup>
const foo =
</script>
4 changes: 4 additions & 0 deletions test-workspace/tsc/failureFixtures/#3632/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../../tsconfig.base.json",
"include": [ "**/*" ]
}
1 change: 1 addition & 0 deletions test-workspace/tsc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"include": [ ],
"references": [
{ "path": "./failureFixtures/#3632" },
// { "path": "./failureFixtures/#4569" }, // TODO: not working with --build flag
{ "path": "./failureFixtures/directives" },

Expand Down

0 comments on commit acddb02

Please sign in to comment.