-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Grouped imports shouldn't have empty newline separation #405
Conversation
This pull request is automatically deployed with Now. |
61d84df
to
d6a6bc0
Compare
This looks a bit unwieldy! I thought I’d try to wrap my head around what’s going on to see what I’d come up. It’s pseudo-code, so it probably won’t work, but it looks a bit cleaner! var merged = []
var length = allNodes.length
var index = -1
var prevType
var currType
var node
var head
while (++index < length) {
node = allNodes[index]
currType = node.default ? 'default' : node.type
if (currType === prevType) {
head.value += node.value
} else {
merged.push(node)
head = node
}
prevType = currType
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm. Removing the mutation made it easier for me to understand what was going on, ymmv.
diff --git a/packages/remark-mdx/extract-imports-and-exports.js b/packages/remark-mdx/extract-imports-and-exports.js
index e4ecaaf..3b006ce 100644
--- a/packages/remark-mdx/extract-imports-and-exports.js
+++ b/packages/remark-mdx/extract-imports-and-exports.js
@@ -57,34 +57,32 @@ module.exports = value => {
// Group adjacent nodes of the same type so that they can be combined
// into a single node later
let currType = allNodes[0].type
- const groupedNodes = allNodes.reduce(
- (acc, curr) => {
- // Default export nodes shouldn't be grouped with other exports
- // because they're handled specially by MDX
- if (curr.default) {
- currType = 'default'
- acc.push([curr])
- } else if (curr.type === currType) {
- acc[acc.length - 1].push(curr)
- } else {
- currType = curr.type
- acc.push([curr])
- }
-
- return acc
- },
- [[]]
- )
-
- // Combine adjacent nodes into a single node
- return groupedNodes
+ const groupedNodes = allNodes
+ .reduce(
+ (acc, curr) => {
+ // Default export nodes shouldn't be grouped with other exports
+ // because they're handled specially by MDX
+ if (curr.default) {
+ currType = 'default'
+ return [...acc, [curr]]
+ } else if (curr.type === currType) {
+ const lastNodes = acc.pop()
+ return [...acc, [...lastNodes, curr]]
+ } else {
+ currType = curr.type
+ return [...acc, [curr]]
+ }
+ },
+ [[]]
+ )
.filter(a => a.length)
.reduce((acc, curr) => {
- const node = curr.shift()
- curr.forEach(n => {
- node.value += n.value
- })
+ // Combine adjacent nodes into a single node
+ const node = curr.reduce((acc, curNode) => ({
+ ...acc,
+ value: acc.value + curNode.value
+ }))
- return acc.concat([node])
+ return [...acc, node]
}, [])
}
Before, each import is turned into its own node, even when grouped like so:
So, when combining back to a string we ended up with:
We now combine adjacent nodes of the same type. Though, if the node is a default export it is treated specially.