@astrojs/language-server
Advanced tools
| import type { ConvertToTSXOptions, TSXExtractedScript, TSXExtractedStyle, TSXResult } from '@astrojs/compiler/types'; | ||
| import type { VirtualCode } from '@volar/language-core'; | ||
| import type { CodeMapping } from '@volar/language-core'; | ||
| import { Range } from '@volar/language-server'; | ||
@@ -9,2 +9,3 @@ export interface LSPTSXRanges { | ||
| styles: TSXExtractedStyle[]; | ||
| generatedComponentExport?: Range; | ||
| } | ||
@@ -47,3 +48,13 @@ export declare function safeConvertToTSX(content: string, options: ConvertToTSXOptions): TSXResult | { | ||
| export declare function astro2tsx(input: string, fileName: string): { | ||
| virtualCode: VirtualCode; | ||
| virtualCode: { | ||
| id: string; | ||
| languageId: string; | ||
| snapshot: { | ||
| getText: (start: number, end: number) => string; | ||
| getLength: () => number; | ||
| getChangeRange: () => undefined; | ||
| }; | ||
| mappings: CodeMapping[]; | ||
| embeddedCodes: never[]; | ||
| }; | ||
| diagnostics: import("@astrojs/compiler/types").DiagnosticMessage[] | { | ||
@@ -60,3 +71,9 @@ code: 1000; | ||
| }[]; | ||
| ranges: LSPTSXRanges; | ||
| ranges: { | ||
| generatedComponentExport: Range | undefined; | ||
| frontmatter: Range; | ||
| body: Range; | ||
| scripts: TSXExtractedScript[]; | ||
| styles: TSXExtractedStyle[]; | ||
| }; | ||
| }; |
+18
-11
@@ -66,10 +66,12 @@ "use strict"; | ||
| const tsx = safeConvertToTSX(input, { filename: fileName }); | ||
| const { virtualCode, generatedComponentExport } = getVirtualCodeTSX(input, tsx, fileName); | ||
| return { | ||
| virtualCode: getVirtualCodeTSX(input, tsx, fileName), | ||
| virtualCode, | ||
| diagnostics: tsx.diagnostics, | ||
| ranges: getTSXRangesAsLSPRanges(tsx), | ||
| ranges: { ...getTSXRangesAsLSPRanges(tsx), generatedComponentExport }, | ||
| }; | ||
| } | ||
| function getVirtualCodeTSX(input, tsx, fileName) { | ||
| tsx.code = (0, utils_js_1.patchTSX)(tsx.code, fileName); | ||
| const patched = (0, utils_js_1.patchTSXWithMetadata)(tsx.code, fileName); | ||
| tsx.code = patched.code; | ||
| const v3Mappings = (0, sourcemap_codec_1.decode)(tsx.map.mappings); | ||
@@ -134,13 +136,18 @@ const sourcedDoc = vscode_html_languageservice_1.TextDocument.create('', 'astro', 0, input); | ||
| return { | ||
| id: 'tsx', | ||
| languageId: 'typescriptreact', | ||
| snapshot: { | ||
| getText: (start, end) => tsx.code.substring(start, end), | ||
| getLength: () => tsx.code.length, | ||
| getChangeRange: () => undefined, | ||
| virtualCode: { | ||
| id: 'tsx', | ||
| languageId: 'typescriptreact', | ||
| snapshot: { | ||
| getText: (start, end) => tsx.code.substring(start, end), | ||
| getLength: () => tsx.code.length, | ||
| getChangeRange: () => undefined, | ||
| }, | ||
| mappings: mappings, | ||
| embeddedCodes: [], | ||
| }, | ||
| mappings: mappings, | ||
| embeddedCodes: [], | ||
| generatedComponentExport: patched.generatedComponentExport | ||
| ? language_server_1.Range.create(genDoc.positionAt(patched.generatedComponentExport.start), genDoc.positionAt(patched.generatedComponentExport.end)) | ||
| : undefined, | ||
| }; | ||
| } | ||
| //# sourceMappingURL=astro2tsx.js.map |
+10
-0
@@ -5,1 +5,11 @@ import type { VirtualCode } from '@volar/language-core'; | ||
| export declare function patchTSX(code: string, filePath: string): string; | ||
| export declare function patchTSXWithMetadata(code: string, filePath: string): { | ||
| code: string; | ||
| generatedComponentExport?: undefined; | ||
| } | { | ||
| code: string; | ||
| generatedComponentExport: { | ||
| start: number; | ||
| end: number; | ||
| }; | ||
| }; |
+13
-2
@@ -6,2 +6,3 @@ "use strict"; | ||
| exports.patchTSX = patchTSX; | ||
| exports.patchTSXWithMetadata = patchTSXWithMetadata; | ||
| const vscode_uri_1 = require("vscode-uri"); | ||
@@ -62,2 +63,5 @@ const importPackage_1 = require("../importPackage"); | ||
| function patchTSX(code, filePath) { | ||
| return patchTSXWithMetadata(code, filePath).code; | ||
| } | ||
| function patchTSXWithMetadata(code, filePath) { | ||
| const url = vscode_uri_1.URI.parse(filePath); | ||
@@ -81,6 +85,13 @@ const basename = vscode_uri_1.Utils.basename(url).slice(0, -vscode_uri_1.Utils.extname(url).length); | ||
| if (!componentNames.has(cleanName)) { | ||
| return patched; | ||
| return { code: patched }; | ||
| } | ||
| return `${patched}\nexport { ${cleanName}AstroComponent as ${cleanName} };\n`; | ||
| const generatedComponentExport = `export { ${cleanName}AstroComponent as ${cleanName} };\n`; | ||
| return { | ||
| code: `${patched}\n${generatedComponentExport}`, | ||
| generatedComponentExport: { | ||
| start: patched.length + 1, | ||
| end: patched.length + 1 + generatedComponentExport.length, | ||
| }, | ||
| }; | ||
| } | ||
| //# sourceMappingURL=utils.js.map |
@@ -23,3 +23,3 @@ "use strict"; | ||
| return codeAction; | ||
| codeAction.edit.documentChanges = codeAction.edit.documentChanges.map((change) => { | ||
| codeAction.edit.documentChanges = codeAction.edit.documentChanges.flatMap((change) => { | ||
| if (language_server_1.TextDocumentEdit.is(change)) { | ||
@@ -31,9 +31,28 @@ const decoded = context.decodeEmbeddedDocumentUri(vscode_uri_1.URI.parse(change.textDocument.uri)); | ||
| if (!virtualCode || !(root instanceof index_js_1.AstroVirtualCode)) | ||
| return change; | ||
| return [change]; | ||
| const hadEdits = change.edits.length > 0; | ||
| if (virtualCode.id === 'tsx') { | ||
| const generatedComponentExport = root.astroMeta.tsxRanges.generatedComponentExport; | ||
| if (generatedComponentExport) { | ||
| change.edits = change.edits.filter((edit) => !rangesOverlap(edit.range, generatedComponentExport)); | ||
| } | ||
| } | ||
| if (hadEdits && change.edits.length === 0) | ||
| return []; | ||
| change.edits = change.edits.map((edit) => (0, utils_js_1.mapEdit)(edit, root, virtualCode.languageId)); | ||
| } | ||
| return change; | ||
| return [change]; | ||
| }); | ||
| return codeAction; | ||
| } | ||
| function rangesOverlap(a, b) { | ||
| const aIsEmpty = comparePositions(a.start, a.end) === 0; | ||
| if (aIsEmpty) { | ||
| return comparePositions(a.start, b.start) >= 0 && comparePositions(a.start, b.end) < 0; | ||
| } | ||
| return comparePositions(a.start, b.end) < 0 && comparePositions(a.end, b.start) > 0; | ||
| } | ||
| function comparePositions(a, b) { | ||
| return a.line === b.line ? a.character - b.character : a.line - b.line; | ||
| } | ||
| //# sourceMappingURL=codeActions.js.map |
+1
-1
| { | ||
| "name": "@astrojs/language-server", | ||
| "version": "2.16.14", | ||
| "version": "2.16.15", | ||
| "author": "withastro", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
215098
1.19%4793
1.35%