From 708167e8f1cc9c249127f254428f13d936f33f21 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Wed, 17 Jul 2024 12:09:36 +0200 Subject: [PATCH] Fixes script and style tags being wrongfully included in the generated TSX (#905) * chore: changeset * test: add more tests --- .changeset/smooth-ravens-visit.md | 7 ++++ .../language-server/src/core/astro2tsx.ts | 6 +++- .../test/typescript/scripts.test.ts | 34 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 .changeset/smooth-ravens-visit.md create mode 100644 packages/language-server/test/typescript/scripts.test.ts diff --git a/.changeset/smooth-ravens-visit.md b/.changeset/smooth-ravens-visit.md new file mode 100644 index 00000000..13ddcb67 --- /dev/null +++ b/.changeset/smooth-ravens-visit.md @@ -0,0 +1,7 @@ +--- +"@astrojs/language-server": patch +"@astrojs/check": patch +"astro-vscode": patch +--- + +Fixes script and style tags being wrongfully included in the generated TSX diff --git a/packages/language-server/src/core/astro2tsx.ts b/packages/language-server/src/core/astro2tsx.ts index 33ff49c5..b3fc2902 100644 --- a/packages/language-server/src/core/astro2tsx.ts +++ b/packages/language-server/src/core/astro2tsx.ts @@ -20,7 +20,11 @@ export interface LSPTSXRanges { export function safeConvertToTSX(content: string, options: ConvertToTSXOptions) { try { - const tsx = convertToTSX(content, { filename: options.filename }); + const tsx = convertToTSX(content, { + filename: options.filename, + includeScripts: false, + includeStyles: false, + }); return tsx; } catch (e) { console.error( diff --git a/packages/language-server/test/typescript/scripts.test.ts b/packages/language-server/test/typescript/scripts.test.ts new file mode 100644 index 00000000..62a2ef2d --- /dev/null +++ b/packages/language-server/test/typescript/scripts.test.ts @@ -0,0 +1,34 @@ +import { FullDocumentDiagnosticReport } from '@volar/language-server'; +import { expect } from 'chai'; +import { type LanguageServer, getLanguageServer } from '../server.js'; + +describe('TypeScript - Diagnostics', async () => { + let languageServer: LanguageServer; + + before(async () => (languageServer = await getLanguageServer())); + + it('treats script tags as modules', async () => { + const document = await languageServer.openFakeDocument( + '', + 'astro' + ); + const diagnostics = (await languageServer.handle.sendDocumentDiagnosticRequest( + document.uri + )) as FullDocumentDiagnosticReport; + + expect(diagnostics.items).length(2); + }); + + it('treats inline script tags as not isolated modules', async () => { + const document = await languageServer.openFakeDocument( + '', + 'astro' + ); + + const diagnostics = (await languageServer.handle.sendDocumentDiagnosticRequest( + document.uri + )) as FullDocumentDiagnosticReport; + + expect(diagnostics.items).length(0); + }); +});