From 3a69b4c41533757ea0e3c7e293bd41818a833ead Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Sat, 16 Nov 2024 17:10:45 +0000 Subject: [PATCH] fix: ensure inline object literals are correctly serialised (#14325) * fix: ensure inline object literals are correctly serialised * Apply suggestions from code review * address feedback * address feedback --------- Co-authored-by: Rich Harris --- .changeset/wicked-readers-knock.md | 5 ++++ .../phases/2-analyze/visitors/shared/utils.js | 27 ++++++++++++++++--- .../samples/inline-expressions-2/_config.js | 11 ++++++++ .../samples/inline-expressions-2/main.svelte | 6 +++++ 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 .changeset/wicked-readers-knock.md create mode 100644 packages/svelte/tests/runtime-legacy/samples/inline-expressions-2/_config.js create mode 100644 packages/svelte/tests/runtime-legacy/samples/inline-expressions-2/main.svelte diff --git a/.changeset/wicked-readers-knock.md b/.changeset/wicked-readers-knock.md new file mode 100644 index 000000000000..4ec417402f20 --- /dev/null +++ b/.changeset/wicked-readers-knock.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: treat property accesses of literals as pure diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js index 82356ea619c1..2dec4361c8a0 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/utils.js @@ -1,4 +1,4 @@ -/** @import { AssignmentExpression, Expression, Identifier, Pattern, PrivateIdentifier, Super, UpdateExpression, VariableDeclarator } from 'estree' */ +/** @import { AssignmentExpression, Expression, Literal, Pattern, PrivateIdentifier, Super, UpdateExpression, VariableDeclarator } from 'estree' */ /** @import { AST, Binding } from '#compiler' */ /** @import { AnalysisState, Context } from '../../types' */ /** @import { Scope } from '../../../scope' */ @@ -176,21 +176,42 @@ export function is_safe_identifier(expression, scope) { } /** - * @param {Expression | Super} node + * @param {Expression | Literal | Super} node * @param {Context} context * @returns {boolean} */ export function is_pure(node, context) { + if (node.type === 'Literal') { + return true; + } + if (node.type === 'CallExpression') { + if (!is_pure(node.callee, context)) { + return false; + } + for (let arg of node.arguments) { + if (!is_pure(arg.type === 'SpreadElement' ? arg.argument : arg, context)) { + return false; + } + } + return true; + } if (node.type !== 'Identifier' && node.type !== 'MemberExpression') { return false; } - const left = object(node); + /** @type {Expression | Super | null} */ + let left = node; + while (left.type === 'MemberExpression') { + left = left.object; + } + if (!left) return false; if (left.type === 'Identifier') { const binding = context.state.scope.get(left.name); if (binding === null) return true; // globals are assumed to be safe + } else if (is_pure(left, context)) { + return true; } // TODO add more cases (safe Svelte imports, etc) diff --git a/packages/svelte/tests/runtime-legacy/samples/inline-expressions-2/_config.js b/packages/svelte/tests/runtime-legacy/samples/inline-expressions-2/_config.js new file mode 100644 index 000000000000..1869540b2c55 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/inline-expressions-2/_config.js @@ -0,0 +1,11 @@ +import { test } from '../../test'; + +export default test({ + html: ` +

Without text expression: 7.36

+

With text expression: 7.36

+

With text expression and function call: 7.36

+

With text expression and property access: 4

+

Hello name!

+

4

` +}); diff --git a/packages/svelte/tests/runtime-legacy/samples/inline-expressions-2/main.svelte b/packages/svelte/tests/runtime-legacy/samples/inline-expressions-2/main.svelte new file mode 100644 index 000000000000..78a325dcc420 --- /dev/null +++ b/packages/svelte/tests/runtime-legacy/samples/inline-expressions-2/main.svelte @@ -0,0 +1,6 @@ +

Without text expression: 7.36

+

With text expression: {7.36}

+

With text expression and function call: {(7.36).toLocaleString()}

+

With text expression and property access: {"test".length}

+

Hello {('name').toUpperCase().toLowerCase()}!

+

{"test".length}