Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/sveltejs/language-tools i…
Browse files Browse the repository at this point in the history
…nto ts-5_7
  • Loading branch information
jasonlyu123 committed Nov 24, 2024
2 parents 42a90df + fda35fe commit 7f5cd38
Show file tree
Hide file tree
Showing 61 changed files with 1,621 additions and 585 deletions.
2 changes: 1 addition & 1 deletion packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"globrex": "^0.1.2",
"lodash": "^4.17.21",
"prettier": "~3.3.3",
"prettier-plugin-svelte": "^3.2.8",
"prettier-plugin-svelte": "^3.3.0",
"svelte": "^4.2.19",
"svelte2tsx": "workspace:~",
"typescript": "^5.7.2",
Expand Down
29 changes: 29 additions & 0 deletions packages/language-server/src/plugins/html/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ const svelteTags: ITagData[] = [
}
]
},
{
name: 'svelte:html',
description:
'This element allows you to add properties and listeners to events on `document.documentElement`. This is useful for attributes such as `lang` which influence how the browser interprets the content.',
attributes: []
},
{
name: 'svelte:document',
description:
Expand Down Expand Up @@ -296,6 +302,17 @@ const svelteTags: ITagData[] = [
'Named slots allow consumers to target specific areas. They can also have fallback content.'
}
]
},
{
name: 'svelte:boundary',
description:
'Represents a boundary in the application. Can catch errors and show fallback UI',
attributes: [
{
name: 'onerror',
description: 'Called when an error occured within the boundary'
}
]
}
];

Expand Down Expand Up @@ -419,6 +436,18 @@ export const svelteHtmlDataProvider = newHTMLDataProvider('svelte-builtin', {
})) ?? []
});

const originalProvideAttributes =
svelteHtmlDataProvider.provideAttributes.bind(svelteHtmlDataProvider);

svelteHtmlDataProvider.provideAttributes = (tag: string) => {
if (tag === 'svelte:boundary' || tag === 'svelte:options') {
// We don't want the global attributes for these tags
return svelteTags.find((t) => t.name === tag)?.attributes ?? [];
}

return originalProvideAttributes(tag);
};

