facebook / facebook/hermes

[hermes-babel] Comments are not found in const nodes when producing AST tree

Open
#1,098 5 comments 3 reactions 0 assignees View on GitHub
enhancement hermes-babel
Dominant language
JavaScript
Stars
11.3k
Forks
859
Avg merge
1h 30m
Merged PRs (30d)
3

Description

## Bug Description
Take the following code
```
/**
* I am a comment
*/
const Box = () => {
return (


);
}

export default Box;
```

When I parse the code using this babel config
```
const config = {
presets: [
[
'@babel/preset-env',
{
targets: { browsers: ['last 2 versions', 'safari >= 7'] },
},
],
'@babel/preset-react',
'@babel/preset-flow',
],
plugins: [
'babel-plugin-syntax-hermes-parser',
'@babel/plugin-transform-modules-commonjs',
],
};

if (process.env.NODE_ENV === 'production') {
const ignoreGlob = [
'*/**/*.test.js',
];

if (process.env.MODE !== 'website') {
ignoreGlob.push('*/**/*.story.js');
ignoreGlob.push('*/**/stories/*');
}

config.presets[0][1].modules = false;
config.ignore = config.ignore
? config.ignore.concat(ignoreGlob)
: ignoreGlob;
}

module.exports = config;
```
It produces a node looking like the following with `babel-plugin-syntax-hermes-parser` @ 0.15.0
```
{
type: 'VariableDeclaration',
loc: {
source: null,
start: { line: 4, column: 0 },
end: { line: 8, column: 1 }
},
kind: 'const',
declarations: [
{
type: 'VariableDeclarator',
loc: [Object],
init: [Object],
id: [Object],
start: 32,
end: 75
}
],
start: 26,
end: 75
}
```

But if I remove the hermes plugin meaning it uses standard babel I get
```
Node {
type: 'VariableDeclaration',
start: 26,
end: 75,
loc: SourceLocation {
start: Position { line: 4, column: 0, index: 26 },
end: Position { line: 8, column: 1, index: 75 },
filename: undefined,
identifierName: undefined
},
declarations: [
Node {
type: 'VariableDeclarator',
start: 32,
end: 75,
loc: [SourceLocation],
id: [Node],
init: [Node]
}
],
kind: 'const',
leadingComments: [
{
type: 'CommentBlock',
value: '*\n * I am a comment\n ',
start: 0,
end: 25,
loc: [SourceLocation]
}
]
}
```

There are some minor differences, but main one I need is `leadingComments` (could also be `trailingComments`) that's missing so the tool I'm using `react-docgen` doesn't work.

Comments are available in the root of the program but given it worked with babel would be nice to have it with hermes :(

Hermes version: 0.15.0
React Native version (if any): N/A
OS version (if any): Windows WSL
Platform (most likely one of arm64-v8a, armeabi-v7a, x86, x86_64): x86_64

## Steps To Reproduce

Clone this repo and run the test to see the output:
https://github.com/Brianzchen/hermes-no-comments

## The Expected Behavior

Described above should be sufficient.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.