fix: ensure is_pure takes into account runes (#14333)

* fix: ensure is_pure takes into account runes

* feedback
pull/14362/head
Dominic Gannaway 10 months ago committed by GitHub
parent 741106879b
commit 747d40833b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure is_pure takes into account $effect.tracking()

@ -4,8 +4,10 @@
/** @import { Scope } from '../../../scope' */
/** @import { NodeLike } from '../../../../errors.js' */
import * as e from '../../../../errors.js';
import { extract_identifiers, object } from '../../../../utils/ast.js';
import { extract_identifiers } from '../../../../utils/ast.js';
import * as w from '../../../../warnings.js';
import * as b from '../../../../utils/builders.js';
import { get_rune } from '../../../scope.js';
/**
* @param {AssignmentExpression | UpdateExpression} node
@ -184,6 +186,7 @@ export function is_pure(node, context) {
if (node.type === 'Literal') {
return true;
}
if (node.type === 'CallExpression') {
if (!is_pure(node.callee, context)) {
return false;
@ -195,10 +198,15 @@ export function is_pure(node, context) {
}
return true;
}
if (node.type !== 'Identifier' && node.type !== 'MemberExpression') {
return false;
}
if (get_rune(b.call(node), context.state.scope) === '$effect.tracking') {
return false;
}
/** @type {Expression | Super | null} */
let left = node;
while (left.type === 'MemberExpression') {

@ -1,11 +0,0 @@
import { test } from '../../test';
export default test({
html: `
<p>Without text expression: 7.36</p>
<p>With text expression: 7.36</p>
<p>With text expression and function call: 7.36</p>
<p>With text expression and property access: 4</p>
<h1>Hello name!</h1>
<p>4</p>`
});

@ -0,0 +1,21 @@
import { test } from '../../test';
export default test({
html: `
<p>Without text expression: 7.36</p>
<p>With text expression: 7.36</p>
<p>With text expression and function call: 7.36</p>
<p>With text expression and property access: 4</p>
<h1>Hello name!</h1>
<p>4</p>
<h1>Tracking: true</h1>`,
ssrHtml: `
<p>Without text expression: 7.36</p>
<p>With text expression: 7.36</p>
<p>With text expression and function call: 7.36</p>
<p>With text expression and property access: 4</p>
<h1>Hello name!</h1>
<p>4</p>
<h1>Tracking: false</h1>`
});

@ -4,3 +4,4 @@
<p>With text expression and property access: {"test".length}</p>
<h1>Hello {('name').toUpperCase().toLowerCase()}!</h1>
<p>{"test".length}</p>
<h1>Tracking: {$effect.tracking()}</h1>
Loading…
Cancel
Save