Skip to content

Commit

Permalink
chore: eslint 9 support
Browse files Browse the repository at this point in the history
  • Loading branch information
DianaSuvorova committed Aug 15, 2024
1 parent 55e2990 commit 9a955b5
Show file tree
Hide file tree
Showing 29 changed files with 12,019 additions and 9,639 deletions.
17 changes: 0 additions & 17 deletions .eslintrc.js

This file was deleted.

21 changes: 21 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
"plugins": {
"react": require("eslint-plugin-react"),
"jsx-a11y": require("eslint-plugin-jsx-a11y"),
"import": require("eslint-plugin-import")
},
"rules": {
"func-names": 0,
"global-require": 0,
"prefer-destructuring": 0,
"strict": 0,
// Include rules from airbnb configuration directly here
// Make sure to copy the rules from the airbnb configuration
},
"languageOptions": {
"globals": {
// Define global variables here for your files
"mocha": true
}
}
};
20 changes: 12 additions & 8 deletions lib/rules/connect-prefer-minimum-two-arguments.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
const isReactReduxConnect = require('../isReactReduxConnect');

const report = function (context, node) {
context.report({
message: 'Connect function should have at least 2 arguments.',
node,
});
};
const create = function (context) {
const report = function (node) {
context.report({
message: 'Connect function should have at least 2 arguments.',
node,
});
};

module.exports = function (context) {
return {
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (node.arguments.length < 2) {
report(context, node);
report(node);
}
}
},
};
};

module.exports = {
create,
};
22 changes: 13 additions & 9 deletions lib/rules/connect-prefer-named-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@ const argumentNames = [
'options',
];

const report = function (context, node, i) {
context.report({
message: `Connect function argument #${i} should be named ${argumentNames[i]}`,
node,
});
};
const create = function (context) {
const report = function (node, i) {
context.report({
message: `Connect function argument #${i + 1} should be named ${argumentNames[i]}`,
node,
});
};

module.exports = function (context) {
return {
CallExpression(node) {
if (isReactReduxConnect(node)) {
node.arguments.forEach((argument, i) => {
if (argument.raw && argument.raw !== 'null') {
report(context, node, i);
report(node, i);
} else if (
!argument.raw
&& argumentNames[i]
&& (!argument.name || argument.name !== argumentNames[i])) {
report(context, node, i);
report(node, i);
}
});
}
},
};
};

module.exports = {
create,
};
6 changes: 5 additions & 1 deletion lib/rules/mapDispatchToProps-prefer-parameters-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const check = function (context, params) {
});
};

