-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
generate-helpers.js
173 lines (145 loc) · 4.96 KB
/
generate-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
import { patchNode } from './parse-helpers';
import getParser from './parsers';
import {
splitSTORYOF,
findAddsMap,
findDependencies,
splitExports,
popParametersObjectFromDefaultExport,
findExportsMap as generateExportsMap,
} from './traverse-helpers';
function isUglyComment(comment, uglyCommentsRegex) {
return uglyCommentsRegex.some(regex => regex.test(comment));
}
function generateSourceWithoutUglyComments(source, { comments, uglyCommentsRegex }) {
let lastIndex = 0;
const parts = [source];
comments
.filter(comment => isUglyComment(comment.value.trim(), uglyCommentsRegex))
.map(patchNode)
.forEach(comment => {
parts.pop();
const start = source.slice(lastIndex, comment.start);
const end = source.slice(comment.end);
parts.push(start, end);
lastIndex = comment.end;
});
return parts.join('');
}
function prettifyCode(source, { prettierConfig, parser, filepath }) {
let config = prettierConfig;
let foundParser = null;
if (parser === 'flow') foundParser = 'flow';
if (parser === 'javascript' || /jsx?/.test(parser)) foundParser = 'javascript';
if (parser === 'typescript' || /tsx?/.test(parser)) foundParser = 'typescript';
if (!config.parser) {
config = {
...prettierConfig,
};
} else if (filepath) {
config = {
...prettierConfig,
filepath,
};
} else {
config = {
...prettierConfig,
};
}
try {
return getParser(foundParser || 'javascript').format(source, config);
} catch (e) {
// Can fail when the source is a JSON
return source;
}
}
const STORY_DECORATOR_STATEMENT =
'.addDecorator(withSourceLoader(__STORY__, __ADDS_MAP__,__MAIN_FILE_LOCATION__,__MODULE_DEPENDENCIES__,__LOCAL_DEPENDENCIES__,__SOURCE_PREFIX__,__IDS_TO_FRAMEWORKS__))';
const ADD_PARAMETERS_STATEMENT =
'.addParameters({ storySource: { source: __STORY__, locationsMap: __ADDS_MAP__ } })';
const IMPORT_DECLARATION_FOR_EXPORTED_STORIES_DECORATOR =
'var addSourceDecorator = require("@storybook/source-loader/preview").addSource;\n';
const applyExportDecoratorStatement = part =>
` addSourceDecorator(${part}, {__STORY__, __ADDS_MAP__,__MAIN_FILE_LOCATION__,__MODULE_DEPENDENCIES__,__LOCAL_DEPENDENCIES__,__SOURCE_PREFIX__,__IDS_TO_FRAMEWORKS__});`;
export function generateSourceWithDecorators(source, ast, withParameters) {
const { comments = [] } = ast;
const partsUsingStoryOfToken = splitSTORYOF(ast, source);
if (partsUsingStoryOfToken.length > 1) {
const newSource = partsUsingStoryOfToken.join(
(withParameters ? ADD_PARAMETERS_STATEMENT : '') + STORY_DECORATOR_STATEMENT
);
return {
storyOfTokenFound: true,
changed: partsUsingStoryOfToken.length > 1,
source: newSource,
comments,
};
}
const partsUsingExports = splitExports(ast, source);
const newSource =
IMPORT_DECLARATION_FOR_EXPORTED_STORIES_DECORATOR +
partsUsingExports
.map((part, i) => (i % 2 === 0 ? part : applyExportDecoratorStatement(part)))
.join('');
return {
exportTokenFound: true,
changed: partsUsingExports.length > 1,
source: newSource,
comments,
};
}
export function generateSourceWithoutDecorators(source, ast) {
const { comments = [] } = ast;
return {
changed: true,
source,
comments,
};
}
export function generateAddsMap(ast, storiesOfIdentifiers) {
return findAddsMap(ast, storiesOfIdentifiers);
}
export function generateStoriesLocationsMap(ast, storiesOfIdentifiers) {
const usingAddsMap = generateAddsMap(ast, storiesOfIdentifiers);
const { addsMap } = usingAddsMap;
if (Object.keys(addsMap).length > 0) {
return usingAddsMap;
}
const usingExportsMap = generateExportsMap(ast);
return usingExportsMap || usingAddsMap;
}
export function generateDependencies(ast) {
return findDependencies(ast);
}
export function generateStorySource({ source, ...options }) {
let storySource = source;
storySource = generateSourceWithoutUglyComments(storySource, options);
storySource = prettifyCode(storySource, options);
return storySource;
}
export function generateSourcesInExportedParameters(source, ast, additionalParameters) {
const {
splicedSource,
parametersSliceOfCode,
indexWhereToAppend,
foundParametersProperty,
} = popParametersObjectFromDefaultExport(source, ast);
if (indexWhereToAppend !== -1) {
const additionalParametersAsJson = JSON.stringify({ storySource: additionalParameters }).slice(
0,
-1
);
const propertyDeclaration = foundParametersProperty ? '' : 'parameters: ';
const comma = foundParametersProperty ? '' : ',';
const newParameters = `${propertyDeclaration}${additionalParametersAsJson},${parametersSliceOfCode.substring(
1
)}${comma}`;
const additionalComma = comma === ',' ? '' : ',';
const result = `${splicedSource.substring(
0,
indexWhereToAppend
)}${newParameters}${additionalComma}${splicedSource.substring(indexWhereToAppend)}`;
return result;
}
return source;
}