Skip to content

Commit

Permalink
feat: add eslint rule for allowing only one enum per file (#3123)
Browse files Browse the repository at this point in the history
* feat: add eslint rule for allowing only one enum per file

* feat: add eslint rule for proper extesion name (#3124)
  • Loading branch information
suyashpatil78 authored Jun 28, 2024
1 parent 9ce99f2 commit f19398e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@
"@typescript-eslint/no-inferrable-types": ["error"],
"no-unused-expressions": ["error"],
"no-console": ["error", { "allow": [""] }],
"custom-rules/one-interface-per-file": "error"
"custom-rules/one-interface-per-file": "error",
"custom-rules/one-enum-per-file": "error",
"custom-rules/prefer-semantic-extension-name": "error"
}
},
{
Expand Down
2 changes: 2 additions & 0 deletions eslint-custom-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ module.exports = {
'prefer-jasmine-matchers': require('./rules/eslint-plugin-prefer-jasmine-matchers'),
'prefer-resolve-to-reject-with': require('./rules/eslint-plugin-prefer-resolve-to-reject-with'),
'one-interface-per-file': require('./rules/eslint-plugin-one-interface-per-file'),
'one-enum-per-file': require('./rules/eslint-plugin-one-enum-per-file'),
'prefer-semantic-extension-name': require('./rules/eslint-plugin-prefer-semantic-extension-name'),
},
};
31 changes: 31 additions & 0 deletions eslint-custom-rules/rules/eslint-plugin-one-enum-per-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Enforce only one TypeScript enum per file.",
category: "TypeScript",
recommended: true,
},
schema: [],
},

create: function(context) {
let enumCount = 0;

return {
TSEnumDeclaration: function(node) {
enumCount++;

if (enumCount > 1) {
context.report({
node,
message: "Only one TypeScript enum is allowed per file.",
});
}
},
"Program:exit": function() {
enumCount = 0; // Reset the counters for the next file
},
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Enforce enums to be defined only in .enum.ts files',
category: 'Best Practices',
recommended: true
},
schema: [],
},
create: function(context) {
return {
TSEnumDeclaration: function(node) {
const fileName = context.getFilename();
if (!fileName.endsWith('.enum.ts')) {
context.report({
node,
message: 'Enums should be defined only in files with .enum.ts extension',
});
}
},
TSInterfaceDeclaration: function(node) {
const fileName = context.getFilename();
if (!fileName.endsWith('.model.ts')) {
context.report({
node,
message: 'Interfaces should be defined only in files with .model.ts extension',
});
}
},
TSTypeAliasDeclaration: function(node) {
const fileName = context.getFilename();
if (!fileName.endsWith('.model.ts')) {
context.report({
node,
message: 'Types should be defined only in files with .model.ts extension',
});
}
}
};
},
};

0 comments on commit f19398e

Please sign in to comment.