Skip to content

chore: merge main into async branch #16197

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 3 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/fair-laws-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: match class and style directives against attribute selector
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ jobs:
- run: pnpm test
env:
CI: true
TestNoAsync:
permissions: {}
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm playwright install chromium
- run: pnpm test runtime-runes
env:
CI: true
SVELTE_NO_ASYNC: true
Lint:
permissions: {}
runs-on: ubuntu-latest
Expand Down
12 changes: 4 additions & 8 deletions documentation/docs/07-misc/02-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ test('Effect', () => {
// effects normally run after a microtask,
// use flushSync to execute all pending effects synchronously
flushSync();
expect(log.value).toEqual([0]);
expect(log).toEqual([0]);

count = 1;
flushSync();

expect(log.value).toEqual([0, 1]);
expect(log).toEqual([0, 1]);
});

cleanup();
Expand All @@ -148,17 +148,13 @@ test('Effect', () => {
*/
export function logger(getValue) {
/** @type {any[]} */
let log = $state([]);
let log = [];

$effect(() => {
log.push(getValue());
});

return {
get value() {
return log;
}
};
return log;
}
```

Expand Down
20 changes: 20 additions & 0 deletions packages/svelte/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# svelte

## 5.34.5

### Patch Changes

- fix: keep spread non-delegated event handlers up to date ([#16180](https://github.com/sveltejs/svelte/pull/16180))

- fix: remove undefined attributes on hydration ([#16178](https://github.com/sveltejs/svelte/pull/16178))

- fix: ensure sources within nested effects still register correctly ([#16193](https://github.com/sveltejs/svelte/pull/16193))

- fix: avoid shadowing a variable in dynamic components ([#16185](https://github.com/sveltejs/svelte/pull/16185))

## 5.34.4

### Patch Changes

- fix: don't set state withing `with_parent` in proxy ([#16176](https://github.com/sveltejs/svelte/pull/16176))

- fix: use compiler-driven reactivity in legacy mode template expressions ([#16100](https://github.com/sveltejs/svelte/pull/16100))

## 5.34.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "svelte",
"description": "Cybernetically enhanced web apps",
"license": "MIT",
"version": "5.34.3",
"version": "5.34.5",
"type": "module",
"types": "./types/index.d.ts",
"engines": {
Expand Down
9 changes: 8 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/state/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ function open(parser) {
error: null,
pending: null,
then: null,
catch: null
catch: null,
metadata: {
expression: create_expression_metadata()
}
});

if (parser.eat('then')) {
Expand Down Expand Up @@ -711,6 +714,9 @@ function special(parser) {
declarations: [{ type: 'VariableDeclarator', id, init, start: id.start, end: init.end }],
start: start + 2, // start at const, not at @const
end: parser.index - 1
},
metadata: {
expression: create_expression_metadata()
}
});
}
Expand All @@ -737,6 +743,7 @@ function special(parser) {
end: parser.index,
expression: /** @type {AST.RenderTag['expression']} */ (expression),
metadata: {
expression: create_expression_metadata(),
dynamic: false,
arguments: [],
path: [],
Expand Down
17 changes: 11 additions & 6 deletions packages/svelte/src/compiler/phases/2-analyze/css/css-prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,7 @@ function relative_selector_might_apply_to_node(relative_selector, rule, element,
}

case 'ClassSelector': {
if (
!attribute_matches(element, 'class', name, '~=', false) &&
!element.attributes.some(
(attribute) => attribute.type === 'ClassDirective' && attribute.name === name
)
) {
if (!attribute_matches(element, 'class', name, '~=', false)) {
return false;
}

Expand Down Expand Up @@ -633,6 +628,16 @@ function attribute_matches(node, name, expected_value, operator, case_insensitiv
if (attribute.type === 'SpreadAttribute') return true;
if (attribute.type === 'BindDirective' && attribute.name === name) return true;

// match attributes against the corresponding directive but bail out on exact matching
if (attribute.type === 'StyleDirective' && name.toLowerCase() === 'style') return true;
if (attribute.type === 'ClassDirective' && name.toLowerCase() === 'class') {
if (operator == '~=') {
if (attribute.name === expected_value) return true;
} else {
return true;
}
}

if (attribute.type !== 'Attribute') continue;
if (attribute.name.toLowerCase() !== name.toLowerCase()) continue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ export function AssignmentExpression(node, context) {
}
}

if (context.state.expression) {
context.state.expression.has_assignment = true;
}

context.next();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ export function AwaitBlock(node, context) {

mark_subtree_dynamic(context.path);

context.next();
context.visit(node.expression, { ...context.state, expression: node.metadata.expression });
if (node.pending) context.visit(node.pending);
if (node.then) context.visit(node.then);
if (node.catch) context.visit(node.catch);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ export function ConstTag(node, context) {
e.const_tag_invalid_placement(node);
}

context.next();
const declaration = node.declaration.declarations[0];

context.visit(declaration.id);
context.visit(declaration.init, { ...context.state, expression: node.metadata.expression });
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ export function HtmlTag(node, context) {
// unfortunately this is necessary in order to fix invalid HTML
mark_subtree_dynamic(context.path);

context.next({
...context.state,
expression: node.metadata.expression
});
context.next({ ...context.state, expression: node.metadata.expression });
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export function Identifier(node, context) {
if (binding) {
if (context.state.expression) {
context.state.expression.dependencies.add(binding);
context.state.expression.references.add(binding);
context.state.expression.has_state ||=
binding.kind !== 'static' &&
!binding.is_function() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ export function KeyBlock(node, context) {

mark_subtree_dynamic(context.path);

context.visit(node.expression, {
...context.state,
expression: node.metadata.expression
});

context.visit(node.expression, { ...context.state, expression: node.metadata.expression });
context.visit(node.fragment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export function MemberExpression(node, context) {
}
}

if (context.state.expression && !is_pure(node, context)) {
context.state.expression.has_state = true;
if (context.state.expression) {
context.state.expression.has_member_expression = true;
context.state.expression.has_state ||= !is_pure(node, context);
}

if (!is_safe_identifier(node, context.state.scope)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function RenderTag(node, context) {

mark_subtree_dynamic(context.path);

context.visit(callee);
context.visit(callee, { ...context.state, expression: node.metadata.expression });

for (const arg of expression.arguments) {
const metadata = create_expression_metadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ export function UpdateExpression(node, context) {
}
}

if (context.state.expression) {
context.state.expression.has_assignment = true;
}

context.next();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export function visit_function(node, context) {
scope: context.state.scope
};

if (context.state.expression) {
for (const [name] of context.state.scope.references) {
const binding = context.state.scope.get(name);

if (binding && binding.scope.function_depth < context.state.scope.function_depth) {
context.state.expression.references.add(binding);
}
}
}

context.next({
...context.state,
function_depth: context.state.function_depth + 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
/** @import { Expression } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types' */
import * as b from '../../../../utils/builders.js';
import { build_expression } from './shared/utils.js';

/**
* @param {AST.AttachTag} node
* @param {ComponentContext} context
*/
export function AttachTag(node, context) {
context.state.init.push(
b.stmt(
b.call(
'$.attach',
context.state.node,
b.thunk(/** @type {Expression} */ (context.visit(node.expression)))
)
)
);
const expression = build_expression(context, node.expression, node.metadata.expression);
context.state.init.push(b.stmt(b.call('$.attach', context.state.node, b.thunk(expression))));
context.next();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/** @import { BlockStatement, Expression, Pattern, Statement } from 'estree' */
/** @import { BlockStatement, Pattern, Statement } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
import { extract_identifiers } from '../../../../utils/ast.js';
import * as b from '#compiler/builders';
import { create_derived } from '../utils.js';
import { get_value } from './shared/declarations.js';
import { build_expression } from './shared/utils.js';

/**
* @param {AST.AwaitBlock} node
Expand All @@ -14,7 +15,7 @@ export function AwaitBlock(node, context) {
context.state.template.push_comment();

// Visit {#await <expression>} first to ensure that scopes are in the correct order
const expression = b.thunk(/** @type {Expression} */ (context.visit(node.expression)));
const expression = b.thunk(build_expression(context, node.expression, node.metadata.expression));

let then_block;
let catch_block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import { build_component } from './shared/component.js';
* @param {ComponentContext} context
*/
export function Component(node, context) {
const component = build_component(
node,
// if it's not dynamic we will just use the node name, if it is dynamic we will use the node name
// only if it's a valid identifier, otherwise we will use a default name
!node.metadata.dynamic || regex_is_valid_identifier.test(node.name) ? node.name : '$$component',
context
);
const component = build_component(node, node.name, context);
context.state.init.push(component);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/** @import { Expression, Pattern } from 'estree' */
/** @import { Pattern } from 'estree' */
/** @import { AST } from '#compiler' */
/** @import { ComponentContext } from '../types' */
import { dev } from '../../../../state.js';
import { extract_identifiers } from '../../../../utils/ast.js';
import * as b from '#compiler/builders';
import { create_derived } from '../utils.js';
import { get_value } from './shared/declarations.js';
import { build_expression } from './shared/utils.js';

/**
* @param {AST.ConstTag} node
Expand All @@ -15,15 +16,8 @@ export function ConstTag(node, context) {
const declaration = node.declaration.declarations[0];
// TODO we can almost certainly share some code with $derived(...)
if (declaration.id.type === 'Identifier') {
context.state.init.push(
b.const(
declaration.id,
create_derived(
context.state,
b.thunk(/** @type {Expression} */ (context.visit(declaration.init)))
)
)
);
const init = build_expression(context, declaration.init, node.metadata.expression);
context.state.init.push(b.const(declaration.id, create_derived(context.state, b.thunk(init))));

context.state.transform[declaration.id.name] = { read: get_value };

Expand All @@ -48,13 +42,15 @@ export function ConstTag(node, context) {

// TODO optimise the simple `{ x } = y` case — we can just return `y`
// instead of destructuring it only to return a new object
const init = build_expression(
{ ...context, state: child_state },
declaration.init,
node.metadata.expression
);
const fn = b.arrow(
[],
b.block([
b.const(
/** @type {Pattern} */ (context.visit(declaration.id, child_state)),
/** @type {Expression} */ (context.visit(declaration.init, child_state))
),
b.const(/** @type {Pattern} */ (context.visit(declaration.id, child_state)), init),
b.return(b.object(identifiers.map((node) => b.prop('init', node, node))))
])
);
Expand Down
Loading