eslint-plugin-absolute 0.11.5 → 0.11.7
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/dist/index.js +62 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1731,6 +1731,48 @@ var noExplicitReturnTypes = createRule({
|
|
|
1731
1731
|
return parent.parent;
|
|
1732
1732
|
return node;
|
|
1733
1733
|
};
|
|
1734
|
+
const collectTypeReferenceNames = (root) => {
|
|
1735
|
+
const names = new Set;
|
|
1736
|
+
const visit = (value) => {
|
|
1737
|
+
if (!value || typeof value !== "object") {
|
|
1738
|
+
return;
|
|
1739
|
+
}
|
|
1740
|
+
if (Array.isArray(value)) {
|
|
1741
|
+
value.forEach(visit);
|
|
1742
|
+
return;
|
|
1743
|
+
}
|
|
1744
|
+
const candidate = value;
|
|
1745
|
+
if (typeof candidate.type !== "string") {
|
|
1746
|
+
return;
|
|
1747
|
+
}
|
|
1748
|
+
const astNode = value;
|
|
1749
|
+
if (astNode.type === "TSTypeReference" && astNode.typeName.type === "Identifier") {
|
|
1750
|
+
names.add(astNode.typeName.name);
|
|
1751
|
+
}
|
|
1752
|
+
for (const key of Object.keys(astNode)) {
|
|
1753
|
+
if (key === "parent") {
|
|
1754
|
+
continue;
|
|
1755
|
+
}
|
|
1756
|
+
visit(astNode[key]);
|
|
1757
|
+
}
|
|
1758
|
+
};
|
|
1759
|
+
visit(root);
|
|
1760
|
+
return names;
|
|
1761
|
+
};
|
|
1762
|
+
const hasReturnOnlyTypeParameter = (node) => {
|
|
1763
|
+
const declaredTypeParams = node.typeParameters?.params;
|
|
1764
|
+
if (!declaredTypeParams || declaredTypeParams.length === 0) {
|
|
1765
|
+
return false;
|
|
1766
|
+
}
|
|
1767
|
+
const returnTypeNames = collectTypeReferenceNames(node.returnType);
|
|
1768
|
+
const parameterTypeNames = new Set;
|
|
1769
|
+
for (const parameter of node.params) {
|
|
1770
|
+
for (const name of collectTypeReferenceNames(parameter)) {
|
|
1771
|
+
parameterTypeNames.add(name);
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
return declaredTypeParams.some((typeParam) => returnTypeNames.has(typeParam.name.name) && !parameterTypeNames.has(typeParam.name.name));
|
|
1775
|
+
};
|
|
1734
1776
|
const referencesOwnName = (node) => {
|
|
1735
1777
|
const ownName = getOwnName(node);
|
|
1736
1778
|
if (!ownName)
|
|
@@ -1759,6 +1801,9 @@ var noExplicitReturnTypes = createRule({
|
|
|
1759
1801
|
if (referencesOwnName(node)) {
|
|
1760
1802
|
return;
|
|
1761
1803
|
}
|
|
1804
|
+
if (hasReturnOnlyTypeParameter(node)) {
|
|
1805
|
+
return;
|
|
1806
|
+
}
|
|
1762
1807
|
context.report({
|
|
1763
1808
|
messageId: "noExplicitReturnType",
|
|
1764
1809
|
node: returnType
|
|
@@ -1769,7 +1814,7 @@ var noExplicitReturnTypes = createRule({
|
|
|
1769
1814
|
defaultOptions: [],
|
|
1770
1815
|
meta: {
|
|
1771
1816
|
docs: {
|
|
1772
|
-
description: "Disallow explicit return type annotations on functions, except when
|
|
1817
|
+
description: "Disallow explicit return type annotations on functions, except when the annotation is load-bearing: type predicates for type guards, inline object literal returns (e.g., style objects), recursive functions, or generics whose type parameter appears only in the return type."
|
|
1773
1818
|
},
|
|
1774
1819
|
messages: {
|
|
1775
1820
|
noExplicitReturnType: "Explicit return types are disallowed; rely on TypeScript's inference instead."
|
|
@@ -2026,6 +2071,22 @@ var visitImmediateReferences = (node, onReference) => {
|
|
|
2026
2071
|
case "FunctionExpression":
|
|
2027
2072
|
case "ArrowFunctionExpression":
|
|
2028
2073
|
return;
|
|
2074
|
+
case "CallExpression":
|
|
2075
|
+
case "NewExpression": {
|
|
2076
|
+
const runAtInit = (invoked) => {
|
|
2077
|
+
if (!invoked) {
|
|
2078
|
+
return;
|
|
2079
|
+
}
|
|
2080
|
+
if (invoked.type === "FunctionDeclaration" || invoked.type === "FunctionExpression" || invoked.type === "ArrowFunctionExpression") {
|
|
2081
|
+
visit(invoked.body);
|
|
2082
|
+
return;
|
|
2083
|
+
}
|
|
2084
|
+
visit(invoked);
|
|
2085
|
+
};
|
|
2086
|
+
runAtInit(current.callee);
|
|
2087
|
+
current.arguments.forEach(runAtInit);
|
|
2088
|
+
return;
|
|
2089
|
+
}
|
|
2029
2090
|
case "MemberExpression":
|
|
2030
2091
|
visit(current.object);
|
|
2031
2092
|
if (current.computed) {
|
package/package.json
CHANGED