Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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('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:
self.emit("%s: %s;" % (fmt_name_case(variant.name, self.use_camel_case),
fmt_type(variant.data_type, namespace)))
self.emit('}')
self.emit()
def _generate_alias_type(self, alias_type):
"""
Generates a TypeScript type for a stone alias.
"""
namespace = alias_type.namespace
self.emit('export type %s = %s;' % (fmt_type_name(alias_type, namespace),
fmt_type_name(alias_type.data_type, namespace)))
self.emit()
# This struct is a particular subtype. Find the applicable .tag value from the
# parent type, which may be an arbitrary number of steps up the inheritance
# hierarchy.
parent = struct_type.parent_type
while not parent.has_enumerated_subtypes():
parent = parent.parent_type
# parent now contains the closest parent type in the inheritance hierarchy that has
# enumerated subtypes. Determine which subtype this is.
for subtype in parent.get_enumerated_subtypes():
if subtype.data_type == struct_type:
self._emit_tsdoc_header('Reference to the %s type, identified by the '
'value of the .tag property.' %
fmt_type_name(struct_type, namespace))
self.emit('export interface %s extends %s {' %
(fmt_polymorphic_type_reference(struct_type, namespace),
fmt_type_name(struct_type, namespace)))
with self.indent(dent=indent_spaces):
self._emit_tsdoc_header('Tag identifying this subtype variant. This '
'field is only present when needed to '
'discriminate between multiple possible '
'subtypes.')
self.emit_wrapped_text('\'.tag\': \'%s\';' % subtype.name)
self.emit('}')
self.emit()
break
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('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:
self.emit("%s: %s;" % (fmt_name_case(variant.name, self.use_camel_case),
fmt_type(variant.data_type, namespace)))
def _generate_alias_type(self, alias_type):
"""
Generates a TypeScript type for a stone alias.
"""
namespace = alias_type.namespace
self.emit('export type %s = %s;' % (fmt_type_name(alias_type, namespace),
fmt_type_name(alias_type.data_type, namespace)))
self.emit()
# This struct is a particular subtype. Find the applicable .tag value from the
# parent type, which may be an arbitrary number of steps up the inheritance
# hierarchy.
parent = struct_type.parent_type
while not parent.has_enumerated_subtypes():
parent = parent.parent_type
# parent now contains the closest parent type in the inheritance hierarchy that has
# enumerated subtypes. Determine which subtype this is.
for subtype in parent.get_enumerated_subtypes():
if subtype.data_type == struct_type:
self._emit_tsdoc_header('Reference to the %s type, identified by the '
'value of the .tag property.' %
fmt_type_name(struct_type, namespace))
self.emit('export interface %s extends %s {' %
(fmt_polymorphic_type_reference(struct_type, namespace),
fmt_type_name(struct_type, namespace)))
with self.indent(dent=indent_spaces):
self._emit_tsdoc_header('Tag identifying this subtype variant. This '
'field is only present when needed to '
'discriminate between multiple possible '
'subtypes.')
self.emit_wrapped_text('\'.tag\': \'%s\';' % subtype.name)
self.emit('}')
self.emit()
break
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))
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(