Skip to content

prevent overwriting export consts #2245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
prevent overwriting export consts - fixes #2236
  • Loading branch information
Rich-Harris committed Mar 17, 2019
commit 2ba37882a845c95ee63b1b835924e43c88a4e7a7
9 changes: 5 additions & 4 deletions src/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ export default class Component {
current_group = { kind: node.kind, declarators: [declarator], insert };
coalesced_declarations.push(current_group);
} else if (insert) {
current_group.insert = insert
current_group.insert = insert;
current_group.declarators.push(declarator);
} else {
current_group.declarators.push(declarator);
Expand Down Expand Up @@ -841,8 +841,9 @@ export default class Component {
});

coalesced_declarations.forEach(group => {
let c = 0;
const writable = group.kind === 'var' || group.kind === 'let';

let c = 0;
let combining = false;

group.declarators.forEach(declarator => {
Expand All @@ -851,7 +852,7 @@ export default class Component {
if (combining) {
code.overwrite(c, id.start, ', ');
} else {
code.appendLeft(id.start, '{ ');
if (writable) code.appendLeft(id.start, '{ ');
combining = true;
}

Expand All @@ -863,7 +864,7 @@ export default class Component {
? `; ${group.insert}`
: '';

const suffix = code.original[c] === ';' ? ` } = $$props${insert}` : ` } = $$props${insert};`;
const suffix = `${writable ? ` } = $$props` : ``}${insert}` + (code.original[c] === ';' ? `` : `;`);
code.appendLeft(c, suffix);
}
});
Expand Down
7 changes: 7 additions & 0 deletions test/runtime/samples/prop-const/Nested.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export let a;
export const b = 2;
</script>

<p>a: {a}</p>
<p>b: {b}</p>
23 changes: 23 additions & 0 deletions test/runtime/samples/prop-const/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default {
props: {
a: 3,
b: 4
},

html: `
<p>a: 3</p>
<p>b: 2</p>
`,

async test({ assert, component, target }) {
await component.$set({
a: 5,
b: 6
});

assert.htmlEqual(target.innerHTML, `
<p>a: 5</p>
<p>b: 2</p>
`);
}
};
8 changes: 8 additions & 0 deletions test/runtime/samples/prop-const/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
import Nested from './Nested.svelte';

export let a;
export let b;
</script>

<Nested a={a} b={b}/>