@heyramzi/lint 2.4.0 → 2.4.1
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/package.json +1 -1
- package/rules/props-interface.js +14 -2
package/package.json
CHANGED
package/rules/props-interface.js
CHANGED
|
@@ -3,11 +3,23 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
|
-
* A union or a conditional type can't be an interface,
|
|
6
|
+
* A union or a conditional type can't be an interface, even nested in a generic like `WithElementRef<A & (B | C)>`, so those stay allowed.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
const NOT_AN_INTERFACE = new Set(["TSUnionType", "TSConditionalType", "TSMappedType"]);
|
|
10
10
|
|
|
11
|
+
function holdsNonInterface(node) {
|
|
12
|
+
if (!node || typeof node.type !== "string") return false;
|
|
13
|
+
if (NOT_AN_INTERFACE.has(node.type)) return true;
|
|
14
|
+
// An object literal's members can hold unions freely; only the alias's own shape matters.
|
|
15
|
+
if (node.type === "TSTypeLiteral") return false;
|
|
16
|
+
return Object.entries(node).some(
|
|
17
|
+
([key, child]) =>
|
|
18
|
+
key !== "parent" &&
|
|
19
|
+
(Array.isArray(child) ? child.some(holdsNonInterface) : holdsNonInterface(child)),
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
11
23
|
export default {
|
|
12
24
|
meta: {
|
|
13
25
|
type: "suggestion",
|
|
@@ -24,7 +36,7 @@ export default {
|
|
|
24
36
|
return {
|
|
25
37
|
TSTypeAliasDeclaration(node) {
|
|
26
38
|
if (node.id.name !== "Props") return;
|
|
27
|
-
if (
|
|
39
|
+
if (holdsNonInterface(node.typeAnnotation)) return;
|
|
28
40
|
const fixable =
|
|
29
41
|
node.typeAnnotation.type === "TSTypeLiteral" && !node.typeParameters && !node.declare;
|
|
30
42
|
context.report({
|