Skip to content
Open
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
2 changes: 2 additions & 0 deletions base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const overrides = require('./overrides');

module.exports = {
extends : ['airbnb-base'],
plugins : ['eslint-plugin-custom-eslint-rules'],
parserOptions : { ecmaVersion: 2020 },
rules : {
...overrides.base,
...overrides.cogoAdmin,
},
};
9 changes: 9 additions & 0 deletions overrides/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const MAXIMUM_LENGTH = 250;
const sharedRules = require('./shared');

const base = {
Expand Down Expand Up @@ -67,12 +68,20 @@ const react = {
'jsx-a11y/media-has-caption' : 'off',
'jsx-a11y/click-events-have-key-events' : 'off',
'jsx-a11y/label-has-associated-control' : 'off',

};

const cogoAdmin = {
'max-lines-per-function' : ['error', MAXIMUM_LENGTH],
'no-magic-numbers' : ['error', { ignoreDefaultValues: true }],
'custom-eslint-rules/naming-convention' : 'error',
};

const overided = {
base,
react,
typescript,
cogoAdmin,
};

module.exports = overided;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cogoport/eslint-config",
"version": "1.1.0",
"version": "1.1.1-beta.2",
"description": "ESLint Config by Cogoport",
"repository": "git@github.com:Cogoport/eslint-config.git",
"license": "MIT",
Expand All @@ -17,7 +17,8 @@
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsx-a11y": "^6.6.1",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0"
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-custom-eslint-rules": "file:plugins/custom-eslint-rules"
},
"devDependencies": {
"eslint": "^8.23.0"
Expand Down
7 changes: 7 additions & 0 deletions plugins/custom-eslint-rules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const namingConvention = require('./naming-convention');

module.exports = {
rules: {
'naming-convention': namingConvention,
},
};
56 changes: 56 additions & 0 deletions plugins/custom-eslint-rules/naming-convention.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function isValidName(name, kind) {
if (kind === 'const') {
return /^[A-Z][A-Z0-9_]*$/.test(name);
}
return /^[a-z][A-Za-z0-9_]*$/.test(name);
}
const DEFAULT_ELEMENT = 0;
module.exports = {
meta: {
type : 'problem',
hasSuggestions : true,
fixable : true,
},
create: (context) => ({
VariableDeclaration(node) {
const { init = {}, id = {} } = node?.declarations[DEFAULT_ELEMENT] || {};

const { name = '' } = id;

const { type = '', properties = [], elements = [] } = init;

if (type === 'Literal') {
if (!isValidName(name, node.kind)) {
context.report({
node,
message: `Variable name ${name} should be in UPPER_CASE`,
});
}
}

if (type === 'ObjectExpression') {
const objectNamingCondition = properties.find((item) => item?.value?.type !== 'Literal');
if (!objectNamingCondition) {
if (!isValidName(name, node.kind)) {
context.report({
node,
message: `Variable name ${name} should be in UPPER_CASE`,
});
}
}
}
if (type === 'ArrayExpression') {
const arrayNamingCondition = elements.find((item) => item?.value?.type !== 'Literal');
if (!arrayNamingCondition) {
if (!isValidName(name, node.kind)) {
context.report({
node,
message: `Variable name ${name} should be in UPPER_CASE`,
});
}
}
}
},
}),

};
Loading