Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Interface Id Match rule (#492)
* create rule and tests * reset readme * update naming conventions
- Loading branch information
Showing
7 changed files
with
128 additions
and
6 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,22 @@ | ||
### `interface-id-match` | ||
|
||
Enforces a consistent naming pattern for interfaces. | ||
|
||
#### Options | ||
|
||
This rule requires a text RegExp: | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/interface-id-match": [ | ||
2, | ||
"^([A-Z][a-z0-9]*)+Type$" | ||
] | ||
} | ||
} | ||
``` | ||
|
||
`'^([A-Z][a-z0-9]*)+Type$$'` is the default pattern. | ||
|
||
<!-- assertions interfaceIdMatch --> |
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
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,29 @@ | ||
const schema = [ | ||
{ | ||
type: 'string', | ||
}, | ||
]; | ||
|
||
const create = (context) => { | ||
const pattern = new RegExp(context.options[0] || '^([A-Z][a-z0-9]*)+Type$'); | ||
|
||
const checkInterface = (interfaceDeclarationNode) => { | ||
const interfaceIdentifierName = interfaceDeclarationNode.id.name; | ||
|
||
if (!pattern.test(interfaceIdentifierName)) { | ||
context.report(interfaceDeclarationNode, 'Interface identifier \'{{name}}\' does not match pattern \'{{pattern}}\'.', { | ||
name: interfaceIdentifierName, | ||
pattern: pattern.toString(), | ||
}); | ||
} | ||
}; | ||
|
||
return { | ||
InterfaceDeclaration: checkInterface, | ||
}; | ||
}; | ||
|
||
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,63 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'interface foo{};', | ||
errors: [ | ||
{ | ||
message: 'Interface identifier \'foo\' does not match pattern \'/^([A-Z][a-z0-9]*)+Type$/\'.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'interface FooType{};', | ||
errors: [ | ||
{ | ||
message: 'Interface identifier \'FooType\' does not match pattern \'/^foo$/\'.', | ||
}, | ||
], | ||
options: [ | ||
'^foo$', | ||
], | ||
}, | ||
], | ||
misconfigured: [ | ||
{ | ||
errors: [ | ||
{ | ||
data: 7, | ||
dataPath: '[0]', | ||
keyword: 'type', | ||
message: 'should be string', | ||
params: { | ||
type: 'string', | ||
}, | ||
parentSchema: { | ||
type: 'string', | ||
}, | ||
schema: 'string', | ||
schemaPath: '#/items/0/type', | ||
}, | ||
], | ||
options: [7], | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: 'interface FooType {};', | ||
}, | ||
{ | ||
code: 'interface foo {};', | ||
options: [ | ||
'^foo$', | ||
], | ||
}, | ||
{ | ||
code: 'interface foo {};', | ||
settings: { | ||
flowtype: { | ||
onlyFilesWithFlowAnnotation: true, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; |
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