$ref: rewriteValue()

in src/schema/firefox-schemas-import.js [139:224]


          $ref: rewriteValue('$ref', $ref, namespace),
          ...rest,
        };
      }
      return {
        allOf: [
          { $ref: rewriteValue('$ref', $ref, namespace) },
          rewriteValue(key, rest, namespace),
        ],
      };
    }
    const rewritten = inner.rewriteObject(value, namespace);
    if ('properties' in rewritten) {
      const { required, ...properties } = rewriteOptionalToRequired(
        rewritten.properties
      );
      if (required.length > 0) {
        return { ...rewritten, properties, required };
      }
      return { ...rewritten, properties };
    }
    return rewritten;
  }
  if (key === '$ref') {
    if (value.includes('#/types')) {
      return value;
    }
    if (value in refMap) {
      return refMap[value];
    }
    return rewriteIdRef(value);
  }
  if (key === 'type' && value === 'any') {
    return undefined;
  }
  if (key === 'id') {
    return undefined;
  }
  if (key === 'pattern') {
    return rewritePatternFlags(value);
  }
  return value;
}

export function rewriteKey(key) {
  if (key === 'choices') {
    return 'anyOf';
  }
  return key;
}

function rewriteImport(schema, namespace) {
  const { $import, ...rest } = schema;
  const $ref = rewriteIdRef($import, namespace);
  return {
    $merge: {
      source: { $ref },
      with: rewriteValue('$merge', rest, namespace),
    },
  };
}

inner.rewriteObject = (schema, namespace) => {
  if ('$import' in schema) {
    return rewriteImport(schema, namespace);
  }
  return Object.keys(schema).reduce((obj, key) => {
    const value = rewriteValue(key, schema[key], namespace);
    if (value === undefined) {
      return obj;
    }
    return { ...obj, [rewriteKey(key)]: value };
  }, {});
};

inner.mapExtendToRef = (schemas) => {
  const updatedSchemas = { ...schemas };
  Object.keys(updatedSchemas).forEach((id) => {
    const { schema } = updatedSchemas[id];
    Object.keys(schema.refs).forEach((ref) => {
      const { namespace, type } = schema.refs[ref];
      const extendSchema = updatedSchemas[namespace].schema;
      const extendType = extendSchema.types[type];
      let updatedType;
      if ('anyOf' in extendType) {
        updatedType = {