-
-
Notifications
You must be signed in to change notification settings - Fork 153
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-indexer-name rule (#425)
* Add require-indexer-name rule * Add require-indexer-name docs
- Loading branch information
Showing
7 changed files
with
110 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
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,23 @@ | ||
### `require-indexer-name` | ||
|
||
This rule validates Flow object indexer name. | ||
|
||
#### Options | ||
|
||
The rule has a string option: | ||
|
||
* `"never"` (default): Never report files that are missing an indexer key name. | ||
* `"always"`: Always report files that are missing an indexer key name. | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/require-indexer-name": [ | ||
2, | ||
"always" | ||
] | ||
} | ||
} | ||
``` | ||
|
||
<!-- assertions requireIndexerName --> |
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,33 @@ | ||
import {getParameterName} from '../utilities'; | ||
|
||
const schema = [ | ||
{ | ||
enum: ['always', 'never'], | ||
type: 'string', | ||
}, | ||
]; | ||
|
||
const create = (context) => { | ||
const always = (context.options[0] || 'always') === 'always'; | ||
|
||
if (always) { | ||
return { | ||
ObjectTypeIndexer (node) { | ||
const id = getParameterName(node, context); | ||
if (id === null) { | ||
context.report({ | ||
message: 'All indexers must be declared with key name.', | ||
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
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,26 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'type foo = { [string]: number };', | ||
errors: [ | ||
{message: 'All indexers must be declared with key name.'}, | ||
], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: 'type foo = { [key: string]: number };', | ||
errors: [], | ||
}, | ||
{ | ||
code: 'type foo = { [key: string]: number };', | ||
errors: [], | ||
options: ['never'], | ||
}, | ||
{ | ||
code: 'type foo = { [string]: number };', | ||
errors: [], | ||
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