_getCommonAttributeValue()

in packages/babel-plugin-fbt/src/babel-processors/JSXFbtProcessor.js [315:342]


  _getCommonAttributeValue() {
    const commonAttr = getAttributeByName(
      this._getOpeningElementAttributes(),
      CommonOption,
    );
    if (commonAttr == null) {
      return null;
    }

    // A JSX/HTML tag attribute without value is default to boolean value true.
    // E.g. `<fbt common>Done</fbt>`
    const commonAttrValue = commonAttr.value;
    if (commonAttrValue == null) {
      return booleanLiteral(true);
    }

    // E.g. `<fbt common={true}>Done</fbt>`
    if (commonAttrValue.type === 'JSXExpressionContainer') {
      const expression = commonAttrValue.expression;
      if (expression.type === 'BooleanLiteral') {
        return expression;
      }
    }

    throw new Error(
      `\`${CommonOption}\` attribute for <${this.moduleName}> requires boolean literal`,
    );
  }