@arnaud-barre/tsl-config 0.1.2 → 0.1.4
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/README.md +2 -0
- package/index.d.ts +2 -0
- package/index.js +46 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -135,6 +135,8 @@ export default defineConfig({
|
|
|
135
135
|
### Custom rules
|
|
136
136
|
|
|
137
137
|
- arrayCallbackReturn: Ensure array callbacks that require a return value (map, filter, find, some, ...) don't have undefined in their return type
|
|
138
|
+
- arrayNoEmptyVariadic: Disallow empty variadic arguments to array methods (push, unshift, concat)
|
|
138
139
|
- jsxFragmentCheckChildren: Validates that JSX shorthand fragment children match ReactNode type to workaround https://github.com/microsoft/TypeScript/issues/62358
|
|
139
140
|
- jsxKey: Validate JSX has key prop when in array or iterator
|
|
140
141
|
- jsxNoNumberTruthiness: Disallow `list.length && ...` in JSX
|
|
142
|
+
- typedEmptyArray: Ensure empty arrays declarations have an explicit type annotation
|
package/index.d.ts
CHANGED
|
@@ -3,9 +3,11 @@ import * as tsl0 from "tsl";
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
4
|
declare const arnaudBarre: tsl0.RulesSet<{
|
|
5
5
|
arrayCallbackReturn: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
6
|
+
arrayNoEmptyVariadic: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
6
7
|
jsxFragmentCheckChildren: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
7
8
|
jsxKey: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
8
9
|
jsxNoNumberTruthiness: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
10
|
+
typedEmptyArray: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
9
11
|
}>;
|
|
10
12
|
declare const allRules: tsl0.Rule<unknown>[];
|
|
11
13
|
//#endregion
|
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.";
|
|
@@ -21,15 +20,14 @@ const methodsRecords = {
|
|
|
21
20
|
reduce: "addTypeAnnotation",
|
|
22
21
|
reduceRight: "addTypeAnnotation"
|
|
23
22
|
};
|
|
24
|
-
const methods = Object.keys(methodsRecords);
|
|
23
|
+
const methods$1 = Object.keys(methodsRecords);
|
|
25
24
|
const arrayCallbackReturn = defineRule(() => ({
|
|
26
25
|
name: "arnaudBarre/arrayCallbackReturn",
|
|
27
26
|
visitor: { PropertyAccessExpression(context, node) {
|
|
28
|
-
if (node.name.kind === SyntaxKind.Identifier && methods.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length >= 1) {
|
|
27
|
+
if (node.name.kind === SyntaxKind.Identifier && methods$1.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();
|
|
@@ -46,13 +44,33 @@ const arrayCallbackReturn = defineRule(() => ({
|
|
|
46
44
|
} }
|
|
47
45
|
}));
|
|
48
46
|
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/rules/arrayNoEmptyVariadic.ts
|
|
49
|
+
const methods = [
|
|
50
|
+
"push",
|
|
51
|
+
"unshift",
|
|
52
|
+
"concat"
|
|
53
|
+
];
|
|
54
|
+
const arrayNoEmptyVariadic = defineRule(() => ({
|
|
55
|
+
name: "arnaudBarre/arrayNoEmptyVariadic",
|
|
56
|
+
visitor: { PropertyAccessExpression(context, node) {
|
|
57
|
+
if (node.name.kind === SyntaxKind.Identifier && methods.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length === 0) context.report({
|
|
58
|
+
start: node.name.getStart(),
|
|
59
|
+
end: node.parent.getEnd(),
|
|
60
|
+
message: "This call does nothing"
|
|
61
|
+
});
|
|
62
|
+
} }
|
|
63
|
+
}));
|
|
64
|
+
|
|
49
65
|
//#endregion
|
|
50
66
|
//#region src/rules/jsxFragmentCheckChildren.ts
|
|
51
67
|
const message$1 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
|
|
52
68
|
const jsxFragmentCheckChildren = defineRule(() => ({
|
|
53
69
|
name: "arnaudBarre/jsxFragmentCheckChildren",
|
|
54
70
|
visitor: { JsxFragment(context, node) {
|
|
55
|
-
|
|
71
|
+
if (context.compilerOptions.jsx !== JsxEmit.ReactJSX) return;
|
|
72
|
+
const reactNodeSymbol = getReactNodeSymbol(context.program);
|
|
73
|
+
const reactNodeType = reactNodeSymbol ? context.checker.getDeclaredTypeOfSymbol(reactNodeSymbol) : void 0;
|
|
56
74
|
if (!reactNodeType) {
|
|
57
75
|
context.report({
|
|
58
76
|
node,
|
|
@@ -72,7 +90,7 @@ const jsxFragmentCheckChildren = defineRule(() => ({
|
|
|
72
90
|
} }
|
|
73
91
|
}));
|
|
74
92
|
const weekMap = /* @__PURE__ */ new WeakMap();
|
|
75
|
-
function
|
|
93
|
+
function getReactNodeSymbol(program) {
|
|
76
94
|
if (weekMap.has(program)) return weekMap.get(program);
|
|
77
95
|
const reactSourceFile = program.getSourceFiles().find((sf) => sf.fileName.includes("@types/react/index.d.ts"));
|
|
78
96
|
if (!reactSourceFile) return;
|
|
@@ -81,12 +99,10 @@ function getReactNodeType(program) {
|
|
|
81
99
|
if (namespace.body?.kind !== SyntaxKind.ModuleBlock) return;
|
|
82
100
|
const statement = namespace.body.statements.find((s) => s.kind === SyntaxKind.TypeAliasDeclaration && s.name.getText() === "ReactNode");
|
|
83
101
|
if (!statement) return;
|
|
84
|
-
const
|
|
85
|
-
const symbol = checker.getSymbolAtLocation(statement.name);
|
|
102
|
+
const symbol = program.getTypeChecker().getSymbolAtLocation(statement.name);
|
|
86
103
|
if (!symbol) return;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return type;
|
|
104
|
+
weekMap.set(program, symbol);
|
|
105
|
+
return symbol;
|
|
90
106
|
}
|
|
91
107
|
|
|
92
108
|
//#endregion
|
|
@@ -114,8 +130,7 @@ const jsxKey = defineRule(() => {
|
|
|
114
130
|
node: attr
|
|
115
131
|
};
|
|
116
132
|
if (attr.kind === SyntaxKind.JsxSpreadAttribute) {
|
|
117
|
-
|
|
118
|
-
if (type.getProperty("key")) context.report({
|
|
133
|
+
if (context.checker.getTypeAtLocation(attr.expression).getProperty("key")) context.report({
|
|
119
134
|
message: messages.keySpread,
|
|
120
135
|
node: attr
|
|
121
136
|
});
|
|
@@ -177,8 +192,7 @@ const jsxKey = defineRule(() => {
|
|
|
177
192
|
const checkIfReactComponentWithKeyInProps = (id, fnNode, context) => {
|
|
178
193
|
if (!reactComponentNameRE.test(id.text)) return;
|
|
179
194
|
if (fnNode.parameters.length === 0) return;
|
|
180
|
-
|
|
181
|
-
if (type.getProperty("key")) context.report({
|
|
195
|
+
if (context.checker.getTypeAtLocation(fnNode.parameters[0]).getProperty("key")) context.report({
|
|
182
196
|
message: messages.keyInProps,
|
|
183
197
|
node: id
|
|
184
198
|
});
|
|
@@ -232,13 +246,27 @@ const jsxNoNumberTruthiness = defineRule(() => ({
|
|
|
232
246
|
} }
|
|
233
247
|
}));
|
|
234
248
|
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/rules/typedEmptyArray.ts
|
|
251
|
+
const typedEmptyArray = defineRule(() => ({
|
|
252
|
+
name: "arnaudBarre/typedEmptyArray",
|
|
253
|
+
visitor: { VariableStatement(context, node) {
|
|
254
|
+
for (const declaration of node.declarationList.declarations) if (declaration.initializer && declaration.initializer.kind === SyntaxKind.ArrayLiteralExpression && declaration.initializer.elements.length === 0 && !declaration.type) context.report({
|
|
255
|
+
node: declaration.name,
|
|
256
|
+
message: "Empty array needs explicit type annotation"
|
|
257
|
+
});
|
|
258
|
+
} }
|
|
259
|
+
}));
|
|
260
|
+
|
|
235
261
|
//#endregion
|
|
236
262
|
//#region src/index.ts
|
|
237
263
|
const arnaudBarre = createRulesSet({
|
|
238
264
|
arrayCallbackReturn,
|
|
265
|
+
arrayNoEmptyVariadic,
|
|
239
266
|
jsxFragmentCheckChildren,
|
|
240
267
|
jsxKey,
|
|
241
|
-
jsxNoNumberTruthiness
|
|
268
|
+
jsxNoNumberTruthiness,
|
|
269
|
+
typedEmptyArray
|
|
242
270
|
});
|
|
243
271
|
const allRules = [
|
|
244
272
|
...core.all(),
|
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.4",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:ArnaudBarre/tsl-config",
|