Skip to content

Commit

Permalink
Codemod arrow-functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dfilipidisz committed Jul 20, 2017
1 parent a8cd2fe commit b82fca2
Show file tree
Hide file tree
Showing 44 changed files with 123 additions and 171 deletions.
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,10 @@ function configureAsError(rules) {
return result;
}

const activeRules = filterRules(allRules, function(rule) {
return !rule.meta.deprecated;
});
const activeRules = filterRules(allRules, rule => !rule.meta.deprecated);
const activeRulesConfig = configureAsError(activeRules);

const deprecatedRules = filterRules(allRules, function(rule) {
return rule.meta.deprecated;
});
const deprecatedRules = filterRules(allRules, rule => rule.meta.deprecated);

module.exports = {
deprecatedRules: deprecatedRules,
Expand Down
10 changes: 5 additions & 5 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = {
}]
},

create: Components.detect(function(context, components, utils) {
create: Components.detect((context, components, utils) => {
const sourceCode = context.getSourceCode();
const config = context.options[0] || {};
const rule = config.rule ? new RegExp(config.rule) : null;
Expand Down Expand Up @@ -118,7 +118,7 @@ module.exports = {
const component = components.get(node) || node;
const invalidProps = component.invalidProps || [];

proptypes.forEach(function (prop) {
proptypes.forEach(prop => {
const propKey = getPropKey(prop);
const flowCheck = (
prop.type === 'ObjectTypeProperty' &&
Expand Down Expand Up @@ -146,7 +146,7 @@ module.exports = {
* @param {Object} component The component to process
*/
function reportInvalidNaming(component) {
component.invalidProps.forEach(function (propNode) {
component.invalidProps.forEach(propNode => {
const propName = getPropName(propNode);
context.report({
node: propNode,
Expand Down Expand Up @@ -192,7 +192,7 @@ module.exports = {
}

// Search for the proptypes declaration
node.properties.forEach(function(property) {
node.properties.forEach(property => {
if (!isPropTypesDeclaration(property.key)) {
return;
}
Expand All @@ -213,7 +213,7 @@ module.exports = {
}

const list = components.list();
Object.keys(list).forEach(function (component) {
Object.keys(list).forEach(component => {
// If this is a functional component that uses a global type, check it
if (
list[component].node.type === 'FunctionDeclaration' &&
Expand Down
24 changes: 10 additions & 14 deletions lib/rules/default-props-match-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {
}]
},

create: Components.detect(function(context, components, utils) {
create: Components.detect((context, components, utils) => {
const configuration = context.options[0] || {};
const allowRequiredDefaults = configuration.allowRequiredDefaults || false;
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
Expand Down Expand Up @@ -139,7 +139,7 @@ module.exports = {

function resolveUnionTypeAnnotation(node) {
// Go through all the union and resolve any generic types.
return node.types.map(function(annotation) {
return node.types.map(annotation => {
if (annotation.type === 'GenericTypeAnnotation') {
return resolveGenericTypeAnnotation(annotation);
}
Expand All @@ -154,11 +154,9 @@ module.exports = {
* @returns {Object[]} Array of PropType object representations, to be consumed by `addPropTypesToComponent`.
*/
function getPropTypesFromObjectExpression(objectExpression) {
const props = objectExpression.properties.filter(function(property) {
return property.type !== 'ExperimentalSpreadProperty';
});
const props = objectExpression.properties.filter(property => property.type !== 'ExperimentalSpreadProperty');

return props.map(function(property) {
return props.map(property => {
return {
name: property.key.name,
isRequired: isRequiredPropType(property.value),
Expand Down Expand Up @@ -188,7 +186,7 @@ module.exports = {

case 'UnionTypeAnnotation':
const union = resolveUnionTypeAnnotation(node.typeAnnotation);
properties = union.reduce(function(acc, curr) {
properties = union.reduce((acc, curr) => {
if (!curr) {
return acc;
}
Expand All @@ -206,11 +204,9 @@ module.exports = {
break;
}

const props = properties.filter(function(property) {
return property.type === 'ObjectTypeProperty';
});
const props = properties.filter(property => property.type === 'ObjectTypeProperty');

return props.map(function(property) {
return props.map(property => {
// the `key` property is not present in ObjectTypeProperty nodes, so we need to get the key name manually.
const tokens = context.getFirstTokens(property, 1);
const name = tokens[0].value;
Expand All @@ -237,7 +233,7 @@ module.exports = {
return 'unresolved';
}

return objectExpression.properties.map(function(defaultProp) {
return objectExpression.properties.map(defaultProp => {
return {
name: defaultProp.key.name,
node: defaultProp
Expand Down Expand Up @@ -348,7 +344,7 @@ module.exports = {
return;
}

defaultProps.forEach(function(defaultProp) {
defaultProps.forEach(defaultProp => {
const prop = propFromName(propTypes, defaultProp.name);

if (prop && (allowRequiredDefaults || !prop.isRequired)) {
Expand Down Expand Up @@ -567,7 +563,7 @@ module.exports = {
}

// Search for the proptypes declaration
node.properties.forEach(function(property) {
node.properties.forEach(property => {
if (property.type === 'ExperimentalSpreadProperty') {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
}]
},

create: Components.detect(function(context, components, utils) {
create: Components.detect((context, components, utils) => {
const sourceCode = context.getSourceCode();
const config = context.options[0] || {};
const ignoreTranspilerName = config.ignoreTranspilerName || false;
Expand Down Expand Up @@ -209,7 +209,7 @@ module.exports = {
ObjectExpression: function(node) {
if (ignoreTranspilerName || !hasTranspilerName(node)) {
// Search for the displayName declaration
node.properties.forEach(function(property) {
node.properties.forEach(property => {
if (!property.key || !isDisplayNameDeclaration(property.key)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/forbid-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = {

const indexedForbidConfigs = {};

forbidConfiguration.forEach(function(item) {
forbidConfiguration.forEach(item => {
if (typeof item === 'string') {
indexedForbidConfigs[item] = {element: item};
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/forbid-foreign-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
},

ObjectPattern: function(node) {
const propTypesNode = node.properties.find(function(property) {
const propTypesNode = node.properties.find(property => {
return property.type === 'Property' && property.key.name === 'propTypes';
});

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = {
* @returns {void}
*/
function checkForbidden(declarations) {
declarations.forEach(function(declaration) {
declarations.forEach(declaration => {
if (declaration.type !== 'Property') {
return;
}
Expand Down Expand Up @@ -154,7 +154,7 @@ module.exports = {
},

ObjectExpression: function(node) {
node.properties.forEach(function(property) {
node.properties.forEach(property => {
if (!property.key) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-equals-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {

return {
JSXOpeningElement: function(node) {
node.attributes.forEach(function(attrNode) {
node.attributes.forEach(attrNode => {
if (!hasEqual(attrNode)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-filename-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module.exports = {
}

const allowedExtensions = getExtensionsConfig();
const isAllowedExtension = allowedExtensions.some(function (extension) {
const isAllowedExtension = allowedExtensions.some(extension => {
return filename.slice(-extension.length) === extension;
});

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-first-prop-new-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = {
(configuration === 'multiline-multiprop' && isMultilineJSX(node) && node.attributes.length > 1) ||
(configuration === 'always')
) {
node.attributes.some(function(decl) {
node.attributes.some(decl => {
if (decl.loc.start.line === node.loc.start.line) {
context.report({
node: decl,
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-indent-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ module.exports = {
* @param {Boolean} excludeCommas skip comma on start of line
*/
function checkNodesIndent(nodes, indent, excludeCommas) {
nodes.forEach(function(node) {
nodes.forEach(node => {
const nodeIndent = getNodeIndent(node, false, excludeCommas);
if (
node.type !== 'ArrayExpression' && node.type !== 'ObjectExpression' &&
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ module.exports = {
}

function getReturnStatement(body) {
return body.filter(function(item) {
return item.type === 'ReturnStatement';
})[0];
return body.filter(item => item.type === 'ReturnStatement')[0];
}

return {
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/jsx-max-props-per-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = {
const back = line[line.length - 1].end;
for (let i = 0; i < line.length; i += max) {
const nodes = line.slice(i, i + max);
output.push(nodes.reduce(function(prev, curr) {
output.push(nodes.reduce((prev, curr) => {
if (prev === '') {
return sourceCode.getText(curr);
}
Expand All @@ -77,7 +77,7 @@ module.exports = {
const firstProp = node.attributes[0];
const linePartitionedProps = [[firstProp]];

node.attributes.reduce(function (last, decl) {
node.attributes.reduce((last, decl) => {
if (last.loc.end.line === decl.loc.start.line) {
linePartitionedProps[linePartitionedProps.length - 1].push(decl);
} else {
Expand All @@ -86,7 +86,7 @@ module.exports = {
return decl;
});

linePartitionedProps.forEach(function (propsInLine) {
linePartitionedProps.forEach(propsInLine => {
if (propsInLine.length > maximum) {
const name = getPropName(propsInLine[maximum]);
context.report({
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = {
}]
},

create: Components.detect(function(context, components, utils) {
create: Components.detect((context, components, utils) => {
const configuration = context.options[0] || {};

return {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-duplicate-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = {
JSXOpeningElement: function (node) {
const props = {};

node.attributes.forEach(function(decl) {
node.attributes.forEach(decl => {
if (decl.type === 'JSXSpreadAttribute') {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function isTargetBlank(attr) {
}

function hasExternalLink(element) {
return element.attributes.some(function (attr) {
return element.attributes.some(attr => {
return attr.name &&
attr.name.name === 'href' &&
attr.value.type === 'Literal' &&
Expand All @@ -24,7 +24,7 @@ function hasExternalLink(element) {
}

function hasSecureRel(element) {
return element.attributes.find(function (attr) {
return element.attributes.find(attr => {
if (attr.type === 'JSXAttribute' && attr.name.name === 'rel') {
const tags = attr.value && attr.value.type === 'Literal' && attr.value.value.toLowerCase().split(' ');
return tags && (tags.indexOf('noopener') >= 0 && tags.indexOf('noreferrer') >= 0);
Expand Down
8 changes: 3 additions & 5 deletions lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function validateReservedFirstConfig(context, reservedFirst) {
if (Array.isArray(reservedFirst)) {
// Only allow a subset of reserved words in customized lists
// eslint-disable-next-line consistent-return
const nonReservedWords = reservedFirst.filter(function(word) {
const nonReservedWords = reservedFirst.filter(word => {
if (!isReservedPropName(word, RESERVED_PROPS_LIST)) {
return true;
}
Expand Down Expand Up @@ -135,12 +135,10 @@ module.exports = {
JSXOpeningElement: function(node) {
// `dangerouslySetInnerHTML` is only "reserved" on DOM components
if (reservedFirst && !isDOMComponent(node)) {
reservedList = reservedList.filter(function(prop) {
return prop !== 'dangerouslySetInnerHTML';
});
reservedList = reservedList.filter(prop => prop !== 'dangerouslySetInnerHTML');
}

node.attributes.reduce(function(memo, decl, idx, attrs) {
node.attributes.reduce((memo, decl, idx, attrs) => {
if (decl.type === 'JSXSpreadAttribute') {
return attrs[idx + 1];
}
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-array-index-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ module.exports = {

if (node.type === 'TemplateLiteral') {
// key={`foo-${bar}`}
node.expressions.filter(isArrayIndex).forEach(function() {
node.expressions.filter(isArrayIndex).forEach(() => {
context.report({node: node, message: ERROR_MESSAGE});
});

Expand All @@ -117,7 +117,7 @@ module.exports = {
// key={'foo' + bar}
const identifiers = getIdentifiersFromBinaryExpression(node);

identifiers.filter(isArrayIndex).forEach(function() {
identifiers.filter(isArrayIndex).forEach(() => {
context.report({node: node, message: ERROR_MESSAGE});
});

Expand All @@ -144,7 +144,7 @@ module.exports = {
return;
}

props.properties.forEach(function (prop) {
props.properties.forEach(prop => {
if (!prop.key || prop.key.name !== 'key') {
// { ...foo }
// { foo: bar }
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/no-children-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ module.exports = {
}

const props = node.arguments[1].properties;
const childrenProp = props.find(function(prop) {
return prop.key && prop.key.name === 'children';
});
const childrenProp = props.find(prop => prop.key && prop.key.name === 'children');

if (childrenProp) {
context.report({
Expand Down

0 comments on commit b82fca2

Please sign in to comment.