-
-
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.
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
Showing
8 changed files
with
317 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,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 | ||
}; | ||
}; |
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,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); | ||
}); | ||
}; |
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,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); |
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,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); |
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,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 | ||
} | ||
} | ||
} | ||
] | ||
}; |
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,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 | ||
} | ||
} | ||
} | ||
] | ||
}; |
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