Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/parser/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
},
};
18 changes: 10 additions & 8 deletions test/snapshot/__snapshots__/acid.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
Expand Down Expand Up @@ -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,
},
},
},
Expand Down
70 changes: 39 additions & 31 deletions test/snapshot/__snapshots__/block.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -524,13 +535,6 @@ Program {
"body": Block {
"children": [],
"kind": "block",
"leadingComments": [
CommentBlock {
"kind": "commentblock",
"offset": 10,
"value": "/* foo */",
},
],
},
"kind": "if",
"shortForm": true,
Expand Down Expand Up @@ -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,
Expand Down
102 changes: 83 additions & 19 deletions test/snapshot/__snapshots__/comment.test.js.snap
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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,
Expand Down Expand Up @@ -1448,24 +1506,30 @@ 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 {
"kind": "commentblock",
"offset": 415,
"value": "/* ignore */",
},
CommentBlock {
"kind": "commentblock",
"offset": 430,
"value": "/* ignore */",
},
CommentBlock {
"kind": "commentblock",
"offset": 451,
"value": "/* ignore */",
},
],
"trailingComments": [
CommentBlock {
Expand Down
8 changes: 8 additions & 0 deletions test/snapshot/comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
`
Expand Down