From 988b01447f419d59dbf06a328308a76ed2ba1d3a Mon Sep 17 00:00:00 2001 From: otofu-square Date: Fri, 6 Sep 2019 18:22:31 +0900 Subject: [PATCH 1/5] feat(eslint-plugin): handle conditional types in no-type-alias rule --- .../eslint-plugin/src/rules/no-type-alias.ts | 16 ++++++++++ .../tests/rules/no-type-alias.test.ts | 31 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/packages/eslint-plugin/src/rules/no-type-alias.ts b/packages/eslint-plugin/src/rules/no-type-alias.ts index 38816b60a2fa..b73678c668ce 100644 --- a/packages/eslint-plugin/src/rules/no-type-alias.ts +++ b/packages/eslint-plugin/src/rules/no-type-alias.ts @@ -22,6 +22,7 @@ type Options = [ { allowAliases?: Values; allowCallbacks?: 'always' | 'never'; + allowConditionalTypes?: 'always' | 'never'; allowLiterals?: Values; allowMappedTypes?: Values; allowTupleTypes?: Values; @@ -62,6 +63,9 @@ export default util.createRule({ allowCallbacks: { enum: ['always', 'never'], }, + allowConditionalTypes: { + enum: ['always', 'never'], + }, allowLiterals: { enum: enumValues, }, @@ -80,6 +84,7 @@ export default util.createRule({ { allowAliases: 'never', allowCallbacks: 'never', + allowConditionalTypes: 'never', allowLiterals: 'never', allowMappedTypes: 'never', allowTupleTypes: 'never', @@ -91,6 +96,7 @@ export default util.createRule({ { allowAliases, allowCallbacks, + allowConditionalTypes, allowLiterals, allowMappedTypes, allowTupleTypes, @@ -220,6 +226,16 @@ export default util.createRule({ 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.TSTypeLiteral) { // literal object type checkAndReport(allowLiterals!, isTopLevel, type, 'Literals'); diff --git a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts index d17348af7460..c36721fb8969 100644 --- a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts +++ b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts @@ -439,6 +439,10 @@ type Foo = { 'type Foo = [string] & [number, number] | keyof [number, number, number];', options: [{ allowTupleTypes: 'in-unions-and-intersections' }], }, + { + code: 'type MyType = T extends number ? number : null;', + options: [{ allowConditionalTypes: 'always' }], + }, ], invalid: [ { @@ -3176,5 +3180,32 @@ type Foo = { }, ], }, + { + code: 'type MyType = T extends number ? number : null;', + errors: [ + { + messageId: 'noTypeAlias', + data: { + alias: 'conditional types', + }, + line: 1, + column: 18, + }, + ], + }, + { + code: 'type MyType = T extends number ? number : null;', + options: [{ allowConditionalTypes: 'never' }], + errors: [ + { + messageId: 'noTypeAlias', + data: { + alias: 'conditional types', + }, + line: 1, + column: 18, + }, + ], + }, ], }); From c8be2c14801d568804a33738365a546698726879 Mon Sep 17 00:00:00 2001 From: otofu-square Date: Fri, 6 Sep 2019 18:28:40 +0900 Subject: [PATCH 2/5] docs(eslint-plugin): add docs for allowConditionalTypes option --- .../eslint-plugin/docs/rules/no-type-alias.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/eslint-plugin/docs/rules/no-type-alias.md b/packages/eslint-plugin/docs/rules/no-type-alias.md index 46230f3d3293..1c30da6deeb6 100644 --- a/packages/eslint-plugin/docs/rules/no-type-alias.md +++ b/packages/eslint-plugin/docs/rules/no-type-alias.md @@ -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"`) - `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"`) @@ -248,6 +249,20 @@ type Foo = (name: string, age: number) => string | Person; type Foo = (name: string, age: number) => string & Person; ``` +### allowConditionalTypes + +This applies to conditional types. + +The setting accepts the following values: + +- `"always"` or `"never"` to active or deactivate the feature. + +Examples of **correct** code for the `{ "allowConditionalTypes": "always" }` option: + +```ts +type Foo = T extends number ? number : null; +``` + ### allowLiterals This applies to literal types (`type Foo = { ... }`). From 2be4f1a73ed16ac2a9b42ab9f9c093c7551a1c93 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 14 Nov 2019 09:27:14 -0800 Subject: [PATCH 3/5] fix incorrect merge --- .../eslint-plugin/tests/rules/no-type-alias.test.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts index 347b9f47d660..6ef683d0fdba 100644 --- a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts +++ b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts @@ -3191,7 +3191,7 @@ type Foo = { { messageId: 'noTypeAlias', data: { - alias: 'conditional types', + alias: 'constructors', }, line: 1, column: 18, @@ -3208,10 +3208,6 @@ type Foo = { }, line: 1, column: 18, - alias: 'constructors', - line: 1, - column: 12, - }, }, ], }, @@ -3226,10 +3222,6 @@ type Foo = { }, line: 1, column: 18, - alias: 'constructors', - line: 1, - column: 12, - }, }, ], }, From b73e26ba0138aee23728074b1721337e840b38d8 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 14 Nov 2019 09:29:47 -0800 Subject: [PATCH 4/5] fix merge --- packages/eslint-plugin/tests/rules/no-type-alias.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts index 6ef683d0fdba..be5ca84b9e5b 100644 --- a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts +++ b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts @@ -3194,7 +3194,7 @@ type Foo = { alias: 'constructors', }, line: 1, - column: 18, + column: 12, }, ], }, From 5bbc65226f0a2db4137d03be9ec1ccba1ac47116 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 14 Nov 2019 09:40:42 -0800 Subject: [PATCH 5/5] fix: broken merge --- packages/eslint-plugin/docs/rules/no-type-alias.md | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/eslint-plugin/docs/rules/no-type-alias.md b/packages/eslint-plugin/docs/rules/no-type-alias.md index aed4dd8cc3f5..1927fdd54c9a 100644 --- a/packages/eslint-plugin/docs/rules/no-type-alias.md +++ b/packages/eslint-plugin/docs/rules/no-type-alias.md @@ -258,6 +258,7 @@ Examples of **correct** code for the `{ "allowConditionalTypes": "always" }` opt ```ts type Foo = T extends number ? number : null; +``` ### allowConstructors