Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with eslint plugin not allowing useDefaultProps #1696

Merged
merged 4 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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), 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