All files / builder/mini-program-compiler/parse-node content.ts

93.33% Statements 14/15
75% Branches 3/4
100% Functions 3/3
93.33% Lines 14/15

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  1x   1x 1x 30x     30x 30x 30x     30x 20x     30x 20x 20x         20x   30x              
import type { Content } from '../../angular-internal/ast.type';
import { NgContentMeta, NgNodeKind, NgNodeMeta, ParsedNode } from './interface';
 
const SELECT_NAME_VALUE_REGEXP = /^\[slot=["']?([^"']*)["']?\]$/;
export class ParsedNgContent implements ParsedNode<NgContentMeta> {
  kind = NgNodeKind.Content;
 
  constructor(
    private node: Content,
    public parent: ParsedNode<NgNodeMeta> | undefined,
    public index: number
  ) {}
  getNodeMeta(): NgContentMeta {
    const nameAttr = this.node.attributes.find(
      (item) => item.name === 'select'
    );
    let value: string | undefined;
    if (nameAttr) {
      const result = nameAttr.value.match(SELECT_NAME_VALUE_REGEXP);
      Iif (!result) {
        throw new Error(
          `ng-content未匹配到指定格式的select,value:${nameAttr.value},需要格式为[slot="xxxx"]`
        );
      }
      value = result[1];
    }
    return {
      kind: NgNodeKind.Content,
      name: value,
      index: this.index,
    };
  }
}