How to use the eslint-plugin-sonarjs/lib/utils/locations.getMainFunctionTokenLocation function in eslint-plugin-sonarjs

To help you get started, we’ve selected a few eslint-plugin-sonarjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SonarSource / SonarJS / eslint-bridge / src / rules / cyclomatic-complexity.ts View on Github external
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":
github SonarSource / SonarJS / eslint-bridge / src / rules / prefer-type-guard.ts View on Github external
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),
      });
    }
  }
}
github SonarSource / SonarJS / eslint-bridge / src / rules / no-inconsistent-returns.ts View on Github external
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),
        });
      }
    }
github SonarSource / SonarJS / eslint-bridge / src / rules / no-invariant-returns.ts View on Github external
}

      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),
        });
      }
    }
github SonarSource / SonarJS / eslint-bridge / src / rules / function-inside-loop.ts View on Github external
[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,
              ),
            });
          }
        }
      },
    };
github SonarSource / SonarJS / eslint-bridge / src / rules / no-function-declaration-in-block.ts View on Github external
":not(FunctionDeclaration, FunctionExpression, ArrowFunctionExpression) > BlockStatement > FunctionDeclaration": (
        node: estree.Node,
      ) => {
        context.report({
          message,
          loc: getMainFunctionTokenLocation(
            node as estree.FunctionDeclaration,
            getParent(context),
            context,
          ),
        });
      },
    };