Skip to content

Commit 50de8c5

Browse files
authored
fix: attach __svelte_meta correctly to elements following a CSS wrapper (sveltejs#15982)
* fix: attach __svelte_meta correctly to elements following a CSS wrapper * Update .changeset/nervous-hotels-clean.md
1 parent c03ea47 commit 50de8c5

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

.changeset/nervous-hotels-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: attach `__svelte_meta` correctly to elements following a CSS wrapper

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/component.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @import { BlockStatement, Expression, ExpressionStatement, Identifier, MemberExpression, Pattern, Property, SequenceExpression, Statement } from 'estree' */
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../../types.js' */
4-
import { dev, is_ignored } from '../../../../../state.js';
4+
import { dev, is_ignored, locator } from '../../../../../state.js';
55
import { get_attribute_chunks, object } from '../../../../../utils/ast.js';
66
import * as b from '#compiler/builders';
77
import { build_bind_this, memoize_expression, validate_binding } from '../shared/utils.js';
@@ -440,6 +440,13 @@ export function build_component(node, component_name, context, anchor = context.
440440
}
441441

442442
if (Object.keys(custom_css_props).length > 0) {
443+
if (dev) {
444+
const loc = locator(node.start);
445+
if (loc) {
446+
context.state.locations.push([loc.line, loc.column]);
447+
}
448+
}
449+
443450
context.state.template.push(
444451
context.state.metadata.namespace === 'svg'
445452
? '<g><!></g>'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<h2>hello from component</h2>
2+
3+
<style>
4+
h2 {
5+
color: var(--color);
6+
}
7+
</style>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
compileOptions: {
5+
dev: true
6+
},
7+
8+
html: `
9+
<h1>hello</h1>
10+
<svelte-css-wrapper style="display: contents; --color: red;">
11+
<h2 class="svelte-13kae5a">hello from component</h2>
12+
</svelte-css-wrapper>
13+
<p>goodbye</p>
14+
`,
15+
16+
async test({ target, assert }) {
17+
const h1 = target.querySelector('h1');
18+
const h2 = target.querySelector('h2');
19+
const p = target.querySelector('p');
20+
21+
// @ts-expect-error
22+
assert.deepEqual(h1.__svelte_meta.loc, {
23+
file: 'main.svelte',
24+
line: 5,
25+
column: 0
26+
});
27+
28+
// @ts-expect-error
29+
assert.deepEqual(h2.__svelte_meta.loc, {
30+
file: 'Component.svelte',
31+
line: 1,
32+
column: 0
33+
});
34+
35+
// @ts-expect-error
36+
assert.deepEqual(p.__svelte_meta.loc, {
37+
file: 'main.svelte',
38+
line: 7,
39+
column: 0
40+
});
41+
}
42+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import Component from './Component.svelte';
3+
</script>
4+
5+
<h1>hello</h1>
6+
<Component --color="red" />
7+
<p>goodbye</p>

0 commit comments

Comments
 (0)