Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global flow typings #558

Merged
merged 5 commits into from Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -81,6 +81,9 @@ for the list of changes made since `v2.0.0-alpha.1`.
- [Vietnamese locale (vi)](https://github.com/date-fns/date-fns/pull/546)
(kudos to [@trongthanh](https://github.com/trongthanh))

- Flow typings for `index.js`, `fp/index.js`, `locale/index.js`, and their ESM equivalents.
See PR [#558](https://github.com/date-fns/date-fns/pull/558)

### Changed

- **BREAKING**: function submodules now use camelCase naming schema:
Expand Down
2 changes: 1 addition & 1 deletion scripts/_lib/listFPFns.js
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')

module.exports = listFPFns

const ignoredFiles = ['index.js', 'test.js']
const ignoredFiles = ['index.js', 'test.js', 'index.js.flow']

function listFPFns () {
const files = fs.readdirSync(path.join(process.cwd(), 'src', 'fp'))
Expand Down
2 changes: 1 addition & 1 deletion scripts/_lib/listFns.js
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')

module.exports = listFns

const ignoredFiles = ['locale', 'esm', 'fp', 'index.js', 'test.js']
const ignoredFiles = ['locale', 'esm', 'fp', 'index.js', 'test.js', 'index.js.flow']

function listFns () {
const files = fs.readdirSync(path.join(process.cwd(), 'src'))
Expand Down
2 changes: 1 addition & 1 deletion scripts/_lib/listLocales.js
@@ -1,7 +1,7 @@
const path = require('path')
const fs = require('fs')

const ignoredFiles = ['index.js', 'test.js']
const ignoredFiles = ['index.js', 'test.js', 'index.js.flow']

module.exports = listLocales

Expand Down
3 changes: 3 additions & 0 deletions scripts/build/package.sh
Expand Up @@ -38,6 +38,9 @@ do
cp -r "./src/$fnDir" "$dir/esm/"
done

# Copy global flow typing
cp ./src/index.js.flow "$dir/esm/index.js.flow"

# Copy esm indices
cp ./src/esm/index.js "$dir/esm/index.js"
cp ./src/esm/fp/index.js "$dir/esm/fp/index.js"
Expand Down
64 changes: 64 additions & 0 deletions scripts/build/typings.js
Expand Up @@ -381,6 +381,28 @@ function generateFlowFnTyping (fn, aliasDeclarations) {
fs.writeFileSync(filename, typingString)
}

function generateFlowFnIndexTyping (fns, aliasDeclarations) {
const filename = `./src/index.js.flow`

const fnsDeclarations = fns.map(({title, args, content}) => {
const params = getParams(args, {indent: 1, leftBorder: '(', rightBorder: ')'})
const returns = getType(content.returns[0].type.names)
return ` ${title}: ${params} => ${returns}`
})

const typingString = ['// @flow']
.concat(generatedAutomaticallyMessage)
.concat('')
.concat(aliasDeclarations.join('\n\n'))
.concat('')
.concat(`declare module.exports: {`)
.concat(fnsDeclarations.join(',\n\n'))
.concat(`}\n`)
.join('\n')

fs.writeFileSync(filename, typingString)
}

function generateFlowFPFnTyping (fn, aliasDeclarations) {
const {title, args, content} = fn
const filename = `./src/fp/${title}/index.js.flow`
Expand All @@ -400,6 +422,28 @@ function generateFlowFPFnTyping (fn, aliasDeclarations) {
fs.writeFileSync(filename, typingString)
}

function generateFlowFPFnIndexTyping (fns, aliasDeclarations) {
const filename = `./src/fp/index.js.flow`

const fnsDeclarations = fns.map(({title, args, content}) =>
` ${title}: ${getFPFnType(args, content.returns[0].type.names)}`
)

const typingString = ['// @flow']
.concat(generatedAutomaticallyMessage)
.concat('')
.concat(aliasDeclarations.join('\n\n'))
.concat('')
.concat(flowFPAliases)
.concat('')
.concat(`declare module.exports: {`)
.concat(fnsDeclarations.join(',\n'))
.concat(`}\n`)
.join('\n')

fs.writeFileSync(filename, typingString)
}

function generateFlowLocaleTyping (locale, localeAliasDeclaration) {
const {fullPath} = locale
const filename = `${fullPath}.flow`
Expand All @@ -415,6 +459,22 @@ function generateFlowLocaleTyping (locale, localeAliasDeclaration) {
fs.writeFileSync(filename, typingString)
}

function generateFlowLocaleIndexTyping (locales, localeAliasDeclaration) {
const filename = './src/locale/index.js.flow'

const typingString = ['// @flow']
.concat(generatedAutomaticallyMessage)
.concat('')
.concat(localeAliasDeclaration)
.concat('')
.concat(`declare module.exports: {`)
.concat(locales.map(({name}) => ` ${name}: Locale`).join(',\n'))
.concat(`}\n`)
.join('\n')

fs.writeFileSync(filename, typingString)
}

function generateFlowTypings (fns, aliases, locales) {
const aliasDeclarations = aliases.map(getFlowTypeAlias)
const localeAliasDeclaration = getFlowTypeAlias(aliases.find((alias) => alias.title === 'Locale'))
Expand All @@ -430,4 +490,8 @@ function generateFlowTypings (fns, aliases, locales) {
locales.forEach((locale) => {
generateFlowLocaleTyping(locale, localeAliasDeclaration)
})

generateFlowFnIndexTyping(fns.filter(({isFPFn}) => !isFPFn), aliasDeclarations)
generateFlowFPFnIndexTyping(fns.filter(({isFPFn}) => isFPFn), aliasDeclarations)
generateFlowLocaleIndexTyping(locales, localeAliasDeclaration)
}