How to use the @typescript-eslint/experimental-utils.AST_NODE_TYPES.Literal 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 / prefer-namespace-keyword.ts View on Github external
TSModuleDeclaration(node): void {
        // Do nothing if the name is a string.
        if (!node.id || node.id.type === AST_NODE_TYPES.Literal) {
          return;
        }
        // Get tokens of the declaration header.
        const moduleType = sourceCode.getTokenBefore(node.id);

        if (
          moduleType &&
          moduleType.type === AST_TOKEN_TYPES.Identifier &&
          moduleType.value === 'module'
        ) {
          context.report({
            node,
            messageId: 'useNamespace',
            fix(fixer) {
              return fixer.replaceText(moduleType, 'namespace');
            },
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-optional-chain.ts View on Github external
}
  },
});

const ALLOWED_MEMBER_OBJECT_TYPES: ReadonlySet = new Set([
  AST_NODE_TYPES.CallExpression,
  AST_NODE_TYPES.Identifier,
  AST_NODE_TYPES.MemberExpression,
  AST_NODE_TYPES.OptionalCallExpression,
  AST_NODE_TYPES.OptionalMemberExpression,
  AST_NODE_TYPES.ThisExpression,
]);
const ALLOWED_COMPUTED_PROP_TYPES: ReadonlySet = new Set([
  AST_NODE_TYPES.BigIntLiteral,
  AST_NODE_TYPES.Identifier,
  AST_NODE_TYPES.Literal,
  AST_NODE_TYPES.MemberExpression,
  AST_NODE_TYPES.OptionalMemberExpression,
  AST_NODE_TYPES.TemplateLiteral,
]);
const ALLOWED_NON_COMPUTED_PROP_TYPES: ReadonlySet = new Set([
  AST_NODE_TYPES.Identifier,
]);

function isValidChainTarget(
  node: TSESTree.Node,
  allowIdentifier: boolean,
): node is ValidChainTarget {
  if (
    node.type === AST_NODE_TYPES.MemberExpression ||
    node.type === AST_NODE_TYPES.OptionalMemberExpression
  ) {
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / no-untyped-public-signature.ts View on Github external
function isIgnoredMethod(
      node: TSESTree.MethodDefinition | TSESTree.TSAbstractMethodDefinition,
      ignoredMethods: Set,
    ): boolean {
      if (
        node.key.type === AST_NODE_TYPES.Literal &&
        typeof node.key.value === 'string'
      ) {
        return ignoredMethods.has(node.key.value);
      }
      if (
        node.key.type === AST_NODE_TYPES.TemplateLiteral &&
        node.key.expressions.length === 0
      ) {
        return ignoredMethods.has(node.key.quasis[0].value.raw);
      }
      if (!node.computed && node.key.type === AST_NODE_TYPES.Identifier) {
        return ignoredMethods.has(node.key.name);
      }

      return false;
    }
github jest-community / eslint-plugin-jest / src / rules / no-jasmine-globals.ts View on Github external
MemberExpression(node) {
        if ('name' in node.object && node.object.name === 'jasmine') {
          const { parent, property } = node;

          if (parent && parent.type === AST_NODE_TYPES.AssignmentExpression) {
            if (
              'name' in property &&
              property.name === 'DEFAULT_TIMEOUT_INTERVAL'
            ) {
              const { right } = parent;

              if (right.type === AST_NODE_TYPES.Literal) {
                context.report({
                  fix: fixer => [
                    fixer.replaceText(
                      parent,
                      `jest.setTimeout(${right.value})`,
                    ),
                  ],
                  node,
                  messageId: 'illegalJasmine',
                });
                return;
              }
            }

            context.report({ node, messageId: 'illegalJasmine' });
          }
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / no-inferrable-types.ts View on Github external
return (
            isFunctionCall(init, 'String') ||
            isLiteral(init, 'string') ||
            init.type === AST_NODE_TYPES.TemplateLiteral
          );

        case AST_NODE_TYPES.TSSymbolKeyword:
          return isFunctionCall(init, 'Symbol');

        case AST_NODE_TYPES.TSTypeReference: {
          if (
            annotation.typeName.type === AST_NODE_TYPES.Identifier &&
            annotation.typeName.name === 'RegExp'
          ) {
            const isRegExpLiteral =
              init.type === AST_NODE_TYPES.Literal &&
              init.value instanceof RegExp;
            const isRegExpNewCall =
              init.type === AST_NODE_TYPES.NewExpression &&
              init.callee.type === AST_NODE_TYPES.Identifier &&
              init.callee.name === 'RegExp';
            const isRegExpCall = isFunctionCall(init, 'RegExp');

            return isRegExpLiteral || isRegExpCall || isRegExpNewCall;
          }

          return false;
        }

        case AST_NODE_TYPES.TSUndefinedKeyword:
          return (
            hasUnaryPrefix(init, 'void') || isIdentifier(init, 'undefined')
github jest-community / eslint-plugin-jest / src / rules / utils.ts View on Github external
const isStringLiteral = (
  node: TSESTree.Node,
  value?: V,
): node is StringLiteral =>
  node.type === AST_NODE_TYPES.Literal &&
  typeof node.value === 'string' &&
  (value === undefined || node.value === value);
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / no-inferrable-types.ts View on Github external
function isLiteral(init: TSESTree.Expression, typeName: string): boolean {
      return (
        init.type === AST_NODE_TYPES.Literal && typeof init.value === typeName
      );
    }
    function isIdentifier(
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-optional-chain.ts View on Github external
- foo !== undefined
  - foo != undefined
  */
  if (
    node.type === AST_NODE_TYPES.BinaryExpression &&
    ['!==', '!='].includes(node.operator) &&
    isValidChainTarget(node.left, allowIdentifier)
  ) {
    if (
      node.right.type === AST_NODE_TYPES.Identifier &&
      node.right.name === 'undefined'
    ) {
      return true;
    }
    if (
      node.right.type === AST_NODE_TYPES.Literal &&
      node.right.value === null
    ) {
      return true;
    }
  }

  return false;
}
github jest-community / eslint-plugin-jest / src / rules / lowercase-name.ts View on Github external
const hasStringAsFirstArgument = (
  node: TSESTree.CallExpression,
): node is FirstArgumentStringCallExpression =>
  node.arguments &&
  node.arguments[0] &&
  (node.arguments[0].type === AST_NODE_TYPES.Literal ||
    node.arguments[0].type === AST_NODE_TYPES.TemplateLiteral);
github jest-community / eslint-plugin-jest / src / rules / prefer-expect-assertions.ts View on Github external
) {
    return false;
  }

  const expectAssertionName = getAccessorValue(expression.callee.property);

  if (expectAssertionName !== 'assertions') {
    return expectAssertionName === 'hasAssertions';
  }

  const [arg] = expression.arguments;

  return (
    expression.arguments &&
    expression.arguments.length === 1 &&
    arg.type === AST_NODE_TYPES.Literal &&
    typeof arg.value === 'number' &&
    Number.isInteger(arg.value)
  );
};

@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