How to use the eslint-plugin-sonarjs/lib/utils/nodes.getParent 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 / no-redundant-parentheses.ts View on Github external
function checkRedundantParentheses(
  sourceCode: SourceCode,
  node: estree.Node,
  context: Rule.RuleContext,
) {
  const parenthesesPairsAroundNode = getParenthesesPairsAround(sourceCode, node, node);
  const parent = getParent(context);

  // Ignore parentheses pair from the parent node
  if (!!parent && isInParentNodeParentheses(node, parent)) {
    parenthesesPairsAroundNode.pop();
  }
  // One pair of parentheses is allowed for readability purposes
  parenthesesPairsAroundNode.shift();

  parenthesesPairsAroundNode.forEach(parentheses => {
    context.report({
      message: toEncodedMessage(`Remove these useless parentheses.`, [
        parentheses.closingParenthesis,
      ]),
      loc: parentheses.openingParenthesis.loc,
    });
  });
github SonarSource / SonarJS / eslint-bridge / src / rules / deprecation.ts View on Github external
function getSymbol(
  id: estree.Identifier,
  services: RequiredParserServices,
  context: Rule.RuleContext,
  tc: tsTypes.TypeChecker,
) {
  let symbol: tsTypes.Symbol | undefined;
  const tsId = services.esTreeNodeToTSNodeMap.get(id as TSESTree.Node) as tsTypes.Identifier;
  const parent = services.esTreeNodeToTSNodeMap.get(getParent(
    context,
  ) as TSESTree.Node) as tsTypes.Node;
  if (parent.kind === ts.SyntaxKind.BindingElement) {
    symbol = tc.getTypeAtLocation(parent.parent).getProperty(tsId.text);
  } else if (
    (isPropertyAssignment(parent) && parent.name === tsId) ||
    (isShorthandPropertyAssignment(parent) && parent.name === tsId)
  ) {
    try {
      symbol = tc.getPropertySymbolOfDestructuringAssignment(tsId);
    } catch (e) {
      // do nothing, we are in object literal, not destructuring
      // no obvious easy way to check that in advance
    }
  } else {
    symbol = tc.getSymbolAtLocation(tsId);
github SonarSource / SonarJS / eslint-bridge / src / rules / function-inside-loop.ts View on Github external
function isAllowedCallbacks(context: Rule.RuleContext) {
  const parent = getParent(context);
  if (parent && parent.type === "CallExpression") {
    const callee = parent.callee;
    if (callee.type === "MemberExpression") {
      return (
        callee.property.type === "Identifier" && allowedCallbacks.includes(callee.property.name)
      );
    }
  }
  return false;
}
github SonarSource / SonarJS / eslint-bridge / src / rules / no-array-delete.ts View on Github external
function raiseIssue(context: Rule.RuleContext): void {
  const deleteKeyword = context.getSourceCode().getFirstToken(getParent(context)!);
  context.report({
    message: `Remove this use of "delete".`,
    loc: deleteKeyword!.loc,
  });
}
github SonarSource / SonarJS / eslint-bridge / src / rules / elseif-without-else.ts View on Github external
function isElseIf(node: estree.IfStatement, context: Rule.RuleContext) {
  const parent = getParent(context);
  return parent && parent.type === "IfStatement" && parent.alternate === node;
}
github SonarSource / SonarJS / eslint-bridge / src / rules / no-ignored-return.ts View on Github external
CallExpression: (node: estree.Node) => {
          const call = node as estree.CallExpression;
          const callee = call.callee;
          if (callee.type === "MemberExpression") {
            const parent = getParent(context);
            if (parent && parent.type === "ExpressionStatement") {
              const methodName = context.getSourceCode().getText(callee.property);
              const objectType = services.program
                .getTypeChecker()
                .getTypeAtLocation(
                  services.esTreeNodeToTSNodeMap.get(callee.object as TSESTree.Node),
                );
              if (
                !hasSideEffect(methodName, objectType, services) &&
                !isReplaceWithCallback(methodName, call.arguments)
              ) {
                context.report({
                  message: message(methodName),
                  node,
                });
              }