Skip to content

Commit

Permalink
Merge branch 'aaron-harvey-refactor-annotation-check'
Browse files Browse the repository at this point in the history
  • Loading branch information
danharper committed Nov 27, 2016
2 parents 6f007cd + d311f22 commit 979458c
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 48 deletions.
60 changes: 37 additions & 23 deletions src/index.js
@@ -1,3 +1,5 @@
import _ from 'lodash';
import {checkFlowFileAnnotation} from './utilities';
import defineFlowType from './rules/defineFlowType';
import genericSpacing from './rules/genericSpacing';
import noWeakTypes from './rules/noWeakTypes';
Expand All @@ -21,33 +23,45 @@ import sortKeys from './rules/sortKeys';
import objectTypeDelimiter from './rules/objectTypeDelimiter';
import recommended from './configs/recommended.json';

const rules = {
'boolean-style': booleanStyle,
'define-flow-type': defineFlowType,
'delimiter-dangle': delimiterDangle,
'generic-spacing': genericSpacing,
'no-dupe-keys': noDupeKeys,
'no-primitive-constructor-types': noPrimitiveConstructorTypes,
'no-weak-types': noWeakTypes,
'object-type-delimiter': objectTypeDelimiter,
'require-parameter-type': requireParameterType,
'require-return-type': requireReturnType,
'require-valid-file-annotation': requireValidFileAnnotation,
'require-variable-type': requireVariableType,
semi,
'sort-keys': sortKeys,
'space-after-type-colon': spaceAfterTypeColon,
'space-before-generic-bracket': spaceBeforeGenericBracket,
'space-before-type-colon': spaceBeforeTypeColon,
'type-id-match': typeIdMatch,
'union-intersection-spacing': unionIntersectionSpacing,
'use-flow-type': useFlowType,
'valid-syntax': validSyntax
};

