-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins/react-x): add jsx-uses-vars
- Loading branch information
Showing
7 changed files
with
241 additions
and
98 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
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
95 changes: 95 additions & 0 deletions
95
packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-vars.spec.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,95 @@ | ||
import { allValid, ruleTester } from "../../../../../test"; | ||
import rule, { RULE_NAME } from "./jsx-uses-vars"; | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
invalid: [], | ||
valid: [ | ||
...allValid, | ||
{ | ||
code: /* tsx */ ` | ||
function foo() { | ||
var App; | ||
var bar = React.render(<App/>); | ||
return bar; | ||
}; | ||
foo() | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
var App; | ||
React.render(<App/>); | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
var a = 1; | ||
React.render(<img src={a} />); | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
var App; | ||
function f() { | ||
return <App />; | ||
} | ||
f(); | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
var App; | ||
<App.Hello /> | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
class HelloMessage {}; | ||
<HelloMessage /> | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
class HelloMessage { | ||
render() { | ||
var HelloMessage = <div>Hello</div>; | ||
return HelloMessage; | ||
} | ||
}; | ||
<HelloMessage /> | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
function foo() { | ||
var App = { Foo: { Bar: {} } }; | ||
var bar = React.render(<App.Foo.Bar/>); | ||
return bar; | ||
}; | ||
foo() | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
function foo() { | ||
var App = { Foo: { Bar: { Baz: {} } } }; | ||
var bar = React.render(<App.Foo.Bar.Baz/>); | ||
return bar; | ||
}; | ||
foo() | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
var object; | ||
React.render(<object.Tag />); | ||
`, | ||
}, | ||
{ | ||
code: /* tsx */ ` | ||
var object; | ||
React.render(<object.tag />); | ||
`, | ||
}, | ||
], | ||
}); |
46 changes: 46 additions & 0 deletions
46
packages/plugins/eslint-plugin-react-x/src/rules/jsx-uses-vars.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,46 @@ | ||
import { O } from "@eslint-react/tools"; | ||
import type { TSESTree } from "@typescript-eslint/types"; | ||
import { AST_NODE_TYPES } from "@typescript-eslint/types"; | ||
import type { CamelCase } from "string-ts"; | ||
|
||
import { createRule } from "../utils"; | ||
|
||
export const RULE_NAME = "jsx-uses-vars"; | ||
|
||
export type MessageID = CamelCase<typeof RULE_NAME>; | ||
|
||
export default createRule<[], MessageID>({ | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
// eslint-disable-next-line eslint-plugin/require-meta-docs-description | ||
description: "a helper rule to mark variables as used", | ||
}, | ||
messages: { | ||
jsxUsesVars: "", | ||
}, | ||
schema: [], | ||
}, | ||
name: RULE_NAME, | ||
create(context) { | ||
function getName(node: TSESTree.Node): O.Option<string> { | ||
switch (node.type) { | ||
case AST_NODE_TYPES.JSXIdentifier: | ||
return O.some(node.name); | ||
case AST_NODE_TYPES.JSXMemberExpression: | ||
return getName(node.object); | ||
default: | ||
return O.none(); | ||
} | ||
} | ||
return { | ||
JSXOpeningElement(node) { | ||
if (node.name.type === AST_NODE_TYPES.JSXIdentifier && /^[a-z]/u.test(node.name.name)) return; | ||
O.map(getName(node.name), (name) => { | ||
context.sourceCode.markVariableAsUsed(name, node); | ||
}); | ||
}, | ||
}; | ||
}, | ||
defaultOptions: [], | ||
}); |
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
Oops, something went wrong.