-
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: add eslint rule for allowing only one enum per file (#3123)
* feat: add eslint rule for allowing only one enum per file * feat: add eslint rule for proper extesion name (#3124)
- Loading branch information
1 parent
9ce99f2
commit f19398e
Showing
4 changed files
with
78 additions
and
1 deletion.
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
31 changes: 31 additions & 0 deletions
31
eslint-custom-rules/rules/eslint-plugin-one-enum-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,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 | ||
}, | ||
}; | ||
}, | ||
}; |
42 changes: 42 additions & 0 deletions
42
eslint-custom-rules/rules/eslint-plugin-prefer-semantic-extension-name.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,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', | ||
}); | ||
} | ||
} | ||
}; | ||
}, | ||
}; |