Skip to content

Commit 861b01d

Browse files
committed
refactor: minor improvements
1 parent 5cfd0b0 commit 861b01d

File tree

7 files changed

+63
-27
lines changed

7 files changed

+63
-27
lines changed

packages/core/src/hook/hook-name.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export const RE_HOOK_NAME = /^use[A-Z\d].*$/u;
1+
export const RE_HOOK_NAME = /^use[A-Z\d]/u;
22

33
export function isValidReactHookName(name: string) {
4-
return !!name && RE_HOOK_NAME.test(name);
4+
return name === "use" || RE_HOOK_NAME.test(name);
55
}

packages/eslint-plugin-naming-convention/src/rules/component-name.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ export const RULE_NAME = "component-name";
1313

1414
export type MessageID = ConstantCase<typeof RULE_NAME>;
1515

16+
type Case = "CONSTANT_CASE" | "PascalCase";
17+
1618
/* eslint-disable no-restricted-syntax */
1719
type Options = readonly [
1820
| {
1921
excepts?: readonly string[];
20-
rule?: "CONSTANT_CASE" | "PascalCase";
22+
rule?: Case;
2123
}
22-
| string
24+
| Case
2325
| undefined,
2426
];
2527
/* eslint-enable no-restricted-syntax */
@@ -76,12 +78,12 @@ export default createRule<Options, MessageID>({
7678
const excepts = P.isString(options) ? [] : options.excepts ?? [];
7779
const rule = P.isString(options) ? options : options.rule ?? "PascalCase";
7880

81+
const validator = getCaseValidator(rule, [...excepts]);
82+
const validate = (name: string) => validator.validate(name);
83+
7984
const collector = componentCollector(context);
8085
const collectorLegacy = componentCollectorLegacy(context);
8186

82-
const validator = getCaseValidator(rule, [...excepts]);
83-
const validate = (n: string) => validator.validate(n);
84-
8587
return {
8688
...collector.listeners,
8789
...collectorLegacy.listeners,

packages/eslint-plugin-naming-convention/src/rules/filename.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { O, P } from "@eslint-react/tools";
33
import { getCaseValidator } from "@eslint-react/utils";
44
import type { ESLintUtils } from "@typescript-eslint/utils";
55
import type { JSONSchema4 } from "@typescript-eslint/utils/json-schema";
6-
import type { ReportDescriptor } from "@typescript-eslint/utils/ts-eslint";
76
import path from "pathe";
87

98
import { createRule } from "../utils";
@@ -15,14 +14,16 @@ export type MessageID =
1514
| "FILENAME_CASE_MISMATCH_SUGGESTION"
1615
| "FILENAME_EMPTY";
1716

17+
type Case = "PascalCase" | "camelCase" | "kebab-case" | "snake_case";
18+
1819
/* eslint-disable no-restricted-syntax */
1920
type Options = readonly [
2021
| {
2122
extensions?: readonly string[];
2223
excepts?: readonly string[];
23-
rule?: "PascalCase" | "camelCase" | "kebab-case" | "snake_case";
24+
rule?: Case;
2425
}
25-
| string
26+
| Case
2627
| undefined,
2728
];
2829
/* eslint-enable no-restricted-syntax */
@@ -100,8 +101,8 @@ export default createRule<Options, MessageID>({
100101
}
101102

102103
const validator = getCaseValidator(rule, [...excepts]);
103-
const validate = (n: string) => validator.validate(n);
104-
const getRecommendedName = (n: string) => validator.getRecommendedName(n);
104+
const validate = (name: string) => validator.validate(name);
105+
const getRecommendedName = (name: string) => validator.getRecommendedName(name);
105106

106107
return {
107108
Program(node) {
@@ -116,27 +117,27 @@ export default createRule<Options, MessageID>({
116117
}
117118

118119
const maybeSuggestion = O.liftThrowable(getRecommendedName)(basename);
119-
const descriptor: ReportDescriptor<MessageID> = O.match(maybeSuggestion, {
120-
onNone: () => ({
121-
data: {
122-
name: basename,
123-
rule,
124-
},
125-
messageId: "FILENAME_CASE_MISMATCH",
126-
node,
127-
}),
128-
onSome: (value) => ({
120+
121+
if (O.isNone(maybeSuggestion)) {
122+
return context.report({
129123
data: {
130124
name: filename,
131125
rule,
132-
suggestion: `${[value, ...rest].join(".")}`,
133126
},
134-
messageId: "FILENAME_CASE_MISMATCH_SUGGESTION",
127+
messageId: "FILENAME_CASE_MISMATCH",
135128
node,
136-
}),
137-
});
129+
});
130+
}
138131

139-
context.report(descriptor);
132+
context.report({
133+
data: {
134+
name: filename,
135+
rule,
136+
suggestion: `${[maybeSuggestion.value, ...rest].join(".")}`,
137+
},
138+
messageId: "FILENAME_CASE_MISMATCH_SUGGESTION",
139+
node,
140+
});
140141
},
141142
};
142143
},

packages/utils/docs/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
### Functions
1717

1818
- [createRuleForPlugin](README.md#createruleforplugin)
19+
- [getCaseConverter](README.md#getcaseconverter)
1920
- [getCaseValidator](README.md#getcasevalidator)
2021
- [getRule](README.md#getrule)
2122
- [mergeConfigs](README.md#mergeconfigs)
@@ -74,6 +75,22 @@ Function to create a rule with the docs URL format.
7475

7576
---
7677

78+
### getCaseConverter
79+
80+
**getCaseConverter**(`rule`): \<T\>(`str`: `T`) => `ConstantCase`\<`T`\> \| \<T\>(`str`: `T`) => `PascalCase`\<`T`\> \| \<T\>(`str`: `T`) => `CamelCase`\<`T`\> \| \<T\>(`str`: `T`) => `KebabCase`\<`T`\> \| \<T\>(`str`: `T`) => `SnakeCase`\<`T`\>
81+
82+
#### Parameters
83+
84+
| Name | Type |
85+
| :----- | :--------------------------------------------------------------------------------------- |
86+
| `rule` | `"CONSTANT_CASE"` \| `"PascalCase"` \| `"camelCase"` \| `"kebab-case"` \| `"snake_case"` |
87+
88+
#### Returns
89+
90+
\<T\>(`str`: `T`) => `ConstantCase`\<`T`\> \| \<T\>(`str`: `T`) => `PascalCase`\<`T`\> \| \<T\>(`str`: `T`) => `CamelCase`\<`T`\> \| \<T\>(`str`: `T`) => `KebabCase`\<`T`\> \| \<T\>(`str`: `T`) => `SnakeCase`\<`T`\>
91+
92+
---
93+
7794
### getCaseValidator
7895

7996
**getCaseValidator**(`ruleName`, `ignorePattern?`): [`CaseValidator`](classes/CaseValidator.md)

packages/utils/src/case-convert.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { camelCase, constantCase, kebabCase, pascalCase, snakeCase } from "string-ts";
2+
3+
const converters = {
4+
CONSTANT_CASE: constantCase,
5+
PascalCase: pascalCase,
6+
camelCase,
7+
"kebab-case": kebabCase,
8+
snake_case: snakeCase,
9+
} as const;
10+
11+
// eslint-disable-next-line security/detect-object-injection
12+
export const getCaseConverter = (rule: keyof typeof converters) => converters[rule];

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./case-convert";
12
export * from "./case-validator";
23
export * from "./create-rule";
34
export * from "./merge-configs";

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)