From 320ebd24d8857570b0c180752765fb1580590367 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:54:27 +0100 Subject: [PATCH] fix: bump `is-reference` dependency to fix `import.meta` bug (#14286) closes #14234 --- .changeset/shaggy-flowers-kneel.md | 5 ++++ packages/svelte/package.json | 2 +- packages/svelte/src/compiler/migrate/index.js | 3 ++ .../src/compiler/phases/2-analyze/index.js | 4 +++ .../visitors/ExportNamedDeclaration.js | 2 ++ .../2-analyze/visitors/ExportSpecifier.js | 15 +++++++--- .../2-analyze/visitors/ImportDeclaration.js | 5 ++-- packages/svelte/src/compiler/utils/ast.js | 11 +++++-- .../svelte/src/compiler/utils/builders.js | 5 ++-- pnpm-lock.yaml | 29 +++++++++++-------- 10 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 .changeset/shaggy-flowers-kneel.md diff --git a/.changeset/shaggy-flowers-kneel.md b/.changeset/shaggy-flowers-kneel.md new file mode 100644 index 000000000000..fb0a041fbcb9 --- /dev/null +++ b/.changeset/shaggy-flowers-kneel.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: bump `is-reference` dependency to fix `import.meta` bug diff --git a/packages/svelte/package.json b/packages/svelte/package.json index 760272680e37..608c8d466d28 100644 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -144,7 +144,7 @@ "axobject-query": "^4.1.0", "esm-env": "^1.0.0", "esrap": "^1.2.2", - "is-reference": "^3.0.2", + "is-reference": "^3.0.3", "locate-character": "^3.0.0", "magic-string": "^0.30.11", "zimmerframe": "^1.1.2" diff --git a/packages/svelte/src/compiler/migrate/index.js b/packages/svelte/src/compiler/migrate/index.js index 9db09158a12c..88f9bbf0eed6 100644 --- a/packages/svelte/src/compiler/migrate/index.js +++ b/packages/svelte/src/compiler/migrate/index.js @@ -507,6 +507,7 @@ const instance_script = { for (let specifier of node.specifiers) { if ( specifier.type === 'ImportSpecifier' && + specifier.imported.type === 'Identifier' && ['beforeUpdate', 'afterUpdate'].includes(specifier.imported.name) ) { const references = state.scope.references.get(specifier.local.name); @@ -544,6 +545,8 @@ const instance_script = { let count_removed = 0; for (const specifier of node.specifiers) { + if (specifier.local.type !== 'Identifier') continue; + const binding = state.scope.get(specifier.local.name); if (binding?.kind === 'bindable_prop') { state.str.remove( diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js index 9e4f72a66d7a..a96652d60b0b 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/index.js +++ b/packages/svelte/src/compiler/phases/2-analyze/index.js @@ -474,6 +474,10 @@ export function analyze_component(root, source, options) { } } else { for (const specifier of node.specifiers) { + if (specifier.local.type !== 'Identifier' || specifier.exported.type !== 'Identifier') { + continue; + } + const binding = instance.scope.get(specifier.local.name); if ( diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/ExportNamedDeclaration.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/ExportNamedDeclaration.js index a9e8a05c96c7..784d2fdd881e 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/ExportNamedDeclaration.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/ExportNamedDeclaration.js @@ -60,6 +60,8 @@ export function ExportNamedDeclaration(node, context) { if (!context.state.ast_type /* .svelte.js module */ || context.state.ast_type === 'module') { for (const specified of node.specifiers) { + if (specified.local.type !== 'Identifier') continue; + const binding = context.state.scope.get(specified.local.name); if (!binding) continue; diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/ExportSpecifier.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/ExportSpecifier.js index 27eb39acbfe2..d0d1ccf93211 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/ExportSpecifier.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/ExportSpecifier.js @@ -9,18 +9,25 @@ import * as e from '../../../errors.js'; * @param {Context} context */ export function ExportSpecifier(node, context) { + const local_name = + node.local.type === 'Identifier' ? node.local.name : /** @type {string} */ (node.local.value); + const exported_name = + node.exported.type === 'Identifier' + ? node.exported.name + : /** @type {string} */ (node.exported.value); + if (context.state.ast_type === 'instance') { if (context.state.analysis.runes) { context.state.analysis.exports.push({ - name: node.local.name, - alias: node.exported.name + name: local_name, + alias: exported_name }); - const binding = context.state.scope.get(node.local.name); + const binding = context.state.scope.get(local_name); if (binding) binding.reassigned = binding.updated = true; } } else { - validate_export(node, context.state.scope, node.local.name); + validate_export(node, context.state.scope, local_name); } } diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/ImportDeclaration.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/ImportDeclaration.js index eb57fee26451..d46218ac64c1 100644 --- a/packages/svelte/src/compiler/phases/2-analyze/visitors/ImportDeclaration.js +++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/ImportDeclaration.js @@ -18,8 +18,9 @@ export function ImportDeclaration(node, context) { for (const specifier of node.specifiers) { if (specifier.type === 'ImportSpecifier') { if ( - specifier.imported.name === 'beforeUpdate' || - specifier.imported.name === 'afterUpdate' + specifier.imported.type === 'Identifier' && + (specifier.imported.name === 'beforeUpdate' || + specifier.imported.name === 'afterUpdate') ) { e.runes_mode_invalid_import(specifier, specifier.imported.name); } diff --git a/packages/svelte/src/compiler/utils/ast.js b/packages/svelte/src/compiler/utils/ast.js index 9aff7546be61..cc8339f16be7 100644 --- a/packages/svelte/src/compiler/utils/ast.js +++ b/packages/svelte/src/compiler/utils/ast.js @@ -433,7 +433,11 @@ export function is_simple_expression(node) { } if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') { - return is_simple_expression(node.left) && is_simple_expression(node.right); + return ( + node.left.type !== 'PrivateIdentifier' && + is_simple_expression(node.left) && + is_simple_expression(node.right) + ); } return false; @@ -475,7 +479,10 @@ export function is_expression_async(expression) { case 'AssignmentExpression': case 'BinaryExpression': case 'LogicalExpression': { - return is_expression_async(expression.left) || is_expression_async(expression.right); + return ( + (expression.left.type !== 'PrivateIdentifier' && is_expression_async(expression.left)) || + is_expression_async(expression.right) + ); } case 'CallExpression': case 'NewExpression': { diff --git a/packages/svelte/src/compiler/utils/builders.js b/packages/svelte/src/compiler/utils/builders.js index fc1cdf4adb41..ecb595d74dbd 100644 --- a/packages/svelte/src/compiler/utils/builders.js +++ b/packages/svelte/src/compiler/utils/builders.js @@ -350,7 +350,7 @@ export function prop(kind, key, value, computed = false) { * @returns {ESTree.PropertyDefinition} */ export function prop_def(key, value, computed = false, is_static = false) { - return { type: 'PropertyDefinition', key, value, computed, static: is_static, decorators: [] }; + return { type: 'PropertyDefinition', key, value, computed, static: is_static }; } /** @@ -551,8 +551,7 @@ export function method(kind, key, params, body, computed = false, is_static = fa kind, value: function_builder(null, params, block(body)), computed, - static: is_static, - decorators: [] + static: is_static }; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1f33a207fc04..83e4522b2f80 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -67,7 +67,7 @@ importers: version: 1.5.0 '@types/estree': specifier: ^1.0.5 - version: 1.0.5 + version: 1.0.6 acorn: specifier: ^8.12.1 version: 8.12.1 @@ -87,8 +87,8 @@ importers: specifier: ^1.2.2 version: 1.2.2 is-reference: - specifier: ^3.0.2 - version: 3.0.2 + specifier: ^3.0.3 + version: 3.0.3 locate-character: specifier: ^3.0.0 version: 3.0.0 @@ -916,6 +916,9 @@ packages: '@types/estree@1.0.5': resolution: {integrity: sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==} + '@types/estree@1.0.6': + resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==} + '@types/json-schema@7.0.15': resolution: {integrity: sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==} @@ -1775,8 +1778,8 @@ packages: is-reference@1.2.1: resolution: {integrity: sha512-U82MsXXiFIrjCK4otLT+o2NA2Cd2g5MLoOVXUZjIOhLurrRxpEXzI8O0KZHr3IjLvlAH1kTPYSuqer5T9ZVBKQ==} - is-reference@3.0.2: - resolution: {integrity: sha512-v3rht/LgVcsdZa3O2Nqs+NMowLOxeOm7Ay9+/ARQ2F+qEoANRcqrjAZKGN0v8ymUetZGgkp26LTnGT7H0Qo9Pg==} + is-reference@3.0.3: + resolution: {integrity: sha512-ixkJoqQvAP88E6wLydLGGqCJsrFUnqoH6HnaczB8XmDH1oaWU+xxdptvikTgaEhtZ53Ky6YXiBuUI2WXLMCwjw==} is-stream@3.0.0: resolution: {integrity: sha512-LnQR4bZ9IADDRSkvpqMGvt/tEJWclzklNgSw48V5EAaAeDd6qGvN8ei6k5p0tvxSR171VmGyHuTiAOfxAbr8kA==} @@ -3468,7 +3471,7 @@ snapshots: '@rollup/pluginutils@5.1.0(rollup@4.22.4)': dependencies: - '@types/estree': 1.0.5 + '@types/estree': 1.0.6 estree-walker: 2.0.2 picomatch: 2.3.1 optionalDependencies: @@ -3611,11 +3614,13 @@ snapshots: '@types/eslint@8.56.12': dependencies: - '@types/estree': 1.0.5 + '@types/estree': 1.0.6 '@types/json-schema': 7.0.15 '@types/estree@1.0.5': {} + '@types/estree@1.0.6': {} + '@types/json-schema@7.0.15': {} '@types/node@12.20.55': {} @@ -4243,7 +4248,7 @@ snapshots: esrap@1.2.2: dependencies: '@jridgewell/sourcemap-codec': 1.5.0 - '@types/estree': 1.0.5 + '@types/estree': 1.0.6 esrecurse@4.3.0: dependencies: @@ -4255,7 +4260,7 @@ snapshots: estree-walker@3.0.3: dependencies: - '@types/estree': 1.0.5 + '@types/estree': 1.0.6 esutils@2.0.3: {} @@ -4556,11 +4561,11 @@ snapshots: is-reference@1.2.1: dependencies: - '@types/estree': 1.0.5 + '@types/estree': 1.0.6 - is-reference@3.0.2: + is-reference@3.0.3: dependencies: - '@types/estree': 1.0.5 + '@types/estree': 1.0.6 is-stream@3.0.0: {}