Leading comment is lost when using named import just before
See original GitHub issueBug Report
- I would like to work on a fix!
Current Behavior
Parsing the code in the section below using @babel/parser
should return a leadingComments on the export declaration
Input Code
import { test } from 'vue-test'
/**
* lost comment
*/
export default {
name: 'name-123',
}
Expected behavior/code
For the leadingComment
to be attached to the exportDeclaration node
Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
const babelParserOptions: ParserOptions = {
sourceType: 'module',
strictMode: false,
tokens: true,
plugins: [
'decorators-legacy',
'doExpressions',
'objectRestSpread',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'exportDefaultFrom',
'exportNamespaceFrom',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding',
'throwExpressions',
'nullishCoalescingOperator'
]
}
Environment
Node 10.17
- Babel version(s): v7.6.0
- Node/npm version: Node 10
- OS: windows 10/linux in CI/MacOS Mojave
- Monorepo: M?A
- How you are using Babel: API
Possible Solution I am guessing a starting point would be in one of those 2 commits https://github.com/babel/babel/pull/10380 https://github.com/babel/babel/pull/10292 https://github.com/babel/babel/pull/10369
Additional context/Screenshots Here is a PR where the issue is visible. https://github.com/vue-styleguidist/vue-styleguidist/pull/681
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Should import statements always be at the top of a module?
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants....
Read more >Avoid Export Default : r/typescript - Reddit
Idk, personally, I love simple default exports/imports, especially being a react dev. 90+% of the time I'm importing a component alone from ...
Read more >Programming FAQ — Python 3.11.1 documentation
Import modules at the top of a file. Doing so makes it clear what other modules your code requires and avoids questions of...
Read more >Higher-Order Components - React
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per...
Read more >Independent brokerage ekes out profit, replaces top executive
It lost $545,000 in the prior quarter, RCS said in regulatory ... at Cetera's three brokerage units, could not be reached for comment....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@elevatebart Under the hood Babel uses comment attachment algorithm to consume the comments and assign to different AST nodes. It lives in
packages/babel-parser/src/parser/comments.js
, you can take a close look at https://github.com/babel/babel/blob/80e95d0c833d639157579c765b6624cd4c2696d6/packages/babel-parser/src/parser/comments.js#L171-L181There is a detailed comment on the third argument
takeAllComments
ofadjustCommentsAfterTrailingComments
thanks to @nicolo-ribaudohttps://github.com/babel/babel/blob/80e95d0c833d639157579c765b6624cd4c2696d6/packages/babel-parser/src/parser/comments.js#L44-L52
When it’s running on an
ImportDeclaration
(import { test } from 'vue-test'
), thecommentPreviousNode
is anImportSpecifier
(test
) and it will take allleadingComments
to be thetrailingComment
ofImportDeclaration
.In your case, I don’t think we can use
takeAllComments: true
here because apparently it can attach to the next node.@elevatebart Correct. It should stay in the state.leadingCommets stack and attach it to the export declaration.