Skip to content

Commit 08ef6ba

Browse files
committed
Add documentation and targeted configuration for ESLint rule
1 parent 7bfcfd1 commit 08ef6ba

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

.eslintrc.context.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
'use strict';
7+
8+
const RULES_DIR = require('eslint-plugin-rulesdir');
9+
RULES_DIR.RULES_DIR = './build/eslint-rules';
10+
11+
module.exports = {
12+
extends: ['.eslintrc.base.json'],
13+
env: {
14+
browser: true,
15+
node: true
16+
},
17+
parserOptions: {
18+
project: 'tsconfig.eslint.json'
19+
},
20+
plugins: ['rulesdir'],
21+
overrides: [
22+
{
23+
files: ['webviews/common/context.tsx'],
24+
rules: {
25+
'rulesdir/public-methods-well-defined-types': 'error'
26+
}
27+
}
28+
]
29+
};

build/eslint-rules/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)