All files / src/language reference-extractor.ts

100% Statements 65/65
100% Branches 31/31
100% Functions 7/7
100% Lines 63/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188                  22x 22x 22x 22x 22x 22x 22x 22x                       22x 22x 50x 50x 50x 50x   11x       22x 55x 55x   109x   22x   22x 22x   22x 22x               22x     8x 8x 8x   8x     8x               8x     19x 19x 19x                     19x     10x 10x 10x               10x     50x   109x     55x       21x     21x   21x   11x 11x                   21x 21x 12x   7x 7x                   21x 21x   5x 5x                   21x 21x   5x 5x                   21x       22x 10x      
import { AstWalker } from './ast-walker.js';
import { NodeType } from './node-type.js';
import type { SpelNodeImpl } from '../ast/spel-node.js';
import { SpelExpressionParser } from '../spel-expression-parser.js';
import { VariableReference } from '../ast/reference/variable-reference.js';
import { PropertyOrFieldReference } from '../ast/reference/property-or-field-reference.js';
import { BeanReference } from '../ast/reference/bean-reference.js';
import { TypeReference } from '../ast/reference/type-reference.js';
 
export enum SpelReferenceKind {
  VARIABLE = 'variable',
  ROOT_PROPERTY = 'root_property',
  THIS_PROPERTY = 'this_property',
  BEAN = 'bean',
  BEAN_FACTORY = 'bean_factory',
  TYPE = 'type',
  FUNCTION = 'function',
}
 
export interface SpelReference {
  kind: SpelReferenceKind;
  name: string;
  path: string[];
  startPos: number;
  endPos: number;
  nodeType: NodeType;
}
 
export namespace SpelReferenceExtractor {
  export function extract(expression: string): SpelReference[] {
    try {
      const parser = new SpelExpressionParser();
      const ast = parser.parseRaw(expression);
      return extractFromAst(ast);
    } catch {
      return extractFallback(expression);
    }
  }
 
  export function extractFromAst(root: SpelNodeImpl): SpelReference[] {
    const refs: SpelReference[] = [];
    AstWalker.walk(root, {
      enterNode(node, ancestors) {
        switch (node.nodeType) {
          case NodeType.VARIABLE_REFERENCE: {
            const varRef = node as unknown as VariableReference;
            const name =
              typeof varRef.getVariableName === 'function' ? varRef.getVariableName() : '';
            const parent = ancestors[ancestors.length - 1];
            const isFunction =
              ancestors.length > 0 && parent?.nodeType === NodeType.METHOD_REFERENCE;
            refs.push({
              kind: isFunction ? SpelReferenceKind.FUNCTION : SpelReferenceKind.VARIABLE,
              name,
              path: [name],
              startPos: node.startPos,
              endPos: node.endPos,
              nodeType: node.nodeType,
            });
            break;
          }
          case NodeType.PROPERTY_OR_FIELD_REFERENCE: {
            const propRef = node as unknown as PropertyOrFieldReference;
            const name = typeof propRef.getName === 'function' ? propRef.getName() : '';
            const parent = ancestors[ancestors.length - 1];
            const kind =
              parent?.nodeType === NodeType.VARIABLE_REFERENCE
                ? SpelReferenceKind.ROOT_PROPERTY
                : SpelReferenceKind.THIS_PROPERTY;
            refs.push({
              kind,
              name,
              path: [name],
              startPos: node.startPos,
              endPos: node.endPos,
              nodeType: node.nodeType,
            });
            break;
          }
          case NodeType.BEAN_REFERENCE: {
            const beanRef = node as unknown as BeanReference;
            const name = typeof beanRef.getBeanName === 'function' ? beanRef.getBeanName() : '';
            refs.push({
              kind:
                typeof beanRef.isFactory === 'function' && beanRef.isFactory()
                  ? SpelReferenceKind.BEAN_FACTORY
                  : SpelReferenceKind.BEAN,
              name,
              path: [name],
              startPos: node.startPos,
              endPos: node.endPos,
              nodeType: node.nodeType,
            });
            break;
          }
          case NodeType.TYPE_REFERENCE: {
            const typeRef = node as unknown as TypeReference;
            const name = typeof typeRef.getTypeName === 'function' ? typeRef.getTypeName() : '';
            refs.push({
              kind: SpelReferenceKind.TYPE,
              name,
              path: [name],
              startPos: node.startPos,
              endPos: node.endPos,
              nodeType: node.nodeType,
            });
            break;
          }
          default:
            break;
        }
        return true;
      },
    });
    return refs;
  }
 
  function extractFallback(expression: string): SpelReference[] {
    const refs: SpelReference[] = [];
 
    // Regex capture groups are always populated when matched — non-null assertions are safe here.
    const varRegex = /#(\w+(?:\.\w+)*)/g;
    let match: RegExpExecArray | null;
    while ((match = varRegex.exec(expression)) !== null) {
      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- regex capture group 1 always populated
      const varName = match[1]!;
      refs.push({
        kind: SpelReferenceKind.VARIABLE,
        name: varName,
        path: varName.split('.'),
        startPos: match.index,
        endPos: match.index + match[0].length,
        nodeType: NodeType.VARIABLE_REFERENCE,
      });
    }
 
    const beanRegex = /@(\w+)/g;
    while ((match = beanRegex.exec(expression)) !== null) {
      if (match.index > 0 && expression[match.index - 1] === '&') continue;
      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- regex capture group 1 always populated
      const beanName = match[1]!;
      refs.push({
        kind: SpelReferenceKind.BEAN,
        name: beanName,
        path: [beanName],
        startPos: match.index,
        endPos: match.index + match[0].length,
        nodeType: NodeType.BEAN_REFERENCE,
      });
    }
 
    const factoryRegex = /&@(\w+)/g;
    while ((match = factoryRegex.exec(expression)) !== null) {
      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- regex capture group 1 always populated
      const factoryName = match[1]!;
      refs.push({
        kind: SpelReferenceKind.BEAN_FACTORY,
        name: factoryName,
        path: [factoryName],
        startPos: match.index,
        endPos: match.index + match[0].length,
        nodeType: NodeType.BEAN_REFERENCE,
      });
    }
 
    const typeRegex = /T\((\w+(?:\.\w+)*)\)/g;
    while ((match = typeRegex.exec(expression)) !== null) {
      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- regex capture group 1 always populated
      const typeName = match[1]!;
      refs.push({
        kind: SpelReferenceKind.TYPE,
        name: typeName,
        path: [typeName],
        startPos: match.index,
        endPos: match.index + match[0].length,
        nodeType: NodeType.TYPE_REFERENCE,
      });
    }
 
    return refs;
  }
 
  /** @internal — exported for testing the fallback regex paths */
  export function _testExtractFallback(expression: string): SpelReference[] {
    return extractFallback(expression);
  }
}