Skip to content

Commit

Permalink
Add config to allow jsx in classes (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahm authored Nov 11, 2022
1 parent fc415e3 commit 962043b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14.16.1
16.16.0
21 changes: 17 additions & 4 deletions src/prefer-function-component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Rule } from "eslint";

export const COMPONENT_SHOULD_BE_FUNCTION = "componentShouldBeFunction";
export const ALLOW_COMPONENT_DID_CATCH = "allowComponentDidCatch";
export const ALLOW_JSX_IN_CLASSES = "allowJsxInClasses";
const COMPONENT_DID_CATCH = "componentDidCatch";
// https://eslint.org/docs/developer-guide/working-with-rules
const PROGRAM_EXIT = "Program:exit";
Expand Down Expand Up @@ -40,6 +41,10 @@ const rule: Rule.RuleModule = {
default: true,
type: "boolean",
},
[ALLOW_JSX_IN_CLASSES]: {
default: false,
type: "boolean",
},
},
additionalProperties: false,
},
Expand All @@ -49,6 +54,8 @@ const rule: Rule.RuleModule = {
create(context: Rule.RuleContext) {
const allowComponentDidCatch =
context.options[0]?.allowComponentDidCatch ?? true;
const allowJsxInClasses =
context.options[0]?.allowComponentDidCatch ?? false;

function shouldPreferFunction(node: Node): boolean {
const properties = node.body.body;
Expand All @@ -72,14 +79,20 @@ const rule: Rule.RuleModule = {
}
}

function detectJsxInClass(node: Node): void {
if (!allowJsxInClasses) {
detect(node);
}
}

return {
"ClassDeclaration:has(JSXElement)": detect,
"ClassDeclaration:has(JSXFragment)": detect,
"ClassDeclaration:has(JSXElement)": detectJsxInClass,
"ClassDeclaration:has(JSXFragment)": detectJsxInClass,
"ClassDeclaration[superClass.object.name='React'][superClass.property.name='Component']":
detect,
"ClassDeclaration[superClass.name='Component']": detect,
"ClassExpression:has(JSXElement)": detect,
"ClassExpression:has(JSXFragment)": detect,
"ClassExpression:has(JSXElement)": detectJsxInClass,
"ClassExpression:has(JSXFragment)": detectJsxInClass,
"ClassExpression[superClass.object.name='React'][superClass.property.name='Component']":
detect,
"ClassExpression[superClass.name='Component']": detect,
Expand Down
16 changes: 16 additions & 0 deletions src/prefer-function-component/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RuleTester } from "eslint";
import rule, {
ALLOW_COMPONENT_DID_CATCH,
ALLOW_JSX_IN_CLASSES,
COMPONENT_SHOULD_BE_FUNCTION,
} from ".";

Expand Down Expand Up @@ -77,6 +78,21 @@ ruleTester.run("prefer-function-component", rule, {
};
`,
},
{
// non-component class with JSX
code: `
class Foo {
getBar() {
return <Bar />;
}
};
`,
options: [
{
[ALLOW_JSX_IN_CLASSES]: true,
},
],
},
{
// object with JSX
code: `
Expand Down

0 comments on commit 962043b

Please sign in to comment.