From e132b5667a87a7d233adef795b3e99ecc85e6aad Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 19 Sep 2024 13:48:20 +0200 Subject: [PATCH] fix: make no Svelte files found a warning (#2507) There were reports that this was too overzealous as some people use this potentially not knowing whether or not this is a Svelte-projects, too. Therefore only issue a warning instead of an error. --- .../src/plugins/typescript/service.ts | 8 +++++--- packages/language-server/src/svelte-check.ts | 16 +++++++++++++--- .../svelte2tsx/processInstanceScriptContent.ts | 8 +++++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/packages/language-server/src/plugins/typescript/service.ts b/packages/language-server/src/plugins/typescript/service.ts index f1b70aa09..0e0d0f27a 100644 --- a/packages/language-server/src/plugins/typescript/service.ts +++ b/packages/language-server/src/plugins/typescript/service.ts @@ -1,6 +1,7 @@ import { dirname, join, resolve, basename } from 'path'; import ts from 'typescript'; import { + DiagnosticSeverity, PublishDiagnosticsParams, RelativePattern, TextDocumentContentChangeEvent @@ -803,7 +804,7 @@ async function createLanguageService( const excludeText = JSON.stringify(exclude); const svelteConfigDiagnostics: ts.Diagnostic[] = [ { - category: ts.DiagnosticCategory.Error, + category: ts.DiagnosticCategory.Warning, code: TsconfigSvelteDiagnostics.NO_SVELTE_INPUT, file: undefined, start: undefined, @@ -977,13 +978,14 @@ async function createLanguageService( diagnostics: svelteConfigDiagnostics.map((d) => ({ message: d.messageText as string, range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } }, - severity: ts.DiagnosticCategory.Error, + severity: DiagnosticSeverity.Warning, source: 'svelte' })) }); - projectConfig.errors = projectConfig.errors + const new_errors = projectConfig.errors .filter((e) => !codes.includes(e.code)) .concat(svelteConfigDiagnostics); + projectConfig.errors.splice(0, projectConfig.errors.length, ...new_errors); } // https://github.com/microsoft/TypeScript/blob/23faef92703556567ddbcb9afb893f4ba638fc20/src/server/project.ts#L1624 diff --git a/packages/language-server/src/svelte-check.ts b/packages/language-server/src/svelte-check.ts index 2d3e11ff3..75a62b173 100644 --- a/packages/language-server/src/svelte-check.ts +++ b/packages/language-server/src/svelte-check.ts @@ -208,19 +208,23 @@ export class SvelteCheck { }; }; - if (lsContainer.configErrors.length > 0) { + if ( + lsContainer.configErrors.some((error) => error.category === ts.DiagnosticCategory.Error) + ) { return reportConfigError(); } const lang = lsContainer.getService(); - if (lsContainer.configErrors.length > 0) { + if ( + lsContainer.configErrors.some((error) => error.category === ts.DiagnosticCategory.Error) + ) { return reportConfigError(); } const files = lang.getProgram()?.getSourceFiles() || []; const options = lang.getProgram()?.getCompilerOptions() || {}; - return await Promise.all( + const diagnostics = await Promise.all( files.map((file) => { const uri = pathToUrl(file.fileName); const doc = this.docManager.get(uri); @@ -314,6 +318,12 @@ export class SvelteCheck { }) ); + if (lsContainer.configErrors.length) { + diagnostics.push(...reportConfigError()); + } + + return diagnostics; + function reportConfigError() { const grouped = groupBy( lsContainer.configErrors, diff --git a/packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts b/packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts index e226c2b1b..50d0e5530 100644 --- a/packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts +++ b/packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts @@ -49,7 +49,13 @@ export function processInstanceScriptContent( const tsAst = ts.createSourceFile( 'component.ts.svelte', scriptContent, - ts.ScriptTarget.Latest, + ts.JSDocParsingMode + ? { + languageVersion: ts.ScriptTarget.Latest, + // Exists since TS 5.3 + jsDocParsingMode: ts.JSDocParsingMode.ParseNone + } + : ts.ScriptTarget.Latest, true, ts.ScriptKind.TS );