How to use the @typescript-eslint/experimental-utils.AST_NODE_TYPES.OptionalCallExpression function in @typescript-eslint/experimental-utils

To help you get started, we’ve selected a few @typescript-eslint/experimental-utils 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 typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / no-non-null-assertion.ts View on Github external
// it is x!?.[y].z
                suggest.push({
                  messageId: 'suggestOptionalChain',
                  fix: removeToken(),
                });
              } else {
                // it is x!?.y.z
                suggest.push({
                  messageId: 'suggestOptionalChain',
                  fix: removeToken(),
                });
              }
            }
          } else if (
            (node.parent.type === AST_NODE_TYPES.CallExpression ||
              node.parent.type === AST_NODE_TYPES.OptionalCallExpression) &&
            node.parent.callee === node
          ) {
            if (!node.parent.optional) {
              // it is x.y?.z!()
              suggest.push({
                messageId: 'suggestOptionalChain',
                fix: convertTokenToOptional('?.'),
              });
            } else {
              // it is x.y.z!?.()
              suggest.push({
                messageId: 'suggestOptionalChain',
                fix: removeToken(),
              });
            }
          }
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / explicit-function-return-type.ts View on Github external
function isFunctionArgument(
      parent: TSESTree.Node,
      callee?: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
    ): boolean {
      return (
        (parent.type === AST_NODE_TYPES.CallExpression ||
          parent.type === AST_NODE_TYPES.OptionalCallExpression) &&
        // make sure this isn't an IIFE
        parent.callee !== callee
      );
    }
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / util / astUtils.ts View on Github external
function isOptionalOptionalChain(
  node: TSESTree.Node,
): node is TSESTree.OptionalCallExpression {
  return (
    node.type === AST_NODE_TYPES.OptionalCallExpression &&
    // this flag means the call expression itself is option
    // i.e. it is foo.bar?.() and not foo?.bar()
    node.optional
  );
}
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-optional-chain.ts View on Github external
isValidChainTarget(node.object, true);
    const isPropertyValid = node.computed
      ? ALLOWED_COMPUTED_PROP_TYPES.has(node.property.type) &&
        // make sure to validate the member expression is of our expected structure
        (node.property.type === AST_NODE_TYPES.MemberExpression ||
        node.property.type === AST_NODE_TYPES.OptionalMemberExpression
          ? isValidChainTarget(node.property, allowIdentifier)
          : true)
      : ALLOWED_NON_COMPUTED_PROP_TYPES.has(node.property.type);

    return isObjectValid && isPropertyValid;
  }

  if (
    node.type === AST_NODE_TYPES.CallExpression ||
    node.type === AST_NODE_TYPES.OptionalCallExpression
  ) {
    return isValidChainTarget(node.callee, allowIdentifier);
  }

  if (
    allowIdentifier &&
    (node.type === AST_NODE_TYPES.Identifier ||
      node.type === AST_NODE_TYPES.ThisExpression)
  ) {
    return true;
  }

  /*
  special case for the following, where we only want the left
  - foo !== null
  - foo != null
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / no-inferrable-types.ts View on Github external
function isFunctionCall(
      init: TSESTree.Expression,
      callName: string,
    ): boolean {
      return (
        (init.type === AST_NODE_TYPES.CallExpression ||
          init.type === AST_NODE_TYPES.OptionalCallExpression) &&
        init.callee.type === AST_NODE_TYPES.Identifier &&
        init.callee.name === callName
      );
    }
    function isLiteral(init: TSESTree.Expression, typeName: string): boolean {
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / no-var-requires.ts View on Github external
'CallExpression, OptionalCallExpression'(
        node: TSESTree.CallExpression | TSESTree.OptionalCallExpression,
      ): void {
        if (
          node.callee.type === AST_NODE_TYPES.Identifier &&
          node.callee.name === 'require' &&
          node.parent &&
          (node.parent.type === AST_NODE_TYPES.VariableDeclarator ||
            node.parent.type === AST_NODE_TYPES.CallExpression ||
            node.parent.type === AST_NODE_TYPES.OptionalCallExpression)
        ) {
          context.report({
            node,
            messageId: 'noVarReqs',
          });
        }
      },
    };
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-includes.ts View on Github external
argNode.type !== AST_NODE_TYPES.TemplateLiteral &&
              argNode.type !== AST_NODE_TYPES.Identifier &&
              argNode.type !== AST_NODE_TYPES.MemberExpression &&
              argNode.type !== AST_NODE_TYPES.OptionalMemberExpression &&
              argNode.type !== AST_NODE_TYPES.CallExpression &&
              argNode.type !== AST_NODE_TYPES.OptionalCallExpression;

            yield fixer.removeRange([callNode.range[0], argNode.range[0]]);
            if (needsParen) {
              yield fixer.insertTextBefore(argNode, '(');
              yield fixer.insertTextAfter(argNode, ')');
            }
            yield fixer.insertTextAfter(
              argNode,
              `${
                callNode.type === AST_NODE_TYPES.OptionalCallExpression
                  ? '?.'
                  : '.'
              }includes(${JSON.stringify(text)}`,
            );
          },
        });
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / unbound-method.ts View on Github external
function isSafeUse(node: TSESTree.Node): boolean {
  const parent = node.parent!;

  switch (parent.type) {
    case AST_NODE_TYPES.IfStatement:
    case AST_NODE_TYPES.ForStatement:
    case AST_NODE_TYPES.MemberExpression:
    case AST_NODE_TYPES.OptionalMemberExpression:
    case AST_NODE_TYPES.SwitchStatement:
    case AST_NODE_TYPES.UpdateExpression:
    case AST_NODE_TYPES.WhileStatement:
      return true;

    case AST_NODE_TYPES.CallExpression:
    case AST_NODE_TYPES.OptionalCallExpression:
      return parent.callee === node;

    case AST_NODE_TYPES.ConditionalExpression:
      return parent.test === node;

    case AST_NODE_TYPES.LogicalExpression:
      return parent.operator !== '||';

    case AST_NODE_TYPES.TaggedTemplateExpression:
      return parent.tag === node;

    case AST_NODE_TYPES.UnaryExpression:
      return parent.operator === 'typeof';

    case AST_NODE_TYPES.BinaryExpression:
      return ['instanceof', '==', '!=', '===', '!=='].includes(parent.operator);
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-includes.ts View on Github external
*fix(fixer) {
            const argNode = callNode.arguments[0];
            const needsParen =
              argNode.type !== AST_NODE_TYPES.Literal &&
              argNode.type !== AST_NODE_TYPES.TemplateLiteral &&
              argNode.type !== AST_NODE_TYPES.Identifier &&
              argNode.type !== AST_NODE_TYPES.MemberExpression &&
              argNode.type !== AST_NODE_TYPES.OptionalMemberExpression &&
              argNode.type !== AST_NODE_TYPES.CallExpression &&
              argNode.type !== AST_NODE_TYPES.OptionalCallExpression;

            yield fixer.removeRange([callNode.range[0], argNode.range[0]]);
            if (needsParen) {
              yield fixer.insertTextBefore(argNode, '(');
              yield fixer.insertTextAfter(argNode, ')');
            }
            yield fixer.insertTextAfter(
              argNode,
              `${
                callNode.type === AST_NODE_TYPES.OptionalCallExpression
                  ? '?.'
                  : '.'
              }includes(${JSON.stringify(text)}`,
            );
          },
        });
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / consistent-type-assertions.ts View on Github external
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
    ): void {
      if (
        options.assertionStyle === 'never' ||
        options.objectLiteralTypeAssertions === 'allow' ||
        node.expression.type !== AST_NODE_TYPES.ObjectExpression
      ) {
        return;
      }

      if (
        options.objectLiteralTypeAssertions === 'allow-as-parameter' &&
        node.parent &&
        (node.parent.type === AST_NODE_TYPES.NewExpression ||
          node.parent.type === AST_NODE_TYPES.CallExpression ||
          node.parent.type === AST_NODE_TYPES.OptionalCallExpression ||
          node.parent.type === AST_NODE_TYPES.ThrowStatement ||
          node.parent.type === AST_NODE_TYPES.AssignmentPattern)
      ) {
        return;
      }

      if (
        checkType(node.typeAnnotation) &&
        node.expression.type === AST_NODE_TYPES.ObjectExpression
      ) {
        context.report({
          node,
          messageId: 'unexpectedObjectTypeAssertion',
        });
      }
    }

@typescript-eslint/experimental-utils

(Experimental) Utilities for working with TypeScript + ESLint together

MIT
Latest version published 10 months ago

Package Health Score

85 / 100
Full package analysis