-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add require-compound-type-alias (#365)
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
### `require-compound-type-alias` | ||
|
||
Requires to make a type alias for all [union](https://flow.org/en/docs/types/unions/) and [intersection](https://flow.org/en/docs/types/intersections/) types. If these are used in "raw" forms it might be tempting to just copy&paste them around the code. However, this brings sort of a source code pollution and unnecessary changes on several parts when these compound types need to be changed. | ||
|
||
#### Options | ||
|
||
The rule has a string option: | ||
|
||
* `"never"` | ||
* `"always"` | ||
|
||
The default value is `"always"`. | ||
|
||
<!-- assertions require-compound-type-alias --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const schema = [ | ||
{ | ||
enum: ['always', 'never'], | ||
type: 'string' | ||
} | ||
]; | ||
|
||
const create = (context) => { | ||
const always = (context.options[0] || 'always') === 'always'; | ||
|
||
if (always) { | ||
return { | ||
IntersectionTypeAnnotation (node) { | ||
if (node.parent.type !== 'TypeAlias') { | ||
context.report({ | ||
message: 'All intersection types must be declared with named type alias.', | ||
node | ||
}); | ||
} | ||
}, | ||
UnionTypeAnnotation (node) { | ||
if (node.parent.type !== 'TypeAlias') { | ||
context.report({ | ||
message: 'All union types must be declared with named type alias.', | ||
node | ||
}); | ||
} | ||
} | ||
}; | ||
} else { | ||
return {}; | ||
} | ||
}; | ||
|
||
export default { | ||
create, | ||
schema | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'function foo(bar: "A" | "B") {}', | ||
errors: [{message: 'All union types must be declared with named type alias.'}] | ||
}, | ||
{ | ||
code: 'const foo: "A" | "B" = "A";', | ||
errors: [{message: 'All union types must be declared with named type alias.'}] | ||
}, | ||
{ | ||
code: 'type Foo = { bar: "A" | "B" };', | ||
errors: [{message: 'All union types must be declared with named type alias.'}] | ||
}, | ||
{ | ||
code: 'function foo(bar: { n: number } | { s: string }) {}', | ||
errors: [{message: 'All union types must be declared with named type alias.'}] | ||
}, | ||
{ | ||
code: 'function foo(bar: { n: number } & { s: string }) {}', | ||
errors: [{message: 'All intersection types must be declared with named type alias.'}] | ||
}, | ||
{ | ||
code: 'const foo: { n: number } & { s: string } = { n: 0, s: "" };', | ||
errors: [{message: 'All intersection types must be declared with named type alias.'}] | ||
}, | ||
{ | ||
code: 'type Foo = { bar: { n: number } & { s: string } };', | ||
errors: [{message: 'All intersection types must be declared with named type alias.'}] | ||
}, | ||
{ | ||
code: 'function foo(bar: { n: number } & { s: string }) {}', | ||
errors: [{message: 'All intersection types must be declared with named type alias.'}] | ||
} | ||
], | ||
misconfigured: [ | ||
{ | ||
errors: [ | ||
{ | ||
data: 'sometimes', | ||
dataPath: '[0]', | ||
keyword: 'enum', | ||
message: 'should be equal to one of the allowed values', | ||
params: { | ||
allowedValues: [ | ||
'always', | ||
'never' | ||
] | ||
}, | ||
parentSchema: { | ||
enum: [ | ||
'always', | ||
'never' | ||
], | ||
type: 'string' | ||
}, | ||
schema: [ | ||
'always', | ||
'never' | ||
], | ||
schemaPath: '#/items/0/enum' | ||
} | ||
], | ||
options: ['sometimes'] | ||
} | ||
], | ||
valid: [ | ||
{ | ||
code: 'type Foo = "A" | "B";' | ||
}, | ||
{ | ||
code: 'type Bar = "A" | "B"; function foo(bar: Bar) {}' | ||
}, | ||
{ | ||
code: 'type Foo = { disjoint: "A", n: number } | { disjoint: "B", s: string };' | ||
}, | ||
{ | ||
code: 'type Foo = { n: number } & { s: string };' | ||
}, | ||
{ | ||
code: 'type Bar = { n: number } & { s: string }; function foo(bar: Bar) {}' | ||
}, | ||
{ | ||
code: 'function foo(bar: "A" | "B") {}', | ||
options: ['never'] | ||
}, | ||
{ | ||
code: 'function foo(bar: { n: number } & { s: string }) {}', | ||
options: ['never'] | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters