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(eslint-plugin): [no-type-alias] handle conditional types #953

Merged
merged 6 commits into from
Nov 14, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/eslint-plugin/docs/rules/no-type-alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ or more of the following you may pass an object with the options set as follows:

- `allowAliases` set to `"always"` will allow you to do aliasing (Defaults to `"never"`).
- `allowCallbacks` set to `"always"` will allow you to use type aliases with callbacks (Defaults to `"never"`)
- `allowConditionalTypes` set to `"always"` will allow you to use type aliases with conditional types (Defaults to `"never"`)
- `allowConstructors` set to `"always"` will allow you to use type aliases with constructors (Defaults to `"never"`)
- `allowLiterals` set to `"always"` will allow you to use type aliases with literal objects (Defaults to `"never"`)
- `allowMappedTypes` set to `"always"` will allow you to use type aliases as mapping tools (Defaults to `"never"`)
Expand Down Expand Up @@ -249,6 +250,16 @@ type Foo = (name: string, age: number) => string | Person;
type Foo = (name: string, age: number) => string & Person;
```

### allowConditionalTypes

This applies to conditional types.

Examples of **correct** code for the `{ "allowConditionalTypes": "always" }` option:

```ts
type Foo<T> = T extends number ? number : null;
```

### allowConstructors

This applies to constructor types.
Expand Down
16 changes: 16 additions & 0 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Options = [
{
allowAliases?: Values;
allowCallbacks?: 'always' | 'never';
allowConditionalTypes?: 'always' | 'never';
allowConstructors?: 'always' | 'never';
allowLiterals?: Values;
allowMappedTypes?: Values;
Expand Down Expand Up @@ -63,6 +64,9 @@ export default util.createRule<Options, MessageIds>({
allowCallbacks: {
enum: ['always', 'never'],
},
allowConditionalTypes: {
enum: ['always', 'never'],
},
allowConstructors: {
enum: ['always', 'never'],
},
Expand All @@ -84,6 +88,7 @@ export default util.createRule<Options, MessageIds>({
{
allowAliases: 'never',
allowCallbacks: 'never',
allowConditionalTypes: 'never',
allowConstructors: 'never',
allowLiterals: 'never',
allowMappedTypes: 'never',
Expand All @@ -96,6 +101,7 @@ export default util.createRule<Options, MessageIds>({
{
allowAliases,
allowCallbacks,
allowConditionalTypes,
allowConstructors,
allowLiterals,
allowMappedTypes,
Expand Down Expand Up @@ -226,6 +232,16 @@ export default util.createRule<Options, MessageIds>({
if (allowCallbacks === 'never') {
reportError(type.node, type.compositionType, isTopLevel, 'Callbacks');
}
} else if (type.node.type === AST_NODE_TYPES.TSConditionalType) {
// conditional type
if (allowConditionalTypes === 'never') {
reportError(
type.node,
type.compositionType,
isTopLevel,
'Conditional types',
);
}
} else if (type.node.type === AST_NODE_TYPES.TSConstructorType) {
if (allowConstructors === 'never') {
reportError(
Expand Down
35 changes: 33 additions & 2 deletions packages/eslint-plugin/tests/rules/no-type-alias.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ type Foo<T> = {
'type Foo = [string] & [number, number] | keyof [number, number, number];',
options: [{ allowTupleTypes: 'in-unions-and-intersections' }],
},
{
code: 'type MyType<T> = T extends number ? number : null;',
options: [{ allowConditionalTypes: 'always' }],
},
{
code: 'type Foo = new (bar: number) => string | null;',
options: [{ allowConstructors: 'always' }],
Expand Down Expand Up @@ -3188,9 +3192,36 @@ type Foo<T> = {
messageId: 'noTypeAlias',
data: {
alias: 'constructors',
line: 1,
column: 12,
},
line: 1,
column: 12,
},
],
},
{
code: 'type MyType<T> = T extends number ? number : null;',
errors: [
{
messageId: 'noTypeAlias',
data: {
alias: 'conditional types',
},
line: 1,
column: 18,
},
],
},
{
code: 'type MyType<T> = T extends number ? number : null;',
options: [{ allowConditionalTypes: 'never' }],
errors: [
{
messageId: 'noTypeAlias',
data: {
alias: 'conditional types',
},
line: 1,
column: 18,
},
],
},
Expand Down