Skip to content

Commit

Permalink
feat: new eslint rule for restricting more than 1 interface (#3084)
Browse files Browse the repository at this point in the history
* feat: new eslint rule for restricting more than 1 interface

* testing this

* minor
  • Loading branch information
suyashpatil78 authored Jun 28, 2024
1 parent 278a72e commit 9ce99f2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
"@typescript-eslint/no-empty-interface": ["error"],
"@typescript-eslint/no-inferrable-types": ["error"],
"no-unused-expressions": ["error"],
"no-console": ["error", { "allow": [""] }]
"no-console": ["error", { "allow": [""] }],
"custom-rules/one-interface-per-file": "error"
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion eslint-custom-rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
'prefer-deep-freeze': require('./rules/eslint-plugin-prefer-deep-freeze'),
'space-before-it-blocks': require('./rules/eslint-plugin-space-before-it-blocks'),
'prefer-jasmine-matchers': require('./rules/eslint-plugin-prefer-jasmine-matchers'),
'prefer-resolve-to-reject-with': require('./rules/eslint-plugin-prefer-resolve-to-reject-with')
'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'),
},
};
45 changes: 45 additions & 0 deletions eslint-custom-rules/rules/eslint-plugin-one-interface-per-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Enforce only one TypeScript interface or type per file.",
category: "TypeScript",
recommended: true,
},
schema: [],
},

create: function(context) {
let interfaceCount = 0;
let typeCount = 0;

return {
TSInterfaceDeclaration: function(node) {
interfaceCount++;

if (interfaceCount > 1 || typeCount > 0) {
context.report({
node,
message: "Only one TypeScript interface or type is allowed per file.",
});
}
},

TSTypeAliasDeclaration: function(node) {
typeCount++;

if (typeCount > 1 || interfaceCount > 0) {
context.report({
node,
message: "Only one TypeScript interface or type is allowed per file.",
});
}
},

"Program:exit": function() {
interfaceCount = 0; // Reset the counters for the next file
typeCount = 0;
},
};
},
};

0 comments on commit 9ce99f2

Please sign in to comment.