Skip to content

Commit 447a0f3

Browse files
committed
refactor: minor improvements
1 parent 53ddbba commit 447a0f3

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { MutRef, O, P } from "@eslint-react/tools";
2-
import type { TSESTree } from "@typescript-eslint/types";
1+
import { MutRef, P } from "@eslint-react/tools";
32
import type { ESLintUtils } from "@typescript-eslint/utils";
43
import type { JSONSchema4 } from "@typescript-eslint/utils/json-schema";
54

@@ -66,30 +65,34 @@ export default createRule<Options, MessageID>({
6665
const cond = P.isString(options) ? options : options.rule ?? "as-needed";
6766

6867
const filename = context.getFilename();
69-
const jsxNodeRef = MutRef.make<O.Option<TSESTree.JSXElement | TSESTree.JSXFragment>>(O.none());
68+
const hasJSXNodeRef = MutRef.make<boolean>(false);
7069

7170
return {
72-
JSXElement(node) {
73-
MutRef.set(jsxNodeRef, O.some(node));
71+
JSXElement() {
72+
MutRef.set(hasJSXNodeRef, true);
7473
},
75-
JSXFragment(node) {
76-
MutRef.set(jsxNodeRef, O.some(node));
74+
JSXFragment() {
75+
MutRef.set(hasJSXNodeRef, true);
7776
},
7877
"Program:exit"(node) {
7978
const fileNameExt = filename.slice(filename.lastIndexOf("."));
79+
const isJSX = isJSXFile(fileNameExt);
80+
const hasJSXCode = MutRef.get(hasJSXNodeRef);
8081

81-
if (O.isSome(MutRef.get(jsxNodeRef))) {
82-
if (!isJSXFile(fileNameExt)) {
83-
context.report({
84-
messageId: "FILE_NAME_EXTENSION_INVALID",
85-
node,
86-
});
87-
}
82+
if (!isJSX && hasJSXCode) {
83+
context.report({
84+
messageId: "FILE_NAME_EXTENSION_INVALID",
85+
node,
86+
});
8887

8988
return;
9089
}
9190

92-
if (cond === "as-needed" && isJSXFile(fileNameExt)) {
91+
if (
92+
isJSX
93+
&& !hasJSXCode
94+
&& cond === "as-needed"
95+
) {
9396
context.report({
9497
messageId: "FILE_NAME_EXTENSION_UNEXPECTED",
9598
node,

0 commit comments

Comments
 (0)