Skip to content

Commit 915b8b1

Browse files
committed
Fix issue where var declarations aren't in a block
1 parent 7352b2d commit 915b8b1

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/compiler/compile/Component.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import TemplateScope from './nodes/shared/TemplateScope';
2525
import fuzzymatch from '../utils/fuzzymatch';
2626
import get_object from './utils/get_object';
2727
import Slot from './nodes/Slot';
28-
import { Node, ImportDeclaration, ExportNamedDeclaration, Identifier, ExpressionStatement, AssignmentExpression, Literal, Property, RestElement, ExportDefaultDeclaration, ExportAllDeclaration, FunctionDeclaration, FunctionExpression } from 'estree';
28+
import { Node, ImportDeclaration, ExportNamedDeclaration, Identifier, ExpressionStatement, AssignmentExpression, Literal, Property, RestElement, ExportDefaultDeclaration, ExportAllDeclaration, FunctionDeclaration, FunctionExpression, Statement } from 'estree';
2929
import add_to_set from './utils/add_to_set';
3030
import check_graph_for_cycles from './utils/check_graph_for_cycles';
3131
import { print, b } from 'code-red';
@@ -38,6 +38,7 @@ import compiler_warnings from './compiler_warnings';
3838
import compiler_errors from './compiler_errors';
3939
import { extract_ignores_above_position, extract_svelte_ignore_from_comments } from '../utils/extract_svelte_ignore';
4040
import check_enable_sourcemap from './utils/check_enable_sourcemap';
41+
import { flatten } from '../utils/flatten';
4142

4243
interface ComponentOptions {
4344
namespace?: string;
@@ -1138,14 +1139,23 @@ export default class Component {
11381139
}
11391140
}
11401141

1141-
if (inserts.length > 0) {
1142-
parent[key].splice(index + 1, 0, ...inserts);
1143-
}
1144-
if (props.length > 0) {
1145-
parent[key].splice(index + 1, 0, b`let { ${props} } = $$props;`);
1146-
}
1147-
if (node.declarations.length == 0) {
1148-
parent[key].splice(index, 1);
1142+
if (Array.isArray(parent[key])) {
1143+
if (inserts.length > 0) {
1144+
inserts.reverse().forEach((insert) => {
1145+
parent[key].splice(index + 1, 0, ...insert);
1146+
});
1147+
}
1148+
if (props.length > 0) {
1149+
parent[key].splice(index + 1, 0, b`let { ${props} } = $$props;`);
1150+
}
1151+
if (node.declarations.length == 0) {
1152+
parent[key].splice(index, 1);
1153+
}
1154+
} else if (inserts.length > 0) {
1155+
this.replace({
1156+
type: 'BlockStatement',
1157+
body: flatten([node, inserts]) as Statement[]
1158+
});
11491159
}
11501160

11511161
return this.skip();

0 commit comments

Comments
 (0)