Skip to content

Commit 85a795a

Browse files
committed
use parameter declarations when printing phantom fields, fix #16
1 parent 427a976 commit 85a795a

File tree

12 files changed

+63
-56
lines changed

12 files changed

+63
-56
lines changed

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
language: node_js
22
node_js:
3-
- "10"
4-
before_script:
5-
- export DISPLAY=:99.0
6-
- sh -e /etc/init.d/xvfb start
3+
- '10'

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
**Note**: Gaps between patch versions are faulty/broken releases.
1414
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.
1515

16+
# 0.3.1
17+
18+
- **Bug Fix**
19+
- use parameter declarations when printing phantom fields, fix #16 (@gcanti)
20+
1621
# 0.3.0
1722

1823
- **Breaking Change**

docs/bundle.js

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

examples/Either.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
export type Either<L, R> = {
1+
export type Either<E, R> = {
22
readonly type: "Left";
3-
readonly value0: L;
3+
readonly value0: E;
44
} | {
55
readonly type: "Right";
66
readonly value0: R;
77
};
88

9-
export function left<L, R>(value0: L): Either<L, R> { return { type: "Left", value0 }; }
9+
export function left<E, R>(value0: E): Either<E, R> { return { type: "Left", value0 }; }
1010

11-
export function right<L, R>(value0: R): Either<L, R> { return { type: "Right", value0 }; }
11+
export function right<E, R>(value0: R): Either<E, R> { return { type: "Right", value0 }; }
1212

13-
export function fold<L, R, R1>(fa: Either<L, R>, onLeft: (value0: L) => R1, onRight: (value0: R) => R1): R1 { switch (fa.type) {
13+
export function fold<E, R, R1>(fa: Either<E, R>, onLeft: (value0: E) => R1, onRight: (value0: R) => R1): R1 { switch (fa.type) {
1414
case "Left": return onLeft(fa.value0);
1515
case "Right": return onRight(fa.value0);
1616
} }
1717

1818
import { Prism } from "monocle-ts";
1919

20-
export function _left<L, R>(): Prism<Either<L, R>, Either<L, R>> { return Prism.fromPredicate(s => s.type === "Left"); }
20+
export function _left<E, R>(): Prism<Either<E, R>, Either<E, R>> { return Prism.fromPredicate(s => s.type === "Left"); }
2121

22-
export function _right<L, R>(): Prism<Either<L, R>, Either<L, R>> { return Prism.fromPredicate(s => s.type === "Right"); }
22+
export function _right<E, R>(): Prism<Either<E, R>, Either<E, R>> { return Prism.fromPredicate(s => s.type === "Right"); }
2323

2424
import { Setoid, fromEquals } from "fp-ts/lib/Setoid";
2525

26-
export function getSetoid<L, R>(setoidLeftValue0: Setoid<L>, setoidRightValue0: Setoid<R>): Setoid<Either<L, R>> { return fromEquals((x, y) => { if (x.type === "Left" && y.type === "Left") {
26+
export function getSetoid<E, R>(setoidLeftValue0: Setoid<E>, setoidRightValue0: Setoid<R>): Setoid<Either<E, R>> { return fromEquals((x, y) => { if (x.type === "Left" && y.type === "Left") {
2727
return setoidLeftValue0.equals(x.value0, y.value0);
2828
} if (x.type === "Right" && y.type === "Right") {
2929
return setoidRightValue0.equals(x.value0, y.value0);

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fp-ts-codegen",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "TypeScript code generation from a haskell-like syntax for ADT",
55
"files": [
66
"lib"
@@ -33,7 +33,7 @@
3333
},
3434
"homepage": "https://github.com/gcanti/fp-ts-codegen",
3535
"dependencies": {
36-
"fp-ts": "^1.15.0",
36+
"fp-ts": "^1.19.1",
3737
"monocle-ts": "^1.5.3",
3838
"parser-ts": "^0.5.1",
3939
"typescript": "^3.2.4"

src/ast.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,15 @@ const getDataFptsEncoding = (d: M.Data): AST<Array<ts.Node>> => {
284284
const getPolymorphicFields = (): Array<ts.PropertyDeclaration> => {
285285
const URI = getPropertyDeclaration('_URI', ts.createTypeReferenceNode('URI', A.empty), undefined, true)
286286
const len = getDataParametersLength(d)
287-
const fptsParameters = URI2HKTParametersNames.slice(0, len).map(name =>
288-
getPropertyDeclaration(`_${name}`, ts.createTypeReferenceNode(name, A.empty), undefined, true)
289-
)
287+
const parameterDeclarations = d.parameterDeclarations.slice().reverse()
288+
const fptsParameters = URI2HKTParametersNames.slice(0, len).map((name, i) => {
289+
return getPropertyDeclaration(
290+
`_${name}`,
291+
ts.createTypeReferenceNode(parameterDeclarations[i].name, A.empty),
292+
undefined,
293+
true
294+
)
295+
})
290296
return [...fptsParameters, URI]
291297
}
292298

@@ -455,7 +461,7 @@ const getLiteralConstructor = (c: M.Constructor, d: M.Data): AST<ts.Node> => {
455461

456462
const getConstructorsLiteralEncoding = (d: M.Data): AST<Array<ts.Node>> => {
457463
const constructors = d.constructors.map(c =>
458-
A.foldL(c.members, () => getLiteralNullaryConstructor(c, d), () => getLiteralConstructor(c, d))
464+
A.foldLeft(() => getLiteralNullaryConstructor(c, d), () => getLiteralConstructor(c, d))(c.members)
459465
)
460466
return A.array.sequence(reader)(constructors)
461467
}
@@ -494,7 +500,7 @@ const getFptsConstructor = (c: M.Constructor, d: M.Data): AST<ts.Node> => {
494500

495501
const getConstructorsFptsEncoding = (d: M.Data): AST<Array<ts.Node>> => {
496502
const constructors = d.constructors.map(c =>
497-
A.foldL(c.members, () => getFptsNullaryConstructor(c, d), () => getFptsConstructor(c, d))
503+
A.foldLeft(() => getFptsNullaryConstructor(c, d), () => getFptsConstructor(c, d))(c.members)
498504
)
499505
return A.array.sequence(reader)(constructors)
500506
}
@@ -745,8 +751,7 @@ export const setoid = (d: M.Data): AST<Array<ts.Node>> => {
745751
})
746752
})
747753
const getReturnValue = (c: M.Constructor): ts.Expression => {
748-
return A.foldL(
749-
c.members,
754+
return A.foldLeft(
750755
() => ts.createTrue(),
751756
() => {
752757
const callExpressions = c.members.map((m, position) => {
@@ -759,7 +764,7 @@ export const setoid = (d: M.Data): AST<Array<ts.Node>> => {
759764
})
760765
return S.fold(semigroupBinaryExpression)(callExpressions[0])(callExpressions.slice(1))
761766
}
762-
)
767+
)(c.members)
763768
}
764769
return ask<Options>().chain(e => {
765770
const setoidImport = getImportDeclaration(['Setoid', 'fromEquals'], 'fp-ts/lib/Setoid')

src/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NonEmptyArray, make } from 'fp-ts/lib/NonEmptyArray2v'
1+
import { NonEmptyArray, cons } from 'fp-ts/lib/NonEmptyArray2v'
22
import { Option, none } from 'fp-ts/lib/Option'
33

44
export type Identifier = string
@@ -89,7 +89,7 @@ export const data = (
8989
): Data => ({
9090
name,
9191
parameterDeclarations,
92-
constructors: make(head, tail)
92+
constructors: cons(head, tail)
9393
})
9494

9595
export const isNullary = (c: Constructor): boolean => {

src/printer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as Ast from './ast'
33
import * as M from './model'
44
import { Reader, reader } from 'fp-ts/lib/Reader'
55
import * as Mon from 'fp-ts/lib/Monoid'
6-
import { array } from 'fp-ts/lib/Array'
6+
import * as A from 'fp-ts/lib/Array'
77

88
export interface Printer<A> extends Reader<Ast.Options, A> {}
99

@@ -42,10 +42,10 @@ export const getMonoid = <A>(M: Mon.Monoid<A>): Mon.Monoid<Printer<A>> => {
4242
}
4343
}
4444

45-
const monoidPrinter: Mon.Monoid<Printer<Array<string>>> = getMonoid(Mon.getArrayMonoid<string>())
45+
const monoidPrinter: Mon.Monoid<Printer<Array<string>>> = getMonoid(A.getMonoid<string>())
4646

4747
export const all = (d: M.Data): Printer<Array<string>> => {
48-
return Mon.fold(monoidPrinter)([data(d).map(array.of), constructors(d), folds(d), prisms(d), setoid(d)])
48+
return Mon.fold(monoidPrinter)([data(d).map(A.array.of), constructors(d), folds(d), prisms(d), setoid(d)])
4949
}
5050

5151
export const print = (d: M.Data, options: Ast.Options): string => {

test/examples.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export const Maybe = M.data('Maybe', [M.parameterDeclaration('A')], M.constructo
1414
// data Either L R = Left L | Right R
1515
export const Either = M.data(
1616
'Either',
17-
[M.parameterDeclaration('L'), M.parameterDeclaration('R')],
18-
M.constructor('Left', [M.member(M.ref('L'))]),
17+
[M.parameterDeclaration('E'), M.parameterDeclaration('R')],
18+
M.constructor('Left', [M.member(M.ref('E'))]),
1919
[M.constructor('Right', [M.member(M.ref('R'))])]
2020
)
2121

0 commit comments

Comments
 (0)