/
traverse-helpers.js
219 lines (193 loc) · 6.49 KB
/
traverse-helpers.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { storyNameFromExport } from '@storybook/router/utils';
import { handleADD, handleSTORYOF, patchNode, handleExportedName } from './parse-helpers';
const estraverse = require('estraverse');
export function splitSTORYOF(ast, source) {
let lastIndex = 0;
const parts = [source];
estraverse.traverse(ast, {
fallback: 'iteration',
enter: node => {
patchNode(node);
if (node.type === 'CallExpression') {
lastIndex = handleSTORYOF(node, parts, source, lastIndex);
}
},
});
return parts;
}
export function splitExports(ast, source) {
const parts = [];
let lastIndex = 0;
estraverse.traverse(ast, {
fallback: 'iteration',
enter: node => {
patchNode(node);
if (
node.type === 'ExportNamedDeclaration' &&
node.declaration &&
node.declaration.declarations &&
node.declaration.declarations.length === 1 &&
node.declaration.declarations[0].type === 'VariableDeclarator' &&
node.declaration.declarations[0].id &&
node.declaration.declarations[0].id.name &&
node.declaration.declarations[0].init &&
['CallExpression', 'ArrowFunctionExpression', 'FunctionExpression'].includes(
node.declaration.declarations[0].init.type
)
) {
const functionNode = node.declaration.declarations[0].init;
parts.push(source.substring(lastIndex, functionNode.start - 1));
parts.push(source.substring(functionNode.start, functionNode.end));
lastIndex = functionNode.end;
}
},
});
if (source.length > lastIndex + 1) parts.push(source.substring(lastIndex + 1));
if (parts.length === 1) return [source];
return parts;
}
export function findAddsMap(ast, storiesOfIdentifiers) {
const addsMap = {};
const idsToFrameworks = {};
estraverse.traverse(ast, {
fallback: 'iteration',
enter: (node, parent) => {
patchNode(node);
if (node.type === 'MemberExpression') {
const { toAdd, idToFramework } = handleADD(node, parent, storiesOfIdentifiers);
Object.assign(addsMap, toAdd);
Object.assign(idsToFrameworks, idToFramework);
}
},
});
return { addsMap, idsToFrameworks };
}
export function findExportsMap(ast) {
const addsMap = {};
const idsToFrameworks = {};
const program = (ast && ast.program) || ast;
const metaDeclaration =
program &&
program.body &&
program.body.find(
d =>
d.type === 'ExportDefaultDeclaration' &&
d.declaration.type === 'ObjectExpression' &&
(d.declaration.properties || []).length
);
const titleProperty =
metaDeclaration &&
metaDeclaration.declaration &&
metaDeclaration.declaration.properties.find(p => p.key && p.key.name === 'title');
if (!titleProperty) {
return { addsMap, idsToFrameworks };
}
const titleValue = titleProperty.value;
let title;
if (titleValue.type === 'TemplateLiteral' && titleValue.quasis.length > 0) {
// if a tagged template string.
title = titleValue.quasis[0].value.raw;
} else {
// if title is string: 'StringLiteral'
title = titleProperty.value.value;
}
if (title) {
estraverse.traverse(ast, {
fallback: 'iteration',
enter: node => {
patchNode(node);
if (
node.type === 'ExportNamedDeclaration' &&
node.declaration &&
node.declaration.declarations &&
node.declaration.declarations.length === 1 &&
node.declaration.declarations[0].type === 'VariableDeclarator' &&
node.declaration.declarations[0].id &&
node.declaration.declarations[0].id.name &&
node.declaration.declarations[0].init &&
['CallExpression', 'ArrowFunctionExpression', 'FunctionExpression'].includes(
node.declaration.declarations[0].init.type
)
) {
const storyName = storyNameFromExport(node.declaration.declarations[0].id.name);
const toAdd = handleExportedName(title, storyName, node.declaration.declarations[0].init);
Object.assign(addsMap, toAdd);
}
},
});
}
return { addsMap, idsToFrameworks };
}
export function findDependencies(ast) {
const dependencies = [];
const storiesOfIdentifiers = {};
estraverse.traverse(ast, {
fallback: 'iteration',
enter: node => {
patchNode(node);
if (node.type === 'ImportDeclaration') {
const candidateSpecifier = (node.specifiers || []).find(
s => (s.imported || {}).name === 'storiesOf'
);
if (node.source.value.startsWith('@storybook/') && candidateSpecifier) {
Object.assign(storiesOfIdentifiers, {
[candidateSpecifier.local.name]: node.source.value,
});
}
dependencies.push(node.source.value);
}
},
});
return { dependencies, storiesOfIdentifiers };
}
export function endOfImports(ast) {
let end = 0;
estraverse.traverse(ast, {
fallback: 'iteration',
enter: node => {
patchNode(node);
if (node.type === 'ImportDeclaration') {
end = Math.max(node.end, end);
}
},
});
return end;
}
export function popParametersObjectFromDefaultExport(source, ast) {
let splicedSource = source;
let parametersSliceOfCode = '';
let indexWhereToAppend = -1;
let foundParametersProperty = false;
estraverse.traverse(ast, {
fallback: 'iteration',
enter: node => {
patchNode(node);
if (
node.type === 'ExportDefaultDeclaration' &&
node.declaration.type === 'ObjectExpression' &&
(node.declaration.properties || []).length
) {
const parametersProperty = node.declaration.properties.find(
p => p.key.name === 'parameters' && p.value.type === 'ObjectExpression'
);
foundParametersProperty = !!parametersProperty;
if (foundParametersProperty) {
patchNode(parametersProperty.value);
} else {
patchNode(node.declaration);
}
splicedSource = parametersProperty
? source.substring(0, parametersProperty.value.start) +
source.substring(parametersProperty.value.end + 1)
: splicedSource;
parametersSliceOfCode = parametersProperty
? source.substring(parametersProperty.value.start, parametersProperty.value.end)
: '{}';
indexWhereToAppend = parametersProperty
? parametersProperty.value.start
: node.declaration.start + 1;
}
},
});
return { splicedSource, parametersSliceOfCode, indexWhereToAppend, foundParametersProperty };
}