Skip to content
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

feat: Add flag to detect unknown named types #106

Merged
merged 1 commit into from
Jun 1, 2021
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
29 changes: 22 additions & 7 deletions docs/interfaces/main.generateoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- [includeImport](main.generateoptions.md#includeimport)
- [includeTypes](main.generateoptions.md#includetypes)
- [rejectCyclicDependencies](main.generateoptions.md#rejectcyclicdependencies)
- [rejectUnknownNamedTypes](main.generateoptions.md#rejectunknownnamedtypes)

## Properties

Expand All @@ -24,7 +25,7 @@

Apply formatting to the output using prettier. Default: true

Defined in: [src/main.ts:59](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/main.ts#L59)
Defined in: [src/main.ts:58](https://github.com/cobraz/generate-runtypes/blob/7317811/src/main.ts#L58)

___

Expand All @@ -34,7 +35,7 @@ ___

Options to use for prettier formatting. Default: undefined

Defined in: [src/main.ts:62](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/main.ts#L62)
Defined in: [src/main.ts:61](https://github.com/cobraz/generate-runtypes/blob/7317811/src/main.ts#L61)

___

Expand All @@ -46,7 +47,7 @@ Function used to format the names of generated runtypes.
The function is passed in a name and must return a string that will be
used in place of that name.

Defined in: [src/main.ts:96](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/main.ts#L96)
Defined in: [src/main.ts:95](https://github.com/cobraz/generate-runtypes/blob/7317811/src/main.ts#L95)

___

Expand All @@ -58,7 +59,7 @@ Function used to format the names of generated type.
The function is passed in a name and must return a string that will be
used in place of that name.

Defined in: [src/main.ts:103](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/main.ts#L103)
Defined in: [src/main.ts:102](https://github.com/cobraz/generate-runtypes/blob/7317811/src/main.ts#L102)

___

Expand All @@ -71,7 +72,7 @@ When turned on, `import * as rt from "runtypes";` will be added at the
top of the generated code.
Default: true

Defined in: [src/main.ts:70](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/main.ts#L70)
Defined in: [src/main.ts:69](https://github.com/cobraz/generate-runtypes/blob/7317811/src/main.ts#L69)

___

Expand All @@ -95,7 +96,7 @@ const myRuntype = rt.Record({ name: rt.String });
type MyRuntype = rt.Static<typeof myRuntype>;
```

Defined in: [src/main.ts:89](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/main.ts#L89)
Defined in: [src/main.ts:88](https://github.com/cobraz/generate-runtypes/blob/7317811/src/main.ts#L88)

___

Expand All @@ -107,4 +108,18 @@ Whether to throw when encountering root types with cyclic dependencies,
or emit possibly broken code for them.
Default: false

Defined in: [src/main.ts:110](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/main.ts#L110)
Defined in: [src/main.ts:109](https://github.com/cobraz/generate-runtypes/blob/7317811/src/main.ts#L109)

___

### rejectUnknownNamedTypes

• `Optional` **rejectUnknownNamedTypes**: *boolean*

Whether to throw when encountering a named type that's not one of the, root
types. Useful for caching typos when generating code. Must be disabled when
using named types that are not part of the ones being generated.

Default: false

Defined in: [src/main.ts:118](https://github.com/cobraz/generate-runtypes/blob/7317811/src/main.ts#L118)
2 changes: 1 addition & 1 deletion docs/modules/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ Re-exports: [rootTypeRt](types.md#roottypert)

**Returns:** *string*

Defined in: [src/main.ts:127](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/main.ts#L127)
Defined in: [src/main.ts:136](https://github.com/cobraz/generate-runtypes/blob/7317811/src/main.ts#L136)
24 changes: 12 additions & 12 deletions docs/modules/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

Ƭ **AnyType**: *rt.Static*<*typeof* anyTypeRt\>

Defined in: [src/types.ts:162](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L162)
Defined in: [src/types.ts:162](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L162)

___

Expand All @@ -44,7 +44,7 @@ ___
| `readonly?` | *boolean* |
| `type` | [*AnyType*](types.md#anytype) |

Defined in: [src/types.ts:78](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L78)
Defined in: [src/types.ts:78](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L78)

___

Expand All @@ -59,7 +59,7 @@ ___
| `kind` | ``"dictionary"`` |
| `valueType` | [*AnyType*](types.md#anytype) |

Defined in: [src/types.ts:95](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L95)
Defined in: [src/types.ts:95](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L95)

___

Expand All @@ -74,23 +74,23 @@ ___
| `kind` | ``"intersect"`` |
| `types` | [*AnyType*](types.md#anytype)[] |

Defined in: [src/types.ts:130](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L130)
Defined in: [src/types.ts:130](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L130)

___

### LiteralType

Ƭ **LiteralType**: *rt.Static*<*typeof* literalTypeRt\>

Defined in: [src/types.ts:32](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L32)
Defined in: [src/types.ts:32](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L32)

___

### NamedType

Ƭ **NamedType**: *rt.Static*<*typeof* namedTypeRt\>

Defined in: [src/types.ts:46](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L46)
Defined in: [src/types.ts:46](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L46)

___

Expand All @@ -108,7 +108,7 @@ ___
| `readonly?` | *boolean* |
| `type` | [*AnyType*](types.md#anytype) |

Defined in: [src/types.ts:48](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L48)
Defined in: [src/types.ts:48](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L48)

___

Expand All @@ -123,23 +123,23 @@ ___
| `fields` | [*RecordField*](types.md#recordfield)[] |
| `kind` | ``"record"`` |

Defined in: [src/types.ts:56](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L56)
Defined in: [src/types.ts:56](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L56)

___

### RootType

Ƭ **RootType**: *rt.Static*<*typeof* [*rootTypeRt*](types.md#roottypert)\>

Defined in: [src/types.ts:174](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L174)
Defined in: [src/types.ts:174](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L174)

___

### SimpleType

Ƭ **SimpleType**: *rt.Static*<*typeof* simpleTypeRt\>

Defined in: [src/types.ts:21](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L21)
Defined in: [src/types.ts:21](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L21)

___

Expand All @@ -154,12 +154,12 @@ ___
| `kind` | ``"union"`` |
| `types` | [*AnyType*](types.md#anytype)[] |

Defined in: [src/types.ts:110](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L110)
Defined in: [src/types.ts:110](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L110)

## Variables

### rootTypeRt

• `Const` **rootTypeRt**: *Intersect*<[*Record*<{ `name`: *String* ; `type`: *Union*<[*Runtype*<[*ArrayType*](types.md#arraytype)\>, *Runtype*<[*DictionaryType*](types.md#dictionarytype)\>, *Runtype*<[*IntersectionType*](types.md#intersectiontype)\>, *Record*<{ `kind`: *Literal*<``"literal"``\> ; `value`: *Union*<[*Boolean*, *Literal*<``null``\>, *Number*, *String*, *Literal*<undefined\>]\> }, ``false``\>, *Record*<{ `kind`: *Literal*<``"named"``\> ; `name`: *String* }, ``false``\>, *Runtype*<[*RecordType*](types.md#recordtype)\>, *Record*<{ `kind`: *Union*<[*Literal*<``"boolean"``\>, *Literal*<``"function"``\>, *Literal*<``"never"``\>, *Literal*<``"null"``\>, *Literal*<``"number"``\>, *Literal*<``"string"``\>, *Literal*<``"symbol"``\>, *Literal*<``"undefined"``\>, *Literal*<``"unknown"``\>]\> }, ``false``\>, *Runtype*<[*UnionType*](types.md#uniontype)\>]\> }, ``false``\>, *InternalRecord*<{ `comment`: *Union*<[*String*, *Arr*<String, ``false``\>]\> ; `export`: *Boolean* }, ``true``, ``false``\>]\>

Defined in: [src/types.ts:164](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/types.ts#L164)
Defined in: [src/types.ts:164](https://github.com/cobraz/generate-runtypes/blob/7317811/src/types.ts#L164)
25 changes: 22 additions & 3 deletions docs/modules/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- [getCyclicDependencies](util.md#getcyclicdependencies)
- [getNamedTypes](util.md#getnamedtypes)
- [getUnknownNamedTypes](util.md#getunknownnamedtypes)
- [groupFieldKinds](util.md#groupfieldkinds)

## Functions
Expand All @@ -32,7 +33,7 @@ types.

**Returns:** [*string*, *string*][]

Defined in: [src/util.ts:123](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/util.ts#L123)
Defined in: [src/util.ts:123](https://github.com/cobraz/generate-runtypes/blob/7317811/src/util.ts#L123)

___

Expand All @@ -52,7 +53,25 @@ public for testing

**Returns:** readonly *string*[]

Defined in: [src/util.ts:72](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/util.ts#L72)
Defined in: [src/util.ts:72](https://github.com/cobraz/generate-runtypes/blob/7317811/src/util.ts#L72)

___

### getUnknownNamedTypes

▸ `Private` **getUnknownNamedTypes**(`roots`: [*RootType*](types.md#roottype)[]): readonly *string*[]

public for testing

#### Parameters

| Name | Type |
| :------ | :------ |
| `roots` | [*RootType*](types.md#roottype)[] |

**Returns:** readonly *string*[]

Defined in: [src/util.ts:154](https://github.com/cobraz/generate-runtypes/blob/7317811/src/util.ts#L154)

___

Expand All @@ -72,4 +91,4 @@ Used to evaluate if `Record` type include `readonly` and/or `nullable`

**Returns:** { `fields`: [*RecordField*](types.md#recordfield)[] ; `nullable`: *boolean* ; `readonly`: *boolean* }[]

Defined in: [src/util.ts:10](https://github.com/cobraz/generate-runtypes/blob/0a259e5/src/util.ts#L10)
Defined in: [src/util.ts:10](https://github.com/cobraz/generate-runtypes/blob/7317811/src/util.ts#L10)
43 changes: 41 additions & 2 deletions src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { RootType } from '../main';
import { getCyclicDependencies, getNamedTypes } from '../util';
import { groupFieldKinds } from '../util';
import {
getCyclicDependencies,
getNamedTypes,
getUnknownNamedTypes,
groupFieldKinds,
} from '../util';

describe('groupFieldKinds', () => {
it('smoke test', () => {
Expand Down Expand Up @@ -199,3 +203,38 @@ describe('getNamedTypes', () => {

it.todo('unions, arrays etc');
});

describe('getUknownNamedTypes', () => {
it('finds unknown types', () => {
const roots: RootType[] = [
{ name: 'internal', type: { kind: 'string' } },
{
name: 'foo',
type: {
kind: 'record',
fields: [
{ name: 'first', type: { kind: 'named', name: 'external' } },
{ name: 'second', type: { kind: 'named', name: 'internal' } },
],
},
},
];
expect(getUnknownNamedTypes(roots)).toEqual(['external']);
});

it('empty result when no unknown', () => {
const roots: RootType[] = [
{ name: 'internal', type: { kind: 'string' } },
{
name: 'foo',
type: {
kind: 'record',
fields: [
{ name: 'second', type: { kind: 'named', name: 'internal' } },
],
},
},
];
expect(getUnknownNamedTypes(roots)).toEqual([]);
});
});
13 changes: 11 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
UnionType,
rootTypeRt,
} from './types';
import { getCyclicDependencies } from './util';
import { groupFieldKinds } from './util';
import { getCyclicDependencies, groupFieldKinds } from './util';

export type {
PrettierOptions,
Expand Down Expand Up @@ -108,6 +107,15 @@ export interface GenerateOptions {
* Default: false
*/
rejectCyclicDependencies?: boolean;

/**
* Whether to throw when encountering a named type that's not one of the, root
* types. Useful for caching typos when generating code. Must be disabled when
* using named types that are not part of the ones being generated.
*
* Default: false
*/
rejectUnknownNamedTypes?: boolean;
}

const defaultOptions: GenerateOptions = {
Expand All @@ -117,6 +125,7 @@ const defaultOptions: GenerateOptions = {
formatRuntypeName: (e) => e[0].toLowerCase() + e.slice(1),
formatTypeName: (e) => e[0].toUpperCase() + e.slice(1),
rejectCyclicDependencies: false,
rejectUnknownNamedTypes: false,
};

/**
Expand Down
12 changes: 12 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,15 @@ export function getCyclicDependencies(roots: RootType[]): [string, string][] {

return ret;
}

/**
* public for testing
* @private
* @param roots
* @returns
*/
export function getUnknownNamedTypes(roots: RootType[]): readonly string[] {
const rootNames = roots.map((e) => e.name);
const namedTypes = roots.flatMap((e) => getNamedTypes(e.type));
return namedTypes.filter((e) => !rootNames.includes(e));
}