Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .README/README.md
Expand Up @@ -54,6 +54,10 @@ npm install eslint babel-eslint eslint-plugin-flowtype --save-dev
2,
"never"
],
"flowtype/interface-type-id-match": [
2,
"^([A-Z][a-z0-9]+)+Type$"
],
Copy link
Contributor

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.

"flowtype/no-mixed": 2,
"flowtype/no-primitive-constructor-types": 2,
"flowtype/no-types-missing-file-annotation": 2,
Expand Down Expand Up @@ -162,6 +166,7 @@ When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/d
{"gitdown": "include", "file": "./rules/delimiter-dangle.md"}
{"gitdown": "include", "file": "./rules/enforce-line-break.md"}
{"gitdown": "include", "file": "./rules/generic-spacing.md"}
{"gitdown": "include", "file": "./rules/interface-type-id-match.md"}
{"gitdown": "include", "file": "./rules/newline-after-flow-annotation.md"}
{"gitdown": "include", "file": "./rules/no-dupe-keys.md"}
{"gitdown": "include", "file": "./rules/no-existential-type.md"}
Expand Down
22 changes: 22 additions & 0 deletions .README/rules/interface-type-id-match.md
@@ -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 -->
11 changes: 5 additions & 6 deletions README.md
Expand Up @@ -1870,7 +1870,7 @@ import Foo from './foo';
// Message: Expected newline after flow annotation

// Options: ["always-windows"]
// @flow
// @flow
import Foo from './foo';
// Message: Expected newline after flow annotation

Expand All @@ -1890,8 +1890,8 @@ The following patterns are not considered problems:
import Foo from './foo';

// Options: ["always-windows"]
// @flow
// @flow

import Foo from './foo';

// Options: ["never"]
Expand Down Expand Up @@ -3028,7 +3028,7 @@ The rule has two options:
}
```

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

The following patterns are considered problems:

Expand Down Expand Up @@ -5392,7 +5392,7 @@ The following patterns are not considered problems:
{ a: string, b: number }) => {}

// Options: ["always",{"allowLineBreak":true}]
(foo:
(foo:
{ a: string, b: number }) => {}

// Options: ["never"]
Expand Down Expand Up @@ -6719,4 +6719,3 @@ function x(foo: Type = bar()) {}
```



3 changes: 3 additions & 0 deletions src/index.js
Expand Up @@ -8,6 +8,7 @@ import defineFlowType from './rules/defineFlowType';
import delimiterDangle from './rules/delimiterDangle';
import enforceLineBreak from './rules/enforceLineBreak';
import genericSpacing from './rules/genericSpacing';
import interfaceTypeIdMatch from './rules/interfaceTypeIdMatch';
import newlineAfterFlowAnnotation from './rules/newlineAfterFlowAnnotation';
import noDupeKeys from './rules/noDupeKeys';
import noExistentialType from './rules/noExistentialType';
Expand Down Expand Up @@ -56,6 +57,7 @@ const rules = {
'delimiter-dangle': delimiterDangle,
'enforce-line-break': enforceLineBreak,
'generic-spacing': genericSpacing,
'interface-type-id-match': interfaceTypeIdMatch,
'newline-after-flow-annotation': newlineAfterFlowAnnotation,
'no-dupe-keys': noDupeKeys,
'no-existential-type': noExistentialType,
Expand Down Expand Up @@ -113,6 +115,7 @@ export default {
'define-flow-type': 0,
'delimiter-dangle': 0,
'generic-spacing': 0,
'interface-type-id-match': 0,
'newline-after-flow-annotation': 0,
'no-dupe-keys': 0,
'no-flow-fix-me-comments': 0,
Expand Down
29 changes: 29 additions & 0 deletions src/rules/interfaceTypeIdMatch.js
@@ -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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const checkType = (interfaceDeclarationNode) => {
const checkInterface = (interfaceDeclarationNode) => {

const typeIdentifierName = interfaceDeclarationNode.id.name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const typeIdentifierName = interfaceDeclarationNode.id.name;
const interfaceIdentifierName = interfaceDeclarationNode.id.name;


if (!pattern.test(typeIdentifierName)) {
context.report(interfaceDeclarationNode, 'Type identifier \'{{name}}\' does not match pattern \'{{pattern}}\'.', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
context.report(interfaceDeclarationNode, 'Type identifier \'{{name}}\' does not match pattern \'{{pattern}}\'.', {
context.report(interfaceDeclarationNode, 'Interface identifier \'{{name}}\' does not match pattern \'{{pattern}}\'.', {

name: typeIdentifierName,
pattern: pattern.toString(),
});
}
};

return {
InterfaceDeclaration: checkType,
};
};

export default {
create,
schema,
};
63 changes: 63 additions & 0 deletions tests/rules/assertions/interfaceTypeIdMatch.js
@@ -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,
},
},
},
],
};
1 change: 1 addition & 0 deletions tests/rules/index.js
Expand Up @@ -19,6 +19,7 @@ const reportingRules = [
'delimiter-dangle',
'enforce-line-break',
'generic-spacing',
'interface-type-id-match',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'interface-type-id-match',
'interface-id-match',

'newline-after-flow-annotation',
'no-dupe-keys',
'no-existential-type',
Expand Down