Skip to content

Commit

Permalink
test: no-type-alias add tests for keyof and readonly tuple types
Browse files Browse the repository at this point in the history
  • Loading branch information
ankeetmaini committed Aug 11, 2019
1 parent 06a28c2 commit 29d8f5d
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 92 deletions.
57 changes: 11 additions & 46 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
Expand Up @@ -10,6 +10,13 @@ type Values =
| 'in-unions'
| 'in-intersections'
| 'in-unions-and-intersections';
const enumValues: Values[] = [
'always',
'never',
'in-unions',
'in-intersections',
'in-unions-and-intersections',
];

type Options = [
{
Expand Down Expand Up @@ -49,61 +56,19 @@ export default util.createRule<Options, MessageIds>({
type: 'object',
properties: {
allowAliases: {
enum: [
'always',
'never',
'in-unions',
'in-intersections',
'in-unions-and-intersections',
],
enum: enumValues,
},
allowCallbacks: {
enum: ['always', 'never'],
},
allowLiterals: {
enum: [
'always',
'never',
'in-unions',
'in-intersections',
'in-unions-and-intersections',
],
enum: enumValues,
},
allowMappedTypes: {
enum: [
'always',
'never',
'in-unions',
'in-intersections',
'in-unions-and-intersections',
],
enum: enumValues,
},
allowTupleTypes: {
enum: [
'always',
'never',
'in-unions',
'in-intersections',
'in-unions-and-intersections',
],
},
allowKeyOf: {
enum: [
'always',
'never',
'in-unions',
'in-intersections',
'in-unions-and-intersections',
],
},
allowReadOnly: {
enum: [
'always',
'never',
'in-unions',
'in-intersections',
'in-unions-and-intersections',
],
enum: enumValues,
},
},
additionalProperties: false,
Expand Down
191 changes: 145 additions & 46 deletions packages/eslint-plugin/tests/rules/no-type-alias.test.ts
Expand Up @@ -7,10 +7,6 @@ const ruleTester = new RuleTester({

ruleTester.run('no-type-alias', rule, {
valid: [
{
code: 'type Foo = keyof [string]',
options: [{ allowTupleTypes: 'always' }],
},
{
code: "type A = 'a' & ('b' | 'c');",
options: [{ allowAliases: 'always' }],
Expand Down Expand Up @@ -380,6 +376,10 @@ type Foo<T> = {
code: 'type Foo = typeof bar | typeof baz;',
options: [{ allowAliases: 'in-unions' }],
},
{
code: 'type Foo = keyof [string]',
options: [{ allowTupleTypes: 'always' }],
},
{
code: 'type Foo = [string] | [number, number];',
options: [{ allowTupleTypes: 'always' }],
Expand All @@ -397,48 +397,48 @@ type Foo<T> = {
'type Foo = [string] & [number, number] | [number, number, number];',
options: [{ allowTupleTypes: 'in-unions-and-intersections' }],
},
// {
// code: 'type Foo = readonly [string] | [number, number];',
// options: [{ allowReadOnly: 'always' }],
// },
// {
// code: 'type Foo = readonly [string] | readonly [number, number];',
// options: [{ allowReadOnly: 'always' }],
// },
// {
// code: 'type Foo = readonly [string] | [number, number];',
// options: [{ allowReadOnly: 'in-unions' }],
// },
// {
// code: 'type Foo = [string] & readonly [number, number];',
// options: [{ allowReadOnly: 'in-intersections' }],
// },
// {
// code:
// 'type Foo = [string] & [number, number] | readonly [number, number, number];',
// options: [{ allowReadOnly: 'in-unions-and-intersections' }],
// },
// {
// code: 'type Foo = keyof [string] | [number, number];',
// options: [{ allowKeyOf: 'always' }],
// },
// {
// code: 'type Foo = keyof [string] | keyof [number, number];',
// options: [{ allowKeyOf: 'always' }],
// },
// {
// code: 'type Foo = keyof [string] | [number, number];',
// options: [{ allowKeyOf: 'in-unions' }],
// },
// {
// code: 'type Foo = [string] & keyof [number, number];',
// options: [{ allowKeyOf: 'in-intersections' }],
// },
// {
// code:
// 'type Foo = [string] & [number, number] | keyof [number, number, number];',
// options: [{ allowKeyOf: 'in-unions-and-intersections' }],
// },
{
code: 'type Foo = readonly [string] | [number, number];',
options: [{ allowTupleTypes: 'always' }],
},
{
code: 'type Foo = readonly [string] | readonly [number, number];',
options: [{ allowTupleTypes: 'always' }],
},
{
code: 'type Foo = readonly [string] | [number, number];',
options: [{ allowTupleTypes: 'in-unions' }],
},
{
code: 'type Foo = [string] & readonly [number, number];',
options: [{ allowTupleTypes: 'in-intersections' }],
},
{
code:
'type Foo = [string] & [number, number] | readonly [number, number, number];',
options: [{ allowTupleTypes: 'in-unions-and-intersections' }],
},
{
code: 'type Foo = keyof [string] | [number, number];',
options: [{ allowTupleTypes: 'always' }],
},
{
code: 'type Foo = keyof [string] | keyof [number, number];',
options: [{ allowTupleTypes: 'always' }],
},
{
code: 'type Foo = keyof [string] | [number, number];',
options: [{ allowTupleTypes: 'in-unions' }],
},
{
code: 'type Foo = [string] & keyof [number, number];',
options: [{ allowTupleTypes: 'in-intersections' }],
},
{
code:
'type Foo = [string] & [number, number] | keyof [number, number, number];',
options: [{ allowTupleTypes: 'in-unions-and-intersections' }],
},
],
invalid: [
{
Expand Down Expand Up @@ -3077,5 +3077,104 @@ type Foo<T> = {
},
],
},
{
code: 'type Foo = readonly [number] | keyof [number, number]',
options: [{ allowTupleTypes: 'never' }],
errors: [
{
messageId: 'noCompositionAlias',
data: {
compositionType: 'union',
typeName: 'Tuple Types',
},
line: 1,
column: 12,
},
{
messageId: 'noCompositionAlias',
data: {
compositionType: 'union',
typeName: 'Tuple Types',
},
line: 1,
column: 32,
},
],
},
{
code: 'type Foo = keyof [number] & [number, number]',
options: [{ allowTupleTypes: 'in-unions' }],
errors: [
{
messageId: 'noCompositionAlias',
data: {
compositionType: 'intersection',
typeName: 'Tuple Types',
},
line: 1,
column: 12,
},
{
messageId: 'noCompositionAlias',
data: {
compositionType: 'intersection',
typeName: 'Tuple Types',
},
line: 1,
column: 29,
},
],
},
{
code: 'type Foo = [number] | readonly [number, number]',
options: [{ allowTupleTypes: 'in-intersections' }],
errors: [
{
messageId: 'noCompositionAlias',
data: {
compositionType: 'union',
typeName: 'Tuple Types',
},
line: 1,
column: 12,
},
{
messageId: 'noCompositionAlias',
data: {
compositionType: 'union',
typeName: 'Tuple Types',
},
line: 1,
column: 23,
},
],
},
{
code: 'type Foo = readonly [number];',
options: [{ allowTupleTypes: 'in-intersections' }],
errors: [
{
messageId: 'noTypeAlias',
},
],
},
{
code: 'type Foo = keyof [number];',
options: [{ allowTupleTypes: 'in-unions' }],
errors: [
{
messageId: 'noTypeAlias',
},
],
},
{
code: 'type Foo = readonly [number];',
options: [{ allowTupleTypes: 'in-unions-and-intersections' }],
errors: [
{
messageId: 'noTypeAlias',
},
],
},
],
});

0 comments on commit 29d8f5d

Please sign in to comment.