New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: new interfaceTypeIdMatch rule #491
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
### `interface-type-id-match` | ||
|
||
Enforces a consistent naming pattern for interfaces. | ||
|
||
#### Options | ||
|
||
This rule requires a text RegExp: | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/interface-type-id-match": [ | ||
2, | ||
"^([A-Z][a-z0-9]*)+Type$" | ||
] | ||
} | ||
} | ||
``` | ||
|
||
`'^([A-Z][a-z0-9]*)+Type$$'` is the default pattern. | ||
|
||
<!-- assertions interfaceTypeIdMatch --> |
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 checkType = (interfaceDeclarationNode) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const typeIdentifierName = interfaceDeclarationNode.id.name; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
if (!pattern.test(typeIdentifierName)) { | ||||||
context.report(interfaceDeclarationNode, 'Type identifier \'{{name}}\' does not match pattern \'{{pattern}}\'.', { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
name: typeIdentifierName, | ||||||
pattern: pattern.toString(), | ||||||
}); | ||||||
} | ||||||
}; | ||||||
|
||||||
return { | ||||||
InterfaceDeclaration: checkType, | ||||||
}; | ||||||
}; | ||||||
|
||||||
export default { | ||||||
create, | ||||||
schema, | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'interface foo{};', | ||
errors: [ | ||
{ | ||
message: 'Type identifier \'foo\' does not match pattern \'/^([A-Z][a-z0-9]*)+Type$/\'.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'interface FooType{};', | ||
errors: [ | ||
{ | ||
message: 'Type 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, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ const reportingRules = [ | |||||
'delimiter-dangle', | ||||||
'enforce-line-break', | ||||||
'generic-spacing', | ||||||
'interface-type-id-match', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'newline-after-flow-annotation', | ||||||
'no-dupe-keys', | ||||||
'no-existential-type', | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think is necessary, but fine to keep.