diff --git a/src/parser/if.js b/src/parser/if.js index 3b4597dbc..f0e0600a3 100644 --- a/src/parser/if.js +++ b/src/parser/if.js @@ -22,8 +22,8 @@ module.exports = { if (this.token === ":") { shortForm = true; - this.next(); body = this.node("block"); + this.next(); const items = []; while (this.token !== this.EOF && this.token !== this.tok.T_ENDIF) { if (this.token === this.tok.T_ELSEIF) { @@ -35,6 +35,13 @@ module.exports = { } items.push(this.read_inner_statement()); } + if ( + items.length === 0 && + this.extractDoc && + this._docs.length > this._docIndex + ) { + items.push(this.node("noop")()); + } body = body(null, items); this.expect(this.tok.T_ENDIF) && this.next(); this.expectEndOfStatement(); @@ -64,8 +71,8 @@ module.exports = { let alternate = null; const result = this.node("if"); const test = this.next().read_if_expr(); - if (this.expect(":")) this.next(); const body = this.node("block"); + if (this.expect(":")) this.next(); const items = []; while (this.token != this.EOF && this.token !== this.tok.T_ENDIF) { if (this.token === this.tok.T_ELSEIF) { @@ -77,18 +84,33 @@ module.exports = { } items.push(this.read_inner_statement()); } + if ( + items.length === 0 && + this.extractDoc && + this._docs.length > this._docIndex + ) { + items.push(this.node("noop")()); + } return result(test, body(null, items), alternate, true); }, /* * */ read_else_short() { - if (this.next().expect(":")) this.next(); + this.next(); const body = this.node("block"); + if (this.expect(":")) this.next(); const items = []; while (this.token != this.EOF && this.token !== this.tok.T_ENDIF) { items.push(this.read_inner_statement()); } + if ( + items.length === 0 && + this.extractDoc && + this._docs.length > this._docIndex + ) { + items.push(this.node("noop")()); + } return body(null, items); }, }; diff --git a/test/snapshot/__snapshots__/acid.test.js.snap b/test/snapshot/__snapshots__/acid.test.js.snap index 266cacc00..2dd269a63 100644 --- a/test/snapshot/__snapshots__/acid.test.js.snap +++ b/test/snapshot/__snapshots__/acid.test.js.snap @@ -6916,11 +6916,12 @@ next: "line": 136, "offset": 2948, }, - "source": "echo \`ls -larth\`;", + "source": ": + echo \`ls -larth\`;", "start": Position { - "column": 4, - "line": 136, - "offset": 2931, + "column": 15, + "line": 135, + "offset": 2925, }, }, }, @@ -7046,13 +7047,14 @@ next: "line": 136, "offset": 2948, }, - "source": "echo \`bar&\`; + "source": ": + echo \`bar&\`; elseif ($bar): echo \`ls -larth\`;", "start": Position { - "column": 4, - "line": 134, - "offset": 2897, + "column": 11, + "line": 133, + "offset": 2891, }, }, }, diff --git a/test/snapshot/__snapshots__/block.test.js.snap b/test/snapshot/__snapshots__/block.test.js.snap index 7627ba0a5..d1cf5abb3 100644 --- a/test/snapshot/__snapshots__/block.test.js.snap +++ b/test/snapshot/__snapshots__/block.test.js.snap @@ -430,16 +430,16 @@ Program { "name": "a", }, "kind": "expressionstatement", + "leadingComments": [ + CommentBlock { + "kind": "commentblock", + "offset": 10, + "value": "/* pre */", + }, + ], }, ], "kind": "block", - "leadingComments": [ - CommentBlock { - "kind": "commentblock", - "offset": 10, - "value": "/* pre */", - }, - ], "trailingComments": [ CommentBlock { "kind": "commentblock", @@ -492,18 +492,18 @@ Program { If { "alternate": If { "alternate": Block { - "children": [], - "kind": "block", - "leadingComments": [ - CommentBlock { - "kind": "commentblock", - "offset": 50, - "value": "/* bar */", + "children": [ + Noop { + "kind": "noop", + "leadingComments": [ + CommentBlock { + "kind": "commentblock", + "offset": 50, + "value": "/* bar */", + }, + ], }, ], - }, - "body": Block { - "children": [], "kind": "block", "leadingComments": [ CommentBlock { @@ -513,7 +513,18 @@ Program { }, ], }, + "body": Block { + "children": [], + "kind": "block", + }, "kind": "if", + "leadingComments": [ + CommentBlock { + "kind": "commentblock", + "offset": 10, + "value": "/* foo */", + }, + ], "shortForm": true, "test": Variable { "curly": false, @@ -524,13 +535,6 @@ Program { "body": Block { "children": [], "kind": "block", - "leadingComments": [ - CommentBlock { - "kind": "commentblock", - "offset": 10, - "value": "/* foo */", - }, - ], }, "kind": "if", "shortForm": true, @@ -610,15 +614,19 @@ Program { If { "alternate": null, "body": Block { - "children": [], - "kind": "block", - "leadingComments": [ - CommentBlock { - "kind": "commentblock", - "offset": 10, - "value": "/* foo */", + "children": [ + Noop { + "kind": "noop", + "leadingComments": [ + CommentBlock { + "kind": "commentblock", + "offset": 10, + "value": "/* foo */", + }, + ], }, ], + "kind": "block", }, "kind": "if", "shortForm": true, diff --git a/test/snapshot/__snapshots__/comment.test.js.snap b/test/snapshot/__snapshots__/comment.test.js.snap index 017f7e571..126abe242 100644 --- a/test/snapshot/__snapshots__/comment.test.js.snap +++ b/test/snapshot/__snapshots__/comment.test.js.snap @@ -1,5 +1,59 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing +exports[`Test comments issues doc comment on function inside if/endif attaches to function not block 1`] = ` +Program { + "children": [ + If { + "alternate": null, + "body": Block { + "children": [ + _Function { + "arguments": [], + "attrGroups": [], + "body": Block { + "children": [], + "kind": "block", + }, + "byref": false, + "kind": "function", + "leadingComments": [ + CommentBlock { + "kind": "commentblock", + "offset": 11, + "value": "/** @since 1.0 */", + }, + ], + "name": Identifier { + "kind": "identifier", + "name": "foo", + }, + "nullable": false, + "type": null, + }, + ], + "kind": "block", + }, + "kind": "if", + "shortForm": true, + "test": Boolean { + "kind": "boolean", + "raw": "true", + "value": true, + }, + }, + ], + "comments": [ + CommentBlock { + "kind": "commentblock", + "offset": 11, + "value": "/** @since 1.0 */", + }, + ], + "errors": [], + "kind": "program", +} +`; + exports[`Test comments issues fix #55 1`] = ` Program { "children": [ @@ -10,16 +64,20 @@ Program { If { "alternate": null, "body": Block { - "children": [], - "kind": "block", - "leadingComments": [ - CommentLine { - "kind": "commentline", - "offset": 51, - "value": "// inner statements -", + "children": [ + Noop { + "kind": "noop", + "leadingComments": [ + CommentLine { + "kind": "commentline", + "offset": 51, + "value": "// inner statements +", + }, + ], }, ], + "kind": "block", }, "kind": "if", "shortForm": true, @@ -1448,7 +1506,23 @@ Program { If { "alternate": null, "body": Block { - "children": [], + "children": [ + Noop { + "kind": "noop", + "leadingComments": [ + CommentBlock { + "kind": "commentblock", + "offset": 430, + "value": "/* ignore */", + }, + CommentBlock { + "kind": "commentblock", + "offset": 451, + "value": "/* ignore */", + }, + ], + }, + ], "kind": "block", "leadingComments": [ CommentBlock { @@ -1456,16 +1530,6 @@ Program { "offset": 415, "value": "/* ignore */", }, - CommentBlock { - "kind": "commentblock", - "offset": 430, - "value": "/* ignore */", - }, - CommentBlock { - "kind": "commentblock", - "offset": 451, - "value": "/* ignore */", - }, ], "trailingComments": [ CommentBlock { diff --git a/test/snapshot/comment.test.js b/test/snapshot/comment.test.js index a20ddf4d6..dc18670c3 100644 --- a/test/snapshot/comment.test.js +++ b/test/snapshot/comment.test.js @@ -87,6 +87,14 @@ call(array // comment expect(ast).toMatchSnapshot(); }); + it("doc comment on function inside if/endif attaches to function not block", function () { + const ast = parser.parseEval( + `if (true): /** @since 1.0 */ function foo() {} endif;`, + { parser: { extractDoc: true } }, + ); + expect(ast).toMatchSnapshot(); + }); + it("fix #189", function () { const ast = parser.parseEval( `