Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Node } from 'estree-walker';
import * as ts from 'typescript';
import {
getBinaryAssignmentExpr,
isFirstInAnExpressionStatement,
isSafeToPrefixWithSemicolon,
isNotPropertyNameOfImport
} from './utils/tsAst';
import { ExportedNames, is$$PropsDeclaration } from './nodes/ExportedNames';
Expand Down Expand Up @@ -164,7 +164,7 @@ export function processInstanceScriptContent(
// - in order to get ts errors if store is not assignable to SvelteStore
// - use $store variable defined above to get ts flow control
const dollar = str.original.indexOf('$', ident.getStart() + astOffset);
const getPrefix = isFirstInAnExpressionStatement(ident) ? ';' : '';
const getPrefix = isSafeToPrefixWithSemicolon(ident) ? ';' : '';
str.overwrite(dollar, dollar + 1, getPrefix + '(__sveltets_1_store_get(');
str.prependLeft(ident.end + astOffset, `), $${storename})`);
};
Expand Down
14 changes: 12 additions & 2 deletions packages/svelte2tsx/src/svelte2tsx/utils/tsAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,23 @@ export function getNamesFromLabeledStatement(node: ts.LabeledStatement): string[
);
}

export function isFirstInAnExpressionStatement(node: ts.Identifier): boolean {
export function isSafeToPrefixWithSemicolon(node: ts.Identifier): boolean {
let parent = node.parent;
while (parent && !ts.isExpressionStatement(parent)) {
parent = parent.parent;
}
if (!parent) {
return false;
}
return parent.getStart() === node.getStart();
return (
parent.getStart() === node.getStart() &&
!(
parent.parent &&
(ts.isIfStatement(parent.parent) ||
ts.isForStatement(parent.parent) ||
ts.isForInStatement(parent.parent) ||
ts.isForOfStatement(parent.parent) ||
ts.isWhileStatement(parent.parent))
)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
///<reference types="svelte" />
<></>;function render() {

const store1 = ''/*Ωignore_startΩ*/;let $store1 = __sveltets_1_store_get(store1);/*Ωignore_endΩ*/;

if (store1)
(__sveltets_1_store_get(store1), $store1)
else if (store1)
(__sveltets_1_store_get(store1), $store1)
else
(__sveltets_1_store_get(store), $store)
if (store1)
store1.set( 1)
if (store1)
(__sveltets_1_store_get(store1), $store1).x = 1

for (let i=0; i < 1; i++)
(__sveltets_1_store_get(store1), $store1)

for (const a of b)
(__sveltets_1_store_get(store1), $store1)

for (const a in b)
(__sveltets_1_store_get(store1), $store1)

while (true)
(__sveltets_1_store_get(store1), $store1)
;
() => (<></>);
return { props: {}, slots: {}, getters: {}, events: {} }}

export default class Input__SvelteComponent_ extends __sveltets_1_createSvelte2TsxComponent(__sveltets_1_partial(__sveltets_1_with_any_event(render()))) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script>
const store1 = '';

if (store1)
$store1
else if (store1)
$store1
else
$store
if (store1)
$store1 = 1
if (store1)
$store1.x = 1

for (let i=0; i < 1; i++)
$store1

for (const a of b)
$store1

for (const a in b)
$store1

while (true)
$store1
</script>