export default {
configs: {
recommended
},
rules: {
'boolean-style': booleanStyle,
'define-flow-type': defineFlowType,
'delimiter-dangle': delimiterDangle,
'generic-spacing': genericSpacing,
'no-dupe-keys': noDupeKeys,
'no-primitive-constructor-types': noPrimitiveConstructorTypes,
'no-weak-types': noWeakTypes,
'object-type-delimiter': objectTypeDelimiter,
'require-parameter-type': requireParameterType,
'require-return-type': requireReturnType,
'require-valid-file-annotation': requireValidFileAnnotation,
'require-variable-type': requireVariableType,
semi,
'sort-keys': sortKeys,
'space-after-type-colon': spaceAfterTypeColon,
'space-before-generic-bracket': spaceBeforeGenericBracket,
'space-before-type-colon': spaceBeforeTypeColon,
'type-id-match': typeIdMatch,
'union-intersection-spacing': unionIntersectionSpacing,
'use-flow-type': useFlowType,
'valid-syntax': validSyntax
},
rules: _.mapValues(rules, (rule) => {
// Support current and deprecated rule formats
if (_.isPlainObject(rule)) {
return {
...rule,
create: _.partial(checkFlowFileAnnotation, rule.create)
};
}

return _.partial(checkFlowFileAnnotation, rule);
}),
rulesConfig: {
'boolean-style': 0,
'define-flow-type': 0,
Expand Down
7 changes: 0 additions & 7 deletions src/rules/requireParameterType.js
@@ -1,18 +1,11 @@
import _ from 'lodash';
import {
getParameterName,
isFlowFile,
iterateFunctionNodes,
quoteName
} from './../utilities';

export default iterateFunctionNodes((context) => {
const checkThisFile = !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation') || isFlowFile(context);

if (!checkThisFile) {
return () => {};
}

const skipArrows = _.get(context, 'options[0].excludeArrowFunctions');
const excludeParameterMatch = new RegExp(_.get(context, 'options[0].excludeParameterMatch', 'a^'));

Expand Down
9 changes: 0 additions & 9 deletions src/rules/requireReturnType.js
@@ -1,15 +1,6 @@
import _ from 'lodash';
import {
isFlowFile
} from './../utilities';

export default (context) => {
const checkThisFile = !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation') || isFlowFile(context);

if (!checkThisFile) {
return () => {};
}

const annotateReturn = (_.get(context, 'options[0]') || 'always') === 'always';
const annotateUndefined = (_.get(context, 'options[1].annotateUndefined') || 'never') === 'always';
const skipArrows = _.get(context, 'options[1].excludeArrowFunctions') || false;
Expand Down
2 changes: 1 addition & 1 deletion src/rules/requireValidFileAnnotation.js
Expand Up @@ -31,7 +31,7 @@ export const schema = [
];

export default (context) => {
const always = context.options[0] === 'always' && !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation', false);
const always = context.options[0] === 'always';
const style = _.get(context, 'options[1].annotationStyle', defaults.annotationStyle);

return {
Expand Down
12 changes: 12 additions & 0 deletions src/utilities/checkFlowFileAnnotation.js
@@ -0,0 +1,12 @@
import _ from 'lodash';
import isFlowFile from './isFlowFile';

export default (cb, context) => {
const checkThisFile = !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation') || isFlowFile(context);

if (!checkThisFile) {
return () => {};
}

return cb(context);
};
1 change: 1 addition & 0 deletions src/utilities/index.js
Expand Up @@ -9,3 +9,4 @@ export quoteName from './quoteName';
export getTokenBeforeParens from './getTokenBeforeParens';
export getTokenAfterParens from './getTokenAfterParens';
export fuzzyStringMatch from './fuzzyStringMatch';
export checkFlowFileAnnotation from './checkFlowFileAnnotation';
9 changes: 9 additions & 0 deletions tests/rules/assertions/booleanStyle.js
Expand Up @@ -29,6 +29,15 @@ export default {
{
code: 'type X = bool',
options: ['bool']
},
{
code: 'type X = bool',
options: ['boolean'],
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
8 changes: 8 additions & 0 deletions tests/rules/assertions/noDupeKeys.js
Expand Up @@ -12,6 +12,14 @@ export default {
valid: [
{
code: 'type FooType = { a: number, b: string, c: number }'
},
{
code: 'type FooType = { a: number, b: string, a: number }',
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
8 changes: 8 additions & 0 deletions tests/rules/assertions/noWeakTypes.js
Expand Up @@ -246,6 +246,14 @@ export default {
{
code: 'type X = Function',
options: [{Function: false}]
},
{
code: 'function foo(thing): Function {}',
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
9 changes: 9 additions & 0 deletions tests/rules/assertions/objectTypeDelimiter.js
Expand Up @@ -142,6 +142,15 @@ export default {
{
code: 'declare class Foo { (): Foo, }',
options: ['comma']
},
{
code: 'type Foo = { a: Foo, b: Bar }',
options: ['semicolon'],
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
8 changes: 8 additions & 0 deletions tests/rules/assertions/requireParameterType.js
Expand Up @@ -190,6 +190,14 @@ export default {
excludeParameterMatch: '^_'
}
]
},
{
code: '(foo) => {}',
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
8 changes: 8 additions & 0 deletions tests/rules/assertions/semi.js
Expand Up @@ -50,6 +50,14 @@ export default {
{
code: 'type FooType = {}',
options: ['never']
},
{
code: 'type FooType = {}',
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
8 changes: 8 additions & 0 deletions tests/rules/assertions/sortKeys.js
Expand Up @@ -79,6 +79,14 @@ export default {
{
code: 'type FooType = { 1:number, 2: number, 10: number }',
options: ['asc', {natural: true}]
},
{
code: 'type FooType = { b: number, a: number }',
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
8 changes: 8 additions & 0 deletions tests/rules/assertions/typeIdMatch.js
Expand Up @@ -29,6 +29,14 @@ export default {
options: [
'^foo$'
]
},
{
code: 'type foo = {};',
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
32 changes: 24 additions & 8 deletions tests/rules/assertions/unionIntersectionSpacing.js
Expand Up @@ -74,10 +74,10 @@ const UNION = {
}
],
valid: [
{code: 'type X = string | number;'},
{code: 'type X = string | number | boolean;'},
{code: 'type X = (string) | number;'},
{code: 'type X = ((string)) | (number | foo);'},
{code: 'type X = string | number;'},
{code: 'type X = string | number | boolean;'},
{code: 'type X = (string) | number;'},
{code: 'type X = ((string)) | (number | foo);'},
{
code: 'type X = string|number',
options: ['never']
Expand All @@ -93,6 +93,14 @@ const UNION = {
'| number',
'}'
].join('\n')
},
{
code: 'type X = string| number;',
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
Expand Down Expand Up @@ -173,10 +181,10 @@ const INTERSECTION = {
}
],
valid: [
{code: 'type X = string & number;'},
{code: 'type X = string & number & boolean;'},
{code: 'type X = (string) & number;'},
{code: 'type X = ((string)) & (number & foo);'},
{code: 'type X = string & number;'},
{code: 'type X = string & number & boolean;'},
{code: 'type X = (string) & number;'},
{code: 'type X = ((string)) & (number & foo);'},
{
code: 'type X = string&number',
options: ['never']
Expand All @@ -192,6 +200,14 @@ const INTERSECTION = {
'& number',
'}'
].join('\n')
},
{
code: 'type X = string& number;',
settings: {
flowtype: {
onlyFilesWithFlowAnnotation: true
}
}
}
]
};
Expand Down

0 comments on commit 979458c

Please sign in to comment.