コード例 #1
0
ファイル: tsd_types.py プロジェクト: ThiagoJunior/stone
    def _generate_union_type(self, union_type, indent_spaces):
        """
        Generates a TypeScript interface for a stone union.
        """
        # Emit an interface for each variant. TypeScript 2.0 supports these tagged unions.
        # https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#tagged-union-types
        parent_type = union_type.parent_type
        namespace = union_type.namespace
        union_type_name = fmt_type_name(union_type, namespace)
        variant_type_names = []
        if parent_type:
            variant_type_names.append(fmt_type_name(parent_type, namespace))
        for variant in union_type.fields:
            if variant.doc:
                self._emit_tsdoc_header(variant.doc)
            variant_name = '%s%s' % (union_type_name, fmt_pascal(variant.name))
            variant_type_names.append(variant_name)
            self.emit('interface %s {' % variant_name)
            with self.indent(dent=indent_spaces):
                # Since field contains non-alphanumeric character, we need to enclose
                # it in quotation marks.
                self.emit("'.tag': '%s';" % variant.name)
                if is_void_type(variant.data_type) is False:
                    self.emit("%s: %s;" % (variant.name, fmt_type(variant.data_type, namespace)))
            self.emit('}')
            self.emit()

        if union_type.doc:
            self._emit_tsdoc_header(union_type.doc)
        self.emit('type %s = %s;' % (union_type_name, ' | '.join(variant_type_names)))
        self.emit()
コード例 #2
0
    def _generate_union_type(self, union_type, indent_spaces):
        """
        Generates a TypeScript interface for a stone union.
        """
        # Emit an interface for each variant. TypeScript 2.0 supports these tagged unions.
        # https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#tagged-union-types
        parent_type = union_type.parent_type
        namespace = union_type.namespace
        union_type_name = fmt_type_name(union_type, namespace)
        variant_type_names = []
        if parent_type:
            variant_type_names.append(fmt_type_name(parent_type, namespace))

        def _is_struct_without_enumerated_subtypes(data_type):
            """
            :param data_type: any data type.
            :return: True if the given data type is a struct which has no enumerated subtypes.
            """
            return is_struct_type(data_type) and (
                not data_type.has_enumerated_subtypes())

        for variant in union_type.fields:
            if variant.doc:
                self._emit_tsdoc_header(variant.doc)
            variant_name = '%s%s' % (union_type_name, fmt_pascal(variant.name))
            variant_type_names.append(variant_name)

            is_struct_without_enumerated_subtypes = _is_struct_without_enumerated_subtypes(
                variant.data_type)

            if is_struct_without_enumerated_subtypes:
                self.emit(
                    'export interface %s extends %s {' %
                    (variant_name, fmt_type(variant.data_type, namespace)))
            else:
                self.emit('export interface %s {' % variant_name)

            with self.indent(dent=indent_spaces):
                # Since field contains non-alphanumeric character, we need to enclose
                # it in quotation marks.
                self.emit("'.tag': '%s';" % variant.name)
                if is_void_type(variant.data_type) is False and (
                        not is_struct_without_enumerated_subtypes):
                    self.emit(
                        "%s: %s;" %
                        (variant.name, fmt_type(variant.data_type, namespace)))
            self.emit('}')
            self.emit()

        if union_type.doc:
            self._emit_tsdoc_header(union_type.doc)
        self.emit('export type %s = %s;' %
                  (union_type_name, ' | '.join(variant_type_names)))
        self.emit()
コード例 #3
0
def fmt_type_name(data_type):
    """
    Returns the JSDoc name for the given data type.
    (Does not attempt to enumerate subtypes.)
    """
    if is_user_defined_type(data_type):
        return fmt_pascal('%s%s' % (data_type.namespace.name, data_type.name))
    else:
        fmted_type = _base_type_table.get(data_type.__class__, 'Object')
        if is_list_type(data_type):
            fmted_type += '.<' + fmt_type(data_type.data_type) + '>'
        return fmted_type
