Skip to content

Commit

Permalink
fix: jsx rule with fragment (QwikDev#2364)
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat authored Dec 4, 2022
1 parent ffda3cd commit e12a414
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
28 changes: 26 additions & 2 deletions packages/eslint-plugin-qwik/qwik.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export default component$(() => {
`,
`
import { component$ } from "@builder.io/qwik";
export interface Props {
serializableTuple: [string, number, boolean];
}
Expand Down Expand Up @@ -445,7 +445,7 @@ export default component$(() => {
{
code: `
import { component$ } from "@builder.io/qwik";
export interface Props {
nonserializableTuple: [string, number, boolean, Function];
}
Expand All @@ -463,4 +463,28 @@ export default component$(() => {
});
});

test('single-jsx-root', () => {
ruleTester.run('my-rule', rules['single-jsx-root'] as any, {
valid: [``],
invalid: [
{
code: `export const HelloWorld = component$(async () => {
return isLoading ? <>Loading...</> : <div>The value is loaded</div>;
});`,
errors: [
'Components in Qwik must have a single JSX root element. Your component has multiple roots on lines: 2, 2. Rewrite your component with a single root such as (`return <>{...}</>`.) and keep all JSX within',
],
},
{
code: `export const HelloWorld = component$(async () => {
return isLoading ? <div>Loading...</div> : <div>The value is loaded</div>
});`,
errors: [
'Components in Qwik must have a single JSX root element. Your component has multiple roots on lines: 2, 2. Rewrite your component with a single root such as (`return <>{...}</>`.) and keep all JSX within',
],
},
],
});
});

export {};
6 changes: 6 additions & 0 deletions packages/eslint-plugin-qwik/src/singleJsxRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export const singleJsxRoot: Rule.RuleModule = {
current.depth--;
}
},
'JSXFragment:exit'() {
const current = stack[stack.length - 1];
if (current) {
current.depth--;
}
},
};
},
};

0 comments on commit e12a414

Please sign in to comment.