-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
generateCodeSnippets.ts
104 lines (95 loc) · 3.44 KB
/
generateCodeSnippets.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
import { NormalizedOutputOptions } from '../rollup/types';
import { RESERVED_NAMES } from './reservedNames';
export interface GenerateCodeSnippets {
_: string;
directReturnFunctionRight: string;
n: string;
namedDirectReturnFunctionRight: string;
s: string;
getDirectReturnFunctionLeft(
params: string[],
options: { functionReturn: boolean; name: string | null }
): string;
getDirectReturnIifeLeft(
params: string[],
returned: string,
options: {
needsArrowReturnParens: boolean | undefined;
needsWrappedFunction: boolean | undefined;
}
): string;
getFunctionIntro(params: string[], options: { isAsync: boolean; name: string | null }): string;
getObject(
fields: [key: string | null, value: string][],
options: { indent: string; lineBreaks: boolean }
): string;
getPropertyAccess(name: string): string;
}
export function getGenerateCodeSnippets({
compact,
generatedCode: { arrowFunctions, objectShorthand, reservedNamesAsProps }
}: NormalizedOutputOptions): GenerateCodeSnippets {
const { _, n, s } = compact ? { _: '', n: '', s: '' } : { _: ' ', n: '\n', s: ';' };
// TODO Lukas use const
const getFunctionIntro: GenerateCodeSnippets['getFunctionIntro'] = arrowFunctions
? (params, { isAsync, name }) => {
const singleParam = params.length === 1;
const asyncString = isAsync ? `async${singleParam ? ' ' : _}` : '';
return `${name ? `var ${name}${_}=${_}` : ''}${asyncString}${
singleParam ? params[0] : `(${params.join(`,${_}`)})`
}${_}=>${_}`;
}
: (params, { isAsync, name }) =>
`${isAsync ? `async ` : ''}function${name ? ` ${name}` : ''}${_}(${params.join(
`,${_}`
)})${_}`;
const getDirectReturnFunctionLeft: GenerateCodeSnippets['getDirectReturnFunctionLeft'] = (
params,
{ functionReturn, name }
) =>
`${getFunctionIntro(params, {
isAsync: false,
name
})}${arrowFunctions ? '' : `{${_}${functionReturn ? 'return ' : ''}`}`;
const directReturnFunctionRight = arrowFunctions ? '' : `${s}${_}}`;
const isValidPropName = reservedNamesAsProps
? (name: string): boolean => validPropName.test(name)
: (name: string): boolean => !RESERVED_NAMES[name] && validPropName.test(name);
return {
_,
directReturnFunctionRight,
getDirectReturnFunctionLeft,
getDirectReturnIifeLeft: (params, returned, { needsArrowReturnParens, needsWrappedFunction }) =>
`${wrapIfNeeded(
`${getDirectReturnFunctionLeft(params, {
functionReturn: true,
name: null
})}${wrapIfNeeded(
returned,
arrowFunctions && needsArrowReturnParens
)}${directReturnFunctionRight}`,
arrowFunctions || needsWrappedFunction
)}(`,
getFunctionIntro,
getObject(fields, { indent, lineBreaks }) {
const prefix = `${lineBreaks ? n : ''}${indent}`;
return `{${fields
.map(([key, value]) => {
if (key === null) return `${prefix}${value}`;
const needsQuotes = !isValidPropName(key);
return key === value && objectShorthand && !needsQuotes
? prefix + key
: `${prefix}${needsQuotes ? `'${key}'` : key}:${_}${value}`;
})
.join(`,`)}${fields.length === 0 ? '' : lineBreaks ? n : indent}}`;
},
getPropertyAccess: (name: string): string =>
isValidPropName(name) ? `.${name}` : `[${JSON.stringify(name)}]`,
n,
namedDirectReturnFunctionRight: `${directReturnFunctionRight}${arrowFunctions ? ';' : ''}`,
s
};
}
const wrapIfNeeded = (code: string, needsParens: boolean | undefined): string =>
needsParens ? `(${code})` : code;
const validPropName = /^(?!\d)[\w$]+$/;