コード例 #4
0
    def fmt_type_name(self, data_type):

        if is_user_defined_type(data_type):
            return fmt_pascal('%s%s' %
                              (data_type.namespace.name, data_type.name))
        else:
            fmted_type = self._base_type_table.get(data_type.__class__,
                                                   'Object')
            if is_list_type(data_type):
                fmted_type = '[2' + self.fmt_type_name(
                    data_type.data_type) + ']'
            return fmted_type
コード例 #5
0
ファイル: tsd_types.py プロジェクト: dropbox/stone
    def _generate_union_type(self, union_type, indent_spaces):
        """
        Generates a TypeScript interface for a stone union.
        """
        # Emit an interface for each variant. TypeScript 2.0 supports these tagged unions.
        # https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#tagged-union-types
        parent_type = union_type.parent_type
        namespace = union_type.namespace
        union_type_name = fmt_type_name(union_type, namespace)
        variant_type_names = []
        if parent_type:
            variant_type_names.append(fmt_type_name(parent_type, namespace))

        def _is_struct_without_enumerated_subtypes(data_type):
            """
            :param data_type: any data type.
            :return: True if the given data type is a struct which has no enumerated subtypes.
            """
            return is_struct_type(data_type) and (
                not data_type.has_enumerated_subtypes())

        for variant in union_type.fields:
            if variant.doc:
                self._emit_tsdoc_header(variant.doc)
            variant_name = '%s%s' % (union_type_name, fmt_pascal(variant.name))
            variant_type_names.append(variant_name)

            is_struct_without_enumerated_subtypes = _is_struct_without_enumerated_subtypes(
                variant.data_type)

            if is_struct_without_enumerated_subtypes:
                self.emit('export interface %s extends %s {' % (
                    variant_name, fmt_type(variant.data_type, namespace)))
            else:
                self.emit('export interface %s {' % variant_name)

            with self.indent(dent=indent_spaces):
                # Since field contains non-alphanumeric character, we need to enclose
                # it in quotation marks.
                self.emit("'.tag': '%s';" % variant.name)
                if is_void_type(variant.data_type) is False and (
                    not is_struct_without_enumerated_subtypes
                ):
                    self.emit("%s: %s;" % (variant.name, fmt_type(variant.data_type, namespace)))
            self.emit('}')
            self.emit()

        if union_type.doc:
            self._emit_tsdoc_header(union_type.doc)
        self.emit('export type %s = %s;' % (union_type_name, ' | '.join(variant_type_names)))
        self.emit()
コード例 #6
0
def fmt_class(name, check_reserved=False):
    s = fmt_pascal(name)
    return _rename_if_reserved(s) if check_reserved else s
コード例 #7
0
def fmt_var(name, export=True, check_reserved=False):
    s = helpers.fmt_pascal(name) if export else helpers.fmt_camel(name)
    return _rename_if_reserved(s) if check_reserved else s
コード例 #8
0
def fmt_class(name, check_reserved=False):
    s = fmt_pascal(name)
    return _rename_if_reserved(s) if check_reserved else s
コード例 #9
0
 def alias_name(self, alias):
     name = fmt_pascal(alias.name)
     if name in RUST_RESERVED_WORDS + RUST_GLOBAL_NAMESPACE:
         name += 'Alias'
     return name
コード例 #10
0
 def enum_variant_name_raw(self, name):
     name = fmt_pascal(name)
     if name in RUST_RESERVED_WORDS:
         name += 'Variant'
     return name
コード例 #11
0
 def enum_name(self, union):
     name = fmt_pascal(union.name)
     if name in RUST_RESERVED_WORDS + RUST_GLOBAL_NAMESPACE:
         name += 'Union'
     return name
コード例 #12
0
 def struct_name(self, struct):
     name = fmt_pascal(struct.name)
     if name in RUST_RESERVED_WORDS + RUST_GLOBAL_NAMESPACE:
         name += 'Struct'
     return name