| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | 'use strict'; |
| 9 | |
| 10 | function getComments(path) { |
| 11 | const allComments = path.hub.file.ast.comments; |
| 12 | if (path.node.leadingComments) { |
| 13 | // Babel AST includes comments. |
| 14 | return path.node.leadingComments; |
| 15 | } |
| 16 | // In Hermes AST we need to find the comments by range. |
| 17 | const comments = []; |
| 18 | let line = path.node.loc.start.line; |
| 19 | let i = allComments.length - 1; |
| 20 | while (i >= 0 && allComments[i].loc.end.line >= line) { |
| 21 | i--; |
| 22 | } |
| 23 | while (i >= 0 && allComments[i].loc.end.line === line - 1) { |
| 24 | line = allComments[i].loc.start.line; |
| 25 | comments.unshift(allComments[i]); |
| 26 | i--; |
| 27 | } |
| 28 | return comments; |
| 29 | } |
| 30 | |
| 31 | module.exports = getComments; |