Skip to content

Commit

Permalink
fix(types): Fixes array over union sub-return-types. (#31)
Browse files Browse the repository at this point in the history
* Fixes array over union sub-return-types.

Wraps the ${first} return type in parentheses so that array type augments the whole sub-type e.g., in case the sub-type is a union.

E.g.,
```
From: mget(...keys: string[]): Promise<string | null[]>;   // string | Array<null>
  to: mget(...keys: string[]): Promise<(string | null)[]>; // Array<string | null>

From: hmget(key: string, ...fields: string[]): Promise<string | null[]>;   // string | Array<null>
  to: hmget(key: string, ...fields: string[]): Promise<(string | null)[]>; // Array<string | null>
```

* Reduces noise in and includes generated code for PR#31

Adds generated code for CI guard
Reduces noise in generated types via extra check for pipe character so that parens are only added when the generated array sub-type is actually a union type. This seems to work fine for the current data types. Hopefully we won't ever need a parser to actually check for balanced parens various expressions before adding more.

* Changes most generated array types to use `Array<>` syntax per mmkal.

* Restores const for `first` return type.

* chore(style): use brackets for simple types
  • Loading branch information
nl-brett-stime authored and mmkal committed Jan 25, 2019
1 parent 62961d4 commit 37b52f9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion scripts/generate-usages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export const getReturnValuesFuncSrc = async () => {

type ReturnValuesMap = Map<string, any[]>;

const simpleTokenRegex = /^\w+$/;
const getReturnType = (sampleValues: any[] | undefined): string => {
if (!sampleValues || sampleValues.length === 0) {
return "any";
Expand All @@ -249,7 +250,7 @@ const getReturnType = (sampleValues: any[] | undefined): string => {
const itemReturnTypes = new Set(sampleValues.map(getReturnType));
if (itemReturnTypes.size === 1) {
const first = itemReturnTypes.values().next().value;
return `${first}[]`;
return simpleTokenRegex.test(first) ? `${first}[]` : `Array<${first}>`;
}
return "any[]";
}
Expand Down
10 changes: 5 additions & 5 deletions src/generated/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export interface IHandyRedis extends AdditionalFunctions {
*/
command(
...args: any[]
): Promise<any[][]>;
): Promise<Array<any[]>>;
/**
* summary: 'Get array of Redis command details'
*
Expand All @@ -704,7 +704,7 @@ export interface IHandyRedis extends AdditionalFunctions {
*
* group: server
*/
command(): Promise<any[][]>;
command(): Promise<Array<any[]>>;
/**
* summary: 'Return the number of keys in the selected database'
*
Expand Down Expand Up @@ -1022,7 +1022,7 @@ export interface IHandyRedis extends AdditionalFunctions {
geopos(
key: string,
...members: string[]
): Promise<string[] | null[]>;
): Promise<Array<string[] | null>>;
/**
* summary: 'Returns the distance between two members of a geospatial index'
*
Expand Down Expand Up @@ -12579,7 +12579,7 @@ export interface IHandyRedis extends AdditionalFunctions {
hmget(
key: string,
...fields: string[]
): Promise<string | null[]>;
): Promise<Array<string | null>>;
/**
* summary: 'Set multiple hash fields to multiple values'
*
Expand Down Expand Up @@ -12999,7 +12999,7 @@ export interface IHandyRedis extends AdditionalFunctions {
*/
mget(
...keys: string[]
): Promise<string | null[]>;
): Promise<Array<string | null>>;
/**
* summary: 'Atomically transfer a key from a Redis instance to another one.'
*
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"tslint:recommended"
],
"rules": {
"array-type": false,
"no-console": [
true
],
Expand Down

0 comments on commit 37b52f9

Please sign in to comment.