Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: function called as tagged template literal is reactively called #12692

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: function called as tagged template literal is reactively called
Co-authored-by: Oscar Dominguez <[email protected]>
  • Loading branch information
paoloricciuti and oscard0m committed Aug 1, 2024
commit fc2983f023213ab0a50db5b2cfd402b13d64fb9d
5 changes: 5 additions & 0 deletions .changeset/serious-owls-think.md
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
---
'svelte': patch
---

feat: function called as tagged template literal is reactively called
4 changes: 3 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 23,7 @@ import { Attribute } from './visitors/Attribute.js';
import { AwaitBlock } from './visitors/AwaitBlock.js';
import { BindDirective } from './visitors/BindDirective.js';
import { CallExpression } from './visitors/CallExpression.js';
import { TaggedTemplateExpression } from './visitors/TaggedTemplateExpression.js';
import { ClassBody } from './visitors/ClassBody.js';
import { ClassDeclaration } from './visitors/ClassDeclaration.js';
import { ClassDirective } from './visitors/ClassDirective.js';
Expand Down Expand Up @@ -164,7 165,8 @@ const visitors = {
Text,
TitleElement,
UpdateExpression,
VariableDeclarator
VariableDeclarator,
TaggedTemplateExpression
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@
import { get_rune } from '../../scope.js';
import * as e from '../../../errors.js';
import { get_parent, unwrap_optional } from '../../../utils/ast.js';
import { is_safe_identifier } from './shared/utils.js';
import { is_known_safe_call, is_safe_identifier } from './shared/utils.js';

/**
* @param {CallExpression} node
Expand Down Expand Up @@ -182,28 182,3 @@ export function CallExpression(node, context) {
context.next();
}
}

/**
* @param {CallExpression} node
* @param {Context} context
* @returns {boolean}
*/
function is_known_safe_call(node, context) {
const callee = node.callee;

// String / Number / BigInt / Boolean casting calls
if (callee.type === 'Identifier') {
const name = callee.name;
const binding = context.state.scope.get(name);
if (
binding === null &&
(name === 'BigInt' || name === 'String' || name === 'Number' || name === 'Boolean')
) {
return true;
}
}

// TODO add more cases

return false;
}
Original file line number Diff line number Diff line change
@@ -0,0 1,23 @@
/** @import { TaggedTemplateExpression, VariableDeclarator } from 'estree' */
/** @import { Context } from '../types' */
import { is_known_safe_call } from './shared/utils.js';

/**
* @param {TaggedTemplateExpression} node
* @param {Context} context
*/
export function TaggedTemplateExpression(node, context) {
if (context.state.expression && !is_known_safe_call(node, context)) {
context.state.expression.has_call = true;
context.state.expression.has_state = true;
}

if (node.tag.type === 'Identifier') {
const binding = context.state.scope.get(node.tag.name);

if (binding !== null) {
binding.is_called = true;
}
}
context.next();
}
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
/** @import { AssignmentExpression, Expression, Pattern, PrivateIdentifier, Super, UpdateExpression, VariableDeclarator } from 'estree' */
/** @import { AssignmentExpression, CallExpression, Expression, Pattern, PrivateIdentifier, Super, TaggedTemplateExpression, UpdateExpression, VariableDeclarator } from 'estree' */
/** @import { Fragment } from '#compiler' */
/** @import { AnalysisState, Context } from '../../types' */
/** @import { Scope } from '../../../scope' */
Expand Down Expand Up @@ -165,3 165,28 @@ export function is_safe_identifier(expression, scope) {
binding.kind !== 'rest_prop'
);
}

/**
* @param {TaggedTemplateExpression | CallExpression} node
* @param {Context} context
* @returns {boolean}
*/
export function is_known_safe_call(node, context) {
const callee = node.type === 'TaggedTemplateExpression' ? node.tag : node.callee;

// String / Number / BigInt / Boolean casting calls
if (callee.type === 'Identifier') {
const name = callee.name;
const binding = context.state.scope.get(name);
if (
binding === null &&
(name === 'BigInt' || name === 'String' || name === 'Number' || name === 'Boolean')
) {
return true;
}
}

// TODO add more cases

return false;
}
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
mode: ['client'],
async test({ assert, target, ok }) {
const button = target.querySelector('button');

assert.htmlEqual(target.innerHTML, `0 <button></button>`);

flushSync(() => {
button?.click();
});

assert.htmlEqual(target.innerHTML, `1 <button></button>`);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
<script>
let count = $state(0);
function showCount() {
return count;
}
</script>

{showCount``} <button onclick={() => count }></button>
Loading