fix logic and add more tests

pull/4960/head
Billy Levin 6 years ago committed by tanhauhau
parent b70b5dfdde
commit 0fc9ea08ff

@ -164,11 +164,13 @@ export default class Expression {
current_scope = current_scope.parent;
}
if (declaration && declaration.kind === 'const' && !deep) {
if (declaration) {
if (declaration.kind === 'const' && !deep) {
component.error(node as any, {
code: 'assignment-to-const',
message: 'You are assigning to a const'
});
}
} else if (variable && variable.writable === false && !deep) {
component.error(node as any, {
code: 'assignment-to-const',

@ -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>

@ -5,13 +5,13 @@
"start": {
"line": 16,
"column": 2,
"character": 237
"character": 225
},
"end": {
"line": 16,
"column": 18,
"character": 253
"character": 241
},
"pos": 237
"pos": 225
}
]

@ -4,21 +4,21 @@
const obj1 = { prop: true };
const obj2 = { prop: 0 };
function funcShouldNotError() {
function shouldNotError() {
obj1.prop = false;
}
function funcShouldNotError2() {
function shouldNotError2() {
obj2.prop++;
}
function funcShouldError() {
function shouldError() {
immutable = true
}
</script>
<button on:click={funcShouldNotError}>click</button>
<button on:click={funcShouldNotError2}>click</button>
<button on:click={funcShouldError}>click</button>
<button on:click={shouldNotError}>click</button>
<button on:click={shouldNotError2}>click</button>
<button on:click={shouldError}>click</button>

Loading…
Cancel
Save