Skip to content

feat: prevent unparenthesized sequence expressions in attributes #11032

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 1 commit into from
Apr 2, 2024
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/long-humans-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

breaking: prevent unparenthesized sequence expressions in attributes
4 changes: 3 additions & 1 deletion packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ const attributes = {
},
'invalid-let-directive-placement': () => 'let directive at invalid position',
'invalid-style-directive-modifier': () =>
`Invalid 'style:' modifier. Valid modifiers are: 'important'`
`Invalid 'style:' modifier. Valid modifiers are: 'important'`,
'invalid-sequence-expression': () =>
`Sequence expressions are not allowed as attribute/directive values in runes mode, unless wrapped in parentheses`
};

/** @satisfies {Errors} */
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function compile(source, options) {
};
}

const analysis = analyze_component(parsed, combined_options);
const analysis = analyze_component(parsed, source, combined_options);

const result = transform_component(analysis, source, combined_options);
return result;
Expand Down
6 changes: 4 additions & 2 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,11 @@ export function analyze_module(ast, options) {

/**
* @param {import('#compiler').Root} root
* @param {string} source
* @param {import('#compiler').ValidatedCompileOptions} options
* @returns {import('../types.js').ComponentAnalysis}
*/
export function analyze_component(root, options) {
export function analyze_component(root, source, options) {
const scope_root = new ScopeRoot();

const module = js(root.module, scope_root, false, null);
Expand Down Expand Up @@ -396,7 +397,8 @@ export function analyze_component(root, options) {
})
: '',
keyframes: []
}
},
source
};

if (!options.customElement && root.options?.customElement) {
Expand Down
23 changes: 22 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ function validate_component(node, context) {
}

if (attribute.type === 'Attribute') {
if (context.state.analysis.runes && is_expression_attribute(attribute)) {
const expression = attribute.value[0].expression;
if (expression.type === 'SequenceExpression') {
let i = /** @type {number} */ (expression.start);
while (--i > 0) {
const char = context.state.analysis.source[i];
if (char === '(') break; // parenthesized sequence expressions are ok
if (char === '{') error(expression, 'invalid-sequence-expression');
}
}
}

validate_attribute_name(attribute, context);

if (attribute.name === 'slot') {
Expand Down Expand Up @@ -81,12 +93,21 @@ function validate_element(node, context) {

for (const attribute of node.attributes) {
if (attribute.type === 'Attribute') {
const is_expression = is_expression_attribute(attribute);

if (context.state.analysis.runes && is_expression) {
const expression = attribute.value[0].expression;
if (expression.type === 'SequenceExpression') {
error(expression, 'invalid-sequence-expression');
}
}

if (regex_illegal_attribute_character.test(attribute.name)) {
error(attribute, 'invalid-attribute-name', attribute.name);
}

if (attribute.name.startsWith('on') && attribute.name.length > 2) {
if (!is_expression_attribute(attribute)) {
if (!is_expression) {
error(attribute, 'invalid-event-attribute-value');
}

Expand Down
1 change: 1 addition & 0 deletions packages/svelte/src/compiler/phases/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface ComponentAnalysis extends Analysis {
hash: string;
keyframes: string[];
};
source: string;
}

declare module 'estree' {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test } from '../../test';

export default test({
error: {
code: 'invalid-sequence-expression',
message:
'Sequence expressions are not allowed as attribute/directive values in runes mode, unless wrapped in parentheses',
position: [163, 170]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
import Child from './Child.svelte';
let { x, y, z } = $props();
</script>

<!-- allowed -->
<Child foo={(x, y, z)} />

<!-- not allowed -->
<Child foo={x, y, z} />