Skip to content

Fixing reactive values in typescript #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ typings/

# tdsx
dist

.DS_STORE
1 change: 1 addition & 0 deletions packages/language-server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
.vscode/
node_modules/
.DS_STORE
4 changes: 2 additions & 2 deletions packages/language-server/src/plugins/TypeScriptPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
mapSeverity,
} from './typescript/utils';
import { getLanguageServiceForDocument, CreateDocument } from './typescript/service';
import { pathToUrl } from '../utils';
import { pathToUrl, isValidSvelteReactiveValueDiagnostic } from '../utils';
import { TextDocument } from '../lib/documents/TextDocument';

export class TypeScriptPlugin
Expand Down Expand Up @@ -103,7 +103,7 @@ export class TypeScriptPlugin
source: isTypescript ? 'ts' : 'js',
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
code: diagnostic.code,
}));
})).filter(diagnostic => isValidSvelteReactiveValueDiagnostic(diagnostic));
}

doHover(document: Document, position: Position): Hover | null {
Expand Down
12 changes: 12 additions & 0 deletions packages/language-server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ export function pathToUrl(path: string) {
export function flatten<T>(arr: T[][]): T[] {
return arr.reduce((all, item) => [...all, ...item], []);
}

const TS2552_REGEX = /Cannot find name '\$([a-zA-Z0-9_]+)'. Did you mean '([a-zA-Z0-9_]+)'\?/i;
export function isValidSvelteReactiveValueDiagnostic(diagnostic: any): boolean {
if (diagnostic.code !== 2552) return true;

/** if error message doesn't contain a reactive value, do nothing */
if (!diagnostic.message.includes('$')) return true;

const [, usedVar, proposedVar] = diagnostic.message.match(TS2552_REGEX) || [];

return !(usedVar && proposedVar && usedVar === proposedVar);
}
1 change: 1 addition & 0 deletions packages/svelte-vscode/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/dist
/node_modules
.DS_STORE