forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-helpers.js
180 lines (157 loc) 路 4.46 KB
/
parse-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
import { sanitize } from '@storybook/csf';
const STORIES_OF = 'storiesOf';
function pushParts(source, parts, from, to) {
const start = source.slice(from, to);
parts.push(start);
const end = source.slice(to);
parts.push(end);
}
export function patchNode(node) {
if (node.range && node.range.length === 2 && node.start === undefined && node.end === undefined) {
const [start, end] = node.range;
// eslint-disable-next-line no-param-reassign
node.start = start;
// eslint-disable-next-line no-param-reassign
node.end = end;
}
if (!node.range && node.start !== undefined && node.end !== undefined) {
// eslint-disable-next-line no-param-reassign
node.range = [node.start, node.end];
}
return node;
}
function findTemplate(templateName, program) {
let template = null;
program.body.find((node) => {
let declarations = null;
if (node.type === 'VariableDeclaration') {
declarations = node.declarations;
} else if (
node.type === 'ExportNamedDeclaration' &&
node.declaration?.type === 'VariableDeclaration'
) {
declarations = node.declaration.declarations;
}
return (
declarations &&
declarations.find((decl) => {
if (
decl.type === 'VariableDeclarator' &&
decl.id.type === 'Identifier' &&
decl.id.name === templateName
) {
template = decl.init;
return true; // stop looking
}
return false;
})
);
});
return template;
}
function expandBindExpression(node, parent) {
if (node.type === 'CallExpression') {
const { callee, arguments: bindArguments } = node;
if (
parent.type === 'Program' &&
callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'bind' &&
(bindArguments.length === 0 ||
(bindArguments.length === 1 &&
bindArguments[0].type === 'ObjectExpression' &&
bindArguments[0].properties.length === 0))
) {
const boundIdentifier = callee.object.name;
const template = findTemplate(boundIdentifier, parent);
if (template) {
return template;
}
}
}
return node;
}
export function handleExportedName(storyName, originalNode, parent) {
const node = expandBindExpression(originalNode, parent);
const startLoc = {
col: node.loc.start.column,
line: node.loc.start.line,
};
const endLoc = {
col: node.loc.end.column,
line: node.loc.end.line,
};
return {
[storyName]: {
startLoc,
endLoc,
startBody: startLoc,
endBody: endLoc,
},
};
}
export function handleADD(node, parent, storiesOfIdentifiers) {
if (!node.property || !node.property.name || node.property.name !== 'add') {
return {};
}
const addArgs = parent.arguments;
if (!addArgs || addArgs.length < 2) {
return {};
}
let tmp = node.object;
while (tmp.callee && tmp.callee.object) {
tmp = tmp.callee.object;
}
const framework = tmp.callee && tmp.callee.name && storiesOfIdentifiers[tmp.callee.name];
const storyName = addArgs[0];
const body = addArgs[1];
const lastArg = addArgs[addArgs.length - 1];
if (storyName.type !== 'Literal' && storyName.type !== 'StringLiteral') {
// if story name is not literal, it's much harder to extract it
return {};
}
if (storyName.value) {
const key = sanitize(storyName.value);
let idToFramework;
if (key && framework) {
idToFramework = { [key]: framework };
}
return {
toAdd: {
[key]: {
// Debug: code: source.slice(storyName.start, lastArg.end),
startLoc: {
col: storyName.loc.start.column,
line: storyName.loc.start.line,
},
endLoc: {
col: lastArg.loc.end.column,
line: lastArg.loc.end.line,
},
startBody: {
col: body.loc.start.column,
line: body.loc.start.line,
},
endBody: {
col: body.loc.end.column,
line: body.loc.end.line,
},
},
},
idToFramework,
};
}
return {};
}
export function handleSTORYOF(node, parts, source, lastIndex) {
if (!node.callee || !node.callee.name || node.callee.name !== STORIES_OF) {
return lastIndex;
}
parts.pop();
pushParts(source, parts, lastIndex, node.end);
return node.end;
}
export function asImport(node) {
return node.source.value;
}