Skip to content

Commit ac75ee6

Browse files
committed
refactor(kit): refactor report function into namespace based API
1 parent 3c5699a commit ac75ee6

File tree

12 files changed

+112
-80
lines changed

12 files changed

+112
-80
lines changed

packages/plugins/eslint-plugin-react-debug/src/rules/jsx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
22
import type { CamelCase } from "string-ts";
33
import { flow } from "@eslint-react/eff";
44
import * as JSX from "@eslint-react/jsx";
5-
import { createReport, JsxConfig, type RuleContext, type RuleFeature } from "@eslint-react/kit";
5+
import { JsxConfig, Report, type RuleContext, type RuleFeature } from "@eslint-react/kit";
66
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";
77
import { match, P } from "ts-pattern";
88
import { JsxEmit } from "typescript";
@@ -70,6 +70,6 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
7070
}
7171

7272
return {
73-
"JSXElement, JSXFragment": flow(getReportDescriptor, createReport(context)),
73+
"JSXElement, JSXFragment": flow(getReportDescriptor, Report.make(context).send),
7474
};
7575
}

packages/plugins/eslint-plugin-react-dom/src/rules/no-unknown-property.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import { getSettingsFromContext } from "@eslint-react/shared";
66
import { createRule } from "../utils";
77
import { compare } from "compare-versions";
8-
import { createReport } from "@eslint-react/kit";
8+
import { Report } from "@eslint-react/kit";
99
import type { RuleContext, RuleFeature } from "@eslint-react/kit";
1010
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
1111

@@ -1090,7 +1090,7 @@ export default createRule({
10901090
});
10911091

