Skip to content

Commit

Permalink
mark module variables as mutated or reassigned (#4469)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored Feb 29, 2020
1 parent f244b80 commit cf660bd
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

* In `vars` array, correctly indicate whether `module` variables are `mutated` or `reassigned` ([#3215](https://github.com/sveltejs/svelte/issues/3215))
* Exclude global variables from `$capture_state` ([#4463](https://github.com/sveltejs/svelte/issues/4463))

## 3.19.1
Expand Down
9 changes: 7 additions & 2 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ export default class Component {

const component = this;
const { content } = script;
const { instance_scope, instance_scope_map: map } = this;
const { instance_scope, module_scope, instance_scope_map: map } = this;

let scope = instance_scope;

Expand All @@ -797,7 +797,12 @@ export default class Component {
const deep = assignee.type === 'MemberExpression';

names.forEach(name => {
if (scope.find_owner(name) === instance_scope) {
const scope_owner = scope.find_owner(name);
if (
scope_owner !== null
? scope_owner === instance_scope
: module_scope && module_scope.has(name)
) {
const variable = component.var_lookup.get(name);
variable[deep ? 'mutated' : 'reassigned'] = true;
}
Expand Down
72 changes: 72 additions & 0 deletions test/vars/samples/modules-vars/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export default {
test(assert, vars) {
assert.deepEqual(vars, [
{
name: "a",
export_name: null,
injected: false,
module: true,
mutated: false,
reassigned: true,
referenced: false,
referenced_from_script: false,
writable: true
},
{
name: "b",
export_name: null,
injected: false,
module: true,
mutated: true,
reassigned: false,
referenced: false,
referenced_from_script: false,
writable: true
},
{
name: "c",
export_name: null,
injected: false,
module: true,
mutated: false,
reassigned: false,
referenced: false,
referenced_from_script: false,
writable: true
},
{
name: "d",
export_name: null,
injected: false,
module: true,
mutated: false,
reassigned: false,
referenced: false,
referenced_from_script: false,
writable: true
},
{
name: "c",
export_name: null,
injected: false,
module: false,
mutated: false,
reassigned: true,
referenced: false,
referenced_from_script: true,
writable: true
},
{
name: "foo",
export_name: null,
injected: false,
module: false,
mutated: false,
reassigned: false,
referenced: false,
referenced_from_script: false,
writable: false
}
]);
}
};
18 changes: 18 additions & 0 deletions test/vars/samples/modules-vars/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script context='module'>
let a = {};
let b = {};
let c = {};
let d = {};
</script>
<script>
let c = {};
a = 1;
b.x = 1;
function foo() {
let d = {};
c = 1;
d.x = 1;
}
</script>

0 comments on commit cf660bd

Please sign in to comment.