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

fix: default values in object destructuring #14554

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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/clean-planets-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: handle default values in object destructuring within "each" blocks when using characters like "}" and "]"
6 changes: 6 additions & 0 deletions documentation/docs/98-reference/.generated/compile-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,12 @@ Unexpected end of input
'%word%' is a reserved word in JavaScript and cannot be used here
```

### unterminated_string_constant

```
Unterminated string constant
```

### void_element_invalid_content

```
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/messages/compile-errors/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ See https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-ele

> '%word%' is a reserved word in JavaScript and cannot be used here

## unterminated_string_constant

> Unterminated string constant

## void_element_invalid_content

> Void elements cannot have children or closing tags
9 changes: 9 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,15 @@ export function unexpected_reserved_word(node, word) {
e(node, "unexpected_reserved_word", `'${word}' is a reserved word in JavaScript and cannot be used here`);
}

/**
* Unterminated string constant
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function unterminated_string_constant(node) {
e(node, "unterminated_string_constant", "Unterminated string constant");
}

/**
* Void elements cannot have children or closing tags
* @param {null | number | NodeLike} node
Expand Down
117 changes: 93 additions & 24 deletions packages/svelte/src/compiler/phases/1-parse/read/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { parse_expression_at } from '../acorn.js';
import { regex_not_newline_characters } from '../../patterns.js';
import * as e from '../../../errors.js';
import { locator } from '../../../state.js';
import read_expression from './expression.js';
import { is_back_quote, is_quote } from '../utils/quote.js';

/**
* @param {Parser} parser
Expand Down Expand Up @@ -41,33 +43,11 @@ export default function read_pattern(parser) {
};
}

if (!is_bracket_open(code)) {
e.expected_pattern(i);
}

const bracket_stack = [code];
i += code <= 0xffff ? 1 : 2;

while (i < parser.template.length) {
const code = full_char_code_at(parser.template, i);
if (is_bracket_open(code)) {
bracket_stack.push(code);
} else if (is_bracket_close(code)) {
const popped = /** @type {number} */ (bracket_stack.pop());
if (!is_bracket_pair(popped, code)) {
e.expected_token(i, String.fromCharCode(/** @type {number} */ (get_bracket_close(popped))));
}
if (bracket_stack.length === 0) {
i += code <= 0xffff ? 1 : 2;
break;
}
}
i += code <= 0xffff ? 1 : 2;
}

i = read_expression_length(parser, start);
parser.index = i;

const pattern_string = parser.template.slice(start, i);

try {
// the length of the `space_with_newline` has to be start - 1
// because we added a `(` in front of the pattern_string,
Expand Down Expand Up @@ -97,6 +77,95 @@ export default function read_pattern(parser) {
}
}

/**
* @param {Parser} parser
* @param {number} start
*/
function read_expression_length(parser, start) {
let i = start;
const code = full_char_code_at(parser.template, i);

if (!is_bracket_open(code)) {
e.expected_pattern(i);
}

const bracket_stack = [code];
i += code <= 0xffff ? 1 : 2;

while (i < parser.template.length) {
let code = full_char_code_at(parser.template, i);
if (is_quote(code)) {
i = read_string_length(parser, i, code);
} else {
if (is_bracket_open(code)) {
bracket_stack.push(code);
} else if (is_bracket_close(code)) {
const popped = /** @type {number} */ (bracket_stack.pop());
if (!is_bracket_pair(popped, code)) {
e.expected_token(
i,
String.fromCharCode(/** @type {number} */ (get_bracket_close(popped)))
);
}
if (bracket_stack.length === 0) {
i += code <= 0xffff ? 1 : 2;
break;
}
}
i += code <= 0xffff ? 1 : 2;
}
}

return i;
}

/**
* @param {Parser} parser
* @param {number} start
* @param {number} quote
*/
function read_string_length(parser, start, quote) {
let i = start;
i += quote <= 0xffff ? 1 : 2;

const BACKSLASH = '\\'.charCodeAt(0);
const DOLAR = '$'.charCodeAt(0);
const LEFT_BRACKET = '{'.charCodeAt(0);

let is_escaped = false;
while (i < parser.template.length) {
const code = full_char_code_at(parser.template, i);
if (!is_escaped && code === quote) {
break;
}

if (!is_escaped && code === BACKSLASH) {
is_escaped = true;
} else if (is_escaped) {
is_escaped = false;
}

if (
i < parser.template.length - 1 &&
is_back_quote(quote) &&
code === DOLAR &&
full_char_code_at(parser.template, i + 1) === LEFT_BRACKET
) {
i++;
i = read_expression_length(parser, i);
} else {
i += code <= 0xffff ? 1 : 2;
}
}

const code = full_char_code_at(parser.template, i);
if (code !== quote) {
e.unterminated_string_constant(start);
}

return i + (code <= 0xffff ? 1 : 2);
}

/**
* @param {Parser} parser
* @returns {any}
Expand Down
13 changes: 13 additions & 0 deletions packages/svelte/src/compiler/phases/1-parse/utils/quote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const SINGLE_QUOTE = "'".charCodeAt(0);
const DOUBLE_QUOTE = '"'.charCodeAt(0);
const BACK_QUOTE = '`'.charCodeAt(0);

/** @param {number} code */
export function is_quote(code) {
return code === SINGLE_QUOTE || code === DOUBLE_QUOTE || code === BACK_QUOTE;
}

/** @param {number} code */
export function is_back_quote(code) {
return code === BACK_QUOTE;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{#each x as { y = 'z' }}{/each}

{#each x as { y = '{' }}{/each}

{#each x as { y = ']' }}{/each}

{#each x as { y = `${`"`}` }}{/each}

{#each x as { y = `${`John`}` }}{/each}
Loading
Loading