Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(eslint-plugin): [no-type-alias] handle Template Literal Types (#5092
)

Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>
  • Loading branch information
a-tarasyuk and JoshuaKGoldberg committed May 29, 2022
1 parent eaa99cf commit 8febf11
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/eslint-plugin/docs/rules/no-type-alias.md
Expand Up @@ -118,6 +118,8 @@ type Foo = string | string[];

type Foo = string & string[];

type Foo = `foo-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}
Expand All @@ -139,6 +141,8 @@ type Foo = string;

type Foo = string & string[];

type Foo = `foo-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}
Expand All @@ -156,6 +160,8 @@ type Foo = 'a' | 'b';

type Foo = string | string[];

type Foo = `a-${number}` | `b-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}
Expand All @@ -175,6 +181,8 @@ type Foo = string;

type Foo = string | string[];

type Foo = `a-${number}` | `b-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}
Expand All @@ -190,6 +198,8 @@ Examples of **correct** code for the `{ "allowAliases": "in-intersections" }` op
// primitives
type Foo = string & string[];

type Foo = `a-${number}` & `b-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}
Expand All @@ -205,6 +215,8 @@ type Foo = 'a';

type Foo = string;

type Foo = `foo-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}
Expand All @@ -222,6 +234,10 @@ type Foo = string | string[];

type Foo = string & string[];

type Foo = `a-${number}` & `b-${number}`;

type Foo = `a-${number}` | `b-${number}`;

// reference types
interface Bar {}
class Baz implements Bar {}
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
Expand Up @@ -133,6 +133,7 @@ export default util.createRule<Options, MessageIds>({
AST_NODE_TYPES.TSLiteralType,
AST_NODE_TYPES.TSTypeQuery,
AST_NODE_TYPES.TSIndexedAccessType,
AST_NODE_TYPES.TSTemplateLiteralType,
]);

/**
Expand Down
121 changes: 121 additions & 0 deletions packages/eslint-plugin/tests/rules/no-type-alias.test.ts
Expand Up @@ -131,6 +131,66 @@ ruleTester.run('no-type-alias', rule, {
code: 'type Foo = 1 | (2 & 3);',
options: [{ allowAliases: 'in-unions-and-intersections' }],
},
{
code: 'type Foo = `a-${number}`;',
options: [{ allowAliases: 'always' }],
},
{
code: 'type Foo = `a-${number}` | `b-${number}`;',
options: [{ allowAliases: 'always' }],
},
{
code: 'type Foo = `a-${number}` | `b-${number}`;',
options: [{ allowAliases: 'in-unions-and-intersections' }],
},
{
code: 'type Foo = `a-${number}` | `b-${number}`;',
options: [{ allowAliases: 'in-unions' }],
},
{
code: 'type Foo = `a-${number}` | `b-${number}` | `c-${number}`;',
options: [{ allowAliases: 'always' }],
},
{
code: 'type Foo = `a-${number}` | `b-${number}` | `c-${number}`;',
options: [{ allowAliases: 'in-unions-and-intersections' }],
},
{
code: 'type Foo = `a-${number}` | `b-${number}` | `c-${number}`;',
options: [{ allowAliases: 'in-unions' }],
},
{
code: 'type Foo = `a-${number}` & `b-${number}`;',
options: [{ allowAliases: 'always' }],
},
{
code: 'type Foo = `a-${number}` & `b-${number}`;',
options: [{ allowAliases: 'in-unions-and-intersections' }],
},
{
code: 'type Foo = `a-${number}` & `b-${number}`;',
options: [{ allowAliases: 'in-intersections' }],
},
{
code: 'type Foo = `a-${number}` & `b-${number}` & `c-${number}`;',
options: [{ allowAliases: 'always' }],
},
{
code: 'type Foo = `a-${number}` & `b-${number}` & `c-${number}`;',
options: [{ allowAliases: 'in-unions-and-intersections' }],
},
{
code: 'type Foo = `a-${number}` & `b-${number}` & `c-${number}`;',
options: [{ allowAliases: 'in-intersections' }],
},
{
code: 'type Foo = `a-${number}` | (`b-${number}` & `c-${number}`);',
options: [{ allowAliases: 'always' }],
},
{
code: 'type Foo = `a-${number}` | (`b-${number}` & `c-${number}`);',
options: [{ allowAliases: 'in-unions-and-intersections' }],
},
{
code: 'type Foo = true;',
options: [{ allowAliases: 'always' }],
Expand Down Expand Up @@ -3340,5 +3400,66 @@ type Foo<T> = {
},
],
},
{
code: 'type Foo = `foo-${number}`;',
errors: [
{
messageId: 'noTypeAlias',
data: {
alias: 'aliases',
},
line: 1,
column: 12,
},
],
},
{
code: 'type Foo = `a-${number}` | `b-${number}`;',
options: [{ allowAliases: 'never' }],
errors: [
{
messageId: 'noCompositionAlias',
data: {
typeName: 'Aliases',
compositionType: 'union',
},
line: 1,
column: 12,
},
{
messageId: 'noCompositionAlias',
data: {
typeName: 'Aliases',
compositionType: 'union',
},
line: 1,
column: 28,
},
],
},
{
code: 'type Foo = `a-${number}` & `b-${number}`;',
options: [{ allowAliases: 'never' }],
errors: [
{
messageId: 'noCompositionAlias',
data: {
typeName: 'Aliases',
compositionType: 'intersection',
},
line: 1,
column: 12,
},
{
messageId: 'noCompositionAlias',
data: {
typeName: 'Aliases',
compositionType: 'intersection',
},
line: 1,
column: 28,
},
],
},
],
});

0 comments on commit 8febf11

Please sign in to comment.