Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const visitNode = (node: estree.Node) => {
let token: ComplexityToken | undefined | null;
if (isFunctionNode(node)) {
if (node !== this.root) {
return;
} else {
token = { loc: getMainFunctionTokenLocation(node, this.parent, this.context) };
}
} else {
switch (node.type) {
case "ConditionalExpression":
token = this.context
.getSourceCode()
.getFirstTokenBetween(node.test, node.consequent, token => token.value === "?");
break;
case "SwitchCase":
// ignore default case
if (!node.test) {
break;
}
case "IfStatement":
case "ForStatement":
case "ForInStatement":
function checkCastedType(
node: FunctionLikeDeclaration,
expression: TSESTree.Expression,
context: Rule.RuleContext,
) {
const sourceCode = context.getSourceCode();
const castedType = getCastTupleFromMemberExpression(expression);
if (castedType && (castedType[1] as TSESTree.Node).type !== "TSAnyKeyword") {
const nOfParam = node.params.length;
if (nOfParam === 1 || (nOfParam === 0 && castedType[0].type === "ThisExpression")) {
const castedExpressionText = sourceCode.getText(castedType[0]);
const castedTypeText = sourceCode.getText(castedType[1]);
context.report({
message: `Declare this function return type using type predicate "${castedExpressionText} is ${castedTypeText}".`,
loc: getMainFunctionTokenLocation(node as estree.Function, getParent(context), context),
});
}
}
}
checkFunctionForImplicitReturn(functionContext);
if (hasInconsistentReturns(functionContext)) {
const [secondaryLocationsHolder, secondaryLocationMessages] = getSecondaryLocations(
functionContext,
node as estree.Node,
);
const message = toEncodedMessage(
`Refactor this function to use "return" consistently.`,
secondaryLocationsHolder,
secondaryLocationMessages,
);
context.report({
message,
loc: getMainFunctionTokenLocation(node as estree.Function, getParent(context), context),
});
}
}
}
const returnedValues = functionContext.returnStatements.map(
returnStatement => returnStatement.argument as estree.Node,
);
if (areAllSameValue(returnedValues, context.getScope())) {
const message = toEncodedMessage(
`Refactor this function to not always return the same value.`,
returnedValues as TSESTree.Node[],
undefined,
returnedValues.length,
);
context.report({
message,
loc: getMainFunctionTokenLocation(node as estree.Function, getParent(context), context),
});
}
}
[functionLike]: (node: estree.Node) => {
const loopNode = getLocalEnclosingLoop(node) as LoopLike;
if (loopNode) {
if (
!isIIEF(node, context) &&
!isAllowedCallbacks(context) &&
context.getScope().through.some(ref => !isSafe(ref, loopNode))
) {
context.report({
message,
loc: getMainFunctionTokenLocation(
node as estree.Function,
getParent(context),
context,
),
});
}
}
},
};
":not(FunctionDeclaration, FunctionExpression, ArrowFunctionExpression) > BlockStatement > FunctionDeclaration": (
node: estree.Node,
) => {
context.report({
message,
loc: getMainFunctionTokenLocation(
node as estree.FunctionDeclaration,
getParent(context),
context,
),
});
},
};