10921092
export function create(context: RuleContext<MessageID, unknown[]>): RuleListener {
1093-
const report = createReport(context);
1093+
const report = Report.make(context);
10941094
function getIgnoreConfig() {
10951095
return context.options[0]?.ignore || DEFAULTS.ignore;
10961096
}
@@ -1117,7 +1117,7 @@ export function create(context: RuleContext<MessageID, unknown[]>): RuleListener
11171117

11181118
if (isValidDataAttribute(name)) {
11191119
if (getRequireDataLowercase() && hasUpperCaseCharacter(name)) {
1120-
report({
1120+
report.send({
11211121
node,
11221122
messageId: "dataLowercaseRequired",
11231123
data: {
@@ -1147,7 +1147,7 @@ export function create(context: RuleContext<MessageID, unknown[]>): RuleListener
11471147
if (tagName && allowedTags) {
11481148
// Scenario 1A: Allowed attribute found where not supposed to, report it
11491149
if (allowedTags.indexOf(tagName) === -1) {
1150-
report({
1150+
report.send({
11511151
node,
11521152
messageId: "invalidPropOnTag",
11531153
data: {
@@ -1174,7 +1174,7 @@ export function create(context: RuleContext<MessageID, unknown[]>): RuleListener
11741174

11751175
if (hasStandardNameButIsNotUsed) {
11761176
// Scenario 2B: The name of the attribute is close to a standard one, report it with the standard name
1177-
report({
1177+
report.send({
11781178
node,
11791179
messageId: "unknownPropWithStandardName",
11801180
data: {
@@ -1189,7 +1189,7 @@ export function create(context: RuleContext<MessageID, unknown[]>): RuleListener
11891189
}
11901190

11911191
// Scenario 3: We have an attribute that is unknown, report it
1192-
report({
1192+
report.send({
11931193
node,
11941194
messageId: "unknownProp",
11951195
data: {

packages/plugins/eslint-plugin-react-x/src/rules/no-array-index-key.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CamelCase } from "string-ts";
44
import * as AST from "@eslint-react/ast";
55
import * as ER from "@eslint-react/core";
66
import { _ } from "@eslint-react/eff";
7-
import { createReport, type RuleContext, type RuleFeature } from "@eslint-react/kit";
7+
import { Report, type RuleContext, type RuleFeature } from "@eslint-react/kit";
88
import { unsafeDecodeSettings } from "@eslint-react/shared";
99
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
1010
import { isMatching } from "ts-pattern";
@@ -109,7 +109,7 @@ export default createRule<[], MessageID>({
109109
});
110110

111111
export function create(context: RuleContext<MessageID, []>): RuleListener {
112-
const report = createReport(context);
112+
const report = Report.make(context);
113113
const indexParamNames: Array<string | _> = [];
114114

115115
function isArrayIndex(node: TSESTree.Node): node is TSESTree.Identifier {
@@ -200,7 +200,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
200200
continue;
201201
}
202202
for (const descriptor of getReportDescriptors(prop.value)) {
203-
report(descriptor);
203+
report.send(descriptor);
204204
}
205205
}
206206
},
@@ -218,7 +218,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
218218
return;
219219
}
220220
for (const descriptor of getReportDescriptors(node.value.expression)) {
221-
report(descriptor);
221+
report.send(descriptor);
222222
}
223223
},
224224
};

packages/plugins/eslint-plugin-react-x/src/rules/no-leaked-conditional-rendering.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ReportDescriptor, RuleListener } from "@typescript-eslint/utils/ts
33
import type { CamelCase } from "string-ts";
44
import * as AST from "@eslint-react/ast";
55
import { _, flow } from "@eslint-react/eff";
6-
import { createReport, type RuleContext, type RuleFeature } from "@eslint-react/kit";
6+
import { Report, type RuleContext, type RuleFeature } from "@eslint-react/kit";
77
import { getSettingsFromContext } from "@eslint-react/shared";
88
import * as VAR from "@eslint-react/var";
99
import { getConstrainedTypeAtLocation } from "@typescript-eslint/type-utils";
@@ -260,7 +260,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
260260
})
261261
.otherwise(() => _);
262262
}
263-
const visitorFunction = flow(getReportDescriptor, createReport(context));
263+
const visitorFunction = flow(getReportDescriptor, Report.make(context).send);
264264
return {
265265
"JSXExpressionContainer > ConditionalExpression": visitorFunction,
266266
"JSXExpressionContainer > LogicalExpression": visitorFunction,

packages/plugins/eslint-plugin-react-x/src/rules/no-missing-key.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ReportDescriptor, RuleListener } from "@typescript-eslint/utils/ts
33
import * as AST from "@eslint-react/ast";
44
import * as ER from "@eslint-react/core";
55
import * as JSX from "@eslint-react/jsx";
6-
import { createReport, type RuleContext, type RuleFeature } from "@eslint-react/kit";
6+
import { Report, type RuleContext, type RuleFeature } from "@eslint-react/kit";
77

88
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
99
import { match } from "ts-pattern";
@@ -36,7 +36,7 @@ export default createRule<[], MessageID>({
3636
});
3737

3838
export function create(context: RuleContext<MessageID, []>): RuleListener {
39-
const report = createReport(context);
39+
const report = Report.make(context);
4040
const state = { isWithinChildrenToArray: false };
4141

4242
function checkIteratorElement(node: TSESTree.Node): null | ReportDescriptor<MessageID> {
@@ -104,7 +104,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
104104
const initialScope = context.sourceCode.getScope(node);
105105
for (const element of elements) {
106106
if (!JSX.hasAttribute("key", element.openingElement.attributes, initialScope)) {
107-
report({
107+
report.send({
108108
messageId: "missingKey",
109109
node: element,
110110
});
@@ -121,10 +121,10 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
121121
if (!AST.isFunction(callback)) return;
122122
const body = callback.body;
123123
if (body.type === T.BlockStatement) {
124-
checkBlockStatement(body).forEach(report);
124+
checkBlockStatement(body).forEach(report.send);
125125
return;
126126
}
127-
report(checkExpression(body));
127+
report.send(checkExpression(body));
128128
},
129129
"CallExpression:exit"(node) {
130130
if (!ER.isChildrenToArrayCall(context, node)) {
@@ -137,7 +137,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
137137
return;
138138
}
139139
if (node.parent.type === T.ArrayExpression) {
140-
report({
140+
report.send({
141141
messageId: "unexpectedFragmentSyntax",
142142
node,
143143
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[**@eslint-react/kit**](../../../README.md)
2+
3+
***
4+
5+
[@eslint-react/kit](../../../README.md) / Report
6+
7+
# Report
8+
9+
## Functions
10+
11+
- [make](functions/make.md)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[**@eslint-react/kit**](../../../../README.md)
2+
3+
***
4+
5+
[@eslint-react/kit](../../../../README.md) / [Report](../README.md) / make
6+
7+
# Function: make()
8+
9+
> **make**\<`TMessageID`\>(`context`): `object`
10+
11+
## Type Parameters
12+
13+
### TMessageID
14+
15+
`TMessageID` *extends* `string`
16+
17+
## Parameters
18+
19+
### context
20+
21+
[`RuleContext`](../../../../type-aliases/RuleContext.md)\<`TMessageID`\>
22+
23+
## Returns
24+
25+
`object`
26+
27+
### send()
28+
29+
> `readonly` **send**(`descriptor`): `void`
30+
31+
#### Parameters
32+
33+
##### descriptor
34+
35+
`undefined` | `null` | `ReportDescriptor`\<`TMessageID`\>
36+
37+
#### Returns
38+
39+
`void`
40+
41+
### sendOrElse()
42+
43+
> `readonly` **sendOrElse**\<`TElse`\>(`descriptor`, `fallback`): `void` \| `TElse`
44+
45+
#### Type Parameters
46+
47+
##### TElse
48+
49+
`TElse`
50+
51+
#### Parameters
52+
53+
##### descriptor
54+
55+
`undefined` | `null` | `ReportDescriptor`\<`TMessageID`\>
56+
57+
##### fallback
58+
59+
() => `TElse`
60+
61+
#### Returns
62+
63+
`void` \| `TElse`

packages/utilities/kit/docs/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [JsxConfig](@eslint-react/namespaces/JsxConfig/README.md)
1010
- [LanguagePreference](@eslint-react/namespaces/LanguagePreference/README.md)
1111
- [RE](@eslint-react/namespaces/RE/README.md)
12+
- [Report](@eslint-react/namespaces/Report/README.md)
1213
- [SEL](@eslint-react/namespaces/SEL/README.md)
1314

1415
## Type Aliases
@@ -17,7 +18,3 @@
1718
- [RuleFeature](type-aliases/RuleFeature.md)
1819
- [RulePreset](type-aliases/RulePreset.md)
1920
- [RuleSeverity](type-aliases/RuleSeverity.md)
20-
21-
## Functions
22-
23-
- [createReport](functions/createReport.md)

packages/utilities/kit/docs/functions/createReport.md

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { ReportDescriptor } from "@typescript-eslint/utils/ts-eslint";
2+
import type { RuleContext } from "./types";
3+
import { type _ } from "@eslint-react/eff";
4+
5+
export function make<TMessageID extends string>(context: RuleContext<TMessageID>) {
6+
return {
7+
send(descriptor: _ | null | ReportDescriptor<TMessageID>) {
8+
if (descriptor == null) return;
9+
return context.report(descriptor);
10+
},
11+
sendOrElse<TElse>(descriptor: _ | null | ReportDescriptor<TMessageID>, fallback: () => TElse) {
12+
if (descriptor == null) return fallback();
13+
return context.report(descriptor);
14+
},
15+
} as const;
16+
}

0 commit comments

Comments
 (0)