|
5 | 5 | import { walk } from 'zimmerframe';
|
6 | 6 | import * as b from '../../../utils/builders.js';
|
7 | 7 | import { set_scope } from '../../scope.js';
|
8 |
| -import { template_visitors } from './visitors/template.js'; |
9 |
| -import { global_visitors } from './visitors/global.js'; |
10 |
| -import { javascript_visitors } from './visitors/javascript.js'; |
11 |
| -import { javascript_visitors_runes } from './visitors/javascript-runes.js'; |
12 |
| -import { javascript_visitors_legacy } from './visitors/javascript-legacy.js'; |
13 | 8 | import { serialize_get_binding } from './utils.js';
|
14 | 9 | import { render_stylesheet } from '../css/index.js';
|
15 | 10 | import { dev, filename } from '../../../state.js';
|
| 11 | +import { AnimateDirective } from './visitors/AnimateDirective.js'; |
| 12 | +import { ArrowFunctionExpression } from './visitors/ArrowFunctionExpression.js'; |
| 13 | +import { AssignmentExpression } from './visitors/AssignmentExpression.js'; |
| 14 | +import { Attribute } from './visitors/Attribute.js'; |
| 15 | +import { AwaitBlock } from './visitors/AwaitBlock.js'; |
| 16 | +import { BinaryExpression } from './visitors/BinaryExpression.js'; |
| 17 | +import { BindDirective } from './visitors/BindDirective.js'; |
| 18 | +import { BreakStatement } from './visitors/BreakStatement.js'; |
| 19 | +import { CallExpression } from './visitors/CallExpression.js'; |
| 20 | +import { ClassBody } from './visitors/ClassBody.js'; |
| 21 | +import { Comment } from './visitors/Comment.js'; |
| 22 | +import { Component } from './visitors/Component.js'; |
| 23 | +import { ConstTag } from './visitors/ConstTag.js'; |
| 24 | +import { DebugTag } from './visitors/DebugTag.js'; |
| 25 | +import { EachBlock } from './visitors/EachBlock.js'; |
| 26 | +import { ExportNamedDeclaration } from './visitors/ExportNamedDeclaration.js'; |
| 27 | +import { ExpressionStatement } from './visitors/ExpressionStatement.js'; |
| 28 | +import { Fragment } from './visitors/Fragment.js'; |
| 29 | +import { FunctionDeclaration } from './visitors/FunctionDeclaration.js'; |
| 30 | +import { FunctionExpression } from './visitors/FunctionExpression.js'; |
| 31 | +import { HtmlTag } from './visitors/HtmlTag.js'; |
| 32 | +import { Identifier } from './visitors/Identifier.js'; |
| 33 | +import { IfBlock } from './visitors/IfBlock.js'; |
| 34 | +import { ImportDeclaration } from './visitors/ImportDeclaration.js'; |
| 35 | +import { KeyBlock } from './visitors/KeyBlock.js'; |
| 36 | +import { LabeledStatement } from './visitors/LabeledStatement.js'; |
| 37 | +import { LetDirective } from './visitors/LetDirective.js'; |
| 38 | +import { MemberExpression } from './visitors/MemberExpression.js'; |
| 39 | +import { OnDirective } from './visitors/OnDirective.js'; |
| 40 | +import { RegularElement } from './visitors/RegularElement.js'; |
| 41 | +import { RenderTag } from './visitors/RenderTag.js'; |
| 42 | +import { SlotElement } from './visitors/SlotElement.js'; |
| 43 | +import { SnippetBlock } from './visitors/SnippetBlock.js'; |
| 44 | +import { SpreadAttribute } from './visitors/SpreadAttribute.js'; |
| 45 | +import { SvelteBody } from './visitors/SvelteBody.js'; |
| 46 | +import { SvelteComponent } from './visitors/SvelteComponent.js'; |
| 47 | +import { SvelteDocument } from './visitors/SvelteDocument.js'; |
| 48 | +import { SvelteElement } from './visitors/SvelteElement.js'; |
| 49 | +import { SvelteFragment } from './visitors/SvelteFragment.js'; |
| 50 | +import { SvelteHead } from './visitors/SvelteHead.js'; |
| 51 | +import { SvelteSelf } from './visitors/SvelteSelf.js'; |
| 52 | +import { SvelteWindow } from './visitors/SvelteWindow.js'; |
| 53 | +import { TitleElement } from './visitors/TitleElement.js'; |
| 54 | +import { TransitionDirective } from './visitors/TransitionDirective.js'; |
| 55 | +import { UpdateExpression } from './visitors/UpdateExpression.js'; |
| 56 | +import { UseDirective } from './visitors/UseDirective.js'; |
| 57 | +import { VariableDeclaration } from './visitors/VariableDeclaration.js'; |
| 58 | + |
| 59 | +/** @type {Visitors} */ |
| 60 | +const visitors = { |
| 61 | + _: set_scope, |
| 62 | + AnimateDirective, |
| 63 | + ArrowFunctionExpression, |
| 64 | + AssignmentExpression, |
| 65 | + Attribute, |
| 66 | + AwaitBlock, |
| 67 | + BinaryExpression, |
| 68 | + BindDirective, |
| 69 | + BreakStatement, |
| 70 | + CallExpression, |
| 71 | + ClassBody, |
| 72 | + Comment, |
| 73 | + Component, |
| 74 | + ConstTag, |
| 75 | + DebugTag, |
| 76 | + EachBlock, |
| 77 | + ExportNamedDeclaration, |
| 78 | + ExpressionStatement, |
| 79 | + Fragment, |
| 80 | + FunctionDeclaration, |
| 81 | + FunctionExpression, |
| 82 | + HtmlTag, |
| 83 | + Identifier, |
| 84 | + IfBlock, |
| 85 | + ImportDeclaration, |
| 86 | + KeyBlock, |
| 87 | + LabeledStatement, |
| 88 | + LetDirective, |
| 89 | + MemberExpression, |
| 90 | + OnDirective, |
| 91 | + RegularElement, |
| 92 | + RenderTag, |
| 93 | + SlotElement, |
| 94 | + SnippetBlock, |
| 95 | + SpreadAttribute, |
| 96 | + SvelteBody, |
| 97 | + SvelteComponent, |
| 98 | + SvelteDocument, |
| 99 | + SvelteElement, |
| 100 | + SvelteFragment, |
| 101 | + SvelteHead, |
| 102 | + SvelteSelf, |
| 103 | + SvelteWindow, |
| 104 | + TitleElement, |
| 105 | + TransitionDirective, |
| 106 | + UpdateExpression, |
| 107 | + UseDirective, |
| 108 | + VariableDeclaration |
| 109 | +}; |
16 | 110 |
|
17 | 111 | /**
|
18 |
| - * This function ensures visitor sets don't accidentally clobber each other |
19 |
| - * @param {...Visitors} array |
20 |
| - * @returns {Visitors} |
21 |
| - */ |
22 |
| -function combine_visitors(...array) { |
23 |
| - /** @type {Record<string, any>} */ |
24 |
| - const visitors = {}; |
25 |
| - |
26 |
| - for (const member of array) { |
27 |
| - for (const key in member) { |
28 |
| - if (visitors[key]) { |
29 |
| - throw new Error(`Duplicate visitor: ${key}`); |
30 |
| - } |
31 |
| - |
32 |
| - // @ts-ignore |
33 |
| - visitors[key] = member[key]; |
34 |
| - } |
35 |
| - } |
36 |
| - |
37 |
| - return visitors; |
38 |
| -} |
39 |
| - |
40 |
| -/** |
41 |
| - * @param {string} source |
42 | 112 | * @param {ComponentAnalysis} analysis
|
43 | 113 | * @param {ValidatedCompileOptions} options
|
44 | 114 | * @returns {ESTree.Program}
|
45 | 115 | */
|
46 |
| -export function client_component(source, analysis, options) { |
| 116 | +export function client_component(analysis, options) { |
47 | 117 | /** @type {ComponentClientTransformState} */
|
48 | 118 | const state = {
|
49 | 119 | analysis,
|
50 | 120 | options,
|
51 | 121 | scope: analysis.module.scope,
|
52 |
| - scopes: analysis.template.scopes, |
| 122 | + scopes: analysis.module.scopes, |
| 123 | + is_instance: false, |
53 | 124 | hoisted: [b.import_all('$', 'svelte/internal/client')],
|
54 | 125 | node: /** @type {any} */ (null), // populated by the root node
|
55 | 126 | legacy_reactive_statements: new Map(),
|
@@ -78,57 +149,25 @@ export function client_component(source, analysis, options) {
|
78 | 149 | };
|
79 | 150 |
|
80 | 151 | const module = /** @type {ESTree.Program} */ (
|
81 |
| - walk( |
82 |
| - /** @type {SvelteNode} */ (analysis.module.ast), |
83 |
| - state, |
84 |
| - combine_visitors( |
85 |
| - set_scope(analysis.module.scopes), |
86 |
| - global_visitors, |
87 |
| - // @ts-expect-error TODO |
88 |
| - javascript_visitors, |
89 |
| - analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy |
90 |
| - ) |
91 |
| - ) |
| 152 | + walk(/** @type {SvelteNode} */ (analysis.module.ast), state, visitors) |
92 | 153 | );
|
93 | 154 |
|
94 |
| - const instance_state = { ...state, scope: analysis.instance.scope }; |
| 155 | + const instance_state = { |
| 156 | + ...state, |
| 157 | + scope: analysis.instance.scope, |
| 158 | + scopes: analysis.instance.scopes, |
| 159 | + is_instance: true |
| 160 | + }; |
| 161 | + |
95 | 162 | const instance = /** @type {ESTree.Program} */ (
|
96 |
| - walk( |
97 |
| - /** @type {SvelteNode} */ (analysis.instance.ast), |
98 |
| - instance_state, |
99 |
| - combine_visitors( |
100 |
| - set_scope(analysis.instance.scopes), |
101 |
| - global_visitors, |
102 |
| - // @ts-expect-error TODO |
103 |
| - javascript_visitors, |
104 |
| - analysis.runes ? javascript_visitors_runes : javascript_visitors_legacy, |
105 |
| - { |
106 |
| - ImportDeclaration(node) { |
107 |
| - state.hoisted.push(node); |
108 |
| - return b.empty; |
109 |
| - }, |
110 |
| - ExportNamedDeclaration(node, context) { |
111 |
| - if (node.declaration) { |
112 |
| - return context.visit(node.declaration); |
113 |
| - } |
114 |
| - |
115 |
| - return b.empty; |
116 |
| - } |
117 |
| - } |
118 |
| - ) |
119 |
| - ) |
| 163 | + walk(/** @type {SvelteNode} */ (analysis.instance.ast), instance_state, visitors) |
120 | 164 | );
|
121 | 165 |
|
122 | 166 | const template = /** @type {ESTree.Program} */ (
|
123 | 167 | walk(
|
124 | 168 | /** @type {SvelteNode} */ (analysis.template.ast),
|
125 |
| - { ...state, scope: analysis.instance.scope }, |
126 |
| - combine_visitors( |
127 |
| - set_scope(analysis.template.scopes), |
128 |
| - global_visitors, |
129 |
| - // @ts-expect-error TODO |
130 |
| - template_visitors |
131 |
| - ) |
| 169 | + { ...state, scope: analysis.instance.scope, scopes: analysis.template.scopes }, |
| 170 | + visitors |
132 | 171 | )
|
133 | 172 | );
|
134 | 173 |
|
@@ -589,17 +628,7 @@ export function client_module(analysis, options) {
|
589 | 628 | };
|
590 | 629 |
|
591 | 630 | const module = /** @type {ESTree.Program} */ (
|
592 |
| - walk( |
593 |
| - /** @type {SvelteNode} */ (analysis.module.ast), |
594 |
| - state, |
595 |
| - combine_visitors( |
596 |
| - set_scope(analysis.module.scopes), |
597 |
| - global_visitors, |
598 |
| - // @ts-expect-error |
599 |
| - javascript_visitors, |
600 |
| - javascript_visitors_runes |
601 |
| - ) |
602 |
| - ) |
| 631 | + walk(/** @type {SvelteNode} */ (analysis.module.ast), state, visitors) |
603 | 632 | );
|
604 | 633 |
|
605 | 634 | return {
|
|
0 commit comments