Skip to content

Commit

Permalink
fix(eslint-plugin): [no-type-alias] handle constructor aliases (#1198)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored and bradzacher committed Nov 12, 2019
1 parent c5835f3 commit 1bb4d63
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
15 changes: 15 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"`)
- `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"`)
- `allowTupleTypes` set to `"always"` will allow you to use type aliases with tuples (Defaults to `"never"`)
Expand Down Expand Up @@ -248,6 +249,20 @@ type Foo = (name: string, age: number) => string | Person;
type Foo = (name: string, age: number) => string & Person;
```

### allowConstructors

This applies to constructor types.

The setting accepts the following values:

- `"always"` or `"never"` to active or deactivate the feature.

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

```ts
type Foo = new () => void;
```

### allowLiterals

This applies to literal types (`type Foo = { ... }`).
Expand Down
15 changes: 15 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';
allowConstructors?: 'always' | 'never';
allowLiterals?: Values;
allowMappedTypes?: Values;
allowTupleTypes?: Values;
Expand Down Expand Up @@ -62,6 +63,9 @@ export default util.createRule<Options, MessageIds>({
allowCallbacks: {
enum: ['always', 'never'],
},
allowConstructors: {
enum: ['always', 'never'],
},
allowLiterals: {
enum: enumValues,
},
Expand All @@ -80,6 +84,7 @@ export default util.createRule<Options, MessageIds>({
{
allowAliases: 'never',
allowCallbacks: 'never',
allowConstructors: 'never',
allowLiterals: 'never',
allowMappedTypes: 'never',
allowTupleTypes: 'never',
Expand All @@ -91,6 +96,7 @@ export default util.createRule<Options, MessageIds>({
{
allowAliases,
allowCallbacks,
allowConstructors,
allowLiterals,
allowMappedTypes,
allowTupleTypes,
Expand Down Expand Up @@ -220,6 +226,15 @@ export default util.createRule<Options, MessageIds>({
if (allowCallbacks === 'never') {
reportError(type.node, type.compositionType, isTopLevel, 'Callbacks');
}
} else if (type.node.type === AST_NODE_TYPES.TSConstructorType) {
if (allowConstructors === 'never') {
reportError(
type.node,
type.compositionType,
isTopLevel,
'Constructors',
);
}
} else if (type.node.type === AST_NODE_TYPES.TSTypeLiteral) {
// literal object type
checkAndReport(allowLiterals!, isTopLevel, type, 'Literals');
Expand Down
18 changes: 18 additions & 0 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 Foo = new (bar: number) => string | null;',
options: [{ allowConstructors: 'always' }],
},
],
invalid: [
{
Expand Down Expand Up @@ -3176,5 +3180,19 @@ type Foo<T> = {
},
],
},
{
code: 'type Foo = new (bar: number) => string | null;',
options: [{ allowConstructors: 'never' }],
errors: [
{
messageId: 'noTypeAlias',
data: {
alias: 'constructors',
line: 1,
column: 12,
},
},
],
},
],
});

0 comments on commit 1bb4d63

Please sign in to comment.