mirror of https://github.com/sveltejs/svelte
warn on assignment to const (#4960)
* warn on assignment to const * fix formatting and switch to error * check most local scopes first * fix logic and add more tests * more formatting * Fix broken test * use find_owner instead Co-authored-by: tanhauhau <lhtan93@gmail.com>pull/7942/head
parent
ab1285a4f8
commit
88ed9931f2
@ -0,0 +1,17 @@
|
||||
[
|
||||
{
|
||||
"code": "assignment-to-const",
|
||||
"message": "You are assigning to a const",
|
||||
"start": {
|
||||
"line": 13,
|
||||
"column": 24,
|
||||
"character": 282
|
||||
},
|
||||
"end": {
|
||||
"line": 13,
|
||||
"column": 35,
|
||||
"character": 293
|
||||
},
|
||||
"pos": 282
|
||||
}
|
||||
]
|
@ -0,0 +1,15 @@
|
||||
<script>
|
||||
const immutable = 0;
|
||||
|
||||
const obj1 = { prop: true };
|
||||
const obj2 = { prop: 0 }
|
||||
</script>
|
||||
|
||||
<!-- should not error -->
|
||||
<button on:click={() => obj1.prop = false}>click</button>
|
||||
<button on:click={() => obj2.prop++}>click</button>
|
||||
|
||||
<!-- should error -->
|
||||
<button on:click={() => immutable++}>click</button>
|
||||
|
||||
|
@ -0,0 +1,17 @@
|
||||
[
|
||||
{
|
||||
"code": "assignment-to-const",
|
||||
"message": "You are assigning to a const",
|
||||
"start": {
|
||||
"line": 14,
|
||||
"column": 3,
|
||||
"character": 172
|
||||
},
|
||||
"end": {
|
||||
"line": 14,
|
||||
"column": 10,
|
||||
"character": 179
|
||||
},
|
||||
"pos": 172
|
||||
}
|
||||
]
|
@ -0,0 +1,20 @@
|
||||
<script>
|
||||
const foo = 'hello';
|
||||
|
||||
function shouldNotError() {
|
||||
let foo = 0;
|
||||
|
||||
function inner() {
|
||||
foo = 1;
|
||||
}
|
||||
}
|
||||
|
||||
function shouldError() {
|
||||
function inner() {
|
||||
foo = 1;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<button on:click={shouldNotError}>click</button>
|
||||
<button on:click={shouldError}>click</button>
|
@ -0,0 +1,17 @@
|
||||
[
|
||||
{
|
||||
"code": "assignment-to-const",
|
||||
"message": "You are assigning to a const",
|
||||
"start": {
|
||||
"line": 17,
|
||||
"column": 2,
|
||||
"character": 189
|
||||
},
|
||||
"end": {
|
||||
"line": 17,
|
||||
"column": 9,
|
||||
"character": 196
|
||||
},
|
||||
"pos": 189
|
||||
}
|
||||
]
|
@ -0,0 +1,21 @@
|
||||
<script>
|
||||
const foo = 'hello';
|
||||
</script>
|
||||
|
||||
<button on:click={() => {
|
||||
let foo = 0;
|
||||
|
||||
function inner() {
|
||||
foo = 1;
|
||||
}
|
||||
}}>
|
||||
click
|
||||
</button>
|
||||
|
||||
<button on:click={() => {
|
||||
function inner() {
|
||||
foo = 1;
|
||||
}
|
||||
}}>
|
||||
click
|
||||
</button>
|
@ -0,0 +1,17 @@
|
||||
[
|
||||
{
|
||||
"code": "assignment-to-const",
|
||||
"message": "You are assigning to a const",
|
||||
"start": {
|
||||
"line": 16,
|
||||
"column": 2,
|
||||
"character": 225
|
||||
},
|
||||
"end": {
|
||||
"line": 16,
|
||||
"column": 18,
|
||||
"character": 241
|
||||
},
|
||||
"pos": 225
|
||||
}
|
||||
]
|
@ -0,0 +1,24 @@
|
||||
<script>
|
||||
const immutable = false;
|
||||
|
||||
const obj1 = { prop: true };
|
||||
const obj2 = { prop: 0 };
|
||||
|
||||
function shouldNotError() {
|
||||
obj1.prop = false;
|
||||
}
|
||||
|
||||
function shouldNotError2() {
|
||||
obj2.prop++;
|
||||
}
|
||||
|
||||
function shouldError() {
|
||||
immutable = true
|
||||
}
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<button on:click={shouldNotError}>click</button>
|
||||
<button on:click={shouldNotError2}>click</button>
|
||||
<button on:click={shouldError}>click</button>
|
Loading…
Reference in new issue