Properly finish while statements - fixes
MODIFY
packages/@romejs/js-parser/parser/statement.ts
packages/@romejs/js-parser/parser/statement.ts
@@ -943,7 +943,7 @@ export function parseWhileStatement(
parser.state.labels.push(loopLabel);
const body = parseStatement(parser, 'while');
parser.state.labels.pop();
- return {loc: parser.finishLoc(start), type: 'WhileStatement', test, body};
+ return parser.finishNode(start, {type: 'WhileStatement', test, body});
}
export function parseWithStatement(

MODIFY
packages/@romejs/js-parser/test-fixtures/comments/basic/surrounding-while-loop-comments/input.test.md
packages/@romejs/js-parser/test-fixtures/comments/basic/surrounding-while-loop-comments/input.test.md
@@ -136,6 +136,8 @@ Program {
}
body: Array [
WhileStatement {
+ leadingComments: Array ['0']
+ trailingComments: Array ['1']
loc: Object {
filename: 'input.js'
end: Object {
@@ -151,7 +153,7 @@ Program {
}
test: BooleanLiteral {
value: true
- leadingComments: Array ['0']
+ leadingComments: undefined
loc: Object {
filename: 'input.js'
end: Object {
@@ -169,7 +171,7 @@ Program {
body: BlockStatement {
body: Array []
directives: Array []
- trailingComments: Array ['1']
+ trailingComments: undefined
loc: Object {
filename: 'input.js'
end: Object {

ADD
packages/@romejs/js-parser/test-fixtures/comments/while/input.js
+function f() {
+ // A
+ while (x) {
+ x;
+ }
+
+ // B
+ if (y) {
+ y;
+ }
+}

ADD
packages/@romejs/js-parser/test-fixtures/comments/while/input.test.md
+# `index.test.ts`
+
+**DO NOT MODIFY**. This file has been autogenerated. Run `rome test packages/@romejs/js-parser/index.test.ts --update-snapshots` to update.
+
+## `comments > while`
+
+```javascript
+Program {
+ corrupt: false
+ diagnostics: Array []
+ directives: Array []
+ filename: 'input.js'
+ hasHoistedVars: false
+ interpreter: undefined
+ mtime: undefined
+ sourceType: 'script'
+ syntax: Array []
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 0
+ index: 79
+ line: 12
+ }
+ start: Object {
+ column: 0
+ index: 0
+ line: 1
+ }
+ }
+ comments: Array [
+ CommentLine {
+ id: '0'
+ value: ' A'
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 6
+ index: 21
+ line: 2
+ }
+ start: Object {
+ column: 2
+ index: 17
+ line: 2
+ }
+ }
+ }
+ CommentLine {
+ id: '1'
+ value: ' B'
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 6
+ index: 54
+ line: 7
+ }
+ start: Object {
+ column: 2
+ index: 50
+ line: 7
+ }
+ }
+ }
+ ]
+ body: Array [
+ FunctionDeclaration {
+ id: BindingIdentifier {
+ name: 'f'
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 10
+ index: 10
+ line: 1
+ }
+ start: Object {
+ column: 9
+ index: 9
+ line: 1
+ }
+ }
+ }
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 1
+ index: 78
+ line: 11
+ }
+ start: Object {
+ column: 0
+ index: 0
+ line: 1
+ }
+ }
+ head: FunctionHead {
+ async: false
+ generator: false
+ hasHoistedVars: false
+ params: Array []
+ predicate: undefined
+ rest: undefined
+ returnType: undefined
+ thisType: undefined
+ typeParameters: undefined
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 13
+ index: 13
+ line: 1
+ }
+ start: Object {
+ column: 10
+ index: 10
+ line: 1
+ }
+ }
+ }
+ body: BlockStatement {
+ directives: Array []
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 1
+ index: 78
+ line: 11
+ }
+ start: Object {
+ column: 13
+ index: 13
+ line: 1
+ }
+ }
+ body: Array [
+ WhileStatement {
+ leadingComments: Array ['0']
+ trailingComments: Array ['1']
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 3
+ index: 46
+ line: 5
+ }
+ start: Object {
+ column: 2
+ index: 24
+ line: 3
+ }
+ }
+ test: ReferenceIdentifier {
+ name: 'x'
+ leadingComments: undefined
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 10
+ index: 32
+ line: 3
+ }
+ start: Object {
+ column: 9
+ index: 31
+ line: 3
+ }
+ }
+ }
+ body: BlockStatement {
+ directives: Array []
+ trailingComments: undefined
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 3
+ index: 46
+ line: 5
+ }
+ start: Object {
+ column: 12
+ index: 34
+ line: 3
+ }
+ }
+ body: Array [
+ ExpressionStatement {
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 6
+ index: 42
+ line: 4
+ }
+ start: Object {
+ column: 4
+ index: 40
+ line: 4
+ }
+ }
+ expression: ReferenceIdentifier {
+ name: 'x'
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 5
+ index: 41
+ line: 4
+ }
+ start: Object {
+ column: 4
+ index: 40
+ line: 4
+ }
+ }
+ }
+ }
+ ]
+ }
+ }
+ IfStatement {
+ alternate: undefined
+ leadingComments: Array ['1']
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 3
+ index: 76
+ line: 10
+ }
+ start: Object {
+ column: 2
+ index: 57
+ line: 8
+ }
+ }
+ test: ReferenceIdentifier {
+ name: 'y'
+ leadingComments: undefined
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 7
+ index: 62
+ line: 8
+ }
+ start: Object {
+ column: 6
+ index: 61
+ line: 8
+ }
+ }
+ }
+ consequent: BlockStatement {
+ directives: Array []
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 3
+ index: 76
+ line: 10
+ }
+ start: Object {
+ column: 9
+ index: 64
+ line: 8
+ }
+ }
+ body: Array [
+ ExpressionStatement {
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 6
+ index: 72
+ line: 9
+ }
+ start: Object {
+ column: 4
+ index: 70
+ line: 9
+ }
+ }
+ expression: ReferenceIdentifier {
+ name: 'y'
+ loc: Object {
+ filename: 'input.js'
+ end: Object {
+ column: 5
+ index: 71
+ line: 9
+ }
+ start: Object {
+ column: 4
+ index: 70
+ line: 9
+ }
+ }
+ }
+ }
+ ]
+ }
+ }
+ ]
+ }
+ }
+ ]
+}
+```

Properly finish while statements - fixes #378