@arnaud-barre/tsl-config 0.1.2 → 0.1.3
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/index.js +11 -15
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { core, createRulesSet, defineRule } from "tsl";
|
|
2
|
-
import "
|
|
3
|
-
import { SyntaxKind, TypeFlags } from "typescript";
|
|
2
|
+
import { JsxEmit, SyntaxKind, TypeFlags } from "typescript";
|
|
4
3
|
|
|
5
4
|
//#region src/rules/arrayCallbackReturn.ts
|
|
6
5
|
const base = "Found undefined in the return type of this callback. It could come from a missing return statement.";
|
|
@@ -28,8 +27,7 @@ const arrayCallbackReturn = defineRule(() => ({
|
|
|
28
27
|
if (node.name.kind === SyntaxKind.Identifier && methods.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length >= 1) {
|
|
29
28
|
const callback = node.parent.arguments[0];
|
|
30
29
|
if (callback.kind === SyntaxKind.ArrowFunction && callback.body.kind !== SyntaxKind.Block) return;
|
|
31
|
-
const
|
|
32
|
-
const signatures = functionType.getCallSignatures();
|
|
30
|
+
const signatures = context.checker.getTypeAtLocation(callback).getCallSignatures();
|
|
33
31
|
if (signatures.length === 0) return;
|
|
34
32
|
for (const signature of signatures) {
|
|
35
33
|
const type = signature.getReturnType();
|
|
@@ -52,7 +50,9 @@ const message$1 = "Children doesn't match ReactNode type. See https://github.com
|
|
|
52
50
|
const jsxFragmentCheckChildren = defineRule(() => ({
|
|
53
51
|
name: "arnaudBarre/jsxFragmentCheckChildren",
|
|
54
52
|
visitor: { JsxFragment(context, node) {
|
|
55
|
-
|
|
53
|
+
if (context.compilerOptions.jsx !== JsxEmit.ReactJSX) return;
|
|
54
|
+
const reactNodeSymbol = getReactNodeSymbol(context.program);
|
|
55
|
+
const reactNodeType = reactNodeSymbol ? context.checker.getDeclaredTypeOfSymbol(reactNodeSymbol) : void 0;
|
|
56
56
|
if (!reactNodeType) {
|
|
57
57
|
context.report({
|
|
58
58
|
node,
|
|
@@ -72,7 +72,7 @@ const jsxFragmentCheckChildren = defineRule(() => ({
|
|
|
72
72
|
} }
|
|
73
73
|
}));
|
|
74
74
|
const weekMap = /* @__PURE__ */ new WeakMap();
|
|
75
|
-
function
|
|
75
|
+
function getReactNodeSymbol(program) {
|
|
76
76
|
if (weekMap.has(program)) return weekMap.get(program);
|
|
77
77
|
const reactSourceFile = program.getSourceFiles().find((sf) => sf.fileName.includes("@types/react/index.d.ts"));
|
|
78
78
|
if (!reactSourceFile) return;
|
|
@@ -81,12 +81,10 @@ function getReactNodeType(program) {
|
|
|
81
81
|
if (namespace.body?.kind !== SyntaxKind.ModuleBlock) return;
|
|
82
82
|
const statement = namespace.body.statements.find((s) => s.kind === SyntaxKind.TypeAliasDeclaration && s.name.getText() === "ReactNode");
|
|
83
83
|
if (!statement) return;
|
|
84
|
-
const
|
|
85
|
-
const symbol = checker.getSymbolAtLocation(statement.name);
|
|
84
|
+
const symbol = program.getTypeChecker().getSymbolAtLocation(statement.name);
|
|
86
85
|
if (!symbol) return;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return type;
|
|
86
|
+
weekMap.set(program, symbol);
|
|
87
|
+
return symbol;
|
|
90
88
|
}
|
|
91
89
|
|
|
92
90
|
//#endregion
|
|
@@ -114,8 +112,7 @@ const jsxKey = defineRule(() => {
|
|
|
114
112
|
node: attr
|
|
115
113
|
};
|
|
116
114
|
if (attr.kind === SyntaxKind.JsxSpreadAttribute) {
|
|
117
|
-
|
|
118
|
-
if (type.getProperty("key")) context.report({
|
|
115
|
+
if (context.checker.getTypeAtLocation(attr.expression).getProperty("key")) context.report({
|
|
119
116
|
message: messages.keySpread,
|
|
120
117
|
node: attr
|
|
121
118
|
});
|
|
@@ -177,8 +174,7 @@ const jsxKey = defineRule(() => {
|
|
|
177
174
|
const checkIfReactComponentWithKeyInProps = (id, fnNode, context) => {
|
|
178
175
|
if (!reactComponentNameRE.test(id.text)) return;
|
|
179
176
|
if (fnNode.parameters.length === 0) return;
|
|
180
|
-
|
|
181
|
-
if (type.getProperty("key")) context.report({
|
|
177
|
+
if (context.checker.getTypeAtLocation(fnNode.parameters[0]).getProperty("key")) context.report({
|
|
182
178
|
message: messages.keyInProps,
|
|
183
179
|
node: id
|
|
184
180
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arnaud-barre/tsl-config",
|
|
3
3
|
"description": "TS-React focused tsl configuration",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:ArnaudBarre/tsl-config",
|