forked from BuilderIO/mitosis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint): no map function in jsx return body (BuilderIO#949)
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/eslint-plugin/src/rules/__tests__/no-map-function-in-jsx-return-body.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { RuleTester } from 'eslint'; | ||
import rule from '../no-map-function-in-jsx-return-body'; | ||
|
||
const opts = { | ||
filename: 'component.lite.tsx', | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
} as const; | ||
|
||
var ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('no-map-function-in-jsx-return-body', rule, { | ||
valid: [ | ||
{ | ||
...opts, | ||
code: ` | ||
import { useStore, For, onMount } from '@builder.io/mitosis'; | ||
export default function MyBasicForComponent() { | ||
const state = useStore({ | ||
name: 'Decadef20', | ||
names: ['Steve', 'Decadef20'], | ||
}); | ||
onMount(() => { | ||
console.log('onMount code'); | ||
}); | ||
return ( | ||
<div> | ||
<For each={state.names}> | ||
{(person) => ( | ||
<> | ||
<input | ||
value={state.name} | ||
onChange={(event) => { | ||
state.name = event.target.value + ' and ' + person; | ||
}} | ||
/> | ||
Hello {person}! I can run in Qwik, Web Component, React, Vue, Solid, or Liquid! | ||
</> | ||
)} | ||
</For> | ||
</div> | ||
); | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
...opts, | ||
code: ` | ||
export default function MyComponent() { | ||
return <div> {[].map()} </div> | ||
} | ||
`, | ||
errors: ['No map function in jsx return body. Please use <For /> component instead.'], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/eslint-plugin/src/rules/no-map-function-in-jsx-return-body.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Rule } from 'eslint'; | ||
import * as types from '@babel/types'; | ||
import isMitosisPath from '../helpers/isMitosisPath'; | ||
|
||
const rule: Rule.RuleModule = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'no map function in jsx return body', | ||
recommended: true, | ||
}, | ||
}, | ||
|
||
create(context) { | ||
if (!isMitosisPath(context.getFilename())) return {}; | ||
|
||
return { | ||
JSXExpressionContainer(node) { | ||
if (types.isJSXExpressionContainer(node)) { | ||
if (types.isCallExpression(node.expression)) { | ||
if ( | ||
types.isMemberExpression(node.expression.callee) && | ||
types.isIdentifier(node.expression.callee.property) && | ||
node.expression.callee.property.name === 'map' | ||
) { | ||
context.report({ | ||
node: node as any, | ||
message: | ||
'No map function in jsx return body. Please use <For /> component instead.', | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
export default rule; |