question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Leading comment is lost when using named import just before

See original GitHub issue

Bug 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:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
JLHwungcommented, Dec 18, 2019

@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-L181

There is a detailed comment on the third argument takeAllComments of adjustCommentsAfterTrailingComments thanks to @nicolo-ribaudo

https://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'), the commentPreviousNode is an ImportSpecifier (test) and it will take all leadingComments to be the trailingComment of ImportDeclaration.

In your case, I don’t think we can use takeAllComments: true here because apparently it can attach to the next node.

0reactions
JLHwungcommented, Dec 19, 2019

@elevatebart Correct. It should stay in the state.leadingCommets stack and attach it to the export declaration.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found