From 19f84ca73045ffdc44cc3174b7042d471235395c Mon Sep 17 00:00:00 2001 From: Yuichiro Yamashita Date: Thu, 16 Nov 2023 06:21:36 +0900 Subject: [PATCH] fix: Added missing context of `svelte.js` for validation (#9394) * fix * add changeset * Update .changeset/eighty-bikes-camp.md Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> * Update .changeset/eighty-bikes-camp.md --------- Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Rich Harris --- .changeset/eighty-bikes-camp.md | 5 +++ .../compiler/phases/2-analyze/validation.js | 45 ++++++++++--------- 2 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 .changeset/eighty-bikes-camp.md diff --git a/.changeset/eighty-bikes-camp.md b/.changeset/eighty-bikes-camp.md new file mode 100644 index 0000000000..273e6ad996 --- /dev/null +++ b/.changeset/eighty-bikes-camp.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: handle private fields in `class` in `.svelte.js` files diff --git a/packages/svelte/src/compiler/phases/2-analyze/validation.js b/packages/svelte/src/compiler/phases/2-analyze/validation.js index d1688a1d68..34be46269f 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/validation.js +++ b/packages/svelte/src/compiler/phases/2-analyze/validation.js @@ -546,6 +546,28 @@ export const validation_runes_js = { }, UpdateExpression(node, { state }) { validate_assignment(node, node.argument, state); + }, + ClassBody(node, context) { + /** @type {string[]} */ + const private_derived_state = []; + + for (const definition of node.body) { + if ( + definition.type === 'PropertyDefinition' && + definition.key.type === 'PrivateIdentifier' && + definition.value?.type === 'CallExpression' + ) { + const rune = get_rune(definition.value, context.state.scope); + if (rune === '$derived') { + private_derived_state.push(definition.key.name); + } + } + } + + context.next({ + ...context.state, + private_derived_state + }); } }; @@ -688,26 +710,5 @@ export const validation_runes = merge(validation, a11y_validators, { } } }, - ClassBody(node, context) { - /** @type {string[]} */ - const private_derived_state = []; - - for (const definition of node.body) { - if ( - definition.type === 'PropertyDefinition' && - definition.key.type === 'PrivateIdentifier' && - definition.value?.type === 'CallExpression' - ) { - const rune = get_rune(definition.value, context.state.scope); - if (rune === '$derived') { - private_derived_state.push(definition.key.name); - } - } - } - - context.next({ - ...context.state, - private_derived_state - }); - } + ClassBody: validation_runes_js.ClassBody });