Skip to content

Commit

Permalink
feat: add allowNull (fixes #390)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 18, 2020
1 parent b1c588f commit d297631
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
26 changes: 22 additions & 4 deletions .README/rules/require-compound-type-alias.md
@@ -1,14 +1,32 @@
### `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.
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:
The rule has two options:

1. a string option

* `"always"` (default)
* `"never"`
* `"always"`

The default value is `"always"`.
2. an object

```js
{
"rules": {
"flowtype/require-compound-type-alias": [
2,
"always",
{
"allowNull": true
}
]
}
}
```

* `allowNull` – allows compound types where one of the members is a `null`, e.g. `string | null`.

<!-- assertions requireCompoundTypeAlias -->
2 changes: 1 addition & 1 deletion .README/rules/type-id-match.md
Expand Up @@ -4,7 +4,7 @@ Enforces a consistent naming pattern for type aliases.

#### Options

This rule needs a text RegExp to operate with Its signature is as follows:
This rule requires a text RegExp:

```js
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"chai": "^4.2.0",
"eclint": "^2.8.1",
"eslint": "^7.0.0",
"eslint-config-canonical": "^19.0.5",
"eslint-config-canonical": "^20.0.4",
"gitdown": "^3.1.3",
"glob": "^7.1.6",
"husky": "^4.2.5",
Expand Down
33 changes: 33 additions & 0 deletions src/rules/requireCompoundTypeAlias.js
Expand Up @@ -3,14 +3,36 @@ const schema = [
enum: ['always', 'never'],
type: 'string',
},
{
additionalProperties: false,
properties: {
allowNull: {
type: 'boolean',
},
},
type: 'object',
},
];

const create = (context) => {
const always = (context.options[0] || 'always') === 'always';

const allowNull = !(context.options[1] && context.options[1].allowNull === false);

if (always) {
return {
IntersectionTypeAnnotation (node) {
if (
allowNull &&
node.types.length === 2 &&
(
node.types[0].type === 'NullLiteralTypeAnnotation' ||
node.types[1].type === 'NullLiteralTypeAnnotation'
)
) {
return;
}

if (node.parent.type !== 'TypeAlias') {
context.report({
message: 'All intersection types must be declared with named type alias.',
Expand All @@ -19,6 +41,17 @@ const create = (context) => {
}
},
UnionTypeAnnotation (node) {
if (
allowNull &&
node.types.length === 2 &&
(
node.types[0].type === 'NullLiteralTypeAnnotation' ||
node.types[1].type === 'NullLiteralTypeAnnotation'
)
) {
return;
}

if (node.parent.type !== 'TypeAlias') {
context.report({
message: 'All union types must be declared with named type alias.',
Expand Down
22 changes: 22 additions & 0 deletions tests/rules/assertions/requireCompoundTypeAlias.js
@@ -1,5 +1,15 @@
export default {
invalid: [
{
code: 'const foo: string | null = null;',
errors: [{message: 'All union types must be declared with named type alias.'}],
options: [
'always',
{
allowNull: false,
},
],
},
{
code: 'function foo(bar: "A" | "B") {}',
errors: [{message: 'All union types must be declared with named type alias.'}],
Expand Down Expand Up @@ -65,6 +75,18 @@ export default {
},
],
valid: [
{
code: 'const foo: string | null = null;',
},
{
code: 'const foo: string | null = null;',
options: [
'always',
{
allowNull: true,
},
],
},
{
code: 'type Foo = "A" | "B";',
},
Expand Down

0 comments on commit d297631

Please sign in to comment.