Skip to content

Commit

Permalink
fix: formatting messing up inline scripts (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh authored Nov 1, 2023
1 parent 861e001 commit bd3d933
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilly-bananas-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astrojs/language-server': patch
'astro-vscode': patch
---

Fix formatting sometimes causing the code to become invalid inside inline events (onclick, onload...)
16 changes: 9 additions & 7 deletions packages/language-server/src/core/parseJS.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { ParentNode, ParseResult } from '@astrojs/compiler/types';
import { is } from '@astrojs/compiler/utils';
import {
FileCapabilities,
FileKind,
FileRangeCapabilities,
VirtualFile,
} from '@volar/language-core';
import { FileKind, FileRangeCapabilities, VirtualFile } from '@volar/language-core';
import * as SourceMap from '@volar/source-map';
import * as muggle from 'muggle-string';
import type ts from 'typescript/lib/tsserverlibrary';
Expand Down Expand Up @@ -225,7 +220,14 @@ function mergeJSContexts(fileName: string, javascriptContexts: JavaScriptContext
getLength: () => text.length,
getChangeRange: () => undefined,
},
capabilities: FileCapabilities.full,
capabilities: {
codeAction: true,
diagnostic: true,
documentFormatting: false,
documentSymbol: true,
foldingRange: true,
inlayHint: true,
},
embeddedFiles: [],
kind: FileKind.TypeScriptHostFile,
mappings,
Expand Down
23 changes: 23 additions & 0 deletions packages/language-server/test/units/parseJS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,27 @@ describe('parseJS - Can find all the scripts in an Astro file', () => {

expect(scriptTags.length).to.equal(0);
});

it('returns the proper capabilities for inline script tags', () => {
const input = `<script is:inline>console.log('hi')</script>`;
const snapshot = ts.ScriptSnapshot.fromString(input);
const html = parseHTML('something/something/hello.astro', snapshot, 0);
const astroAst = getAstroMetadata(input).ast;

const scriptTags = extractScriptTags(
'something/something/hello.astro',
snapshot,
html.htmlDocument,
astroAst
);

expect(scriptTags[0].capabilities).to.deep.equal({
diagnostic: true,
foldingRange: true,
documentFormatting: false,
documentSymbol: true,
codeAction: true,
inlayHint: true,
});
});
});

0 comments on commit bd3d933

Please sign in to comment.