Skip to content

Commit cbea2c0

Browse files
authored
Merge pull request #2 from macabeus/feat/review
Improve code sanity
2 parents bf4f1e2 + fbfc57f commit cbea2c0

8 files changed

+50
-56
lines changed
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// This file is automatically generated.
22
// Please do not change this file!
33

4-
import { ComplexPattern } from '@fluent/bundle/esm/ast'
5-
import { FluentBundle } from '@fluent/bundle'
4+
import { FluentBundle, FluentArgument } from '@fluent/bundle'
65

7-
type Pattern<T> = T | ComplexPattern
6+
type Pattern<T extends MessagesKey> = T | Parameters<FluentBundle['formatPattern']>[0]
87

9-
type Message<T> = {
8+
type Message<T extends MessagesKey> = {
109
id: T
1110
value: Pattern<T>
1211
attributes: Record<string, Pattern<T>>
@@ -15,7 +14,7 @@ type Message<T> = {
1514
declare global {
1615
interface FluentBundleTyped extends FluentBundle {
1716
getMessage<T extends MessagesKey>(id: T): Message<T>
18-
formatPattern<T extends MessagesKey>(pattern: Pattern<T>, args?: PatternArguments<T>, errors?: Array<Error> | null): string
17+
formatPattern: <T extends MessagesKey>(pattern: Pattern<T>, ...args: PatternArguments<T>) => string
1918
}
2019
}
2120

@@ -24,10 +23,10 @@ type MessagesKey = 'hello' |
2423
'bye'
2524
type PatternArguments<T extends MessagesKey> = (
2625
T extends 'hello'
27-
? { 'name': string | number }:
26+
? [{ 'name': FluentArgument }]:
2827
T extends 'how-are-you'
29-
? undefined:
28+
? []:
3029
T extends 'bye'
31-
? undefined
30+
? []
3231
: never
3332
)

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"test": "jest"
1818
},
1919
"dependencies": {
20-
"@fluent/bundle": "0.15.1",
20+
"@fluent/syntax": "0.15.0",
2121
"chokidar": "3.4.0",
2222
"dedent-js": "1.0.1",
2323
"loader-utils": "2.0.0"

src/build-header.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ const bannerMessage = (
55
)
66

77
const header = dedent`
8-
import { ComplexPattern } from '@fluent/bundle/esm/ast'
9-
import { FluentBundle } from '@fluent/bundle'
8+
import { FluentBundle, FluentArgument } from '@fluent/bundle'
109
11-
type Pattern<T> = T | ComplexPattern
10+
type Pattern<T extends MessagesKey> = T | Parameters<FluentBundle['formatPattern']>[0]
1211
13-
type Message<T> = {
12+
type Message<T extends MessagesKey> = {
1413
id: T
1514
value: Pattern<T>
1615
attributes: Record<string, Pattern<T>>
@@ -19,7 +18,7 @@ const header = dedent`
1918
declare global {
2019
interface FluentBundleTyped extends FluentBundle {
2120
getMessage<T extends MessagesKey>(id: T): Message<T>
22-
formatPattern<T extends MessagesKey>(pattern: Pattern<T>, args?: PatternArguments<T>, errors?: Array<Error> | null): string
21+
formatPattern: <T extends MessagesKey>(pattern: Pattern<T>, ...args: PatternArguments<T>) => string
2322
}
2423
}
2524
`

src/build-type-messages-key.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
const buildTypeMessagesKey = (resource) => {
2-
const interfaceFields = resource
3-
.map(message => `'${message.id}'`)
1+
const buildTypeMessagesKey = (ast) => {
2+
const messages = ast.body
3+
.filter(node => node.type === 'Message')
4+
5+
const interfaceFields = messages
6+
.map(message => `'${message.id.name}'`)
47
.join(' |\n')
58

69
return `type MessagesKey = ${interfaceFields}`

src/build-type-pattern-arguments.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
import dedent from 'dedent-js'
22

3-
const messageVariables = (message) => {
4-
if (typeof message.value === 'string') {
5-
return []
6-
}
7-
8-
const variables = message.value.reduce(
9-
(acc, current) => current.type === 'var'
10-
? [...acc, current.name]
11-
: acc
12-
, []
13-
)
14-
15-
return variables
16-
}
3+
const messageVariablesName = (message) =>
4+
message.value.elements
5+
.filter(element => element.type === 'Placeable')
6+
.filter(placeable => placeable.expression.type === 'VariableReference')
7+
.map(placeable => placeable.expression.id.name)
178

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

2011
const hasVariables = variables => variables.length > 0
2112

22-
const buildTypePatternArguments = (resource) => {
23-
const options = resource
13+
const buildTypePatternArguments = (ast) => {
14+
const messages = ast.body
15+
const options = messages
2416
.map(message => {
25-
const variables = messageVariables(message)
17+
const variablesName = messageVariablesName(message)
2618

27-
if (hasVariables(variables)) {
19+
if (hasVariables(variablesName)) {
2820
return dedent`
29-
T extends '${message.id}'
30-
? { ${wrapVariables(messageVariables(message)).join(',')} }
21+
T extends '${message.id.name}'
22+
? [{ ${wrapVariables(variablesName).join(',')} }]
3123
`
3224
}
3325

3426
return dedent`
35-
T extends '${message.id}'
36-
? undefined
27+
T extends '${message.id.name}'
28+
? []
3729
`
3830
}).join(':\n')
3931

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { FluentResource } from '@fluent/bundle'
1+
import { FluentParser } from '@fluent/syntax'
22
import dedent from 'dedent-js'
33
import buildHeader from './build-header'
44
import buildTypeMessagesKey from './build-type-messages-key'
55
import buildTypePatternArguments from './build-type-pattern-arguments'
66

77
const buildFluentTypeModule = (content) => {
8-
const resource = (new FluentResource(content)).body
8+
const parser = new FluentParser({ withSpans: false })
9+
const ast = parser.parse(content)
10+
911
const fluentTypeModule = dedent`
1012
${buildHeader()}
11-
${buildTypeMessagesKey(resource)}
12-
${buildTypePatternArguments(resource)}
13+
${buildTypeMessagesKey(ast)}
14+
${buildTypePatternArguments(ast)}
1315
`
1416

1517
return fluentTypeModule

test/index.test.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ test('Should match the types definitions', async () => {
1414
// This file is automatically generated.
1515
// Please do not change this file!
1616
17-
import { ComplexPattern } from '@fluent/bundle/esm/ast'
18-
import { FluentBundle } from '@fluent/bundle'
17+
import { FluentBundle, FluentArgument } from '@fluent/bundle'
1918
20-
type Pattern<T> = T | ComplexPattern
19+
type Pattern<T extends MessagesKey> = T | Parameters<FluentBundle['formatPattern']>[0]
2120
22-
type Message<T> = {
21+
type Message<T extends MessagesKey> = {
2322
id: T
2423
value: Pattern<T>
2524
attributes: Record<string, Pattern<T>>
@@ -28,7 +27,7 @@ test('Should match the types definitions', async () => {
2827
declare global {
2928
interface FluentBundleTyped extends FluentBundle {
3029
getMessage<T extends MessagesKey>(id: T): Message<T>
31-
formatPattern<T extends MessagesKey>(pattern: Pattern<T>, args?: PatternArguments<T>, errors?: Array<Error> | null): string
30+
formatPattern: <T extends MessagesKey>(pattern: Pattern<T>, ...args: PatternArguments<T>) => string
3231
}
3332
}
3433
@@ -37,11 +36,11 @@ test('Should match the types definitions', async () => {
3736
'bye'
3837
type PatternArguments<T extends MessagesKey> = (
3938
T extends 'hello'
40-
? { 'name': string | number }:
39+
? [{ 'name': FluentArgument }]:
4140
T extends 'how-are-you'
42-
? undefined:
41+
? []:
4342
T extends 'bye'
44-
? undefined
43+
? []
4544
: never
4645
)
4746
`)

0 commit comments

Comments
 (0)