checkConstructModelFields()

in lib/semantic.js [2239:2284]


  checkConstructModelFields(ast, model, modelName, env) {
    if (!ast.object) {
      return;
    }

    let aliasId = ast.aliasId.lexeme;
    this.visitObject(ast.object, env);

    for (let i = 0; i < ast.object.fields.length; i++) {
      const field = ast.object.fields[i];
      const name = field.fieldName.lexeme;
      let find;
      if (ast.aliasId.isModel) {
        find = this.findProperty(model, name, model.isException);
        if (!find) {
          this.error(`the property "${name}" is undefined in model "${modelName}"`, field.fieldName);
        }
      } else {
        const checker = this.dependencies.get(aliasId);
        find = checker.findProperty(model, name, model.isException);
        if (!find) {
          this.error(`the property "${name}" is undefined in model "${aliasId}.${modelName}"`, field.fieldName);
        }
      }
      const currentModelName = find.modelName;
      const modelField = find.modelField;
      const type = this.getExprType(field.expr, env);
      const moduleName = find.moduleName || aliasId;
      let expected;
      if (ast.aliasId.isModel) {
        expected = this.getFieldType(modelField, currentModelName);
      } else {
        const checker = this.dependencies.get(aliasId);
        expected = checker.getFieldType(modelField, currentModelName, moduleName);
      }
      
      
      if (!eql([expected], [type])) {
        this.error(`the field type are mismatched. expected ` +
          `${display(expected)}, but ${display(type)}`, field.fieldName);
      }
      field.extendFrom = find.extendFrom;
      field.inferred = type;
      field.expectedType = expected;
    }
  }