Skip to content

[JavaScript] Generator options and template improvements #2258

@demonfiddler

Description

@demonfiddler

This is to propose the following changes to the JavaScript code generator:

  1. RENAME omitModelMethods [=false] to emitModelMethods [=false]. The rationale is two-fold: firstly to suppress their emission by default, since getter and setter methods are not a typical JavaScript programming idiom; their presence bloats code and adds little or no value. Even if it were felt desirable to access properties via a function call, ECMA 5.1 provides for transparent property get set accessor functions. Secondly, the new option name would be consistent with option 2 below.
  2. ADD emitJSDoc [=true] so that JSDoc comments are emitted by default but disabled if desired. The JSDoc tags and format to be carefully chosen to conform to the requirements of both JSDoc (for API documentation generation) and Tern.js (to support content assist and validation).
  3. ADD useInheritance [=true]. By default generated classes for 'allOf' composed types would use idiomatic JavaScript inheritance via prototype chains for the 'parent' axis and mix in any additional types from the 'interfaces' axis. When set to false, all composed types would be mixed in as at present.
  4. REMOVE the toJson() [sic] method from the template, as it bloats code, serves no useful purpose and causes confusion with the toJSON() method recognised by JSON.stringify(). See Move common functionality to new model base class #2075, JS client: Model methods should be added to prototype #2044.
  5. MOVE model methods to the prototype. See JS client: Model methods should be added to prototype #2044.
  6. DEFINE prototype properties for those owned properties with default values, rather than adding them to the instance as at present. Adding such properties to the instance causes their values to be serialised to JSON, thus defeating the purpose of default values and furthermore making it impossible to restore the default value without a priori knowledge of the default value! Defining the default property value on the prototype allows the default to be restored by simply deleting the instance property.

I have most of the above implemented and when complete I should like to submit a PR. The changes will include unit and functional tests as appropriate.

Comments invited.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions