Prettify JS output + comments

pull/8442/head
Nguyen Tran 3 years ago
parent defb2f1a3d
commit 8cb8909640

@ -1163,21 +1163,26 @@ export default class Component {
parent[key].splice(index + 1, 0, b`let { ${props} } = $$props;`); parent[key].splice(index + 1, 0, b`let { ${props} } = $$props;`);
} }
if (node.declarations.length == 0) { if (node.declarations.length == 0) {
// After extracting all the prop names, remove if there are no declarations left
parent[key].splice(index, 1); parent[key].splice(index, 1);
} }
} else if (subscriptions.length > 0) { } else if (subscriptions.length > 0) {
if (key === 'left' && (parent.type === 'ForInStatement' || parent.type === 'ForOfStatement')) {
// if it is for (var x in array) or for (var x of array) // if it is for (var x in array) or for (var x of array)
// we are transforming from: // we are transforming from:
// //
// for (var x of array) { // for (var x of array) {
// // body // // body
// } // }
// or
// for (var x of array)
// // statement
// to: // to:
// for (var x of array) { // for (var x of array) {
// // subscription inserts // // subscription inserts
// // body // // body or statement
// } // }
if (key === 'left' && (parent.type === 'ForInStatement' || parent.type === 'ForOfStatement')) {
if (parent.body.type !== 'BlockStatement') { if (parent.body.type !== 'BlockStatement') {
parent.body = { parent.body = {
type: 'BlockStatement', type: 'BlockStatement',
@ -1185,9 +1190,10 @@ export default class Component {
}; };
} }
parent.body.body.unshift(...flattened_subscriptions); parent.body.body.unshift(...flattened_subscriptions);
} else { } else if (key === 'init' && parent.type === 'ForStatement') {
// If the variable declaration is not part of a block, we instead get a dummy variable setting // If the variable declaration is like for (var i = writable(1); ...), we instead get a dummy variable setting
// calling an immediately-invoked function expression containing all the subscription functions // calling an immediately-invoked function expression containing all the subscription functions
// e.g. for (var i = writable(1), $$subscriptions = (() => { subscription inserts })(); ...)
node.declarations.push({ node.declarations.push({
type: 'VariableDeclarator', type: 'VariableDeclarator',
id: component.get_unique_name('$$subscriptions', scope), id: component.get_unique_name('$$subscriptions', scope),
@ -1195,6 +1201,21 @@ export default class Component {
${flattened_subscriptions} ${flattened_subscriptions}
})()` })()`
}); });
} else {
// Variable declaration is a statement without a parent list of statements, so we transform it by
// putting the var in a block statement and call subscription inserts
// e.g.
// if (condition)
// var a = writable(1);
// turns into
// if (condition) {
// var a = writable(1);
// // subscription inserts here
// }
parent[key] = {
type: 'BlockStatement',
body: [parent[key], ...flattened_subscriptions]
};
} }
} }

Loading…
Cancel
Save