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

fix(eslint-plugin): [no-type-alias] handle Template Literal Types #5092

Merged
merged 2 commits into from May 29, 2022
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
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,
},
],
},
],
});