|
| 1 | +# Local ESLint Rules |
| 2 | + |
| 3 | +This directory contains custom ESLint rules specific to this repository. |
| 4 | + |
| 5 | +## public-methods-well-defined-types |
| 6 | + |
| 7 | +This rule enforces that public methods in exported classes return well-defined types (no inline types). |
| 8 | + |
| 9 | +### Rule Details |
| 10 | + |
| 11 | +- **Purpose**: Ensure public methods return named interfaces, types, or classes instead of inline object literals or anonymous types |
| 12 | +- **Scope**: Applies only to `webviews/common/context.tsx` |
| 13 | +- **Level**: Error |
| 14 | + |
| 15 | +### Examples |
| 16 | + |
| 17 | +**Incorrect** (will trigger the rule): |
| 18 | +```typescript |
| 19 | +export class MyClass { |
| 20 | + public badMethod(): { foo: string; bar: number } { |
| 21 | + return { foo: 'test', bar: 123 }; |
| 22 | + } |
| 23 | + |
| 24 | + public badUnionMethod(): string | { error: string } { |
| 25 | + return 'test'; |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +**Correct** (will not trigger the rule): |
| 31 | +```typescript |
| 32 | +interface ResultType { |
| 33 | + foo: string; |
| 34 | + bar: number; |
| 35 | +} |
| 36 | + |
| 37 | +export class MyClass { |
| 38 | + public goodMethod(): ResultType { |
| 39 | + return { foo: 'test', bar: 123 }; |
| 40 | + } |
| 41 | + |
| 42 | + public goodPromiseMethod(): Promise<string> { |
| 43 | + return Promise.resolve('test'); |
| 44 | + } |
| 45 | + |
| 46 | + // Private methods are not checked |
| 47 | + private privateMethod(): { foo: string } { |
| 48 | + return { foo: 'test' }; |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Configuration |
| 54 | + |
| 55 | +To use this rule, include it in your ESLint configuration: |
| 56 | + |
| 57 | +```javascript |
| 58 | +const RULES_DIR = require('eslint-plugin-rulesdir'); |
| 59 | +RULES_DIR.RULES_DIR = './build/eslint-rules'; |
| 60 | + |
| 61 | +module.exports = { |
| 62 | + plugins: ['rulesdir'], |
| 63 | + rules: { |
| 64 | + 'rulesdir/public-methods-well-defined-types': 'error' |
| 65 | + } |
| 66 | +}; |
| 67 | +``` |
| 68 | + |
| 69 | +### Testing |
| 70 | + |
| 71 | +Use `.eslintrc.context.js` to test the rule on the target file: |
| 72 | + |
| 73 | +```bash |
| 74 | +npx eslint --config .eslintrc.context.js webviews/common/context.tsx |
| 75 | +``` |
0 commit comments