private addChild()

in src/construct.ts [387:413]


  private addChild(child: Construct, childName: string) {
    if (this.locked) {

      // special error if root is locked
      if (!this.path) {
        throw new Error('Cannot add children during synthesis');
      }

      throw new Error(`Cannot add children to "${this.path}" during synthesis`);
    }

    if (childName in this._children) {
      const name = this.id ?? '';
      const typeName = this.host.constructor.name;
      throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);
    }

    if (!childName && this.id) {
      throw new Error(`cannot add a nameless construct to the named scope: ${this.path}`);
    }

    this._children[childName] = child;

    if (Object.keys(this._children).length > 1 && Object.keys(this._children).filter(x => !x).length > 0) {
      throw new Error('only a single construct is allowed in a scope if it has an empty name');
    }
  }