Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
const existingImports = this.getExistingImports(document);
const wordRangeStartPosition = document.positionAt(wordRange.start);
const completionItems = completions
.filter(isValidCompletion(document, position))
.filter(isValidCompletion(document, position, !!tsDoc.parserError))
.map((comp) =>
this.toCompletionItem(
tsDoc,
Expand Down Expand Up @@ -857,10 +857,24 @@ const svelte2tsxTypes = new Set([
'SvelteStore'
]);

const startsWithUppercase = /^[A-Z]/;

function isValidCompletion(
document: Document,
position: Position
position: Position,
hasParserError: boolean
): (value: ts.CompletionEntry) => boolean {
// Make fallback completions for tags inside the template a bit better
const isAtStartTag =
!isInTag(position, document.scriptInfo) &&
/<\w*$/.test(
document.getText(Range.create(position.line, 0, position.line, position.character))
);
const noWrongCompletionAtStartTag =
isAtStartTag && hasParserError
? (value: ts.CompletionEntry) => startsWithUppercase.test(value.name)
: () => true;

const isNoSvelte2tsxCompletion = (value: ts.CompletionEntry) =>
value.kindModifiers !== 'declare' ||
(!value.name.startsWith('__sveltets_') && !svelte2tsxTypes.has(value.name));
Expand All @@ -880,5 +894,7 @@ function isValidCompletion(
// Remove jsx attributes on html tags because they are doubled by the HTML
// attribute suggestions, and for events they are wrong (onX instead of on:X).
// Therefore filter them out.
value.kind !== ts.ScriptElementKind.jsxAttribute && isNoSvelte2tsxCompletion(value);
value.kind !== ts.ScriptElementKind.jsxAttribute &&
isNoSvelte2tsxCompletion(value) &&
noWrongCompletionAtStartTag(value);
}