diff --git a/src/parser/switch.js b/src/parser/switch.js index d1774359f..a269b2496 100644 --- a/src/parser/switch.js +++ b/src/parser/switch.js @@ -42,9 +42,10 @@ module.exports = { } else { this.expect(["{", ":"]); } + this.next(); // OPTIONNAL ';' // https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L570 - if (this.next().token === ";") { + if (this.token === ";") { this.next(); } // EXTRACTING CASES @@ -66,20 +67,20 @@ module.exports = { read_case_list: function(stopToken) { const result = this.node("case"); let test = null; - let body = null; - const items = []; if (this.token === this.tok.T_CASE) { test = this.next().read_expr(); } else if (this.token === this.tok.T_DEFAULT) { - // the defaut entry - no condition + // the default entry - no condition this.next(); } else { this.expect([this.tok.T_CASE, this.tok.T_DEFAULT]); } + // case_separator this.expect([":", ";"]) && this.next(); - body = this.node("block"); + const body = this.node("block"); + const items = []; while ( - this.token != this.EOF && + this.token !== this.EOF && this.token !== stopToken && this.token !== this.tok.T_CASE && this.token !== this.tok.T_DEFAULT diff --git a/test/snapshot/__snapshots__/switch.test.js.snap b/test/snapshot/__snapshots__/switch.test.js.snap index 158a20a3d..1362d0fb6 100644 --- a/test/snapshot/__snapshots__/switch.test.js.snap +++ b/test/snapshot/__snapshots__/switch.test.js.snap @@ -1,6 +1,1946 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Test SWITCH statements parse correctly 1`] = ` +exports[`switch statements colon and ';' separator #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and ';' separator 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and multiple difference cases #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"2\\"", + "unicode": false, + "value": "2", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and multiple difference cases 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"2\\"", + "unicode": false, + "value": "2", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and multiple difference cases with default #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"2\\"", + "unicode": false, + "value": "2", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and multiple difference cases with default 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"2\\"", + "unicode": false, + "value": "2", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and multiple same cases #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and multiple same cases 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and multiple same cases with default #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and multiple same cases with default 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and one case #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements colon and one case 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and ';' separator #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and ';' separator 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and multiple difference cases #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"2\\"", + "unicode": false, + "value": "2", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and multiple difference cases 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"2\\"", + "unicode": false, + "value": "2", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and multiple difference cases with default #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"2\\"", + "unicode": false, + "value": "2", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and multiple difference cases with default 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"2\\"", + "unicode": false, + "value": "2", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and multiple same cases #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and multiple same cases 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and multiple same cases with default #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and multiple same cases with default 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + Case { + "body": null, + "kind": "case", + "test": Number { + "kind": "number", + "value": "2", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"3\\"", + "unicode": false, + "value": "3", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "3", + }, + }, + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"5\\"", + "unicode": false, + "value": "5", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": null, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and one case #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements curly and one case 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [ + Case { + "body": Block { + "children": Array [ + Return { + "expr": String { + "isDoubleQuote": true, + "kind": "string", + "raw": "\\"1\\"", + "unicode": false, + "value": "1", + }, + "kind": "return", + }, + ], + "kind": "block", + }, + "kind": "case", + "test": Number { + "kind": "number", + "value": "1", + }, + }, + ], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements empty case list #2 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements empty case list #3 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements empty case list #4 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [], + "kind": "block", + }, + "kind": "switch", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements empty case list 1`] = ` +Program { + "children": Array [ + Switch { + "body": Block { + "children": Array [], + "kind": "block", + }, + "kind": "switch", + "shortForm": false, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "errors": Array [], + "kind": "program", +} +`; + +exports[`switch statements parse correctly 1`] = ` Program { "children": Array [ Switch { @@ -193,7 +2133,7 @@ Program { } `; -exports[`Test SWITCH statements test errors 1`] = ` +exports[`switch statements test errors 1`] = ` Program { "children": Array [ Switch { diff --git a/test/snapshot/switch.test.js b/test/snapshot/switch.test.js index fd2691a0b..5514ace50 100644 --- a/test/snapshot/switch.test.js +++ b/test/snapshot/switch.test.js @@ -1,6 +1,188 @@ -const parser = require('../main'); +const parser = require("../main"); + +describe("switch statements", function() { + it("empty case list", function() { + var ast = parser.parseEval("switch (true) {}"); + expect(ast).toMatchSnapshot(); + }); + + it("empty case list #2", function() { + var ast = parser.parseEval("switch (true) {;}"); + expect(ast).toMatchSnapshot(); + }); + + it("empty case list #3", function() { + var ast = parser.parseEval("switch (true): endswitch;"); + expect(ast).toMatchSnapshot(); + }); + + it("empty case list #4", function() { + var ast = parser.parseEval("switch (true):; endswitch;"); + expect(ast).toMatchSnapshot(); + }); + + it("curly and one case", function() { + var ast = parser.parseEval('switch (true) { case 1: return "1"; }'); + expect(ast).toMatchSnapshot(); + }); + + it("curly and one case #2", function() { + var ast = parser.parseEval('switch (true) {; case 1: return "1"; }'); + expect(ast).toMatchSnapshot(); + }); + + it("colon and one case", function() { + var ast = parser.parseEval('switch (true): case 1: return "1"; endswitch;'); + expect(ast).toMatchSnapshot(); + }); + + it("colon and one case #2", function() { + var ast = parser.parseEval( + 'switch (true):; case 1: return "1"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and multiple difference cases", function() { + var ast = parser.parseEval( + 'switch (true) { case 1: return "1"; case 2: return "2"; case 3: return "3"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and multiple difference cases #2", function() { + var ast = parser.parseEval( + 'switch (true) {; case 1: return "1"; case 2: return "2"; case 3: return "3"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and multiple difference cases", function() { + var ast = parser.parseEval( + 'switch (true): case 1: return "1"; case 2: return "2"; case 3: return "3"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and multiple difference cases #2", function() { + var ast = parser.parseEval( + 'switch (true):; case 1: return "1"; case 2: return "2"; case 3: return "3"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and multiple same cases", function() { + var ast = parser.parseEval( + 'switch (true) { case 1: case 2: case 3: return "3"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and multiple same cases #2", function() { + var ast = parser.parseEval( + 'switch (true) {; case 1: case 2: case 3: return "3"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and multiple same cases", function() { + var ast = parser.parseEval( + 'switch (true): case 1: case 2: case 3: return "3"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and multiple same cases #2", function() { + var ast = parser.parseEval( + 'switch (true):; case 1: case 2: case 3: return "3"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and multiple difference cases with default", function() { + var ast = parser.parseEval( + 'switch (true) { case 1: return "1"; case 2: return "2"; case 3: return "3"; default: return "5"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and multiple difference cases with default #2", function() { + var ast = parser.parseEval( + 'switch (true) {; case 1: return "1"; case 2: return "2"; case 3: return "3"; default: return "5"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and multiple difference cases with default", function() { + var ast = parser.parseEval( + 'switch (true): case 1: return "1"; case 2: return "2"; case 3: return "3"; default: return "5"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and multiple difference cases with default #2", function() { + var ast = parser.parseEval( + 'switch (true):; case 1: return "1"; case 2: return "2"; case 3: return "3"; default: return "5"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and multiple same cases with default", function() { + var ast = parser.parseEval( + 'switch (true) { case 1: case 2: case 3: return "3"; default: return "5"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and multiple same cases with default #2", function() { + var ast = parser.parseEval( + 'switch (true) {; case 1: case 2: case 3: return "3"; default: return "5"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and multiple same cases with default", function() { + var ast = parser.parseEval( + 'switch (true): case 1: case 2: case 3: return "3"; default: return "5"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and multiple same cases with default #2", function() { + var ast = parser.parseEval( + 'switch (true):; case 1: case 2: case 3: return "3"; default: return "5"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and ';' separator", function() { + var ast = parser.parseEval( + 'switch (true) { case 1; case 2; case 3; return "3"; default; return "5"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("curly and ';' separator #2", function() { + var ast = parser.parseEval( + 'switch (true) {; case 1; case 2; case 3; return "3"; default; return "5"; }' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and ';' separator", function() { + var ast = parser.parseEval( + 'switch (true): case 1; case 2; case 3; return "3"; default; return "5"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); + + it("colon and ';' separator #2", function() { + var ast = parser.parseEval( + 'switch (true):; case 1; case 2; case 3; return "3"; default; return "5"; endswitch;' + ); + expect(ast).toMatchSnapshot(); + }); -describe("Test SWITCH statements", function() { it("parse correctly", function() { var ast = parser.parseEval( `