Skip to content

Commit

Permalink
feat: Implement array style rules
Browse files Browse the repository at this point in the history
This commit adds two new rules: array-style-complex-type and
array-style-simple-type. In Flow, there are two array notation styles,
verbose (Array<Type>) and shorthand (Type[]). These rules enforce
consistent notation. Array element types are also divided into two
categories: complex and simple. This is useful for e.g. using shorthand
notation for simple types and and verbose notation for complex types.
What "simple" and "complex" means is explained in rules' documentation.
  • Loading branch information
pnevyk committed Sep 29, 2017
1 parent 1232069 commit afd4210
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import _ from 'lodash';
import recommended from './configs/recommended.json';
import arrayStyleComplexType from './rules/arrayStyleComplexType';
import arrayStyleSimpleType from './rules/arrayStyleSimpleType';
import booleanStyle from './rules/booleanStyle';
import defineFlowType from './rules/defineFlowType';
import delimiterDangle from './rules/delimiterDangle';
Expand All @@ -26,6 +28,8 @@ import validSyntax from './rules/validSyntax';
import {checkFlowFileAnnotation} from './utilities';

const rules = {
'array-style-complex-type': arrayStyleComplexType,
'array-style-simple-type': arrayStyleSimpleType,
'boolean-style': booleanStyle,
'define-flow-type': defineFlowType,
'delimiter-dangle': delimiterDangle,
Expand Down
34 changes: 34 additions & 0 deletions src/rules/arrayStyle/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import isSimpleType from './isSimpleType';

const schema = [
{
enum: ['verbose', 'shorthand'],
type: 'string'
}
];

export default (defaultConfig, shorthandHandler, verboseHandler) => {
const create = (context) => {
const verbose = (context.options[0] || defaultConfig) === 'verbose';

return {
// shorthand
ArrayTypeAnnotation (node) {
shorthandHandler(isSimpleType(node.elementType), verbose, context, node);
},
// verbose
GenericTypeAnnotation (node) {
if (node.id.name === 'Array') {
if (node.typeParameters.params.length === 1) {
verboseHandler(isSimpleType(node.typeParameters.params[0]), verbose, context, node);
}
}
}
};
};

return {
create,
schema
};
};
30 changes: 30 additions & 0 deletions src/rules/arrayStyle/isSimpleType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Types considered simple:
*
* - primitive types
* - literal types
* - mixed and any types
* - generic types (such as Date, Promise<string>, $Keys<T>, etc.)
* - array type written in shorthand notation
*
* Types not considered simple:
*
* - maybe type
* - function type
* - object type
* - tuple type
* - union and intersection types
*
* Reminder: if you change these semantics, don't forget to modify documentation of `array-style-...` rules
*/

const simpleTypePatterns = [
/^(?:Any|Array|Boolean|Generic|Mixed|Number|String|Void)TypeAnnotation$/,
/.+LiteralTypeAnnotation$/
];

export default (node) => {
return simpleTypePatterns.some((pattern) => {
return pattern.test(node.type);
});
};
21 changes: 21 additions & 0 deletions src/rules/arrayStyleComplexType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import makeArrayStyleRule from './arrayStyle';

const shorthandHandler = (isSimpleType, verbose, context, node) => {
if (!isSimpleType && verbose) {
context.report({
message: 'Use "Array<ComplexType>", not "ComplexType[]"',
node
});
}
};

const verboseHandler = (isSimpleType, verbose, context, node) => {
if (!isSimpleType && !verbose) {
context.report({
message: 'Use "ComplexType[]", not "Array<ComplexType>"',
node
});
}
};

export default makeArrayStyleRule('verbose', shorthandHandler, verboseHandler);
21 changes: 21 additions & 0 deletions src/rules/arrayStyleSimpleType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import makeArrayStyleRule from './arrayStyle';

const shorthandHandler = (isSimpleType, verbose, context, node) => {
if (isSimpleType && verbose) {
context.report({
message: 'Use "Array<SimpleType>", not "SimpleType[]"',
node
});
}
};

const verboseHandler = (isSimpleType, verbose, context, node) => {
if (isSimpleType && !verbose) {
context.report({
message: 'Use "SimpleType[]", not "Array<SimpleType>"',
node
});
}
};

export default makeArrayStyleRule('shorthand', shorthandHandler, verboseHandler);
95 changes: 95 additions & 0 deletions tests/rules/assertions/arrayStyleComplexType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
export default {
invalid: [
{
code: 'type X = (?string)[]',
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}]
},
{
code: 'type X = (?string)[]',
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}],
options: ['verbose']
},
{
code: 'type X = Array<?string>',
errors: [{message: 'Use "ComplexType[]", not "Array<ComplexType>"'}],
options: ['shorthand']
},
{
code: 'type X = (string | number)[]',
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}]
},
{
code: 'type X = (string & number)[]',
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}]
},
{
code: 'type X = [string, number][]',
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}]
},
{
code: 'type X = ({foo: string})[]',
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}]
},
{
code: 'type X = (string => number)[]',
errors: [{message: 'Use "Array<ComplexType>", not "ComplexType[]"'}]
}
],
misconfigured: [
{
errors: [
{
data: 'normal',
dataPath: '[0]',
keyword: 'enum',
message: 'should be equal to one of the allowed values',
params: {
allowedValues: [
'verbose',
'shorthand'
]
},
parentSchema: {
enum: [
'verbose',
'shorthand'
],
type: 'string'
},
schema: [
'verbose',
'shorthand'
],
schemaPath: '#/items/0/enum'
}
],
options: ['normal']
}
],
valid: [
{
code: 'type X = Array<?string>'
},
{
code: 'type X = Array<?string>',
options: ['verbose']
},
{
code: 'type X = (?string)[]',
options: ['shorthand']
},
{
code: 'type X = Array<string>',
options: ['shorthand']
},
{
code: 'type X = Array<?string>',
options: ['shorthand'],
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
110 changes: 110 additions & 0 deletions tests/rules/assertions/arrayStyleSimpleType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
export default {
invalid: [
{
code: 'type X = Array<string>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}]
},
{
code: 'type X = string[]',
errors: [{message: 'Use "Array<SimpleType>", not "SimpleType[]"'}],
options: ['verbose']
},
{
code: 'type X = Array<string>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}],
options: ['shorthand']
},
{
code: 'type X = Array<Date>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}]
},
{
code: 'type X = Array<Promise<string>>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}]
},
{
code: 'type X = Array<$Keys<{ foo: string }>>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}]
},
{
code: 'type X = Array<any>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}]
},
{
code: 'type X = Array<mixed>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}]
},
{
code: 'type X = Array<void>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}]
},
{
code: 'type X = Array<null>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}]
},
{
code: 'type X = Array<string[]>',
errors: [{message: 'Use "SimpleType[]", not "Array<SimpleType>"'}]
}
],
misconfigured: [
{
errors: [
{
data: 'normal',
dataPath: '[0]',
keyword: 'enum',
message: 'should be equal to one of the allowed values',
params: {
allowedValues: [
'verbose',
'shorthand'
]
},
parentSchema: {
enum: [
'verbose',
'shorthand'
],
type: 'string'
},
schema: [
'verbose',
'shorthand'
],
schemaPath: '#/items/0/enum'
}
],
options: ['normal']
}
],
valid: [
{
code: 'type X = string[]'
},
{
code: 'type X = Array<string>',
options: ['verbose']
},
{
code: 'type X = string[]',
options: ['shorthand']
},
{
code: 'type X = string[][]'
},
{
code: 'type X = (?string)[]',
options: ['verbose']
},
{
code: 'type X = string[]',
options: ['verbose'],
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
2 changes: 2 additions & 0 deletions tests/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ rules.importPlugin(plugin, 'flowtype');
const ruleTester = new RuleTester();

const reportingRules = [
'array-style-complex-type',
'array-style-simple-type',
'boolean-style',
'define-flow-type',
'delimiter-dangle',
Expand Down

0 comments on commit afd4210

Please sign in to comment.