function isEvent(attr: IAttributeData) {
return attr.name.startsWith('on');
}
Expand Down
10 changes: 5 additions & 5 deletions packages/language-server/src/plugins/svelte/SvelteDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type PositionMapper = Pick<DocumentMapper, 'getGeneratedPosition' | 'getOriginal
* Represents a text document that contains a svelte component.
*/
export class SvelteDocument {
private transpiledDoc: ITranspiledSvelteDocument | undefined;
private compileResult: SvelteCompileResult | undefined;
private transpiledDoc: Promise<ITranspiledSvelteDocument> | undefined;
private compileResult: Promise<SvelteCompileResult> | undefined;
private svelteVersion: [number, number] | undefined;

public script: TagInformation | null;
Expand Down Expand Up @@ -76,12 +76,12 @@ export class SvelteDocument {
const [major, minor] = this.getSvelteVersion();

if (major > 3 || (major === 3 && minor >= 32)) {
this.transpiledDoc = await TranspiledSvelteDocument.create(
this.transpiledDoc = TranspiledSvelteDocument.create(
this.parent,
await this.config
);
} else {
this.transpiledDoc = await FallbackTranspiledSvelteDocument.create(
this.transpiledDoc = FallbackTranspiledSvelteDocument.create(
this.parent,
(await this.config)?.preprocess
);
Expand All @@ -92,7 +92,7 @@ export class SvelteDocument {

async getCompiled(): Promise<SvelteCompileResult> {
if (!this.compileResult) {
this.compileResult = await this.getCompiledWith((await this.config)?.compilerOptions);
this.compileResult = this.getCompiledWith((await this.config)?.compilerOptions);
}

return this.compileResult;
Expand Down
18 changes: 12 additions & 6 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ async function createLanguageService(
: undefined;

const changedFilesForExportCache = new Set<string>();
const svelteTsxFiles = getSvelteShimFiles();
const svelteTsxFilesToOriginalCasing = getSvelteShimFiles();

let languageServiceReducedMode = false;
let projectVersion = 0;
Expand Down Expand Up @@ -700,7 +700,10 @@ async function createLanguageService(
...clientFiles.filter(
(file) => !canonicalProjectFileNames.has(getCanonicalFileName(file))
),
...svelteTsxFiles
// Use original casing here, too: people could have their VS Code extensions in a case insensitive
// folder but their project in a case sensitive one; and if we copy the shims into the case sensitive
// part it would break when canonicalizing it.
...svelteTsxFilesToOriginalCasing.values()
])
);
}
Expand Down Expand Up @@ -1220,14 +1223,17 @@ async function createLanguageService(
svelteTsPath,
docContext.isSvelteCheck ? undefined : tsconfigPath || workspacePath
);
const result = new FileSet(tsSystem.useCaseSensitiveFileNames);
const pathToOriginalCasing = new Map<string, string>();
for (const file of svelteTsxFiles) {
const normalizedPath = normalizePath(file);
pathToOriginalCasing.set(getCanonicalFileName(normalizedPath), normalizedPath);
}

svelteTsxFiles.forEach((f) => result.add(normalizePath(f)));
return result;
return pathToOriginalCasing;
}

function isShimFiles(filePath: string) {
return svelteTsxFiles.has(normalizePath(filePath));
return svelteTsxFilesToOriginalCasing.has(getCanonicalFileName(normalizePath(filePath)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/svelte2tsx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte2tsx",
"version": "0.7.23",
"version": "0.7.25",
"description": "Convert Svelte components to TSX for type checking",
"author": "David Pershouse",
"license": "MIT",
Expand Down
13 changes: 7 additions & 6 deletions packages/svelte2tsx/repl/index.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<svelte:options runes />
<input bind:value={get, set} />
<input bind:value={() => v, new_v => v = new_v} />

<script>
let name = "world"
let name2 = "world"
export { name as name3, name2 as name4 };
</script>
<div bind:clientWidth={null, set} />
<div bind:contentRect={null, set} />

<Input bind:value={get, set} />
<Input bind:value={() => v, new_v => v = new_v} />
26 changes: 18 additions & 8 deletions packages/svelte2tsx/src/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export function get_global_types(
typesPath: string,
hiddenFolderPath?: string
): string[] {
const svelteHtmlPath = isSvelte3 ? undefined : join(sveltePath, 'svelte-html.d.ts');
const svelteHtmlPathExists = svelteHtmlPath && tsSystem.fileExists(svelteHtmlPath);
const svelteHtmlFile = svelteHtmlPathExists ? svelteHtmlPath : './svelte-jsx-v4.d.ts';
let svelteHtmlPath = isSvelte3 ? undefined : join(sveltePath, 'svelte-html.d.ts');
svelteHtmlPath =
svelteHtmlPath && tsSystem.fileExists(svelteHtmlPath) ? svelteHtmlPath : undefined;

let svelteTsxFiles: string[];
if (isSvelte3) {
svelteTsxFiles = ['./svelte-shims.d.ts', './svelte-jsx.d.ts', './svelte-native-jsx.d.ts'];
} else {
svelteTsxFiles = ['./svelte-shims-v4.d.ts', './svelte-native-jsx.d.ts'];
if (!svelteHtmlPathExists) {
svelteTsxFiles.push(svelteHtmlPath);
if (!svelteHtmlPath) {
svelteTsxFiles.push('./svelte-jsx-v4.d.ts');
}
}
svelteTsxFiles = svelteTsxFiles.map((f) => tsSystem.resolvePath(resolve(typesPath, f)));
Expand Down Expand Up @@ -53,19 +53,29 @@ export function get_global_types(
for (const f of svelteTsxFiles) {
const hiddenFile = resolve(hiddenPath, basename(f));
const existing = tsSystem.readFile(hiddenFile);
const toWrite = tsSystem.readFile(f) || '';
const toWrite = tsSystem.readFile(f);

if (!toWrite) {
throw new Error(`Could not read file: ${f}`);
}

if (existing !== toWrite) {
tsSystem.writeFile(hiddenFile, toWrite);
// TS doesn't throw an error if the file wasn't written
if (!tsSystem.fileExists(hiddenFile)) {
throw new Error(`Could not write file: ${hiddenFile}`);
}
}

newFiles.push(hiddenFile);
}
svelteTsxFiles = newFiles;
}
} catch (e) {}
}

if (svelteHtmlPathExists) {
svelteTsxFiles.push(tsSystem.resolvePath(resolve(typesPath, svelteHtmlFile)));
if (svelteHtmlPath) {
svelteTsxFiles.push(tsSystem.resolvePath(resolve(typesPath, svelteHtmlPath)));
}

return svelteTsxFiles;
Expand Down
Loading

0 comments on commit 7f5cd38

Please sign in to comment.