Skip to content

Improve code sanity #2

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

Merged
merged 4 commits into from
May 2, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions example/assets/locales/pt-br/translations.ftl.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// This file is automatically generated.
// Please do not change this file!

import { ComplexPattern } from '@fluent/bundle/esm/ast'
import { FluentBundle } from '@fluent/bundle'
import { FluentBundle, FluentArgument } from '@fluent/bundle'

type Pattern<T> = T | ComplexPattern
type Pattern<T extends MessagesKey> = T | Parameters<FluentBundle['formatPattern']>[0]

type Message<T> = {
type Message<T extends MessagesKey> = {
id: T
value: Pattern<T>
attributes: Record<string, Pattern<T>>
Expand All @@ -15,7 +14,7 @@ type Message<T> = {
declare global {
interface FluentBundleTyped extends FluentBundle {
getMessage<T extends MessagesKey>(id: T): Message<T>
formatPattern<T extends MessagesKey>(pattern: Pattern<T>, args?: PatternArguments<T>, errors?: Array<Error> | null): string
formatPattern: <T extends MessagesKey>(pattern: Pattern<T>, ...args: PatternArguments<T>) => string
}
}

Expand All @@ -24,10 +23,10 @@ type MessagesKey = 'hello' |
'bye'
type PatternArguments<T extends MessagesKey> = (
T extends 'hello'
? { 'name': string | number }:
? [{ 'name': FluentArgument }]:
T extends 'how-are-you'
? undefined:
? []:
T extends 'bye'
? undefined
? []
: never
)
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test": "jest"
},
"dependencies": {
"@fluent/bundle": "0.15.1",
"@fluent/syntax": "0.15.0",
"chokidar": "3.4.0",
"dedent-js": "1.0.1",
"loader-utils": "2.0.0"
Expand Down
9 changes: 4 additions & 5 deletions src/build-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ const bannerMessage = (
)

const header = dedent`
import { ComplexPattern } from '@fluent/bundle/esm/ast'
import { FluentBundle } from '@fluent/bundle'
import { FluentBundle, FluentArgument } from '@fluent/bundle'

type Pattern<T> = T | ComplexPattern
type Pattern<T extends MessagesKey> = T | Parameters<FluentBundle['formatPattern']>[0]

type Message<T> = {
type Message<T extends MessagesKey> = {
id: T
value: Pattern<T>
attributes: Record<string, Pattern<T>>
Expand All @@ -19,7 +18,7 @@ const header = dedent`
declare global {
interface FluentBundleTyped extends FluentBundle {
getMessage<T extends MessagesKey>(id: T): Message<T>
formatPattern<T extends MessagesKey>(pattern: Pattern<T>, args?: PatternArguments<T>, errors?: Array<Error> | null): string
formatPattern: <T extends MessagesKey>(pattern: Pattern<T>, ...args: PatternArguments<T>) => string
}
}
`
Expand Down
9 changes: 6 additions & 3 deletions src/build-type-messages-key.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const buildTypeMessagesKey = (resource) => {
const interfaceFields = resource
.map(message => `'${message.id}'`)
const buildTypeMessagesKey = (ast) => {
const messages = ast.body
.filter(node => node.type === 'Message')

const interfaceFields = messages
.map(message => `'${message.id.name}'`)
.join(' |\n')

return `type MessagesKey = ${interfaceFields}`
Expand Down
38 changes: 15 additions & 23 deletions src/build-type-pattern-arguments.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
import dedent from 'dedent-js'

const messageVariables = (message) => {
if (typeof message.value === 'string') {
return []
}

const variables = message.value.reduce(
(acc, current) => current.type === 'var'
? [...acc, current.name]
: acc
, []
)

return variables
}
const messageVariablesName = (message) =>
message.value.elements
.filter(element => element.type === 'Placeable')
.filter(placeable => placeable.expression.type === 'VariableReference')
.map(placeable => placeable.expression.id.name)

const wrapVariables = variables => variables.map(i => `'${i}': string | number`)
const wrapVariables = variables => variables.map(i => `'${i}': FluentArgument`)

const hasVariables = variables => variables.length > 0

const buildTypePatternArguments = (resource) => {
const options = resource
const buildTypePatternArguments = (ast) => {
const messages = ast.body
const options = messages
.map(message => {
const variables = messageVariables(message)
const variablesName = messageVariablesName(message)

if (hasVariables(variables)) {
if (hasVariables(variablesName)) {
return dedent`
T extends '${message.id}'
? { ${wrapVariables(messageVariables(message)).join(',')} }
T extends '${message.id.name}'
? [{ ${wrapVariables(variablesName).join(',')} }]
`
}

return dedent`
T extends '${message.id}'
? undefined
T extends '${message.id.name}'
? []
`
}).join(':\n')

Expand Down
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { FluentResource } from '@fluent/bundle'
import { FluentParser } from '@fluent/syntax'
import dedent from 'dedent-js'
import buildHeader from './build-header'
import buildTypeMessagesKey from './build-type-messages-key'
import buildTypePatternArguments from './build-type-pattern-arguments'

const buildFluentTypeModule = (content) => {
const resource = (new FluentResource(content)).body
const parser = new FluentParser({ withSpans: false })
const ast = parser.parse(content)

const fluentTypeModule = dedent`
${buildHeader()}
${buildTypeMessagesKey(resource)}
${buildTypePatternArguments(resource)}
${buildTypeMessagesKey(ast)}
${buildTypePatternArguments(ast)}
`

return fluentTypeModule
Expand Down
15 changes: 7 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ test('Should match the types definitions', async () => {
// This file is automatically generated.
// Please do not change this file!

import { ComplexPattern } from '@fluent/bundle/esm/ast'
import { FluentBundle } from '@fluent/bundle'
import { FluentBundle, FluentArgument } from '@fluent/bundle'

type Pattern<T> = T | ComplexPattern
type Pattern<T extends MessagesKey> = T | Parameters<FluentBundle['formatPattern']>[0]

type Message<T> = {
type Message<T extends MessagesKey> = {
id: T
value: Pattern<T>
attributes: Record<string, Pattern<T>>
Expand All @@ -28,7 +27,7 @@ test('Should match the types definitions', async () => {
declare global {
interface FluentBundleTyped extends FluentBundle {
getMessage<T extends MessagesKey>(id: T): Message<T>
formatPattern<T extends MessagesKey>(pattern: Pattern<T>, args?: PatternArguments<T>, errors?: Array<Error> | null): string
formatPattern: <T extends MessagesKey>(pattern: Pattern<T>, ...args: PatternArguments<T>) => string
}
}

Expand All @@ -37,11 +36,11 @@ test('Should match the types definitions', async () => {
'bye'
type PatternArguments<T extends MessagesKey> = (
T extends 'hello'
? { 'name': string | number }:
? [{ 'name': FluentArgument }]:
T extends 'how-are-you'
? undefined:
? []:
T extends 'bye'
? undefined
? []
: never
)
`)
Expand Down