inline variables in module scope

hoist-unmodified-var
Ben McCann 9 months ago
parent eec1110b80
commit fcb076b6c1

@ -346,8 +346,8 @@ export function client_component(source, analysis, options) {
}
const body = [
...state.hoisted,
...module.body,
...state.hoisted,
b.export_default(
b.function_declaration(
b.id(analysis.name),

@ -17,7 +17,7 @@ import { is_custom_element_node, is_element_node } from '../../../nodes.js';
import * as b from '../../../../utils/builders.js';
import { error } from '../../../../errors.js';
import {
can_hoist_declaration,
can_inline_variable,
function_visitor,
get_assignment_value,
serialize_get_binding,
@ -571,9 +571,7 @@ function serialize_element_attribute_update_assignment(element, node_id, attribu
const binding = context.state.scope
.owner(value.expression.name)
?.declarations.get(value.expression.name);
// TODO: use can_inline_variable instead
// and insert template strings after module variables
can_inline ||= can_hoist_declaration(binding, value.expression.name);
can_inline ||= can_inline_variable(binding, value.expression.name);
}
}
}
@ -1696,7 +1694,6 @@ function serialize_template_literal(values, visit, state) {
/** @type {import('estree').Expression[]} */
const expressions = [];
const scope = state.scope;
let contains_call_expression = false;
quasis.push(b.quasi(''));

@ -1,10 +1,13 @@
// index.svelte (Svelte VERSION)
// Note: compiler output will change before 5.0 is released!
import "svelte/internal/disclose-version";
const autocapitalize = 'words';
import * as $ from "svelte/internal";
const boolean = false;
var frag = $.template(`<p contenteditable="${boolean}">hello world</p>`);
var frag = $.template(`<p autocapitalize="${autocapitalize}" contenteditable="${boolean}">hello world</p>`);
export default function Hoist_unmodified_var($$anchor, $$props) {
$.push($$props, true);

@ -2,11 +2,13 @@
// Note: compiler output will change before 5.0 is released!
import * as $ from "svelte/internal/server";
const autocapitalize = 'words';
export default function Hoist_unmodified_var($$payload, $$props) {
$.push(true);
let boolean = false;
$$payload.out += `<p${$.attr("contenteditable", boolean, false)}>hello world</p>`;
$$payload.out += `<p${$.attr("autocapitalize", autocapitalize, false)}${$.attr("contenteditable", boolean, false)}>hello world</p>`;
$.pop();
}

@ -1,7 +1,11 @@
<svelte:options runes={true} />
<script context="module">
const autocapitalize = 'words';
</script>
<script>
let boolean = false;
</script>
<p contenteditable={boolean}>hello world</p>
<p {autocapitalize} contenteditable={boolean}>hello world</p>

Loading…
Cancel
Save