Skip to content

Commit

Permalink
fix: issue with eslint plugin not allowing useDefaultProps (#1696)
Browse files Browse the repository at this point in the history
* fix: issue with eslint plugin not allowing useDefaultProps

* chore: update only-default-function-and-imports tests and description

* Update packages/eslint-plugin/src/rules/only-default-function-and-imports.ts

---------

Co-authored-by: Sami Jaber <[email protected]>
  • Loading branch information
nmerget and samijaber authored Feb 28, 2025
1 parent e0ba666 commit 0f2a9ee
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-oranges-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/eslint-plugin-mitosis': patch
---

Fix issue with plugin marking 'useDefaultProps' as an error
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/constants/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export const HOOKS = {
CONTEXT: 'useContext',
REF: 'useRef',
META_DATA: 'useMetadata',
DEFAULT_PROPS: 'useDefaultProps',
} as const;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RuleTester } from 'eslint';
import rule from '../only-default-function-and-imports';
import rule, { onlyDefaultFunctionAndImportsMessage } from '../only-default-function-and-imports';

const opts = {
filename: 'component.lite.tsx',
Expand Down Expand Up @@ -76,6 +76,20 @@ ruleTester.run('only-default-function-and-imports', rule, {
}
`,
},
{
...opts,
code: `
useDefaultProps({
test: "XXX"
});
export default function RenderComponent(props) {
return (
<div>{props.test}</div>
);
}
`,
},
{
...opts,
code: `
Expand All @@ -102,9 +116,7 @@ ruleTester.run('only-default-function-and-imports', rule, {
}
export const x = y;
`,
errors: [
'Mitosis component files should only contain import declarations, the component itself (in a default export), and type declarations',
],
errors: [onlyDefaultFunctionAndImportsMessage],
},
{
...opts,
Expand All @@ -123,9 +135,22 @@ ruleTester.run('only-default-function-and-imports', rule, {
);
}
`,
errors: [
'Mitosis component files should only contain import declarations, the component itself (in a default export), and type declarations',
],
errors: [onlyDefaultFunctionAndImportsMessage],
},
{
...opts,
code: `
useDefault({
test: "XXX"
});
export default function RenderComponent(props) {
return (
<div>{props.test}</div>
);
}
`,
errors: [onlyDefaultFunctionAndImportsMessage],
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import isMitosisPath from '../helpers/isMitosisPath';
// Rule Definition
// ------------------------------------------------------------------------------

export const onlyDefaultFunctionAndImportsMessage =
'Mitosis component files should only contain import declarations, the component itself (in a default export), module-scope hooks, and type declarations';

const rule: Rule.RuleModule = {
meta: {
type: 'problem',
docs: {
description:
'disallow anything other than import declarations, the component itself (in a default export), and type declarations inside the component file.',
'disallow anything other than import declarations, the component itself (in a default export), module-scope hooks, and type declarations inside the component file.',
recommended: true,
},
},
Expand Down Expand Up @@ -56,12 +59,12 @@ const rule: Rule.RuleModule = {
(!types.isExpressionStatement(child) ||
!types.isCallExpression(child.expression) ||
!types.isIdentifier(child.expression.callee) ||
child.expression.callee.name !== HOOKS.META_DATA)
(child.expression.callee.name !== HOOKS.META_DATA &&
child.expression.callee.name !== HOOKS.DEFAULT_PROPS))
) {
context.report({
node: child as any,
message:
'Mitosis component files should only contain import declarations, the component itself (in a default export), and type declarations',
message: onlyDefaultFunctionAndImportsMessage,
});
}
}
Expand Down

0 comments on commit 0f2a9ee

Please sign in to comment.