/
extract-function.ts
114 lines (111 loc) · 3.29 KB
/
extract-function.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Story, Example } from '@component-controls/core';
import traverse, { NodePath } from '@babel/traverse';
import { FunctionDeclaration, VariableDeclarator } from '@babel/types';
import { extractAttributes } from './extract-attributes';
import { extractFunctionParameters } from './extract-function-parameters';
import { followStoryImport } from './follow-story';
import { sourceLocation } from '../misc/source-location';
import { InstrumentOptions } from '../types';
export const extractFunction = (
_options: InstrumentOptions,
{ source, filePath }: { source: string; filePath: string },
path: NodePath,
declaration: VariableDeclarator | FunctionDeclaration,
name: string,
template: Example | undefined,
locals: Record<string, any>,
): Story | undefined => {
if (declaration.type === 'VariableDeclarator' && declaration.init) {
switch (declaration.init.type) {
case 'ArrowFunctionExpression': {
const el = declaration.init.body;
const story: Story = {
loc: sourceLocation(el.loc),
name,
id: name,
};
traverse(path.node, extractFunctionParameters(story), path.scope);
return story;
}
case 'Identifier': {
return followStoryImport(
name,
declaration.init.name,
filePath,
source,
_options,
);
}
case 'ObjectExpression': {
const attributes = extractAttributes(declaration.init);
const story: Story = {
name,
id: name,
...attributes,
};
if (template) {
story.loc = template.loc;
story.arguments = template.arguments;
}
return story;
}
case 'CallExpression': {
//template.bind
const callee: any = declaration.init.callee;
if (callee.property?.name === 'bind') {
const calleeFn = callee.object;
if (calleeFn?.name) {
const template = locals[calleeFn.name];
if (template) {
const story: Story = {
loc: template.loc,
name,
id: name,
arguments: template.arguments,
};
return story;
}
}
}
}
}
} else if (declaration.type === 'FunctionDeclaration') {
const story: Story = {
loc: sourceLocation(declaration.body.loc),
name,
id: name,
};
traverse(path.node, extractFunctionParameters(story), path.scope);
return story;
}
return undefined;
};
export const extractVarFunction = (
_options: InstrumentOptions,
{ source, filePath }: { source: string; filePath: string },
path: any,
template: Example | undefined,
locals: Record<string, any>,
): Story | undefined => {
const { declarations } = path.node;
if (Array.isArray(declarations) && declarations.length > 0) {
const declaration = declarations[0];
if (declaration) {
const name = declaration.id.name;
const story = extractFunction(
_options,
{ source, filePath },
path,
declaration,
name,
template,
locals,
);
if (story && story.name) {
return story;
}
}
}
return undefined;
};