diff --git a/examples/gen-js/js/analytics/generated/index.d.ts b/examples/gen-js/js/analytics/generated/index.d.ts index 766921bf..09b3b528 100644 --- a/examples/gen-js/js/analytics/generated/index.d.ts +++ b/examples/gen-js/js/analytics/generated/index.d.ts @@ -44,19 +44,19 @@ export default class Analytics { constructor(analytics: any); feedViewed( - message: FeedViewed, + message?: FeedViewed, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; photoViewed( - message: PhotoViewed, + message?: PhotoViewed, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; profileViewed( - message: ProfileViewed, + message?: ProfileViewed, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; diff --git a/examples/gen-js/js/analytics/generated/index.js b/examples/gen-js/js/analytics/generated/index.js index 2b9dba8c..e2d97278 100644 --- a/examples/gen-js/js/analytics/generated/index.js +++ b/examples/gen-js/js/analytics/generated/index.js @@ -18,7 +18,7 @@ export default class Analytics { } this.analytics = analytics || { track: () => null }; } - feedViewed(props, context) { + feedViewed(props = {}, context) { var validate = function( data, dataPath, @@ -101,7 +101,7 @@ export default class Analytics { } this.analytics.track("Feed Viewed", props, genOptions(context)); } - photoViewed(props, context) { + photoViewed(props = {}, context) { var validate = function( data, dataPath, @@ -184,7 +184,7 @@ export default class Analytics { } this.analytics.track("Photo Viewed", props, genOptions(context)); } - profileViewed(props, context) { + profileViewed(props = {}, context) { var validate = function( data, dataPath, diff --git a/examples/gen-js/node/analytics/generated/index.d.ts b/examples/gen-js/node/analytics/generated/index.d.ts index 342d70b1..a69ec270 100644 --- a/examples/gen-js/node/analytics/generated/index.d.ts +++ b/examples/gen-js/node/analytics/generated/index.d.ts @@ -168,7 +168,7 @@ export default class Analytics { constructor(analytics: any); orderCompleted( - message: TrackMessage, + message?: TrackMessage, callback?: AnalyticsNodeCallback ): void; } diff --git a/examples/gen-js/node/analytics/generated/index.js b/examples/gen-js/node/analytics/generated/index.js index 0198cbca..4a6cf989 100644 --- a/examples/gen-js/node/analytics/generated/index.js +++ b/examples/gen-js/node/analytics/generated/index.js @@ -19,7 +19,7 @@ class Analytics { } this.analytics = analytics || { track: () => null }; } - orderCompleted(message, callback) { + orderCompleted(message = {}, callback) { var validate = function( data, dataPath, diff --git a/examples/gen-js/ts/analytics/generated/index.d.ts b/examples/gen-js/ts/analytics/generated/index.d.ts index 766921bf..09b3b528 100644 --- a/examples/gen-js/ts/analytics/generated/index.d.ts +++ b/examples/gen-js/ts/analytics/generated/index.d.ts @@ -44,19 +44,19 @@ export default class Analytics { constructor(analytics: any); feedViewed( - message: FeedViewed, + message?: FeedViewed, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; photoViewed( - message: PhotoViewed, + message?: PhotoViewed, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; profileViewed( - message: ProfileViewed, + message?: ProfileViewed, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; diff --git a/examples/gen-js/ts/analytics/generated/index.js b/examples/gen-js/ts/analytics/generated/index.js index 2b9dba8c..e2d97278 100644 --- a/examples/gen-js/ts/analytics/generated/index.js +++ b/examples/gen-js/ts/analytics/generated/index.js @@ -18,7 +18,7 @@ export default class Analytics { } this.analytics = analytics || { track: () => null }; } - feedViewed(props, context) { + feedViewed(props = {}, context) { var validate = function( data, dataPath, @@ -101,7 +101,7 @@ export default class Analytics { } this.analytics.track("Feed Viewed", props, genOptions(context)); } - photoViewed(props, context) { + photoViewed(props = {}, context) { var validate = function( data, dataPath, @@ -184,7 +184,7 @@ export default class Analytics { } this.analytics.track("Photo Viewed", props, genOptions(context)); } - profileViewed(props, context) { + profileViewed(props = {}, context) { var validate = function( data, dataPath, diff --git a/src/commands/gen-js/library.ts b/src/commands/gen-js/library.ts index a7ed2825..7b6176d2 100644 --- a/src/commands/gen-js/library.ts +++ b/src/commands/gen-js/library.ts @@ -61,11 +61,11 @@ export function genJS( let trackCall = '' let validateCall = '' if (client === Client.js) { - parameters = 'props, context' + parameters = 'props = {}, context' trackCall = `this.analytics.track('${name}', props, genOptions(context))` validateCall = 'validate({ properties: props })' } else if (client === Client.node) { - parameters = 'message, callback' + parameters = 'message = {}, callback' trackCall = ` message = { ...message, diff --git a/src/commands/gen-js/typescript.ts b/src/commands/gen-js/typescript.ts index 01707372..85ee319c 100644 --- a/src/commands/gen-js/typescript.ts +++ b/src/commands/gen-js/typescript.ts @@ -217,14 +217,14 @@ class AJSTSDeclarationsRenderer extends TypeScriptRenderer { if (this.ajsOptions.client === Client.js) { this.emitLine([ camelCaseName, - '(message: ', + '(props?: ', this.sourceFor(t).source, ', options?: SegmentOptions, callback?: AnalyticsJSCallback): void' ]) } else if (this.ajsOptions.client === Client.node) { this.emitLine([ camelCaseName, - '(message: TrackMessage<', + '(message?: TrackMessage<', this.sourceFor(t).source, '>, callback?: AnalyticsNodeCallback): void' ]) diff --git a/tests/commands/gen-js/__snapshots__/index.amd.js b/tests/commands/gen-js/__snapshots__/index.amd.js index eef8dcd0..90cd55c7 100644 --- a/tests/commands/gen-js/__snapshots__/index.amd.js +++ b/tests/commands/gen-js/__snapshots__/index.amd.js @@ -21,7 +21,7 @@ define(["require", "exports"], function(require, exports) { } this.analytics = analytics || { track: () => null }; } - terribleEventName3(props, context) { + terribleEventName3(props = {}, context) { var validate = function( data, dataPath, @@ -79,7 +79,7 @@ define(["require", "exports"], function(require, exports) { genOptions(context) ); } - emptyEvent(props, context) { + emptyEvent(props = {}, context) { var validate = function( data, dataPath, @@ -142,7 +142,7 @@ define(["require", "exports"], function(require, exports) { } this.analytics.track("Empty Event", props, genOptions(context)); } - exampleEvent(props, context) { + exampleEvent(props = {}, context) { var pattern0 = new RegExp("FOO|BAR"); var validate = function( data, @@ -1020,7 +1020,7 @@ define(["require", "exports"], function(require, exports) { } this.analytics.track("Example Event", props, genOptions(context)); } - draft04Event(props, context) { + draft04Event(props = {}, context) { var validate = function( data, dataPath, @@ -1083,7 +1083,7 @@ define(["require", "exports"], function(require, exports) { } this.analytics.track("Draft-04 Event", props, genOptions(context)); } - draft06Event(props, context) { + draft06Event(props = {}, context) { var validate = function( data, dataPath, diff --git a/tests/commands/gen-js/__snapshots__/index.d.ts b/tests/commands/gen-js/__snapshots__/index.d.ts index 49c275f3..ae65f1db 100644 --- a/tests/commands/gen-js/__snapshots__/index.d.ts +++ b/tests/commands/gen-js/__snapshots__/index.d.ts @@ -167,7 +167,7 @@ export default class Analytics { constructor(analytics: any); the42TerribleEventName3( - message: The42_TerribleEventName3, + props?: The42_TerribleEventName3, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; @@ -178,13 +178,13 @@ export default class Analytics { * Required object (empty) property */ emptyEvent( - message: { [key: string]: any }, + props?: { [key: string]: any }, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; exampleEvent( - message: ExampleEvent, + props?: ExampleEvent, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; @@ -195,7 +195,7 @@ export default class Analytics { * Required object (empty) property */ draft04Event( - message: { [key: string]: any }, + props?: { [key: string]: any }, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; @@ -206,7 +206,7 @@ export default class Analytics { * Required object (empty) property */ draft06Event( - message: { [key: string]: any }, + props?: { [key: string]: any }, options?: SegmentOptions, callback?: AnalyticsJSCallback ): void; diff --git a/tests/commands/gen-js/__snapshots__/index.es5.js b/tests/commands/gen-js/__snapshots__/index.es5.js new file mode 100644 index 00000000..92d217dd --- /dev/null +++ b/tests/commands/gen-js/__snapshots__/index.es5.js @@ -0,0 +1,1149 @@ +"use strict"; +var __assign = + (this && this.__assign) || + function() { + __assign = + Object.assign || + function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) + if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); + }; +Object.defineProperty(exports, "__esModule", { value: true }); +var genOptions = function(context) { + if (context === void 0) { + context = {}; + } + return { + context: __assign({}, context, { + typewriter: { + name: "gen-js", + version: "5.1.4" + } + }) + }; +}; +var Analytics = /** @class */ (function() { + /** + * Instantiate a wrapper around an analytics library instance + * @param {Analytics} analytics - The analytics.js library to wrap + */ + function Analytics(analytics) { + if (!analytics) { + throw new Error("An instance of analytics.js must be provided"); + } + this.analytics = analytics || { + track: function() { + return null; + } + }; + } + Analytics.prototype.terribleEventName3 = function(props, context) { + if (props === void 0) { + props = {}; + } + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 !== undefined) { + var errs_1 = errors; + if (data1 && typeof data1 === "object" && !Array.isArray(data1)) { + var errs__1 = errors; + var valid2 = true; + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate({ properties: props })) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + this.analytics.track( + "42_--terrible==event++name~!3", + props, + genOptions(context) + ); + }; + Analytics.prototype.emptyEvent = function(props, context) { + if (props === void 0) { + props = {}; + } + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 === undefined) { + valid1 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + "", + schemaPath: "#/required", + params: { missingProperty: "properties" }, + message: "should have required property 'properties'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_1 = errors; + if (!data1 || typeof data1 !== "object" || Array.isArray(data1)) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate({ properties: props })) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + this.analytics.track("Empty Event", props, genOptions(context)); + }; + Analytics.prototype.exampleEvent = function(props, context) { + if (props === void 0) { + props = {}; + } + var pattern0 = new RegExp("FOO|BAR"); + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 === undefined) { + valid1 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + "", + schemaPath: "#/required", + params: { missingProperty: "properties" }, + message: "should have required property 'properties'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_1 = errors; + if (data1 && typeof data1 === "object" && !Array.isArray(data1)) { + if (data1["required any"] === undefined) { + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required any" }, + message: "should have required property 'required any'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var errs__1 = errors; + var valid2 = true; + var data2 = data1["optional array"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (Array.isArray(data2)) { + var errs__2 = errors; + var valid2; + for (var i2 = 0; i2 < data2.length; i2++) { + var data3 = data2[i2]; + var errs_3 = errors; + if ( + data3 && + typeof data3 === "object" && + !Array.isArray(data3) + ) { + var errs__3 = errors; + var valid4 = true; + if (data3["optional sub-property"] !== undefined) { + var errs_4 = errors; + if (typeof data3["optional sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional array'][" + + i2 + + "]['optional sub-property']", + schemaPath: + "#/properties/properties/properties/optional%20array/items/properties/optional%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid4 = errors === errs_4; + } + if (data3["required sub-property"] === undefined) { + valid4 = false; + var err = { + keyword: "required", + dataPath: + (dataPath || "") + + ".properties['optional array'][" + + i2 + + "]", + schemaPath: + "#/properties/properties/properties/optional%20array/items/required", + params: { missingProperty: "required sub-property" }, + message: + "should have required property 'required sub-property'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_4 = errors; + if (typeof data3["required sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional array'][" + + i2 + + "]['required sub-property']", + schemaPath: + "#/properties/properties/properties/optional%20array/items/properties/required%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid4 = errors === errs_4; + } + } else { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional array'][" + + i2 + + "]", + schemaPath: + "#/properties/properties/properties/optional%20array/items/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional array']", + schemaPath: + "#/properties/properties/properties/optional%20array/type", + params: { type: "array" }, + message: "should be array" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["optional array (empty)"] !== undefined) { + var errs_2 = errors; + if (!Array.isArray(data1["optional array (empty)"])) { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['optional array (empty)']", + schemaPath: + "#/properties/properties/properties/optional%20array%20(empty)/type", + params: { type: "array" }, + message: "should be array" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["optional boolean"] !== undefined) { + var errs_2 = errors; + if (typeof data1["optional boolean"] !== "boolean") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['optional boolean']", + schemaPath: + "#/properties/properties/properties/optional%20boolean/type", + params: { type: "boolean" }, + message: "should be boolean" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["optional int"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (typeof data2 !== "number" || data2 % 1 || data2 !== data2) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional int']", + schemaPath: + "#/properties/properties/properties/optional%20int/type", + params: { type: "integer" }, + message: "should be integer" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["optional number"] !== undefined) { + var errs_2 = errors; + if (typeof data1["optional number"] !== "number") { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional number']", + schemaPath: + "#/properties/properties/properties/optional%20number/type", + params: { type: "number" }, + message: "should be number" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["optional object"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (data2 && typeof data2 === "object" && !Array.isArray(data2)) { + var errs__2 = errors; + var valid3 = true; + if (data2["optional sub-property"] !== undefined) { + var errs_3 = errors; + if (typeof data2["optional sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional object']['optional sub-property']", + schemaPath: + "#/properties/properties/properties/optional%20object/properties/optional%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + if (data2["required sub-property"] === undefined) { + valid3 = false; + var err = { + keyword: "required", + dataPath: + (dataPath || "") + ".properties['optional object']", + schemaPath: + "#/properties/properties/properties/optional%20object/required", + params: { missingProperty: "required sub-property" }, + message: + "should have required property 'required sub-property'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_3 = errors; + if (typeof data2["required sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional object']['required sub-property']", + schemaPath: + "#/properties/properties/properties/optional%20object/properties/required%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional object']", + schemaPath: + "#/properties/properties/properties/optional%20object/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["optional object (empty)"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (!data2 || typeof data2 !== "object" || Array.isArray(data2)) { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['optional object (empty)']", + schemaPath: + "#/properties/properties/properties/optional%20object%20(empty)/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["optional string"] !== undefined) { + var errs_2 = errors; + if (typeof data1["optional string"] !== "string") { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional string']", + schemaPath: + "#/properties/properties/properties/optional%20string/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["optional string regex"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (typeof data2 === "string") { + if (!pattern0.test(data2)) { + var err = { + keyword: "pattern", + dataPath: + (dataPath || "") + ".properties['optional string regex']", + schemaPath: + "#/properties/properties/properties/optional%20string%20regex/pattern", + params: { pattern: "FOO|BAR" }, + message: 'should match pattern "FOO|BAR"' + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + } else { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['optional string regex']", + schemaPath: + "#/properties/properties/properties/optional%20string%20regex/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required array"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required array" }, + message: "should have required property 'required array'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (Array.isArray(data2)) { + var errs__2 = errors; + var valid2; + for (var i2 = 0; i2 < data2.length; i2++) { + var data3 = data2[i2]; + var errs_3 = errors; + if ( + data3 && + typeof data3 === "object" && + !Array.isArray(data3) + ) { + var errs__3 = errors; + var valid4 = true; + if (data3["optional sub-property"] !== undefined) { + var errs_4 = errors; + if (typeof data3["optional sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required array'][" + + i2 + + "]['optional sub-property']", + schemaPath: + "#/properties/properties/properties/required%20array/items/properties/optional%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid4 = errors === errs_4; + } + if (data3["required sub-property"] === undefined) { + valid4 = false; + var err = { + keyword: "required", + dataPath: + (dataPath || "") + + ".properties['required array'][" + + i2 + + "]", + schemaPath: + "#/properties/properties/properties/required%20array/items/required", + params: { missingProperty: "required sub-property" }, + message: + "should have required property 'required sub-property'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_4 = errors; + if (typeof data3["required sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required array'][" + + i2 + + "]['required sub-property']", + schemaPath: + "#/properties/properties/properties/required%20array/items/properties/required%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid4 = errors === errs_4; + } + } else { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required array'][" + + i2 + + "]", + schemaPath: + "#/properties/properties/properties/required%20array/items/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required array']", + schemaPath: + "#/properties/properties/properties/required%20array/type", + params: { type: "array" }, + message: "should be array" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["required array (empty)"] === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required array (empty)" }, + message: + "should have required property 'required array (empty)'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (!Array.isArray(data1["required array (empty)"])) { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['required array (empty)']", + schemaPath: + "#/properties/properties/properties/required%20array%20(empty)/type", + params: { type: "array" }, + message: "should be array" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["required boolean"] === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required boolean" }, + message: "should have required property 'required boolean'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data1["required boolean"] !== "boolean") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['required boolean']", + schemaPath: + "#/properties/properties/properties/required%20boolean/type", + params: { type: "boolean" }, + message: "should be boolean" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required int"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required int" }, + message: "should have required property 'required int'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data2 !== "number" || data2 % 1 || data2 !== data2) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required int']", + schemaPath: + "#/properties/properties/properties/required%20int/type", + params: { type: "integer" }, + message: "should be integer" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["required number"] === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required number" }, + message: "should have required property 'required number'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data1["required number"] !== "number") { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required number']", + schemaPath: + "#/properties/properties/properties/required%20number/type", + params: { type: "number" }, + message: "should be number" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required object"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required object" }, + message: "should have required property 'required object'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (data2 && typeof data2 === "object" && !Array.isArray(data2)) { + var errs__2 = errors; + var valid3 = true; + if (data2["optional sub-property"] !== undefined) { + var errs_3 = errors; + if (typeof data2["optional sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required object']['optional sub-property']", + schemaPath: + "#/properties/properties/properties/required%20object/properties/optional%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + if (data2["required sub-property"] === undefined) { + valid3 = false; + var err = { + keyword: "required", + dataPath: + (dataPath || "") + ".properties['required object']", + schemaPath: + "#/properties/properties/properties/required%20object/required", + params: { missingProperty: "required sub-property" }, + message: + "should have required property 'required sub-property'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_3 = errors; + if (typeof data2["required sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required object']['required sub-property']", + schemaPath: + "#/properties/properties/properties/required%20object/properties/required%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required object']", + schemaPath: + "#/properties/properties/properties/required%20object/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required object (empty)"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required object (empty)" }, + message: + "should have required property 'required object (empty)'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (!data2 || typeof data2 !== "object" || Array.isArray(data2)) { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['required object (empty)']", + schemaPath: + "#/properties/properties/properties/required%20object%20(empty)/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["required string"] === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required string" }, + message: "should have required property 'required string'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data1["required string"] !== "string") { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required string']", + schemaPath: + "#/properties/properties/properties/required%20string/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required string regex"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required string regex" }, + message: "should have required property 'required string regex'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data2 === "string") { + if (!pattern0.test(data2)) { + var err = { + keyword: "pattern", + dataPath: + (dataPath || "") + ".properties['required string regex']", + schemaPath: + "#/properties/properties/properties/required%20string%20regex/pattern", + params: { pattern: "FOO|BAR" }, + message: 'should match pattern "FOO|BAR"' + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + } else { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['required string regex']", + schemaPath: + "#/properties/properties/properties/required%20string%20regex/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate({ properties: props })) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + this.analytics.track("Example Event", props, genOptions(context)); + }; + Analytics.prototype.draft04Event = function(props, context) { + if (props === void 0) { + props = {}; + } + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 === undefined) { + valid1 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + "", + schemaPath: "#/required", + params: { missingProperty: "properties" }, + message: "should have required property 'properties'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_1 = errors; + if (!data1 || typeof data1 !== "object" || Array.isArray(data1)) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate({ properties: props })) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + this.analytics.track("Draft-04 Event", props, genOptions(context)); + }; + Analytics.prototype.draft06Event = function(props, context) { + if (props === void 0) { + props = {}; + } + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 === undefined) { + valid1 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + "", + schemaPath: "#/required", + params: { missingProperty: "properties" }, + message: "should have required property 'properties'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_1 = errors; + if (!data1 || typeof data1 !== "object" || Array.isArray(data1)) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate({ properties: props })) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + this.analytics.track("Draft-06 Event", props, genOptions(context)); + }; + return Analytics; +})(); +exports.default = Analytics; diff --git a/tests/commands/gen-js/__snapshots__/index.es5.node.js b/tests/commands/gen-js/__snapshots__/index.es5.node.js new file mode 100644 index 00000000..cbb2e7b2 --- /dev/null +++ b/tests/commands/gen-js/__snapshots__/index.es5.node.js @@ -0,0 +1,1160 @@ +"use strict"; +var __assign = + (this && this.__assign) || + function() { + __assign = + Object.assign || + function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) + if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; + }; + return __assign.apply(this, arguments); + }; +Object.defineProperty(exports, "__esModule", { value: true }); +var genOptions = function(context) { + if (context === void 0) { + context = {}; + } + return { + context: __assign({}, context, { + typewriter: { + name: "gen-js", + version: "5.1.4" + } + }) + }; +}; +var Analytics = /** @class */ (function() { + /** + * Instantiate a wrapper around an analytics library instance + * @param {Analytics} analytics - The analytics-node library to wrap + */ + function Analytics(analytics) { + if (!analytics) { + throw new Error("An instance of analytics-node must be provided"); + } + this.analytics = analytics || { + track: function() { + return null; + } + }; + } + Analytics.prototype.terribleEventName3 = function(message, callback) { + if (message === void 0) { + message = {}; + } + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 !== undefined) { + var errs_1 = errors; + if (data1 && typeof data1 === "object" && !Array.isArray(data1)) { + var errs__1 = errors; + var valid2 = true; + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate(message)) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + message = __assign({}, message, genOptions(message.context), { + event: "42_--terrible==event++name~!3" + }); + this.analytics.track(message, callback); + }; + Analytics.prototype.emptyEvent = function(message, callback) { + if (message === void 0) { + message = {}; + } + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 === undefined) { + valid1 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + "", + schemaPath: "#/required", + params: { missingProperty: "properties" }, + message: "should have required property 'properties'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_1 = errors; + if (!data1 || typeof data1 !== "object" || Array.isArray(data1)) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate(message)) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + message = __assign({}, message, genOptions(message.context), { + event: "Empty Event" + }); + this.analytics.track(message, callback); + }; + Analytics.prototype.exampleEvent = function(message, callback) { + if (message === void 0) { + message = {}; + } + var pattern0 = new RegExp("FOO|BAR"); + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 === undefined) { + valid1 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + "", + schemaPath: "#/required", + params: { missingProperty: "properties" }, + message: "should have required property 'properties'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_1 = errors; + if (data1 && typeof data1 === "object" && !Array.isArray(data1)) { + if (data1["required any"] === undefined) { + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required any" }, + message: "should have required property 'required any'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var errs__1 = errors; + var valid2 = true; + var data2 = data1["optional array"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (Array.isArray(data2)) { + var errs__2 = errors; + var valid2; + for (var i2 = 0; i2 < data2.length; i2++) { + var data3 = data2[i2]; + var errs_3 = errors; + if ( + data3 && + typeof data3 === "object" && + !Array.isArray(data3) + ) { + var errs__3 = errors; + var valid4 = true; + if (data3["optional sub-property"] !== undefined) { + var errs_4 = errors; + if (typeof data3["optional sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional array'][" + + i2 + + "]['optional sub-property']", + schemaPath: + "#/properties/properties/properties/optional%20array/items/properties/optional%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid4 = errors === errs_4; + } + if (data3["required sub-property"] === undefined) { + valid4 = false; + var err = { + keyword: "required", + dataPath: + (dataPath || "") + + ".properties['optional array'][" + + i2 + + "]", + schemaPath: + "#/properties/properties/properties/optional%20array/items/required", + params: { missingProperty: "required sub-property" }, + message: + "should have required property 'required sub-property'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_4 = errors; + if (typeof data3["required sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional array'][" + + i2 + + "]['required sub-property']", + schemaPath: + "#/properties/properties/properties/optional%20array/items/properties/required%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid4 = errors === errs_4; + } + } else { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional array'][" + + i2 + + "]", + schemaPath: + "#/properties/properties/properties/optional%20array/items/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional array']", + schemaPath: + "#/properties/properties/properties/optional%20array/type", + params: { type: "array" }, + message: "should be array" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["optional array (empty)"] !== undefined) { + var errs_2 = errors; + if (!Array.isArray(data1["optional array (empty)"])) { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['optional array (empty)']", + schemaPath: + "#/properties/properties/properties/optional%20array%20(empty)/type", + params: { type: "array" }, + message: "should be array" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["optional boolean"] !== undefined) { + var errs_2 = errors; + if (typeof data1["optional boolean"] !== "boolean") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['optional boolean']", + schemaPath: + "#/properties/properties/properties/optional%20boolean/type", + params: { type: "boolean" }, + message: "should be boolean" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["optional int"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (typeof data2 !== "number" || data2 % 1 || data2 !== data2) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional int']", + schemaPath: + "#/properties/properties/properties/optional%20int/type", + params: { type: "integer" }, + message: "should be integer" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["optional number"] !== undefined) { + var errs_2 = errors; + if (typeof data1["optional number"] !== "number") { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional number']", + schemaPath: + "#/properties/properties/properties/optional%20number/type", + params: { type: "number" }, + message: "should be number" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["optional object"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (data2 && typeof data2 === "object" && !Array.isArray(data2)) { + var errs__2 = errors; + var valid3 = true; + if (data2["optional sub-property"] !== undefined) { + var errs_3 = errors; + if (typeof data2["optional sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional object']['optional sub-property']", + schemaPath: + "#/properties/properties/properties/optional%20object/properties/optional%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + if (data2["required sub-property"] === undefined) { + valid3 = false; + var err = { + keyword: "required", + dataPath: + (dataPath || "") + ".properties['optional object']", + schemaPath: + "#/properties/properties/properties/optional%20object/required", + params: { missingProperty: "required sub-property" }, + message: + "should have required property 'required sub-property'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_3 = errors; + if (typeof data2["required sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['optional object']['required sub-property']", + schemaPath: + "#/properties/properties/properties/optional%20object/properties/required%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional object']", + schemaPath: + "#/properties/properties/properties/optional%20object/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["optional object (empty)"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (!data2 || typeof data2 !== "object" || Array.isArray(data2)) { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['optional object (empty)']", + schemaPath: + "#/properties/properties/properties/optional%20object%20(empty)/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["optional string"] !== undefined) { + var errs_2 = errors; + if (typeof data1["optional string"] !== "string") { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['optional string']", + schemaPath: + "#/properties/properties/properties/optional%20string/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["optional string regex"]; + if (data2 !== undefined) { + var errs_2 = errors; + if (typeof data2 === "string") { + if (!pattern0.test(data2)) { + var err = { + keyword: "pattern", + dataPath: + (dataPath || "") + ".properties['optional string regex']", + schemaPath: + "#/properties/properties/properties/optional%20string%20regex/pattern", + params: { pattern: "FOO|BAR" }, + message: 'should match pattern "FOO|BAR"' + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + } else { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['optional string regex']", + schemaPath: + "#/properties/properties/properties/optional%20string%20regex/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required array"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required array" }, + message: "should have required property 'required array'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (Array.isArray(data2)) { + var errs__2 = errors; + var valid2; + for (var i2 = 0; i2 < data2.length; i2++) { + var data3 = data2[i2]; + var errs_3 = errors; + if ( + data3 && + typeof data3 === "object" && + !Array.isArray(data3) + ) { + var errs__3 = errors; + var valid4 = true; + if (data3["optional sub-property"] !== undefined) { + var errs_4 = errors; + if (typeof data3["optional sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required array'][" + + i2 + + "]['optional sub-property']", + schemaPath: + "#/properties/properties/properties/required%20array/items/properties/optional%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid4 = errors === errs_4; + } + if (data3["required sub-property"] === undefined) { + valid4 = false; + var err = { + keyword: "required", + dataPath: + (dataPath || "") + + ".properties['required array'][" + + i2 + + "]", + schemaPath: + "#/properties/properties/properties/required%20array/items/required", + params: { missingProperty: "required sub-property" }, + message: + "should have required property 'required sub-property'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_4 = errors; + if (typeof data3["required sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required array'][" + + i2 + + "]['required sub-property']", + schemaPath: + "#/properties/properties/properties/required%20array/items/properties/required%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid4 = errors === errs_4; + } + } else { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required array'][" + + i2 + + "]", + schemaPath: + "#/properties/properties/properties/required%20array/items/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required array']", + schemaPath: + "#/properties/properties/properties/required%20array/type", + params: { type: "array" }, + message: "should be array" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["required array (empty)"] === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required array (empty)" }, + message: + "should have required property 'required array (empty)'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (!Array.isArray(data1["required array (empty)"])) { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['required array (empty)']", + schemaPath: + "#/properties/properties/properties/required%20array%20(empty)/type", + params: { type: "array" }, + message: "should be array" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["required boolean"] === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required boolean" }, + message: "should have required property 'required boolean'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data1["required boolean"] !== "boolean") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['required boolean']", + schemaPath: + "#/properties/properties/properties/required%20boolean/type", + params: { type: "boolean" }, + message: "should be boolean" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required int"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required int" }, + message: "should have required property 'required int'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data2 !== "number" || data2 % 1 || data2 !== data2) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required int']", + schemaPath: + "#/properties/properties/properties/required%20int/type", + params: { type: "integer" }, + message: "should be integer" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["required number"] === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required number" }, + message: "should have required property 'required number'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data1["required number"] !== "number") { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required number']", + schemaPath: + "#/properties/properties/properties/required%20number/type", + params: { type: "number" }, + message: "should be number" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required object"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required object" }, + message: "should have required property 'required object'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (data2 && typeof data2 === "object" && !Array.isArray(data2)) { + var errs__2 = errors; + var valid3 = true; + if (data2["optional sub-property"] !== undefined) { + var errs_3 = errors; + if (typeof data2["optional sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required object']['optional sub-property']", + schemaPath: + "#/properties/properties/properties/required%20object/properties/optional%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + if (data2["required sub-property"] === undefined) { + valid3 = false; + var err = { + keyword: "required", + dataPath: + (dataPath || "") + ".properties['required object']", + schemaPath: + "#/properties/properties/properties/required%20object/required", + params: { missingProperty: "required sub-property" }, + message: + "should have required property 'required sub-property'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_3 = errors; + if (typeof data2["required sub-property"] !== "string") { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + + ".properties['required object']['required sub-property']", + schemaPath: + "#/properties/properties/properties/required%20object/properties/required%20sub-property/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid3 = errors === errs_3; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required object']", + schemaPath: + "#/properties/properties/properties/required%20object/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required object (empty)"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required object (empty)" }, + message: + "should have required property 'required object (empty)'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (!data2 || typeof data2 !== "object" || Array.isArray(data2)) { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['required object (empty)']", + schemaPath: + "#/properties/properties/properties/required%20object%20(empty)/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + if (data1["required string"] === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required string" }, + message: "should have required property 'required string'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data1["required string"] !== "string") { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties['required string']", + schemaPath: + "#/properties/properties/properties/required%20string/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + var data2 = data1["required string regex"]; + if (data2 === undefined) { + valid2 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/required", + params: { missingProperty: "required string regex" }, + message: "should have required property 'required string regex'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_2 = errors; + if (typeof data2 === "string") { + if (!pattern0.test(data2)) { + var err = { + keyword: "pattern", + dataPath: + (dataPath || "") + ".properties['required string regex']", + schemaPath: + "#/properties/properties/properties/required%20string%20regex/pattern", + params: { pattern: "FOO|BAR" }, + message: 'should match pattern "FOO|BAR"' + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + } else { + var err = { + keyword: "type", + dataPath: + (dataPath || "") + ".properties['required string regex']", + schemaPath: + "#/properties/properties/properties/required%20string%20regex/type", + params: { type: "string" }, + message: "should be string" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid2 = errors === errs_2; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate(message)) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + message = __assign({}, message, genOptions(message.context), { + event: "Example Event" + }); + this.analytics.track(message, callback); + }; + Analytics.prototype.draft04Event = function(message, callback) { + if (message === void 0) { + message = {}; + } + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 === undefined) { + valid1 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + "", + schemaPath: "#/required", + params: { missingProperty: "properties" }, + message: "should have required property 'properties'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_1 = errors; + if (!data1 || typeof data1 !== "object" || Array.isArray(data1)) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate(message)) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + message = __assign({}, message, genOptions(message.context), { + event: "Draft-04 Event" + }); + this.analytics.track(message, callback); + }; + Analytics.prototype.draft06Event = function(message, callback) { + if (message === void 0) { + message = {}; + } + var validate = function( + data, + dataPath, + parentData, + parentDataProperty, + rootData + ) { + "use strict"; + var vErrors = null; + var errors = 0; + if (data && typeof data === "object" && !Array.isArray(data)) { + var errs__0 = errors; + var valid1 = true; + var data1 = data.properties; + if (data1 === undefined) { + valid1 = false; + var err = { + keyword: "required", + dataPath: (dataPath || "") + "", + schemaPath: "#/required", + params: { missingProperty: "properties" }, + message: "should have required property 'properties'" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } else { + var errs_1 = errors; + if (!data1 || typeof data1 !== "object" || Array.isArray(data1)) { + var err = { + keyword: "type", + dataPath: (dataPath || "") + ".properties", + schemaPath: "#/properties/properties/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + var valid1 = errors === errs_1; + } + } else { + var err = { + keyword: "type", + dataPath: (dataPath || "") + "", + schemaPath: "#/type", + params: { type: "object" }, + message: "should be object" + }; + if (vErrors === null) vErrors = [err]; + else vErrors.push(err); + errors++; + } + validate.errors = vErrors; + return errors === 0; + }; + if (!validate(message)) { + throw new Error(JSON.stringify(validate.errors, null, 2)); + } + message = __assign({}, message, genOptions(message.context), { + event: "Draft-06 Event" + }); + this.analytics.track(message, callback); + }; + return Analytics; +})(); +exports.default = Analytics; diff --git a/tests/commands/gen-js/__snapshots__/index.js b/tests/commands/gen-js/__snapshots__/index.js index 09d68e72..b33e8846 100644 --- a/tests/commands/gen-js/__snapshots__/index.js +++ b/tests/commands/gen-js/__snapshots__/index.js @@ -18,7 +18,7 @@ export default class Analytics { } this.analytics = analytics || { track: () => null }; } - terribleEventName3(props, context) { + terribleEventName3(props = {}, context) { var validate = function( data, dataPath, @@ -76,7 +76,7 @@ export default class Analytics { genOptions(context) ); } - emptyEvent(props, context) { + emptyEvent(props = {}, context) { var validate = function( data, dataPath, @@ -139,7 +139,7 @@ export default class Analytics { } this.analytics.track("Empty Event", props, genOptions(context)); } - exampleEvent(props, context) { + exampleEvent(props = {}, context) { var pattern0 = new RegExp("FOO|BAR"); var validate = function( data, @@ -978,7 +978,7 @@ export default class Analytics { } this.analytics.track("Example Event", props, genOptions(context)); } - draft04Event(props, context) { + draft04Event(props = {}, context) { var validate = function( data, dataPath, @@ -1041,7 +1041,7 @@ export default class Analytics { } this.analytics.track("Draft-04 Event", props, genOptions(context)); } - draft06Event(props, context) { + draft06Event(props = {}, context) { var validate = function( data, dataPath, diff --git a/tests/commands/gen-js/__snapshots__/index.node.d.ts b/tests/commands/gen-js/__snapshots__/index.node.d.ts index 38a341c3..a1900d48 100644 --- a/tests/commands/gen-js/__snapshots__/index.node.d.ts +++ b/tests/commands/gen-js/__snapshots__/index.node.d.ts @@ -211,7 +211,7 @@ export default class Analytics { constructor(analytics: any); the42TerribleEventName3( - message: TrackMessage, + message?: TrackMessage, callback?: AnalyticsNodeCallback ): void; @@ -221,12 +221,12 @@ export default class Analytics { * Required object (empty) property */ emptyEvent( - message: TrackMessage<{ [key: string]: any }>, + message?: TrackMessage<{ [key: string]: any }>, callback?: AnalyticsNodeCallback ): void; exampleEvent( - message: TrackMessage, + message?: TrackMessage, callback?: AnalyticsNodeCallback ): void; @@ -236,7 +236,7 @@ export default class Analytics { * Required object (empty) property */ draft04Event( - message: TrackMessage<{ [key: string]: any }>, + message?: TrackMessage<{ [key: string]: any }>, callback?: AnalyticsNodeCallback ): void; @@ -246,7 +246,7 @@ export default class Analytics { * Required object (empty) property */ draft06Event( - message: TrackMessage<{ [key: string]: any }>, + message?: TrackMessage<{ [key: string]: any }>, callback?: AnalyticsNodeCallback ): void; } diff --git a/tests/commands/gen-js/__snapshots__/index.node.js b/tests/commands/gen-js/__snapshots__/index.node.js index d7ac13b0..fa04d4be 100644 --- a/tests/commands/gen-js/__snapshots__/index.node.js +++ b/tests/commands/gen-js/__snapshots__/index.node.js @@ -19,7 +19,7 @@ class Analytics { } this.analytics = analytics || { track: () => null }; } - terribleEventName3(message, callback) { + terribleEventName3(message = {}, callback) { var validate = function( data, dataPath, @@ -76,7 +76,7 @@ class Analytics { }); this.analytics.track(message, callback); } - emptyEvent(message, callback) { + emptyEvent(message = {}, callback) { var validate = function( data, dataPath, @@ -142,7 +142,7 @@ class Analytics { }); this.analytics.track(message, callback); } - exampleEvent(message, callback) { + exampleEvent(message = {}, callback) { var pattern0 = new RegExp("FOO|BAR"); var validate = function( data, @@ -984,7 +984,7 @@ class Analytics { }); this.analytics.track(message, callback); } - draft04Event(message, callback) { + draft04Event(message = {}, callback) { var validate = function( data, dataPath, @@ -1050,7 +1050,7 @@ class Analytics { }); this.analytics.track(message, callback); } - draft06Event(message, callback) { + draft06Event(message = {}, callback) { var validate = function( data, dataPath, diff --git a/tests/commands/gen-js/__snapshots__/index.prod.js b/tests/commands/gen-js/__snapshots__/index.prod.js index e2df56f9..2356029a 100644 --- a/tests/commands/gen-js/__snapshots__/index.prod.js +++ b/tests/commands/gen-js/__snapshots__/index.prod.js @@ -15,23 +15,23 @@ export default class Analytics { constructor(analytics) { this.analytics = analytics || { track: () => null }; } - terribleEventName3(props, context) { + terribleEventName3(props = {}, context) { this.analytics.track( "42_--terrible==event++name~!3", props, genOptions(context) ); } - emptyEvent(props, context) { + emptyEvent(props = {}, context) { this.analytics.track("Empty Event", props, genOptions(context)); } - exampleEvent(props, context) { + exampleEvent(props = {}, context) { this.analytics.track("Example Event", props, genOptions(context)); } - draft04Event(props, context) { + draft04Event(props = {}, context) { this.analytics.track("Draft-04 Event", props, genOptions(context)); } - draft06Event(props, context) { + draft06Event(props = {}, context) { this.analytics.track("Draft-06 Event", props, genOptions(context)); } } diff --git a/tests/commands/gen-js/__snapshots__/index.prod.node.js b/tests/commands/gen-js/__snapshots__/index.prod.node.js index c26d3cb2..2333f26b 100644 --- a/tests/commands/gen-js/__snapshots__/index.prod.node.js +++ b/tests/commands/gen-js/__snapshots__/index.prod.node.js @@ -16,31 +16,31 @@ class Analytics { constructor(analytics) { this.analytics = analytics || { track: () => null }; } - terribleEventName3(message, callback) { + terribleEventName3(message = {}, callback) { message = Object.assign({}, message, genOptions(message.context), { event: "42_--terrible==event++name~!3" }); this.analytics.track(message, callback); } - emptyEvent(message, callback) { + emptyEvent(message = {}, callback) { message = Object.assign({}, message, genOptions(message.context), { event: "Empty Event" }); this.analytics.track(message, callback); } - exampleEvent(message, callback) { + exampleEvent(message = {}, callback) { message = Object.assign({}, message, genOptions(message.context), { event: "Example Event" }); this.analytics.track(message, callback); } - draft04Event(message, callback) { + draft04Event(message = {}, callback) { message = Object.assign({}, message, genOptions(message.context), { event: "Draft-04 Event" }); this.analytics.track(message, callback); } - draft06Event(message, callback) { + draft06Event(message = {}, callback) { message = Object.assign({}, message, genOptions(message.context), { event: "Draft-06 Event" }); diff --git a/tests/commands/gen-js/__snapshots__/index.system.js b/tests/commands/gen-js/__snapshots__/index.system.js index 0b074b66..91f23620 100644 --- a/tests/commands/gen-js/__snapshots__/index.system.js +++ b/tests/commands/gen-js/__snapshots__/index.system.js @@ -25,7 +25,7 @@ System.register([], function(exports_1, context_1) { } this.analytics = analytics || { track: () => null }; } - terribleEventName3(props, context) { + terribleEventName3(props = {}, context) { var validate = function( data, dataPath, @@ -87,7 +87,7 @@ System.register([], function(exports_1, context_1) { genOptions(context) ); } - emptyEvent(props, context) { + emptyEvent(props = {}, context) { var validate = function( data, dataPath, @@ -154,7 +154,7 @@ System.register([], function(exports_1, context_1) { } this.analytics.track("Empty Event", props, genOptions(context)); } - exampleEvent(props, context) { + exampleEvent(props = {}, context) { var pattern0 = new RegExp("FOO|BAR"); var validate = function( data, @@ -1061,7 +1061,7 @@ System.register([], function(exports_1, context_1) { } this.analytics.track("Example Event", props, genOptions(context)); } - draft04Event(props, context) { + draft04Event(props = {}, context) { var validate = function( data, dataPath, @@ -1128,7 +1128,7 @@ System.register([], function(exports_1, context_1) { } this.analytics.track("Draft-04 Event", props, genOptions(context)); } - draft06Event(props, context) { + draft06Event(props = {}, context) { var validate = function( data, dataPath, diff --git a/tests/commands/gen-js/__snapshots__/index.umd.js b/tests/commands/gen-js/__snapshots__/index.umd.js index e0e4dc7f..9bb7b8a9 100644 --- a/tests/commands/gen-js/__snapshots__/index.umd.js +++ b/tests/commands/gen-js/__snapshots__/index.umd.js @@ -28,7 +28,7 @@ } this.analytics = analytics || { track: () => null }; } - terribleEventName3(props, context) { + terribleEventName3(props = {}, context) { var validate = function( data, dataPath, @@ -86,7 +86,7 @@ genOptions(context) ); } - emptyEvent(props, context) { + emptyEvent(props = {}, context) { var validate = function( data, dataPath, @@ -149,7 +149,7 @@ } this.analytics.track("Empty Event", props, genOptions(context)); } - exampleEvent(props, context) { + exampleEvent(props = {}, context) { var pattern0 = new RegExp("FOO|BAR"); var validate = function( data, @@ -1027,7 +1027,7 @@ } this.analytics.track("Example Event", props, genOptions(context)); } - draft04Event(props, context) { + draft04Event(props = {}, context) { var validate = function( data, dataPath, @@ -1090,7 +1090,7 @@ } this.analytics.track("Draft-04 Event", props, genOptions(context)); } - draft06Event(props, context) { + draft06Event(props = {}, context) { var validate = function( data, dataPath, diff --git a/tests/commands/gen-js/library.test.ts b/tests/commands/gen-js/library.test.ts index a3f5b13f..e697dd39 100644 --- a/tests/commands/gen-js/library.test.ts +++ b/tests/commands/gen-js/library.test.ts @@ -48,3 +48,17 @@ test('genJS - compiled output matches snapshot - no runtime validation (analytic 'index.prod.node.js' ) }) + +test('genJS - compiled output matches snapshot - ES5', async () => { + await testSnapshotSingleFile( + events => genJS(events, ScriptTarget.ES5, ModuleKind.CommonJS, Client.js), + 'index.es5.js' + ) +}) + +test('genJS - compiled output matches snapshot - ES5 (analytics-node)', async () => { + await testSnapshotSingleFile( + events => genJS(events, ScriptTarget.ES5, ModuleKind.CommonJS, Client.node), + 'index.es5.node.js' + ) +})