tsl-react 0.0.0 → 0.0.1-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Rel1cx
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,15 +1,69 @@
1
1
  # tsl-react
2
2
 
3
- To install dependencies:
3
+ [![Version](https://img.shields.io/npm/v/tsl-react?style=flat&colorA=000000&colorB=000000)](https://npmjs.com/package/tsl-react)
4
+
5
+ (WIP) Bring the same linting functionality that [`eslint-react.xyz`](https://eslint-react.xyz) has to the TypeScript LSP.
6
+
7
+ This package provides the rulesets from [`beta.eslint-react.xyz/docs/rules/overview`](https://beta.eslint-react.xyz/docs/rules/overview) as custom rules for the [ArnaudBarre/tsl](https://github.com/ArnaudBarre/tsl) linting tool.
8
+
9
+ ## Installation
4
10
 
5
11
  ```bash
6
- bun install
12
+ pnpm add -D tsl tsl-react
7
13
  ```
8
14
 
9
- To run:
15
+ Then follow the [installation guide](https://github.com/ArnaudBarre/tsl?tab=readme-ov-file#installation) for tsl.
10
16
 
11
- ```bash
12
- bun run index.ts
17
+ ## Enabling rules
18
+
19
+ ```diff
20
+ // tsl.config.ts
21
+ import { core, defineConfig } from "tsl";
22
+ + import { rules as react } from "tsl-react";
23
+
24
+ export default defineConfig({
25
+ rules: [
26
+ ...core.all(),
27
+ + react.noLeakedConditionalRendering(),
28
+ ],
29
+ });
30
+ ```
31
+
32
+ ## Specifying project-aware React configuration
33
+
34
+ In your `tsconfig.json` or `jsconfig.json` add the following:
35
+
36
+ ```diff
37
+ {
38
+ "compilerOptions": {
39
+ + "jsx": "react-jsx",
40
+ "plugins": [{ "name": "tsl/plugin" }],
41
+ },
42
+ + "react": {
43
+ + "version": "19.1.0" // or "detect" to automatically detect the version
44
+ + // other options can be added here
45
+ + }
46
+ }
13
47
  ```
14
48
 
15
- This project was created using `bun init` in bun v1.2.17. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
49
+ ## Status
50
+
51
+ This package is currently a work in progress. The rules are being developed and tested, and we welcome contributions from the community.
52
+
53
+ ### Implemented Rules
54
+
55
+ - [x] `noLeakedConditionalRendering`: Prevents problematic leaked values from being rendered.
56
+ - [ ] ...
57
+
58
+ ## License
59
+
60
+ This project is licensed under the [MIT License](./LICENSE).
61
+
62
+ ## Acknowledgements
63
+
64
+ We extend our gratitude to:
65
+
66
+ - **[ArnaudBarre/tsl](https://github.com/ArnaudBarre/tsl)** for the core and AST type rewrite, which significantly streamlined custom rules development within TypeScript Language Service Plugin.
67
+ - **[johnsoncodehk/tsslint](https://github.com/johnsoncodehk/tsslint)** for their early explorations of exposing the TypeScript Language Server diagnostic interface.
68
+ - **[typescript-eslint/typescript-eslint](https://github.com/typescript-eslint/typescript-eslint)** for providing the foundation where these custom rules were initially developed and tested.
69
+ - **[Effect-TS/language-service](https://github.com/Effect-TS/language-service)** for inspiring the creation of [`typescriptreact-language-service`](https://github.com/react-analyzer/tsl/commit/01ab1d8d954d555bff65246c61af8c1028be78f1#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5) (now [`tsl-react`](https://github.com/react-analyzer/tsl)).
@@ -0,0 +1,58 @@
1
+ import * as tsl from 'tsl';
2
+
3
+ declare const rules: tsl.RulesSet<{
4
+ /**
5
+ * Prevents problematic leaked values from being rendered.
6
+ *
7
+ * Using the && operator to render some element conditionally in JSX can cause unexpected values being rendered, or even crashing the rendering.
8
+ *
9
+ * **Examples**
10
+ *
11
+ * ```tsx
12
+ * import React from "react";
13
+ *
14
+ * interface MyComponentProps {
15
+ * count: number;
16
+ * }
17
+ *
18
+ * function MyComponent({ count }: MyComponentProps) {
19
+ * return <div>{count && <span>There are {count} results</span>}</div>;
20
+ * // ^^^^^
21
+ * // - Potential leaked value 'count' that might cause unintentionally rendered values or rendering crashes.
22
+ * }
23
+ * ```
24
+ *
25
+ * ```tsx
26
+ * import React from "react";
27
+ *
28
+ * interface MyComponentProps {
29
+ * items: string[];
30
+ * }
31
+ *
32
+ * function MyComponent({ items }: MyComponentProps) {
33
+ * return <div>{items.length && <List items={items} />}</div>;
34
+ * // ^^^^^^^^^^^^
35
+ * // - Potential leaked value 'items.length' that might cause unintentionally rendered values or rendering crashes.
36
+ * }
37
+ * ```
38
+ *
39
+ * ```tsx
40
+ * import React from "react";
41
+ *
42
+ * interface MyComponentProps {
43
+ * items: string[];
44
+ * }
45
+ *
46
+ * function MyComponent({ items }: MyComponentProps) {
47
+ * return <div>{items[0] && <List items={items} />}</div>;
48
+ * // ^^^^^^^^
49
+ * // - Potential leaked value 'items[0]' that might cause unintentionally rendered values or rendering crashes.
50
+ * }
51
+ * ```
52
+ *
53
+ * @since 0.0.0
54
+ */
55
+ noLeakedConditionalRendering: (options?: "off" | undefined) => tsl.Rule<unknown>;
56
+ }>;
57
+
58
+ export { rules };
package/dist/index.js ADDED
@@ -0,0 +1,123 @@
1
+ import { defineRule, createRulesSet } from 'tsl';
2
+ import { compare } from 'compare-versions';
3
+ import { SyntaxKind } from 'typescript';
4
+ import { isLogicalNegationExpression } from '@react-analyzer/ast';
5
+ import { unit } from '@react-analyzer/eff';
6
+ import { Report } from '@react-analyzer/kit';
7
+ import { getAnalyzerOptions } from '@react-analyzer/shared';
8
+ import * as RA from '@react-analyzer/core';
9
+
10
+ // src/index.ts
11
+ var messages = {
12
+ noLeakedConditionalRendering: (p) => `Potential leaked value ${p.value} that might cause unintentionally rendered values or rendering crashes.`
13
+ };
14
+ var noLeakedConditionalRendering = defineRule(() => {
15
+ return {
16
+ name: "@react-analyzer/noLeakedConditionalRendering",
17
+ createData(ctx) {
18
+ const { version } = getAnalyzerOptions(ctx);
19
+ const state = {
20
+ isWithinJsxExpression: false
21
+ };
22
+ const allowedVariants = [
23
+ "any",
24
+ "boolean",
25
+ "nullish",
26
+ "object",
27
+ "falsy boolean",
28
+ "truthy bigint",
29
+ "truthy boolean",
30
+ "truthy number",
31
+ "truthy string",
32
+ ...compare(version, "18.0.0", "<") ? [] : ["string", "falsy string"]
33
+ ];
34
+ function getReportDescriptor(node) {
35
+ if (isLogicalNegationExpression(node.left)) return unit;
36
+ const leftType = ctx.utils.getConstrainedTypeAtLocation(node.left);
37
+ const leftTypeVariants = RA.getVariantsOfTypes(ctx.utils.unionConstituents(leftType));
38
+ const areAllLeftTypeVariantsAllowed = Array.from(leftTypeVariants.values()).every((type) => allowedVariants.some((allowed) => allowed === type));
39
+ if (!areAllLeftTypeVariantsAllowed) {
40
+ return {
41
+ node: node.left,
42
+ message: messages.noLeakedConditionalRendering({ value: node.left.getText() })
43
+ };
44
+ }
45
+ return unit;
46
+ }
47
+ return { state, version, allowedVariants, getReportDescriptor };
48
+ },
49
+ visitor: {
50
+ JsxExpression(ctx) {
51
+ ctx.data.state.isWithinJsxExpression = true;
52
+ },
53
+ JsxExpression_exit(ctx) {
54
+ ctx.data.state.isWithinJsxExpression = false;
55
+ },
56
+ BinaryExpression(ctx, node) {
57
+ const { state, getReportDescriptor } = ctx.data;
58
+ if (!state.isWithinJsxExpression) return;
59
+ if (node.operatorToken.kind !== SyntaxKind.AmpersandAmpersandToken) return;
60
+ Report.report(ctx, getReportDescriptor(node));
61
+ }
62
+ }
63
+ };
64
+ });
65
+
66
+ // src/index.ts
67
+ var rules = createRulesSet({
68
+ /**
69
+ * Prevents problematic leaked values from being rendered.
70
+ *
71
+ * Using the && operator to render some element conditionally in JSX can cause unexpected values being rendered, or even crashing the rendering.
72
+ *
73
+ * **Examples**
74
+ *
75
+ * ```tsx
76
+ * import React from "react";
77
+ *
78
+ * interface MyComponentProps {
79
+ * count: number;
80
+ * }
81
+ *
82
+ * function MyComponent({ count }: MyComponentProps) {
83
+ * return <div>{count && <span>There are {count} results</span>}</div>;
84
+ * // ^^^^^
85
+ * // - Potential leaked value 'count' that might cause unintentionally rendered values or rendering crashes.
86
+ * }
87
+ * ```
88
+ *
89
+ * ```tsx
90
+ * import React from "react";
91
+ *
92
+ * interface MyComponentProps {
93
+ * items: string[];
94
+ * }
95
+ *
96
+ * function MyComponent({ items }: MyComponentProps) {
97
+ * return <div>{items.length && <List items={items} />}</div>;
98
+ * // ^^^^^^^^^^^^
99
+ * // - Potential leaked value 'items.length' that might cause unintentionally rendered values or rendering crashes.
100
+ * }
101
+ * ```
102
+ *
103
+ * ```tsx
104
+ * import React from "react";
105
+ *
106
+ * interface MyComponentProps {
107
+ * items: string[];
108
+ * }
109
+ *
110
+ * function MyComponent({ items }: MyComponentProps) {
111
+ * return <div>{items[0] && <List items={items} />}</div>;
112
+ * // ^^^^^^^^
113
+ * // - Potential leaked value 'items[0]' that might cause unintentionally rendered values or rendering crashes.
114
+ * }
115
+ * ```
116
+ *
117
+ * @since 0.0.0
118
+ */
119
+ noLeakedConditionalRendering
120
+ // TODO: Port more rules from https://beta.eslint-react.xyz/docs/rules/overview
121
+ });
122
+
123
+ export { rules };
package/package.json CHANGED
@@ -1,12 +1,57 @@
1
1
  {
2
2
  "name": "tsl-react",
3
- "version": "0.0.0",
4
- "module": "index.ts",
3
+ "version": "0.0.1-next.0",
4
+ "description": "A unified plugin that combines all individual plugins from the react-analyzer monorepo into one.",
5
+ "keywords": [
6
+ "react",
7
+ "tsl",
8
+ "tsl-react",
9
+ "tsl-react-x",
10
+ "tsl-react-dom",
11
+ "tsl-react-web-api",
12
+ "tsl-react-naming-convention"
13
+ ],
14
+ "license": "MIT",
15
+ "author": "Rel1cx<dokimondex@gmail.com>",
16
+ "sideEffects": false,
5
17
  "type": "module",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ },
23
+ "./package.json": "./package.json"
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "./package.json"
28
+ ],
29
+ "dependencies": {
30
+ "compare-versions": "^6.1.1",
31
+ "@react-analyzer/ast": "0.0.1-next.0",
32
+ "@react-analyzer/core": "0.0.1-next.0",
33
+ "@react-analyzer/shared": "0.0.1-next.0",
34
+ "@react-analyzer/eff": "0.0.1-next.0",
35
+ "@react-analyzer/kit": "0.0.1-next.0"
36
+ },
6
37
  "devDependencies": {
7
- "@types/bun": "latest"
38
+ "tsup": "^8.5.0",
39
+ "@local/configs": "0.0.0"
8
40
  },
9
41
  "peerDependencies": {
10
- "typescript": "^5"
42
+ "tsl": "^1.0.18",
43
+ "typescript": "^4.9.5 || ^5.4.5"
44
+ },
45
+ "engines": {
46
+ "bun": ">=1.2.18",
47
+ "node": ">=22.17.0"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public"
51
+ },
52
+ "scripts": {
53
+ "build": "tsup --dts-resolve",
54
+ "lint:publish": "pnpm publint",
55
+ "lint:ts": "tsl"
11
56
  }
12
- }
57
+ }
package/bun.lock DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "lockfileVersion": 1,
3
- "workspaces": {
4
- "": {
5
- "name": "tsl-react",
6
- "devDependencies": {
7
- "@types/bun": "latest",
8
- },
9
- "peerDependencies": {
10
- "typescript": "^5",
11
- },
12
- },
13
- },
14
- "packages": {
15
- "@types/bun": ["@types/bun@1.2.17", "", { "dependencies": { "bun-types": "1.2.17" } }, "sha512-l/BYs/JYt+cXA/0+wUhulYJB6a6p//GTPiJ7nV+QHa8iiId4HZmnu/3J/SowP5g0rTiERY2kfGKXEK5Ehltx4Q=="],
16
-
17
- "@types/node": ["@types/node@24.0.10", "", { "dependencies": { "undici-types": "~7.8.0" } }, "sha512-ENHwaH+JIRTDIEEbDK6QSQntAYGtbvdDXnMXnZaZ6k13Du1dPMmprkEHIL7ok2Wl2aZevetwTAb5S+7yIF+enA=="],
18
-
19
- "bun-types": ["bun-types@1.2.17", "", { "dependencies": { "@types/node": "*" } }, "sha512-ElC7ItwT3SCQwYZDYoAH+q6KT4Fxjl8DtZ6qDulUFBmXA8YB4xo+l54J9ZJN+k2pphfn9vk7kfubeSd5QfTVJQ=="],
20
-
21
- "typescript": ["typescript@5.8.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ=="],
22
-
23
- "undici-types": ["undici-types@7.8.0", "", {}, "sha512-9UJ2xGDvQ43tYyVMpuHlsgApydB8ZKfVYTsLDhXkFL/6gfkp+U8xTGdh8pMJv1SpZna0zxG1DwsKZsreLbXBxw=="],
24
- }
25
- }
package/index.ts DELETED
@@ -1 +0,0 @@
1
- console.log("Hello via Bun!");
package/tsconfig.json DELETED
@@ -1,29 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- // Environment setup & latest features
4
- "lib": ["ESNext"],
5
- "target": "ESNext",
6
- "module": "Preserve",
7
- "moduleDetection": "force",
8
- "jsx": "react-jsx",
9
- "allowJs": true,
10
-
11
- // Bundler mode
12
- "moduleResolution": "bundler",
13
- "allowImportingTsExtensions": true,
14
- "verbatimModuleSyntax": true,
15
- "noEmit": true,
16
-
17
- // Best practices
18
- "strict": true,
19
- "skipLibCheck": true,
20
- "noFallthroughCasesInSwitch": true,
21
- "noUncheckedIndexedAccess": true,
22
- "noImplicitOverride": true,
23
-
24
- // Some stricter flags (disabled by default)
25
- "noUnusedLocals": false,
26
- "noUnusedParameters": false,
27
- "noPropertyAccessFromIndexSignature": false
28
- }
29
- }