Skip to content

Commit

Permalink
Fixes script and style tags being wrongfully included in the generate…
Browse files Browse the repository at this point in the history
…d TSX (#905)

* chore: changeset

* test: add more tests
  • Loading branch information
Princesseuh authored Jul 17, 2024
1 parent 1f78295 commit 708167e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/smooth-ravens-visit.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion packages/language-server/src/core/astro2tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
34 changes: 34 additions & 0 deletions packages/language-server/test/typescript/scripts.test.ts
Original file line number Diff line number Diff line change
@@ -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(
'<script>import * as path from "node:path";path;const hello = "Hello, Astro!";</script><script>console.log(hello);</script>',
'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(
'<script is:inline>const hello = "Hello, Astro!";</script><script is:inline>console.log(hello);</script>',
'astro'
);

const diagnostics = (await languageServer.handle.sendDocumentDiagnosticRequest(
document.uri
)) as FullDocumentDiagnosticReport;

expect(diagnostics.items).length(0);
});
});

0 comments on commit 708167e

Please sign in to comment.