MCPcopy Create free account
hub / github.com/ampproject/amphtml / PrintEnumFor

Function PrintEnumFor

validator/validator_gen_js.py:456–502  ·  view source on GitHub ↗

Prints a Javascript enum for the given enum descriptor. Args: enum_desc: The descriptor for a particular enum type. out: a list of lines to output (without the newline characters) wrapped as an OutputFormatter instance, to which this function will append.

(enum_desc, out)

Source from the content-addressed store, hash-verified

454
455
456def PrintEnumFor(enum_desc, out):
457 """Prints a Javascript enum for the given enum descriptor.
458
459 Args:
460 enum_desc: The descriptor for a particular enum type.
461 out: a list of lines to output (without the newline characters) wrapped as
462 an OutputFormatter instance, to which this function will append.
463 """
464 out.Line('/**')
465 out.Line(' * @enum {string}')
466 out.Line(' */')
467 out.Line('%s = {' % LocalModuleName(enum_desc.full_name))
468 out.PushIndent(2)
469 names = []
470 for v in enum_desc.values:
471 names.append('%s' % v.name)
472 out.Line("%s: '%s'," % (v.name, v.name))
473 out.PopIndent()
474 out.Line('};')
475
476 out.Line('exports.%s = %s;' % (LocalModuleName(
477 enum_desc.full_name), LocalModuleName(enum_desc.full_name)))
478 out.Line('/** @type {!Array<string>} */')
479 out.Line('%s_NamesByIndex = ["%s"];' %
480 (LocalModuleName(enum_desc.full_name), '","'.join(names)))
481 out.Line('/** @type {!Array<!%s>} */' % LocalModuleName(enum_desc.full_name))
482 out.Line(
483 '%s_ValuesByIndex = [%s];' %
484 (LocalModuleName(enum_desc.full_name), ','.join([
485 '%s.%s' % (LocalModuleName(enum_desc.full_name), n) for n in names
486 ])))
487 out.Line('/** @type (!Object<%s, number>) */' %
488 LocalModuleName(enum_desc.full_name))
489 out.Line('%s_NumberByName = {' % LocalModuleName(enum_desc.full_name))
490 out.PushIndent(2)
491 for v in enum_desc.values:
492 out.Line("'%s': %s," % (v.name, v.number))
493 out.PopIndent()
494 out.Line('};')
495 out.Line('/** @type (!Object<number, %s>) */' %
496 LocalModuleName(enum_desc.full_name))
497 out.Line('%s_NameByNumber = {' % LocalModuleName(enum_desc.full_name))
498 out.PushIndent(2)
499 for v in enum_desc.values:
500 out.Line("%s: '%s'," % (v.number, v.name))
501 out.PopIndent()
502 out.Line('};')
503
504
505def TagSpecName(tag_spec):

Callers 1

Calls 5

LocalModuleNameFunction · 0.85
LineMethod · 0.80
PushIndentMethod · 0.80
PopIndentMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected