Skip to content

Commit ca9bc64

Browse files
committed
Vendor eslint-plugin-jsx-expressions
Alternative to waiting for the PR with support for new versions of typescript-eslint: - hluisson/eslint-plugin-jsx-expressions#17
1 parent c7a22a1 commit ca9bc64

17 files changed

+2789
-174
lines changed

eslint.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import upleveled from './index.js';
44
const config = [
55
...upleveled,
66
{
7-
ignores: ['__tests__/**/*'],
7+
ignores: [
8+
// Tests
9+
'__tests__/**/*',
10+
// Vendored packages (workarounds for incompatibilities)
11+
'vendor/**/*',
12+
],
813
},
914
];
1015

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import typescriptParser from '@typescript-eslint/parser';
66
import gitignore from 'eslint-config-flat-gitignore';
77
import eslintImportX from 'eslint-plugin-import-x';
88
import jsxA11y from 'eslint-plugin-jsx-a11y';
9-
import jsxExpressions from 'eslint-plugin-jsx-expressions';
109
import react from 'eslint-plugin-react';
1110
import reactHooks from 'eslint-plugin-react-hooks';
1211
import security from 'eslint-plugin-security';
1312
import sonarjs from 'eslint-plugin-sonarjs';
1413
import unicorn from 'eslint-plugin-unicorn';
1514
import upleveled from 'eslint-plugin-upleveled';
1615
import globals from 'globals';
16+
import jsxExpressions from './vendor/eslint-plugin-jsx-expressions/dist/index.js';
1717

1818
/** @type
1919
* {import('@typescript-eslint/utils/ts-eslint').FlatConfig.RuleLevelAndOptions}

package.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"eslint-import-resolver-typescript": "3.6.1",
4545
"eslint-plugin-import-x": "0.5.0",
4646
"eslint-plugin-jsx-a11y": "6.8.0",
47-
"eslint-plugin-jsx-expressions": "1.3.2",
4847
"eslint-plugin-react": "7.34.1",
4948
"eslint-plugin-react-hooks": "4.6.2",
5049
"eslint-plugin-security": "3.0.0",
@@ -76,12 +75,5 @@
7675
"packageManager": "pnpm@9.1.1+sha256.9551e803dcb7a1839fdf5416153a844060c7bce013218ce823410532504ac10b",
7776
"engines": {
7877
"node": ">=20.9.0"
79-
},
80-
"pnpm": {
81-
"overrides": {
82-
"@typescript-eslint/eslint-plugin": "7.9.1-alpha.6",
83-
"@typescript-eslint/parser": "7.9.1-alpha.6",
84-
"@typescript-eslint/utils": "7.9.1-alpha.6"
85-
}
8678
}
8779
}

pnpm-lock.yaml

Lines changed: 2496 additions & 164 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# eslint-plugin-jsx-expressions
2+
3+
Rules for safe logical expressions in JSX
4+
5+
## Installation
6+
7+
You'll first need to install [ESLint](https://eslint.org/):
8+
9+
```sh
10+
npm i eslint --save-dev
11+
```
12+
13+
Next, install `eslint-plugin-jsx-expressions`:
14+
15+
```sh
16+
npm install eslint-plugin-jsx-expressions --save-dev
17+
```
18+
19+
## Usage
20+
21+
Add `jsx-expressions` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
22+
23+
```json
24+
{
25+
"plugins": ["jsx-expressions"]
26+
}
27+
```
28+
29+
Then configure the rules you want to use under the rules section.
30+
31+
```json
32+
{
33+
"rules": {
34+
"jsx-expressions/strict-logical-expressions": "error"
35+
}
36+
}
37+
```
38+
39+
## Supported Rules
40+
41+
- [jsx-expressions/strict-logical-expressions](https://github.com/hluisson/eslint-plugin-jsx-expressions/blob/master/docs/rules/strict-logical-expressions.md): Enforces logical "&&" expressions do not use potentially falsey numbers or strings.

vendor/eslint-plugin-jsx-expressions/dist/index.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/eslint-plugin-jsx-expressions/dist/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/eslint-plugin-jsx-expressions/dist/rules/index.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/eslint-plugin-jsx-expressions/dist/rules/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/eslint-plugin-jsx-expressions/dist/rules/strict-logical-expressions.js

Lines changed: 126 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/eslint-plugin-jsx-expressions/dist/rules/strict-logical-expressions.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/eslint-plugin-jsx-expressions/dist/util/createRule.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/eslint-plugin-jsx-expressions/dist/util/createRule.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/eslint-plugin-jsx-expressions/dist/util/types.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/eslint-plugin-jsx-expressions/dist/util/types.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Strict JSX expressions for safe conditional renders. (strict-logical-expressions)
2+
3+
Forbids the usage of potentially falsey string or number values in logical && expressions.
4+
Oftentimes, these logical expressions are used in jsx to enable conditional rendering behavior.
5+
6+
However, one can encounter unexpected behavior when a 0 or empty string values is returned as the result of the expression and rendered accidentally.
7+
8+
## Rule Details
9+
10+
Examples of **incorrect** code for this rule:
11+
12+
```js
13+
// Potentially falsey strings are not allowed
14+
let str = '';
15+
<App>{str && <Foo />}</App>;
16+
17+
// Potentially falsey numbers are not allowed
18+
let num = 0;
19+
<App>{num && <Foo />}</App>;
20+
21+
// Includes types that may be a string or number
22+
let thisOrThat: Record<any, any> | string | number;
23+
<App>{thisOrThat && <Foo />}</App>;
24+
```
25+
26+
Examples of **correct** code for this rule:
27+
28+
```js
29+
// Coalescing to boolean is ok
30+
let str = "Foo";
31+
<App>{!!str && <Foo />}</App>;
32+
33+
// Turning into a boolean is ok
34+
let str = "Foo";
35+
<App>{Boolean(str) && <Foo />}</App>;
36+
37+
// Constant values are ok
38+
const str = "Foo";
39+
<App>{str && <Foo />}</App>;
40+
41+
// Constant values are ok
42+
const num = 1;
43+
<App>{num && <Foo />}</App>;
44+
```
45+
46+
### Options
47+
48+
```ts
49+
type Options = [
50+
{
51+
allowString?: boolean;
52+
allowNumber?: boolean;
53+
}
54+
];
55+
56+
defaultOptions: [
57+
{
58+
allowString: false,
59+
allowNumber: false,
60+
},
61+
];
62+
```
63+
64+
## Further Reading
65+
66+
- [React docs on conditional rendering](https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator)
67+
- [Conditional rendering in React Native may crash your app](https://koprowski.it/2020/conditional-rendering-react-native-text-crash/)
68+
- https://github.com/facebook/react/blob/95a313ec0b957f71798a69d8e83408f40e76765b/packages/react-reconciler/src/ReactChildFiber.js#L461
69+
- https://github.com/yannickcr/eslint-plugin-react/issues/2073
70+
- https://github.com/facebook/react-native/issues/20764
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "commonjs"
3+
}

0 commit comments

Comments
 (0)