-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
chore: simplify assignments in server code #12614
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8d3b17d
remove some junk
Rich-Harris 497f639
tweak
Rich-Harris fa6dd4d
simplify
Rich-Harris a16e0c1
improve code generation
Rich-Harris 44076f7
move code, improve generation
Rich-Harris 20c9695
simplify
Rich-Harris 5c81887
tidy up
Rich-Harris d093af0
move some code
Rich-Harris d49f285
unnecessary
Rich-Harris bbdad4b
fix
Rich-Harris 3a90f07
tweak
Rich-Harris 84ed961
tidy up
Rich-Harris 897a091
changeset
Rich-Harris f9aef71
more consistent naming
Rich-Harris df19c83
Update packages/svelte/src/compiler/phases/3-transform/server/visitor…
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: correctly update stores when reassigning with operator other than `=` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 111 additions & 4 deletions
115
packages/svelte/src/compiler/phases/3-transform/server/visitors/AssignmentExpression.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,118 @@ | ||
/** @import { AssignmentExpression } from 'estree' */ | ||
/** @import { Context } from '../types.js' */ | ||
import { serialize_set_binding } from './shared/utils.js'; | ||
/** @import { AssignmentExpression, AssignmentOperator, BinaryOperator, Expression, Node, Pattern } from 'estree' */ | ||
/** @import { SvelteNode } from '#compiler' */ | ||
/** @import { Context, ServerTransformState } from '../types.js' */ | ||
import * as b from '../../../../utils/builders.js'; | ||
import { extract_paths } from '../../../../utils/ast.js'; | ||
import { serialize_get_binding } from './shared/utils.js'; | ||
|
||
/** | ||
* @param {AssignmentExpression} node | ||
* @param {Context} context | ||
*/ | ||
export function AssignmentExpression(node, context) { | ||
return serialize_set_binding(node, context, context.next); | ||
const parent = /** @type {Node} */ (context.path.at(-1)); | ||
const is_standalone = parent.type.endsWith('Statement'); | ||
|
||
if ( | ||
node.left.type === 'ArrayPattern' || | ||
node.left.type === 'ObjectPattern' || | ||
node.left.type === 'RestElement' | ||
) { | ||
const value = /** @type {Expression} */ (context.visit(node.right)); | ||
const should_cache = value.type !== 'Identifier'; | ||
const rhs = should_cache ? b.id('$$value') : value; | ||
|
||
let changed = false; | ||
|
||
const assignments = extract_paths(node.left).map((path) => { | ||
const value = path.expression?.(rhs); | ||
|
||
let assignment = serialize_assignment('=', path.node, value, context); | ||
if (assignment !== null) changed = true; | ||
|
||
return assignment ?? b.assignment('=', path.node, value); | ||
}); | ||
|
||
if (!changed) { | ||
// No change to output -> nothing to transform -> we can keep the original assignment | ||
return context.next(); | ||
} | ||
|
||
const sequence = b.sequence(assignments); | ||
|
||
if (!is_standalone) { | ||
// this is part of an expression, we need the sequence to end with the value | ||
sequence.expressions.push(rhs); | ||
} | ||
|
||
if (should_cache) { | ||
// the right hand side is a complex expression, wrap in an IIFE to cache it | ||
return b.call(b.arrow([rhs], sequence), value); | ||
} | ||
|
||
return sequence; | ||
} | ||
|
||
return serialize_assignment(node.operator, node.left, node.right, context) || context.next(); | ||
} | ||
|
||
/** | ||
* Only returns an expression if this is not a `$store` assignment, as others can be kept as-is | ||
* @param {AssignmentOperator} operator | ||
* @param {Pattern} left | ||
* @param {Expression} right | ||
* @param {import('zimmerframe').Context<SvelteNode, ServerTransformState>} context | ||
* @returns {Expression | null} | ||
*/ | ||
function serialize_assignment(operator, left, right, context) { | ||
let object = left; | ||
|
||
while (object.type === 'MemberExpression') { | ||
// @ts-expect-error | ||
object = object.object; | ||
} | ||
|
||
if (object.type !== 'Identifier' || !is_store_name(object.name)) { | ||
return null; | ||
} | ||
|
||
const name = object.name.slice(1); | ||
|
||
if (!context.state.scope.get(name)) { | ||
return null; | ||
} | ||
|
||
if (object === left) { | ||
let value = /** @type {Expression} */ (context.visit(right)); | ||
|
||
if (operator !== '=') { | ||
// turn `x += 1` into `x = x + 1` | ||
value = b.binary( | ||
/** @type {BinaryOperator} */ (operator.slice(0, -1)), | ||
serialize_get_binding(left, context.state), | ||
value | ||
); | ||
} | ||
|
||
return b.call('$.store_set', b.id(name), value); | ||
} | ||
|
||
return b.call( | ||
'$.store_mutate', | ||
b.assignment('??=', b.id('$$store_subs'), b.object([])), | ||
b.literal(object.name), | ||
b.id(name), | ||
b.assignment( | ||
operator, | ||
/** @type {Pattern} */ (context.visit(left)), | ||
/** @type {Expression} */ (context.visit(right)) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param {string} name | ||
*/ | ||
function is_store_name(name) { | ||
return name[0] === '$' && /[A-Za-z_]/.test(name[1]); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/svelte/tests/runtime-runes/samples/store-assignments/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
html: '<p>3</p>' | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/svelte/tests/runtime-runes/samples/store-assignments/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script> | ||
import { writable } from 'svelte/store'; | ||
|
||
const count = writable(0); | ||
|
||
$count += 1; | ||
$count += 1; | ||
$count += 1; | ||
</script> | ||
|
||
<p>{$count}</p> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.