From 63dee902bd281c792f1dd760b6df268682032ed0 Mon Sep 17 00:00:00 2001 From: mck89 Date: Thu, 11 Jan 2024 14:36:12 +0000 Subject: [PATCH] Fixed bug when parsing sequence expressions using older ES versions. Fixes #65 --- doc/changelog.md | 1 + lib/Peast/Syntax/Parser.php | 7 +- .../ES2015/files/Misc/Sequence2.Render.txt | 5 + .../ES2015/files/Misc/Sequence2.Tokens.json | 130 +++++++++++++++ .../Syntax/ES2015/files/Misc/Sequence2.js | 1 + .../Syntax/ES2015/files/Misc/Sequence2.json | 150 ++++++++++++++++++ 6 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 test/Peast/Syntax/ES2015/files/Misc/Sequence2.Render.txt create mode 100644 test/Peast/Syntax/ES2015/files/Misc/Sequence2.Tokens.json create mode 100644 test/Peast/Syntax/ES2015/files/Misc/Sequence2.js create mode 100644 test/Peast/Syntax/ES2015/files/Misc/Sequence2.json diff --git a/doc/changelog.md b/doc/changelog.md index 09d3ad5..f58e39c 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -3,6 +3,7 @@ Changelog #### 1.16.0 * Implemented ES2024 parser, no new syntax features have been introduced +* Fixed bug when parsing sequence expressions using older ES versions #### 1.15.4 * Fixed rendering of `let` and `const` inside `switch` cases that always require brackets diff --git a/lib/Peast/Syntax/Parser.php b/lib/Peast/Syntax/Parser.php index 9444d25..b2bde15 100644 --- a/lib/Peast/Syntax/Parser.php +++ b/lib/Peast/Syntax/Parser.php @@ -1609,9 +1609,14 @@ protected function parseFormalParameterList() break; } } + //Check if it ends with a comma, then check if the comma is a trailing comma, + //in that case throw an error if the trailing comma feature is not enabled if ($hasComma && !$this->features->trailingCommaFunctionCallDeclaration) { - $this->error(); + $token = $this->scanner->getToken(); + if ($token && $token->value === ")") { + $this->error(); + } } return $list; } diff --git a/test/Peast/Syntax/ES2015/files/Misc/Sequence2.Render.txt b/test/Peast/Syntax/ES2015/files/Misc/Sequence2.Render.txt new file mode 100644 index 0000000..a349894 --- /dev/null +++ b/test/Peast/Syntax/ES2015/files/Misc/Sequence2.Render.txt @@ -0,0 +1,5 @@ +(a = 1, 2); +/**************************************************/ +(a=1,2); +/**************************************************/ +( a = 1, 2 ); \ No newline at end of file diff --git a/test/Peast/Syntax/ES2015/files/Misc/Sequence2.Tokens.json b/test/Peast/Syntax/ES2015/files/Misc/Sequence2.Tokens.json new file mode 100644 index 0000000..da94b83 --- /dev/null +++ b/test/Peast/Syntax/ES2015/files/Misc/Sequence2.Tokens.json @@ -0,0 +1,130 @@ +[ + { + "type": "Punctuator", + "value": "(", + "location": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 1, + "column": 1, + "index": 1 + } + } + }, + { + "type": "Identifier", + "value": "a", + "location": { + "start": { + "line": 1, + "column": 1, + "index": 1 + }, + "end": { + "line": 1, + "column": 2, + "index": 2 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "location": { + "start": { + "line": 1, + "column": 2, + "index": 2 + }, + "end": { + "line": 1, + "column": 3, + "index": 3 + } + } + }, + { + "type": "Numeric", + "value": "1", + "location": { + "start": { + "line": 1, + "column": 3, + "index": 3 + }, + "end": { + "line": 1, + "column": 4, + "index": 4 + } + } + }, + { + "type": "Punctuator", + "value": ",", + "location": { + "start": { + "line": 1, + "column": 4, + "index": 4 + }, + "end": { + "line": 1, + "column": 5, + "index": 5 + } + } + }, + { + "type": "Numeric", + "value": "2", + "location": { + "start": { + "line": 1, + "column": 5, + "index": 5 + }, + "end": { + "line": 1, + "column": 6, + "index": 6 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "location": { + "start": { + "line": 1, + "column": 6, + "index": 6 + }, + "end": { + "line": 1, + "column": 7, + "index": 7 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "location": { + "start": { + "line": 1, + "column": 7, + "index": 7 + }, + "end": { + "line": 1, + "column": 8, + "index": 8 + } + } + } +] \ No newline at end of file diff --git a/test/Peast/Syntax/ES2015/files/Misc/Sequence2.js b/test/Peast/Syntax/ES2015/files/Misc/Sequence2.js new file mode 100644 index 0000000..2b9b856 --- /dev/null +++ b/test/Peast/Syntax/ES2015/files/Misc/Sequence2.js @@ -0,0 +1 @@ +(a=1,2); \ No newline at end of file diff --git a/test/Peast/Syntax/ES2015/files/Misc/Sequence2.json b/test/Peast/Syntax/ES2015/files/Misc/Sequence2.json new file mode 100644 index 0000000..761e0c5 --- /dev/null +++ b/test/Peast/Syntax/ES2015/files/Misc/Sequence2.json @@ -0,0 +1,150 @@ +{ + "type": "Program", + "location": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 1, + "column": 8, + "index": 8 + } + }, + "leadingComments": [], + "trailingComments": [], + "body": [ + { + "type": "ExpressionStatement", + "location": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 1, + "column": 8, + "index": 8 + } + }, + "leadingComments": [], + "trailingComments": [], + "expression": { + "type": "ParenthesizedExpression", + "location": { + "start": { + "line": 1, + "column": 0, + "index": 0 + }, + "end": { + "line": 1, + "column": 7, + "index": 7 + } + }, + "leadingComments": [], + "trailingComments": [], + "expression": { + "type": "SequenceExpression", + "location": { + "start": { + "line": 1, + "column": 1, + "index": 1 + }, + "end": { + "line": 1, + "column": 6, + "index": 6 + } + }, + "leadingComments": [], + "trailingComments": [], + "expressions": [ + { + "type": "AssignmentExpression", + "location": { + "start": { + "line": 1, + "column": 1, + "index": 1 + }, + "end": { + "line": 1, + "column": 4, + "index": 4 + } + }, + "leadingComments": [], + "trailingComments": [], + "left": { + "type": "Identifier", + "location": { + "start": { + "line": 1, + "column": 1, + "index": 1 + }, + "end": { + "line": 1, + "column": 2, + "index": 2 + } + }, + "leadingComments": [], + "trailingComments": [], + "name": "a", + "rawName": "a" + }, + "operator": "=", + "right": { + "type": "Literal", + "location": { + "start": { + "line": 1, + "column": 3, + "index": 3 + }, + "end": { + "line": 1, + "column": 4, + "index": 4 + } + }, + "leadingComments": [], + "trailingComments": [], + "value": 1, + "raw": "1", + "format": "decimal" + } + }, + { + "type": "Literal", + "location": { + "start": { + "line": 1, + "column": 5, + "index": 5 + }, + "end": { + "line": 1, + "column": 6, + "index": 6 + } + }, + "leadingComments": [], + "trailingComments": [], + "value": 2, + "raw": "2", + "format": "decimal" + } + ] + } + } + } + ], + "sourceType": "script" +} \ No newline at end of file