fix formatting and switch to error

pull/4960/head
Billy Levin 6 years ago committed by tanhauhau
parent d6a9182d21
commit d0619dcd84

@ -788,26 +788,25 @@ export default class Component {
scope = map.get(node); scope = map.get(node);
} }
let deep = false; let deep = false;
let names: string[] | null; let names: string[] | null;
if (node.type === 'AssignmentExpression') { if (node.type === 'AssignmentExpression') {
deep = node.left.type === 'MemberExpression'; deep = node.left.type === 'MemberExpression';
names = deep names = deep
? [get_object(node.left).name] ? [get_object(node.left).name]
: extract_names(node.left); : extract_names(node.left);
} else if (node.type === 'UpdateExpression') { } else if (node.type === 'UpdateExpression') {
deep = node.argument.type === 'MemberExpression'; deep = node.argument.type === 'MemberExpression';
const { name } = get_object(node.argument); const { name } = get_object(node.argument);
names = [name]; names = [name];
} }
if (names) { if (names) {
names.forEach(name => { names.forEach(name => {
const variable = component.var_lookup.get(name); const variable = component.var_lookup.get(name);
if (variable && variable.writable === false && !deep) { if (variable && variable.writable === false && !deep) {
component.warn(node as any, { component.error(node as any, {
code: 'assignment-to-const', code: 'assignment-to-const',
message: 'You are assigning to a const' message: 'You are assigning to a const'
}); });

@ -147,13 +147,13 @@ export default class Expression {
} else { } else {
component.add_reference(node, name); component.add_reference(node, name);
const variable = component.var_lookup.get(name); const variable = component.var_lookup.get(name);
if (variable) { if (variable) {
variable[deep ? 'mutated' : 'reassigned'] = true; variable[deep ? 'mutated' : 'reassigned'] = true;
if (!deep && variable.writable === false) { if (!deep && variable.writable === false) {
component.warn(node as any, { component.error(node as any, {
code: 'assignment-to-const', code: 'assignment-to-const',
message: 'You are assigning to a const' message: 'You are assigning to a const'
}); });

@ -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": 16,
"column": 2,
"character": 237
},
"end": {
"line": 16,
"column": 18,
"character": 253
},
"pos": 237
}
]

@ -1,34 +1,24 @@
<script> <script>
const immutable1 = false; const immutable = false;
const immutable2 = 0;
// these ones should not warn
const obj1 = { prop: true }; const obj1 = { prop: true };
const obj2 = { prop: 0 }; const obj2 = { prop: 0 };
function func1() { function funcShouldNotError() {
immutable1 = true obj1.prop = false;
} }
function func2() { function funcShouldNotError2() {
immutable2++; obj2.prop++;
} }
function func3() { function funcShouldError() {
obj1.prop = false; immutable = true
} }
function func4() {
obj2.prop++;
}
</script> </script>
<button on:click={func1}>click</button> <button on:click={funcShouldNotError}>click</button>
<button on:click={func2}>click</button> <button on:click={funcShouldNotError2}>click</button>
<button on:click={func3}>click</button> <button on:click={funcShouldError}>click</button>
<button on:click={func4}>click</button>
<button on:click={() => immutable2 = true}>click</button>
<button on:click={() => immutable2++}>click</button>
<button on:click={() => obj1.prop = false}>click</button>
<button on:click={() => obj2.prop++}>click</button>

@ -1,62 +0,0 @@
[
{
"code": "assignment-to-const",
"message": "You are assigning to a const",
"start": {
"line": 31,
"column": 24,
"character": 512
},
"end": {
"line": 31,
"column": 41,
"character": 529
},
"pos": 512
},
{
"code": "assignment-to-const",
"message": "You are assigning to a const",
"start": {
"line": 32,
"column": 24,
"character": 570
},
"end": {
"line": 32,
"column": 36,
"character": 582
},
"pos": 570
},
{
"code": "assignment-to-const",
"message": "You are assigning to a const",
"start": {
"line": 10,
"column": 2,
"character": 171
},
"end": {
"line": 10,
"column": 19,
"character": 188
},
"pos": 171
},
{
"code": "assignment-to-const",
"message": "You are assigning to a const",
"start": {
"line": 14,
"column": 2,
"character": 215
},
"end": {
"line": 14,
"column": 14,
"character": 227
},
"pos": 215
}
]
Loading…
Cancel
Save