Svelte: Throw a warning if we are declaring a variable with const and also using bind directive to mutate its value.

Created on 27 Feb 2020  路  5Comments  路  Source: sveltejs/svelte

Is your feature request related to a problem? Please describe.
While defining a variable in #Svelte, make sure to use let/var and not const, otherwise those bindings won't work, as it can't be updated by its compiler.
image

Describe the solution you'd like
Throw a warning or error.

Describe alternatives you've considered
Using let always.

How important is this feature to you?
It brings a delight to users.

enhancement good first issue

Most helpful comment

This sounds reasonable. I think I'd favor an outright compilation error in this case. We need to be careful though because binding to foo.bar when foo is a const should continue to be valid.

All 5 comments

This sounds reasonable. I think I'd favor an outright compilation error in this case. We need to be careful though because binding to foo.bar when foo is a const should continue to be valid.

Yes absolutely. Making sure that primitives cannot be mutated once assigned with const. Sounds reasonable?

Some pointers for getting started on the good first issue:

  1. Read our Contribution Guide
  2. We checked whether the bind: variable is defined or not here in src/compiler/compile/nodes/Binding.ts

    • You can add another check to see whether the variable can be mutated.

    • Exhaustive list of the properties of the variables can be found in the vars section of the doc

  3. tests for compile time validation goes into the test/validator/ folder (you can refer to this test sample)

I am working on this. I already have a working implementation, but need to fix some tests.

sure @vlasy . Was planning to pick it up this weekend.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sskyy picture sskyy  路  3Comments

clitetailor picture clitetailor  路  3Comments

Rich-Harris picture Rich-Harris  路  3Comments

rob-balfre picture rob-balfre  路  3Comments

bestguy picture bestguy  路  3Comments