How to use the eslint-utils.getStaticValue function in eslint-utils

To help you get started, we’ve selected a few eslint-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 sikidamjanovic / cowrite / node_modules / @typescript-eslint / eslint-plugin / dist / rules / prefer-includes.js View on Github external
function parseRegExp(node) {
            const evaluated = eslint_utils_1.getStaticValue(node, globalScope);
            if (evaluated == null || !(evaluated.value instanceof RegExp)) {
                return null;
            }
            const { pattern, flags } = regexpp_1.parseRegExpLiteral(evaluated.value);
            if (pattern.alternatives.length !== 1 ||
                flags.ignoreCase ||
                flags.global) {
                return null;
            }
            // Check if it can determine a unique string.
            const chars = pattern.alternatives[0].elements;
            if (!chars.every(c => c.type === 'Character')) {
                return null;
            }
            // To string.
            return String.fromCodePoint(...chars.map(c => c.value));
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-includes.ts View on Github external
function parseRegExp(node: TSESTree.Node): string | null {
      const evaluated = getStaticValue(node, globalScope);
      if (evaluated == null || !(evaluated.value instanceof RegExp)) {
        return null;
      }

      const { pattern, flags } = parseRegExpLiteral(evaluated.value);
      if (
        pattern.alternatives.length !== 1 ||
        flags.ignoreCase ||
        flags.global
      ) {
        return null;
      }

      // Check if it can determine a unique string.
      const chars = pattern.alternatives[0].elements;
      if (!chars.every(c => c.type === 'Character')) {
github sikidamjanovic / cowrite / node_modules / @typescript-eslint / eslint-plugin / dist / rules / prefer-string-starts-ends-with.js View on Github external
function parseRegExp(node) {
            const evaluated = eslint_utils_1.getStaticValue(node, globalScope);
            if (evaluated == null || !(evaluated.value instanceof RegExp)) {
                return null;
            }
            const { source, flags } = evaluated.value;
            const isStartsWith = source.startsWith('^');
            const isEndsWith = source.endsWith('$');
            if (isStartsWith === isEndsWith ||
                flags.includes('i') ||
                flags.includes('m')) {
                return null;
            }
            const text = parseRegExpText(source, flags.includes('u'));
            if (text == null) {
                return null;
            }
            return { isEndsWith, isStartsWith, text };
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-string-starts-ends-with.ts View on Github external
function parseRegExp(
      node: TSESTree.Node,
    ): { isStartsWith: boolean; isEndsWith: boolean; text: string } | null {
      const evaluated = getStaticValue(node, globalScope);
      if (evaluated == null || !(evaluated.value instanceof RegExp)) {
        return null;
      }

      const { source, flags } = evaluated.value;
      const isStartsWith = source.startsWith('^');
      const isEndsWith = source.endsWith('$');
      if (
        isStartsWith === isEndsWith ||
        flags.includes('i') ||
        flags.includes('m')
      ) {
        return null;
      }

      const text = parseRegExpText(source, flags.includes('u'));
github sikidamjanovic / cowrite / node_modules / @typescript-eslint / eslint-plugin / dist / rules / prefer-string-starts-ends-with.js View on Github external
function isLengthExpression(node, expectedObjectNode) {
            if (node.type === 'MemberExpression') {
                return (eslint_utils_1.getPropertyName(node, globalScope) === 'length' &&
                    isSameTokens(node.object, expectedObjectNode));
            }
            const evaluatedLength = eslint_utils_1.getStaticValue(node, globalScope);
            const evaluatedString = eslint_utils_1.getStaticValue(expectedObjectNode, globalScope);
            return (evaluatedLength != null &&
                evaluatedString != null &&
                typeof evaluatedLength.value === 'number' &&
                typeof evaluatedString.value === 'string' &&
                evaluatedLength.value === evaluatedString.value.length);
        }
        /**
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-string-starts-ends-with.ts View on Github external
function isLengthExpression(
      node: TSESTree.Node,
      expectedObjectNode: TSESTree.Node,
    ): boolean {
      if (node.type === 'MemberExpression') {
        return (
          getPropertyName(node, globalScope) === 'length' &&
          isSameTokens(node.object, expectedObjectNode)
        );
      }

      const evaluatedLength = getStaticValue(node, globalScope);
      const evaluatedString = getStaticValue(expectedObjectNode, globalScope);
      return (
        evaluatedLength != null &&
        evaluatedString != null &&
        typeof evaluatedLength.value === 'number' &&
        typeof evaluatedString.value === 'string' &&
        evaluatedLength.value === evaluatedString.value.length
      );
    }
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-includes.ts View on Github external
function isNumber(node: TSESTree.Node, value: number): boolean {
      const evaluated = getStaticValue(node, globalScope);
      return evaluated !== null && evaluated.value === value;
    }
github typescript-eslint / typescript-eslint / packages / eslint-plugin / src / rules / prefer-string-starts-ends-with.ts View on Github external
function isNumber(
      node: TSESTree.Node,
      value: number,
    ): node is TSESTree.Literal {
      const evaluated = getStaticValue(node, globalScope);
      return evaluated != null && evaluated.value === value;
    }