react/scripts/babel/getComments.js
Jan Kassens b83baf63f7
Transform updates to support Flow this annotation syntax (#25918)
Flow introduced a new syntax to annotated the context type of a
function, this tries to update the rest and add 1 example usage.

- 2b1fb91a55 already added the changes
required for eslint.
- Jest transform is updated to use the recommended `hermes-parser` which
can parse current and Flow syntax and will be updated in the future.
- Rollup uses a new plugin to strip the flow types. This isn't ideal as
the npm module is deprecated in favor of using `hermes-parser`, but I
couldn't figure out how to integrate that with Rollup.
2023-01-05 15:41:49 -05:00

32 lines
832 B
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
function getComments(path) {
const allComments = path.hub.file.ast.comments;
if (path.node.leadingComments) {
// Babel AST includes comments.
return path.node.leadingComments;
}
// In Hermes AST we need to find the comments by range.
const comments = [];
let line = path.node.loc.start.line;
let i = allComments.length - 1;
while (i >= 0 && allComments[i].loc.end.line >= line) {
i--;
}
while (i >= 0 && allComments[i].loc.end.line === line - 1) {
line = allComments[i].loc.start.line;
comments.unshift(allComments[i]);
i--;
}
return comments;
}
module.exports = getComments;