TWNTY-3825 - ESLint rule: const naming (#4171)
* ESLint rule: const naming Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: KlingerMatheus <klinger.matheus@gitstart.dev> * Refactor according to review Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: KlingerMatheus <klinger.matheus@gitstart.dev> * refactor: Reverts changes on `twenty-server` Co-authored-by: KlingerMatheus <klinger.matheus@gitstart.dev> Co-authored-by: v1b3m <vibenjamin6@gmail.com> --------- Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com> Co-authored-by: v1b3m <vibenjamin6@gmail.com> Co-authored-by: KlingerMatheus <klinger.matheus@gitstart.dev>
This commit is contained in:
committed by
GitHub
parent
a108d36040
commit
f543191552
@ -10,6 +10,10 @@ import {
|
||||
rule as matchingStateVariable,
|
||||
RULE_NAME as matchingStateVariableName,
|
||||
} from './rules/matching-state-variable';
|
||||
import {
|
||||
rule as maxConstsPerFile,
|
||||
RULE_NAME as maxConstsPerFileName,
|
||||
} from './rules/max-consts-per-file';
|
||||
import {
|
||||
rule as noHardcodedColors,
|
||||
RULE_NAME as noHardcodedColorsName,
|
||||
@ -60,5 +64,6 @@ module.exports = {
|
||||
[sortCssPropertiesAlphabeticallyName]: sortCssPropertiesAlphabetically,
|
||||
[styledComponentsPrefixedWithStyledName]:
|
||||
styledComponentsPrefixedWithStyled,
|
||||
[maxConstsPerFileName]: maxConstsPerFile,
|
||||
},
|
||||
};
|
||||
|
||||
25
tools/eslint-rules/rules/max-consts-per-file.spec.ts
Normal file
25
tools/eslint-rules/rules/max-consts-per-file.spec.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { TSESLint } from '@typescript-eslint/utils';
|
||||
|
||||
import { rule, RULE_NAME } from './max-consts-per-file';
|
||||
|
||||
const max = 1;
|
||||
|
||||
const ruleTester = new TSESLint.RuleTester({
|
||||
parser: require.resolve('@typescript-eslint/parser'),
|
||||
});
|
||||
|
||||
ruleTester.run(RULE_NAME, rule, {
|
||||
valid: [
|
||||
{
|
||||
code: 'const A = 1;',
|
||||
options: [{ max }],
|
||||
},
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
code: 'const NAME_A = 1;\nconst NAME_B = 2;',
|
||||
options: [{ max }],
|
||||
errors: [{ messageId: 'tooManyConstants', data: { max } }],
|
||||
},
|
||||
],
|
||||
});
|
||||
55
tools/eslint-rules/rules/max-consts-per-file.ts
Normal file
55
tools/eslint-rules/rules/max-consts-per-file.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
||||
|
||||
// NOTE: The rule will be available in ESLint configs as "@nx/workspace-max-consts-per-file"
|
||||
export const RULE_NAME = 'max-consts-per-file';
|
||||
|
||||
export const rule = ESLintUtils.RuleCreator(() => __filename)({
|
||||
name: RULE_NAME,
|
||||
meta: {
|
||||
type: 'problem',
|
||||
docs: {
|
||||
description:
|
||||
'Ensure there are at most a specified number of const declarations constant file',
|
||||
recommended: 'recommended',
|
||||
},
|
||||
fixable: 'code',
|
||||
schema: [
|
||||
{
|
||||
type: 'object',
|
||||
properties: {
|
||||
max: {
|
||||
type: 'integer',
|
||||
minimum: 0,
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
],
|
||||
messages: {
|
||||
tooManyConstants:
|
||||
'Only a maximum of ({{ max }}) const declarations are allowed in this file.',
|
||||
},
|
||||
},
|
||||
defaultOptions: [],
|
||||
create: (context) => {
|
||||
const [{ max }] = context.options;
|
||||
|
||||
let constCount = 0;
|
||||
|
||||
return {
|
||||
VariableDeclaration: (node: TSESTree.VariableDeclaration) => {
|
||||
constCount++;
|
||||
|
||||
if (constCount > max) {
|
||||
context.report({
|
||||
node,
|
||||
messageId: 'tooManyConstants',
|
||||
data: {
|
||||
max,
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user