Skip to content

Commit 027ab23

Browse files
authored
chore: better "cannot use bind:" error message (#2429)
1 parent 8d3a335 commit 027ab23

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

packages/language-server/src/plugins/typescript/features/DiagnosticsProvider.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@ import {
2121
isStoreVariableIn$storeDeclaration,
2222
get$storeOffsetOf$storeDeclaration
2323
} from './utils';
24-
import {
25-
not,
26-
flatten,
27-
passMap,
28-
swapRangeStartEndIfNecessary,
29-
memoize,
30-
traverseTypeString
31-
} from '../../../utils';
24+
import { not, flatten, passMap, swapRangeStartEndIfNecessary, memoize } from '../../../utils';
3225
import { LSConfigManager } from '../../../ls-config';
3326
import { isAttributeName, isEventHandler } from '../svelte-ast-utils';
3427

@@ -206,7 +199,25 @@ function moveBindingErrorMessage(
206199
(attr: any) => attr.type === 'Binding' && attr.name === name
207200
);
208201
if (binding) {
209-
diagnostic.message = 'Cannot bind: to this property\n\n' + diagnostic.message;
202+
// try to make the error more readable for english users
203+
if (
204+
diagnostic.message.startsWith("Type '") &&
205+
diagnostic.message.includes("is not assignable to type '")
206+
) {
207+
const idx = diagnostic.message.indexOf(`Type '"`) + `Type '"`.length;
208+
const propName = diagnostic.message.substring(
209+
idx,
210+
diagnostic.message.indexOf('"', idx)
211+
);
212+
diagnostic.message =
213+
"Cannot use 'bind:' with this property. It is declared as non-bindable inside the component.\n" +
214+
`To mark a property as bindable: 'let { ${propName} = $bindable() = $props()'`;
215+
} else {
216+
diagnostic.message =
217+
"Cannot use 'bind:' with this property. It is declared as non-bindable inside the component.\n" +
218+
`To mark a property as bindable: 'let { prop = $bindable() = $props()'\n\n` +
219+
diagnostic.message;
220+
}
210221
diagnostic.range = {
211222
start: document.positionAt(binding.start),
212223
end: document.positionAt(binding.end)

packages/language-server/test/plugins/typescript/features/diagnostics/fixtures/bindings/expected_svelte_5.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": 2322,
4-
"message": "Cannot bind: to this property\n\nType '\"readonly\"' is not assignable to type '\"can_bind\"'.",
4+
"message": "Cannot use 'bind:' with this property. It is declared as non-bindable inside the component.\nTo mark a property as bindable: 'let { readonly = $bindable() = $props()'",
55
"range": {
66
"end": {
77
"character": 20,
@@ -35,7 +35,7 @@
3535
},
3636
{
3737
"code": 2322,
38-
"message": "Cannot bind: to this property\n\nType '\"only_bind\"' is not assignable to type '\"can_bind\"'.",
38+
"message": "Cannot use 'bind:' with this property. It is declared as non-bindable inside the component.\nTo mark a property as bindable: 'let { only_bind = $bindable() = $props()'",
3939
"range": {
4040
"end": {
4141
"character": 21,

0 commit comments

Comments
 (0)