-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new eslint rule for restricting more than 1 interface (#3084)
* feat: new eslint rule for restricting more than 1 interface * testing this * minor
- Loading branch information
1 parent
278a72e
commit 9ce99f2
Showing
3 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
eslint-custom-rules/rules/eslint-plugin-one-interface-per-file.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}; | ||
}, | ||
}; |