Skip to content

Commit 0aab322

Browse files
committed
refactor
1 parent 470504c commit 0aab322

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

src/parser/index.ts

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -135,25 +135,7 @@ function parseAsSvelte(
135135
analyzeStoreScope(resultScript.scopeManager!); // for reactive vars
136136

137137
// Add $$xxx variable
138-
const globalScope = resultScript.scopeManager!.globalScope;
139-
for (const $$name of globals) {
140-
if (globalScope.set.has($$name)) continue;
141-
const variable = new Variable();
142-
variable.name = $$name;
143-
(variable as any).scope = globalScope;
144-
globalScope.variables.push(variable);
145-
globalScope.set.set($$name, variable);
146-
globalScope.through = globalScope.through.filter((reference) => {
147-
if (reference.identifier.name === $$name) {
148-
// Links the variable and the reference.
149-
// And this reference is removed from `Scope#through`.
150-
reference.resolved = variable;
151-
addReference(variable.references, reference);
152-
return false;
153-
}
154-
return true;
155-
});
156-
}
138+
addGlobalVariables(resultScript.scopeManager!, globals);
157139

158140
const ast = resultTemplate.ast;
159141

@@ -239,16 +221,30 @@ function parseAsScript(
239221
: parseScript(code, { lang }, parserOptions);
240222

241223
// Add $$xxx variable
242-
const globalScope = resultScript.scopeManager!.globalScope;
243-
for (const $$name of globalsForSvelteScript) {
244-
if (globalScope.set.has($$name)) continue;
224+
addGlobalVariables(resultScript.scopeManager!, globalsForSvelteScript);
225+
226+
resultScript.services = Object.assign(resultScript.services || {}, {
227+
isSvelte: false,
228+
isSvelteScript: true,
229+
});
230+
resultScript.visitorKeys = Object.assign({}, KEYS, resultScript.visitorKeys);
231+
return resultScript as any;
232+
}
233+
234+
function addGlobalVariables(
235+
scopeManager: ScopeManager,
236+
globals: readonly string[],
237+
) {
238+
const globalScope = scopeManager.globalScope;
239+
for (const globalName of globals) {
240+
if (globalScope.set.has(globalName)) continue;
245241
const variable = new Variable();
246-
variable.name = $$name;
242+
variable.name = globalName;
247243
(variable as any).scope = globalScope;
248244
globalScope.variables.push(variable);
249-
globalScope.set.set($$name, variable);
245+
globalScope.set.set(globalName, variable);
250246
globalScope.through = globalScope.through.filter((reference) => {
251-
if (reference.identifier.name === $$name) {
247+
if (reference.identifier.name === globalName) {
252248
// Links the variable and the reference.
253249
// And this reference is removed from `Scope#through`.
254250
reference.resolved = variable;
@@ -258,13 +254,6 @@ function parseAsScript(
258254
return true;
259255
});
260256
}
261-
262-
resultScript.services = Object.assign(resultScript.services || {}, {
263-
isSvelte: false,
264-
isSvelteScript: true,
265-
});
266-
resultScript.visitorKeys = Object.assign({}, KEYS, resultScript.visitorKeys);
267-
return resultScript as any;
268257
}
269258

270259
/** Extract tokens */

0 commit comments

Comments
 (0)