forked from date-fns/date-fns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow.js
185 lines (143 loc) · 4.76 KB
/
flow.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
const fs = require('fs')
const path = require('path')
const prettier = require('../prettier')
const { getParams, getType, getFPFnType } = require('./common')
const { addSeparator, formatBlock, formatFlowFile } = require('./formatBlock')
/**
* Return curried function type aliases for a specific FP function arity.
* @param {Number} [arity=4]
*/
const getFlowFPTypeAliases = (arity = 4) =>
[
'type CurriedFn1<A, R> = <A>(a: A) => R',
formatBlock`
type CurriedFn2<A, B, R> = <A>(a: A) => CurriedFn1<B, R>
| <A, B>(a: A, b: B) => R
`,
formatBlock`
type CurriedFn3<A, B, C, R> = <A>(a: A) => CurriedFn2<B, C, R>
| <A,B>(a: A, b: B) => CurriedFn1<C, R>
| <A,B,C>(a: A, b: B, c: C) => R
`,
formatBlock`
type CurriedFn4<A, B, C, D, R> = <A>(a: A) => CurriedFn3<B, C, D, R>
| <A,B>(a: A, b: B) => CurriedFn2<C, D, R>
| <A,B,C>(a: A, b: B, c: C) => CurriedFn1<D, R>
| <A,B,C,D>(a: A, b: B, c: C, d: D) => R
`
].slice(0, arity)
function getFlowTypeAlias(type) {
const { title, properties } = type
return `type ${title} = ${getParams(properties)}`
}
function generateFlowFnTyping(fn, aliasDeclarations) {
const { title, args, content } = fn
const params = getParams(args, { leftBorder: '(', rightBorder: ')' })
const returns = getType(content.returns[0].type.names)
const moduleDeclaration = `declare module.exports: ${params} => ${returns}`
const typingFile = formatFlowFile`
${addSeparator(aliasDeclarations, '\n')}
${moduleDeclaration}
`
writeFile(`src/${title}/index.js.flow`, typingFile)
}
function generateFlowFnIndexTyping(fns, aliasDeclarations, constants) {
const fnsDeclarations = fns.map(({ title, args, content }) => {
const params = getParams(args, { leftBorder: '(', rightBorder: ')' })
const returns = getType(content.returns[0].type.names)
return `${title}: ${params} => ${returns}`
})
const typingFile = formatFlowFile`
${addSeparator(aliasDeclarations, '\n')}
declare module.exports: {
${addSeparator(
fnsDeclarations.concat(generateConstantsDeclarations(constants)),
',\n'
)}
}
`
writeFile(`src/index.js.flow`, typingFile)
}
function generateFlowFPFnTyping(fn, aliasDeclarations) {
const { title, args, content } = fn
const type = getFPFnType(args, content.returns[0].type.names)
const typingFile = formatFlowFile`
${addSeparator(aliasDeclarations, '\n')}
${addSeparator(getFlowFPTypeAliases(args.length), '\n')}
declare module.exports: ${type}
`
writeFile(`src/fp/${title}/index.js.flow`, typingFile)
}
function generateFlowFPFnIndexTyping(fns, aliasDeclarations, constants) {
const fnsDeclarations = fns.map(
({ title, args, content }) =>
`${title}: ${getFPFnType(args, content.returns[0].type.names)}`
)
const typingFile = formatFlowFile`
${addSeparator(aliasDeclarations, '\n')}
${addSeparator(getFlowFPTypeAliases(), '\n')}
declare module.exports: {
${addSeparator(
fnsDeclarations.concat(generateConstantsDeclarations(constants)),
','
)}
}
`
writeFile(`src/fp/index.js.flow`, typingFile)
}
function generateFlowLocaleTyping(locale, localeAliasDeclaration) {
const { fullPath } = locale
const typingFile = formatFlowFile`
${localeAliasDeclaration}
declare module.exports: Locale
`
writeFile(`${fullPath}.flow`, typingFile)
}
function generateFlowLocaleIndexTyping(locales, localeAliasDeclaration) {
const typingFile = formatFlowFile`
${localeAliasDeclaration}
declare module.exports: {
${addSeparator(locales.map(({ name }) => `${name}: Locale`), ',')}
}
`
writeFile('src/locale/index.js.flow', typingFile)
}
function generateFlowTypings(fns, aliases, locales, constants) {
const aliasDeclarations = aliases.map(getFlowTypeAlias)
const localeAliasDeclaration = getFlowTypeAlias(
aliases.find(alias => alias.title === 'Locale')
)
fns.forEach((fn, index) => {
if (fn.isFPFn) {
generateFlowFPFnTyping(fn, aliasDeclarations)
} else {
generateFlowFnTyping(fn, aliasDeclarations)
}
})
locales.forEach(locale => {
generateFlowLocaleTyping(locale, localeAliasDeclaration)
})
generateFlowFnIndexTyping(
fns.filter(({ isFPFn }) => !isFPFn),
aliasDeclarations,
constants
)
generateFlowFPFnIndexTyping(
fns.filter(({ isFPFn }) => isFPFn),
aliasDeclarations,
constants
)
generateFlowLocaleIndexTyping(locales, localeAliasDeclaration)
}
function generateConstantsDeclarations(constants) {
return constants.map(c => `${c.name}: ${c.type.names.join(' | ')}`)
}
function writeFile(relativePath, content) {
return fs.writeFileSync(
path.resolve(process.cwd(), relativePath),
prettier(content, 'flow')
)
}
module.exports = {
generateFlowTypings
}