|
1 | 1 | /** @import { VariableDeclaration, VariableDeclarator, Expression, CallExpression, Pattern, Identifier } from 'estree' */ |
2 | 2 | /** @import { Binding } from '#compiler' */ |
3 | 3 | /** @import { Context } from '../types.js' */ |
| 4 | +/** @import { ComponentAnalysis } from '../../../types.js' */ |
4 | 5 | /** @import { Scope } from '../../../scope.js' */ |
5 | 6 | import { build_fallback, extract_paths } from '../../../../utils/ast.js'; |
6 | 7 | import * as b from '#compiler/builders'; |
@@ -50,23 +51,25 @@ export function VariableDeclaration(node, context) { |
50 | 51 | } |
51 | 52 | } |
52 | 53 | }); |
| 54 | + const to_remove = [b.prop('init', b.id('$$events'), b.id('$$events'))]; |
| 55 | + // we don't want to spread away `$$slots` if `$$slots` is used in the component |
| 56 | + // otherwise there will be a reference to `$$slots` in the component already |
| 57 | + // the typecast is fine since `$props` can only be used in a component |
| 58 | + if (!(/** @type {ComponentAnalysis} */ (context.state.analysis).uses_slots)) { |
| 59 | + to_remove.unshift(b.prop('init', b.id('$$slots'), b.id('$$slots'))); |
| 60 | + } |
53 | 61 | if (id.type === 'ObjectPattern' && has_rest) { |
54 | 62 | // If a rest pattern is used within an object pattern, we need to ensure we don't expose $$slots or $$events |
55 | 63 | id.properties.splice( |
56 | 64 | id.properties.length - 1, |
57 | 65 | 0, |
58 | 66 | // @ts-ignore |
59 | | - b.prop('init', b.id('$$slots'), b.id('$$slots')), |
60 | | - b.prop('init', b.id('$$events'), b.id('$$events')) |
| 67 | + ...to_remove |
61 | 68 | ); |
62 | 69 | } else if (id.type === 'Identifier') { |
63 | 70 | // If $props is referenced as an identifier, we need to ensure we don't expose $$slots or $$events as properties |
64 | 71 | // on the identifier reference |
65 | | - id = b.object_pattern([ |
66 | | - b.prop('init', b.id('$$slots'), b.id('$$slots')), |
67 | | - b.prop('init', b.id('$$events'), b.id('$$events')), |
68 | | - b.rest(b.id(id.name)) |
69 | | - ]); |
| 72 | + id = b.object_pattern([...to_remove, b.rest(b.id(id.name))]); |
70 | 73 | } |
71 | 74 | declarations.push( |
72 | 75 | b.declarator(/** @type {Pattern} */ (context.visit(id)), b.id('$$props')) |
|
0 commit comments