All files / builder/platform/template-transform-strategy/wx-like wx-transform.base.ts

97.43% Statements 38/39
83.33% Branches 5/6
100% Functions 10/10
97.43% Lines 38/39

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 901x   1x 1x   1x   1x 17x 17x       17x     17x               226x   226x 1057x   226x 226x 226x 226x 226x   222x                         170x     441x 441x 441x 441x 126x 126x   441x           17x 1151x 1151x 490x 490x 490x 490x 130x 130x                 360x     1151x 360x        
import { strings } from '@angular-devkit/core';
import type { NgNodeMeta } from '../../../mini-program-compiler';
import { TemplateTransformBase } from '../transform.base';
import { WxContainer } from './wx-container';
 
export const EVENT_PREFIX_REGEXP =
  /^(bind|catch|mut-bind|capture-bind|capture-catch)(.*)$/;
export abstract class WxTransformLike extends TemplateTransformBase {
  seq = ':';
  templateInterpolation: [string, string] = ['{{', '}}'];
  abstract directivePrefix: string;
 
  constructor() {
    super();
  }
  init() {
    WxContainer.initWxContainerFactory({
      seq: this.seq,
      directivePrefix: this.directivePrefix,
      eventListConvert: this.eventListConvert,
      templateInterpolation: this.templateInterpolation,
    });
  }
  compile(nodes: NgNodeMeta[]) {
    const container = new WxContainer();
 
    nodes.forEach((node) => {
      container.compileNode(node);
    });
    const result = container.export();
    const metaCollectionGroup = container.exportMetaCollectionGroup();
    const inlineMetaCollection = metaCollectionGroup.$inline;
    delete metaCollectionGroup.$inline;
    return {
      content: `${inlineMetaCollection.templateList
        .map((item) => item.content)
        .join('')}<block ${this.directivePrefix}${this.seq}if="{{hasLoad}}">${
        result.wxmlTemplate
      }</block>`,
      useComponentPath: {
        localPath: [...inlineMetaCollection.localPath],
        libraryPath: [...inlineMetaCollection.libraryPath],
      },
      otherMetaGroup: metaCollectionGroup,
    };
  }
 
  getData() {
    return { directivePrefix: this.directivePrefix };
  }
  eventNameConvert(tagEventMeta: string) {
    const result = tagEventMeta.match(EVENT_PREFIX_REGEXP);
    let prefix: string = 'bind';
    let type: string = tagEventMeta;
    if (result) {
      prefix = result[1];
      type = result[2];
    }
    return {
      prefix,
      type,
      name: `${prefix}:${type}`,
    };
  }
  eventListConvert = (list: string[]) => {
    const eventMap = new Map();
    list.forEach((eventName) => {
      const result = this.eventNameConvert(eventName);
      const prefix = strings.camelize(result.prefix);
      const bindEventName = `${prefix}Event`;
      if (eventMap.has(result.name)) {
        Eif (eventMap.get(result.name) === bindEventName) {
          return;
        } else {
          throw new Error(
            `事件名[${result.name}]解析异常,原绑定${eventMap.get(
              result.name
            )},现绑定${bindEventName}`
          );
        }
      }
      eventMap.set(result.name, bindEventName);
    });
 
    return Array.from(eventMap.entries())
      .map(([key, value]) => `${key}="${value}"`)
      .join(' ');
  };
}