Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jun 27, 2024
2 parents 4e848a8 + 00212c4 commit 5fb6d31
Show file tree
Hide file tree
Showing 101 changed files with 849 additions and 335 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint": "prettier --check ."
},
"dependencies": {
"typescript": "^5.4.5"
"typescript": "^5.5.2"
},
"devDependencies": {
"cross-env": "^7.0.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
"svelte": "^3.57.0",
"svelte-preprocess": "^6.0.0",
"svelte2tsx": "workspace:~",
"typescript": "^5.3.2",
"typescript-auto-import-cache": "^0.3.2",
"typescript": "^5.5.2",
"typescript-auto-import-cache": "^0.3.3",
"vscode-css-languageservice": "~6.2.10",
"vscode-html-languageservice": "~5.1.1",
"vscode-languageserver": "8.0.2",
Expand Down
24 changes: 21 additions & 3 deletions packages/language-server/src/lib/FallbackWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { FSWatcher, watch } from 'chokidar';
import { debounce } from 'lodash';
import { join } from 'path';
import { DidChangeWatchedFilesParams, FileChangeType, FileEvent } from 'vscode-languageserver';
import {
DidChangeWatchedFilesParams,
FileChangeType,
FileEvent,
RelativePattern
} from 'vscode-languageserver';
import { pathToUrl } from '../utils';
import { fileURLToPath } from 'url';

type DidChangeHandler = (para: DidChangeWatchedFilesParams) => void;

Expand All @@ -14,10 +20,10 @@ export class FallbackWatcher {

private undeliveredFileEvents: FileEvent[] = [];

constructor(glob: string, workspacePaths: string[]) {
constructor(recursivePatterns: string, workspacePaths: string[]) {
const gitOrNodeModules = /\.git|node_modules/;
this.watcher = watch(
workspacePaths.map((workspacePath) => join(workspacePath, glob)),
workspacePaths.map((workspacePath) => join(workspacePath, recursivePatterns)),
{
ignored: (path: string) =>
gitOrNodeModules.test(path) &&
Expand Down Expand Up @@ -65,6 +71,18 @@ export class FallbackWatcher {
this.callbacks.push(callback);
}

watchDirectory(patterns: RelativePattern[]) {
for (const pattern of patterns) {
const basePath = fileURLToPath(
typeof pattern.baseUri === 'string' ? pattern.baseUri : pattern.baseUri.uri
);
if (!basePath) {
continue;
}
this.watcher.add(join(basePath, pattern.pattern));
}
}

dispose() {
this.watcher.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ export class DocumentManager {
let document: Document;
if (this.documents.has(textDocument.uri)) {
document = this.documents.get(textDocument.uri)!;
// open state should only be updated when the document is closed
document.openedByClient ||= openedByClient;
document.setText(textDocument.text);
} else {
document = this.createDocument(textDocument);
document.openedByClient = openedByClient;
this.documents.set(textDocument.uri, document);
this.notify('documentOpen', document);
}

this.notify('documentChange', document);
document.openedByClient = openedByClient;

return document;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ export interface ComponentInfoProvider {
export class JsOrTsComponentInfoProvider implements ComponentInfoProvider {
private constructor(
private readonly typeChecker: ts.TypeChecker,
private readonly classType: ts.Type
private readonly classType: ts.Type,
private readonly useSvelte5PlusPropsParameter: boolean = false
) {}

getEvents(): ComponentPartInfo {
const eventType = this.getType('$$events_def');
const eventType = this.getType(
this.useSvelte5PlusPropsParameter ? '$$events' : '$$events_def'
);
if (!eventType) {
return [];
}
Expand All @@ -26,7 +29,7 @@ export class JsOrTsComponentInfoProvider implements ComponentInfoProvider {
}

getSlotLets(slot = 'default'): ComponentPartInfo {
const slotType = this.getType('$$slot_def');
const slotType = this.getType(this.useSvelte5PlusPropsParameter ? '$$slots' : '$$slot_def');
if (!slotType) {
return [];
}
Expand All @@ -45,12 +48,18 @@ export class JsOrTsComponentInfoProvider implements ComponentInfoProvider {
}

getProps() {
const props = this.getType('$$prop_def');
if (!props) {
return [];
if (!this.useSvelte5PlusPropsParameter) {
const props = this.getType('$$prop_def');
if (!props) {
return [];
}

return this.mapPropertiesOfType(props);
}

return this.mapPropertiesOfType(props);
return this.mapPropertiesOfType(this.classType).filter(
(prop) => !prop.name.startsWith('$$')
);
}

private getType(classProperty: string) {
Expand Down Expand Up @@ -87,32 +96,65 @@ export class JsOrTsComponentInfoProvider implements ComponentInfoProvider {
* The result of this shouldn't be cached as it could lead to memory leaks. The type checker
* could become old and then multiple versions of it could exist.
*/
static create(lang: ts.LanguageService, def: ts.DefinitionInfo): ComponentInfoProvider | null {
static create(
lang: ts.LanguageService,
def: ts.DefinitionInfo,
isSvelte5Plus: boolean
): ComponentInfoProvider | null {
const program = lang.getProgram();
const sourceFile = program?.getSourceFile(def.fileName);

if (!program || !sourceFile) {
return null;
}

const defClass = findContainingNode(
sourceFile,
def.textSpan,
(node): node is ts.ClassDeclaration | ts.VariableDeclaration =>
ts.isClassDeclaration(node) || ts.isTypeAliasDeclaration(node)
);
const defIdentifier = findContainingNode(sourceFile, def.textSpan, ts.isIdentifier);

if (!defClass) {
if (!defIdentifier) {
return null;
}

const typeChecker = program.getTypeChecker();
const classType = typeChecker.getTypeAtLocation(defClass);

if (!classType) {
const componentSymbol = typeChecker.getSymbolAtLocation(defIdentifier);

if (!componentSymbol) {
return null;
}

return new JsOrTsComponentInfoProvider(typeChecker, classType);
const type = typeChecker.getTypeOfSymbolAtLocation(componentSymbol, defIdentifier);

if (type.isClass()) {
return new JsOrTsComponentInfoProvider(typeChecker, type);
}

const constructorSignatures = type.getConstructSignatures();
if (constructorSignatures.length === 1) {
return new JsOrTsComponentInfoProvider(
typeChecker,
constructorSignatures[0].getReturnType()
);
}

if (!isSvelte5Plus) {
return null;
}

const signatures = type.getCallSignatures();
if (signatures.length !== 1) {
return null;
}

const propsParameter = signatures[0].parameters[1];
if (!propsParameter) {
return null;
}
const propsParameterType = typeChecker.getTypeOfSymbol(propsParameter);

return new JsOrTsComponentInfoProvider(
typeChecker,
propsParameterType,
/** useSvelte5PlusPropsParameter */ true
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export namespace DocumentSnapshot {

if (normalizedPath.endsWith('node_modules/svelte/types/index.d.ts')) {
const startIdx = originalText.indexOf(`declare module '*.svelte' {`);
const endIdx = originalText.indexOf(`}`, originalText.indexOf(';', startIdx)) + 1;
const endIdx = originalText.indexOf(`\n}`, startIdx + 1) + 2;
originalText =
originalText.substring(0, startIdx) +
' '.repeat(endIdx - startIdx) +
Expand Down
Loading

0 comments on commit 5fb6d31

Please sign in to comment.