Skip to content

Commit

Permalink
refactor: move flow file checks into a rule wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Harvey authored and danharper committed Nov 27, 2016
1 parent 67b078e commit 52b71f3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 40 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';

0 comments on commit 52b71f3

Please sign in to comment.