Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform arrow functions #1281

Merged
merged 3 commits into from
Jul 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"guard-for-in": 2,
"no-alert": 2,
"no-caller": 2,
"no-confusing-arrow": 2,
"no-div-regex": 2,
"no-else-return": 2,
"no-eq-null": 2,
Expand Down Expand Up @@ -81,6 +82,7 @@
"no-void": 0,
"no-warning-comments": 2,
"no-with": 2,
"prefer-arrow-callback": 2,
"radix": 2,
"vars-on-top": 0,
"wrap-iife": 2,
Expand All @@ -105,6 +107,9 @@
"indent": [2, 2, {
"SwitchCase": 1
}],
"arrow-body-style": [2, "as-needed"],
"arrow-parens": [2, "as-needed"],
"arrow-spacing": 2,
"brace-style": 2,
"camelcase": 0,
"comma-spacing": 2,
Expand Down
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
44 changes: 18 additions & 26 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 @@ -91,7 +91,7 @@ module.exports = {
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
*/
function findVariableByName(name) {
const variable = variableUtil.variablesInScope(context).find((item) => item.name === name);
const variable = variableUtil.variablesInScope(context).find(item => item.name === name);

if (!variable || !variable.defs[0] || !variable.defs[0].node) {
return null;
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,17 +154,13 @@ 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 {
name: property.key.name,
isRequired: isRequiredPropType(property.value),
node: property
};
});
return props.map(property => ({
name: property.key.name,
isRequired: isRequiredPropType(property.value),
node: property
}));
}

/**
Expand All @@ -188,7 +184,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 +202,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,12 +231,10 @@ module.exports = {
return 'unresolved';
}

return objectExpression.properties.map(function(defaultProp) {
return {
name: defaultProp.key.name,
node: defaultProp
};
});
return objectExpression.properties.map(defaultProp => ({
name: defaultProp.key.name,
node: defaultProp
}));
}

/**
Expand Down Expand Up @@ -348,7 +340,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 +559,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
4 changes: 1 addition & 3 deletions lib/rules/forbid-foreign-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ module.exports = {
},

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

if (propTypesNode) {
context.report({
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-boolean-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const NEVER = 'never';
const errorData = new WeakMap();
function getErrorData(exceptions) {
if (!errorData.has(exceptions)) {
const exceptionProps = Array.from(exceptions, (name) => `\`${name}\``).join(', ');
const exceptionProps = Array.from(exceptions, name => `\`${name}\``).join(', ');
const exceptionsMessage = exceptions.size > 0 ? ` for the following props: ${exceptionProps}` : '';
errorData.set(exceptions, {exceptionsMessage: exceptionsMessage});
}
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
4 changes: 1 addition & 3 deletions lib/rules/jsx-filename-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ module.exports = {
}

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

if (isAllowedExtension) {
return;
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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving a reminder to myself later to change this to .find

}

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
8 changes: 3 additions & 5 deletions lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@ function isTargetBlank(attr) {
}

function hasExternalLink(element) {
return element.attributes.some(function (attr) {
return attr.name &&
return element.attributes.some(attr => attr.name &&
attr.name.name === 'href' &&
attr.value.type === 'Literal' &&
/^(?:\w+:|\/\/)/.test(attr.value.value);
});
/^(?:\w+:|\/\/)/.test(attr.value.value));
}

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
Loading