Skip to content

Commit

Permalink
Merge pull request #56 from grafana/disjunct-parens
Browse files Browse the repository at this point in the history
ts: Generate all unions inside parentheses
  • Loading branch information
sam boyer authored Jun 1, 2022
2 parents c119b17 + e6d773d commit 6e43973
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion testdata/defaults_interface.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ NestedFoo: {

-- ts --

export type AType = 'foo' | 'bar' | 'baz';
export type AType = ('foo' | 'bar' | 'baz');

export const defaultAType: AType = 'baz';

Expand Down
Empty file.
4 changes: 2 additions & 2 deletions testdata/imports/force_text.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export enum MyType {
B = 2,
}

export type ModeOptions = ModeOptionsA | ModeOptionsB;
export type ModeOptions = (ModeOptionsA | ModeOptionsB);

export interface ModeOptionsA {
aProp: boolean;
Expand All @@ -35,4 +35,4 @@ export interface ModeOptionsA {
export interface ModeOptionsB {
bProp: boolean;
type: MyType.B;
}
}
4 changes: 2 additions & 2 deletions testdata/imports/union.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export interface LocalStruct {

export type LocalScalar = string;

export type UnionType = With | Without | LocalStruct | LocalScalar;
export type UnionType = (With | Without | LocalStruct | LocalScalar);

export interface UnionInterface {
field: With | Without | LocalStruct | LocalScalar;
field: (With | Without | LocalStruct | LocalScalar);
}
41 changes: 41 additions & 0 deletions testdata/imports/union_paren.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- cue.mod/module.cue --
module: "example.com"

-- one.cue --
package test

import "example.com/dep"

U1: "foo" | "bar" | "baz" @cuetsy(kind="type")
U2: U1 | "bix" @cuetsy(kind="type")
U3: {
bigU: dep.U1 | U2 | U1
listU: [...(dep.U1 | U2 | U1)]
} @cuetsy(kind="interface")

-- dep/file.cue --
package dep

U1: 1 | 2 | 3 @cuetsy(kind="type")

NoAttrib: {
ImportedInner: {
nestedval: string
}
importedNoAttribField: string
}

-- out/gen --

export type U1 = ('foo' | 'bar' | 'baz');

export type U2 = (U1 | 'bix');

export interface U3 {
bigU: (dep.U1 | U2 | U1);
listU: (dep.U1 | U2 | U1)[];
}

export const defaultU3: Partial<U3> = {
listU: [],
};
2 changes: 1 addition & 1 deletion testdata/interfaces.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export enum E2 {

export interface I1 {
I1_FloatLiteral: 4.4;
I1_OptionalDisjunctionLiteral?: 'other' | 'values' | 2;
I1_OptionalDisjunctionLiteral?: ('other' | 'values' | 2);
I1_Top: any;
}

Expand Down
13 changes: 13 additions & 0 deletions ts/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ func (k KeyValueExpr) innerString(eol EOL, lvl int) string {
return fmt.Sprintf("%s: %s", k.Key, innerString(eol, lvl, k.Value))
}

type ParenExpr struct {
Expr Expr
}

func (p ParenExpr) expr() {}
func (p ParenExpr) String() string {
return p.innerString(EOL(""), 0)
}

func (p ParenExpr) innerString(eol EOL, lvl int) string {
return fmt.Sprintf("(%s)", innerString(eol, lvl, p.Expr))
}

type UnaryExpr struct {
Op string // operator
Expr Expr // operand
Expand Down
2 changes: 1 addition & 1 deletion ts/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Union(elems ...Expr) Expr {
}
}

return U
return ast.ParenExpr{Expr: U}
}

func Export(decl ast.Decl) Decl {
Expand Down

0 comments on commit 6e43973

Please sign in to comment.