module.exports = function (context) {
const create = function (context) {
return {
VariableDeclaration(node) {
node.declarations.forEach((decl) => {
Expand Down Expand Up @@ -52,3 +52,7 @@ module.exports = function (context) {
},
};
};

module.exports = {
create,
};
16 changes: 11 additions & 5 deletions lib/rules/mapDispatchToProps-prefer-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ const report = function (context, node) {
});
};

const getParamsString = (params, context) =>
params.map(param => context.getSource(param)).join(',');
const getParamsString = (params, context) => {
const sourceCode = context.sourceCode ?? context.getSourceCode();
return params.map(param => sourceCode.getText(param)).join(',')
}


const propertyCanUseShortHandButDoesnt = (context, prop, dispatchName) => {
const propName = prop.key && prop.key.name;
const sourceCode = context.getSource(prop.value).replace(/(\r\n|\n|\r|\t| |;)/gm, '');
const sourceCodeImpl = context.sourceCode ?? context.getSourceCode();
const sourceCode = sourceCodeImpl.getText(prop.value).replace(/(\r\n|\n|\r|\t| |;)/gm, '');
if (prop.value && prop.value.type === 'ArrowFunctionExpression') {
const fncDef = prop.value;
const paramString = getParamsString(fncDef.params, context);
Expand Down Expand Up @@ -42,8 +45,7 @@ const checkReturnNode = function (context, returnNode, dispatchName) {
}
};


module.exports = function (context) {
const create = function (context) {
return {
VariableDeclaration(node) {
node.declarations.forEach((decl) => {
Expand Down Expand Up @@ -84,3 +86,7 @@ module.exports = function (context) {
},
};
};

module.exports = {
create,
};
7 changes: 5 additions & 2 deletions lib/rules/mapDispatchToProps-returns-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const report = function (context, node) {
});
};


module.exports = function (context) {
const create = function (context) {
return {
VariableDeclaration(node) {
node.declarations.forEach((decl) => {
Expand Down Expand Up @@ -50,3 +49,7 @@ module.exports = function (context) {
},
};
};

module.exports = {
create,
};
6 changes: 5 additions & 1 deletion lib/rules/mapStateToProps-no-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const checkFunction = function (context, body, firstParamName) {
}
};

module.exports = function (context) {
const create = function (context) {
return {
VariableDeclaration(node) {
node.declarations.forEach((decl) => {
Expand Down Expand Up @@ -78,3 +78,7 @@ module.exports = function (context) {
},
};
};

module.exports = {
create,
};
7 changes: 5 additions & 2 deletions lib/rules/mapStateToProps-prefer-hoisted.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ const checkProp = (node, context) => {
}
};


const checkFunction = function (context, body) {
const returnNode = utils.getReturnNode(body);
if (returnNode && returnNode.type === 'ObjectExpression') {
returnNode.properties.forEach(prop => checkProp(prop.value, context));
}
};

module.exports = function (context) {
const create = function (context) {
return {
VariableDeclaration(node) {
node.declarations.forEach((decl) => {
Expand All @@ -62,3 +61,7 @@ module.exports = function (context) {
},
};
};

module.exports = {
create,
};
6 changes: 5 additions & 1 deletion lib/rules/mapStateToProps-prefer-parameters-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const check = function (context, params) {
});
};

module.exports = function (context) {
const create = function (context) {
return {
VariableDeclaration(node) {
node.declarations.forEach((decl) => {
Expand Down Expand Up @@ -52,3 +52,7 @@ module.exports = function (context) {
},
};
};

module.exports = {
create,
};
13 changes: 12 additions & 1 deletion lib/rules/mapStateToProps-prefer-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const check = function (context, node, matching, validateParams) {
}
};

module.exports = function (context) {
const create = function (context) {
const config = context.options[0] || {};
return {
VariableDeclaration(node) {
Expand Down Expand Up @@ -95,3 +95,14 @@ module.exports = function (context) {
},
};
};

module.exports = {
create,
meta: {
schema: {
matching: {
type: 'string'
}
}
},
};
2 changes: 1 addition & 1 deletion lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ const getPropNameFromReduxRuleMessage = message => message.replace('exclude:', '
module.exports = filterReports([
propsUsedInRedux,
noUnusedPropTypesReact,
], getPropNameFromReactRuleMessage, getPropNameFromReduxRuleMessage);
], getPropNameFromReactRuleMessage, getPropNameFromReduxRuleMessage);
47 changes: 25 additions & 22 deletions lib/rules/prefer-separate-component-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,30 @@ const report = function (context, node) {
});
};

module.exports = function (context) {
return {
CallExpression(node) {
if (isReactReduxConnect(node)) {
const component =
node.parent &&
node.parent.arguments &&
node.parent.arguments[0];
if (component) {
const vars = context.getScope().variables;
vars.forEach((definedVar) => {
if (component.name === definedVar.name) {
definedVar.defs.forEach((def) => {
if (!(def.type === 'ImportBinding' || context.getSource(def.node).includes('require'))) {
report(context, component);
}
});
}
});
module.exports = {
create(context) {
const sourceCode = context.sourceCode ?? context.getSourceCode();
return {
CallExpression(node) {
if (isReactReduxConnect(node)) {
const component =
node.parent &&
node.parent.arguments &&
node.parent.arguments[0];
if (component) {
const vars = sourceCode.getScope(component).variables;
vars.forEach((definedVar) => {
if (component.name === definedVar.name) {
definedVar.defs.forEach((def) => {
if (!(def.type === 'ImportBinding' || sourceCode.getText(def.node).includes('require'))) {
report(context, component);
}
});
}
});
}
}
}
},
};
},
};
},
};
59 changes: 34 additions & 25 deletions lib/rules/useSelector-prefer-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,40 @@ function reportNoSelector(context, node) {
});
}

module.exports = function (context) {
const config = context.options[0] || {};
let hookNames = ['useSelector', 'useAppSelector'];
module.exports = {
meta: {
schema: {
matching: {
type: 'string'
}
}
},
create(context) {
const config = context.options[0] || {};
let hookNames = ['useSelector', 'useAppSelector'];

// Ensure hookNames is an array
if (config.hook) {
hookNames = Array.isArray(config.hook) ? config.hook : [config.hook];
}
// Ensure hookNames is an array
if (config.hook) {
hookNames = Array.isArray(config.hook) ? config.hook : [config.hook];
}

return {
CallExpression(node) {
if (!isUseSelector(node, hookNames)) return;
const selector = node.arguments && node.arguments[0];
if (selector && (
selector.type === 'ArrowFunctionExpression' ||
selector.type === 'FunctionExpression')
) {
reportNoSelector(context, node);
} else if (
selector && selector.type === 'Identifier' &&
config.matching &&
!selector.name.match(new RegExp(config.matching))
) {
reportWrongName(context, node, selector.name, config.matching);
}
},
};
return {
CallExpression(node) {
if (!isUseSelector(node, hookNames)) return;
const selector = node.arguments && node.arguments[0];
if (selector && (
selector.type === 'ArrowFunctionExpression' ||
selector.type === 'FunctionExpression')
) {
reportNoSelector(context, node);
} else if (
selector && selector.type === 'Identifier' &&
config.matching &&
!selector.name.match(new RegExp(config.matching))
) {
reportWrongName(context, node, selector.name, config.matching);
}
},
};
},
};
Loading

0 comments on commit 9a955b5

Please sign in to comment.