diff --git a/.eslintrc.json b/.eslintrc.json index 456579f6fe..badf9bfd81 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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" } }, { diff --git a/eslint-custom-rules/index.js b/eslint-custom-rules/index.js index 9d0998fce5..38ffdc886c 100644 --- a/eslint-custom-rules/index.js +++ b/eslint-custom-rules/index.js @@ -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'), }, }; \ No newline at end of file diff --git a/eslint-custom-rules/rules/eslint-plugin-one-enum-per-file.js b/eslint-custom-rules/rules/eslint-plugin-one-enum-per-file.js new file mode 100644 index 0000000000..544feb183e --- /dev/null +++ b/eslint-custom-rules/rules/eslint-plugin-one-enum-per-file.js @@ -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 + }, + }; + }, +}; diff --git a/eslint-custom-rules/rules/eslint-plugin-prefer-semantic-extension-name.js b/eslint-custom-rules/rules/eslint-plugin-prefer-semantic-extension-name.js new file mode 100644 index 0000000000..63cac7bc6f --- /dev/null +++ b/eslint-custom-rules/rules/eslint-plugin-prefer-semantic-extension-name.js @@ -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', + }); + } + } + }; + }, +};