-
Notifications
You must be signed in to change notification settings - Fork 633
Expand file tree
/
Copy pathindex.ts
More file actions
54 lines (46 loc) · 1.86 KB
/
Copy pathindex.ts
File metadata and controls
54 lines (46 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { walk } from 'svelte/compiler';
import { generate } from 'astring';
import type { Ast } from 'svelte/types/compiler/interfaces';
import type { BaseNode, ExportNamedDeclaration, Identifier, VariableDeclaration } from 'estree';
import type { SveltosisComponent } from '../types';
function handleExportNamedDeclaration(json: SveltosisComponent, node: ExportNamedDeclaration) {
const declarations = (node.declaration as VariableDeclaration)?.declarations;
// FIXME(milahu): loop declarations
//for (const declaration of node.declarations) {}
if (declarations?.length) {
const declaration = declarations[0];
const property = (declaration.id as Identifier).name;
const isFunction =
declaration.init?.type === 'FunctionExpression' ||
declaration.init?.type === 'ArrowFunctionExpression';
const exportObject = {
[property]: {
code: generate(node),
isFunction,
},
};
json.exports = { ...json.exports, ...exportObject };
}
}
function handleVariableDeclaration(_json: SveltosisComponent, _node: VariableDeclaration) {
throw new Error('not implemented: VariableDeclaration in svelte <script context="module">');
// TODO(milahu): implement, similar to handleExportNamedDeclaration.
// VariableDeclaration in <script context="module">
// has the meaning of a static variable.
// https://svelte.dev/docs#component-format-script-context-module
//for (const declaration of node.declarations) {}
}
export function parseModule(ast: Ast, json: SveltosisComponent) {
walk(ast.module as BaseNode, {
enter(node) {
switch (node.type) {
case 'ExportNamedDeclaration':
handleExportNamedDeclaration(json, node as ExportNamedDeclaration);
break;
case 'VariableDeclaration':
handleVariableDeclaration(json, node as VariableDeclaration);
break;
}
},
});
}