Skip to content

Commit 2290408

Browse files
committed
react-update-imports: Don't remove leading comments
1 parent d6fd8e7 commit 2290408

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Hello world.
3+
*/
4+
5+
import * as React from "react";
6+
7+
<div></div>;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Hello world.
3+
*/
4+
5+
<div></div>;

transforms/__tests__/update-react-imports-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const tests = [
1515
'flow-default-and-type-specifier-import',
1616
'jsx-element',
1717
'jsx-fragment',
18+
'leading-comment',
1819
'react-basic-default-export-jsx-element-react-variable',
1920
'react-basic-default-export-jsx-element',
2021
'react-basic-default-export',

transforms/update-react-imports.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ module.exports = function(file, api, options) {
1111
const printOptions = options.printOptions || {};
1212
const root = j(file.source);
1313

14+
// https://github.com/facebook/jscodeshift/blob/master/recipes/retain-first-comment.md
15+
function getFirstNode() {
16+
return root.find(j.Program).get('body', 0).node;
17+
}
18+
19+
// Save the comments attached to the first node
20+
const firstNode = getFirstNode();
21+
const { comments } = firstNode;
22+
1423
function isVariableDeclared(variable) {
1524
return (
1625
root
@@ -355,6 +364,12 @@ module.exports = function(file, api, options) {
355364
return null;
356365
}
357366
}
358-
367+
368+
// If the first node has been modified or deleted, reattach the comments
369+
const firstNode2 = getFirstNode();
370+
if (firstNode2 !== firstNode) {
371+
firstNode2.comments = comments;
372+
}
373+
359374
return root.toSource(printOptions);
360375
};

0 commit comments

Comments
 (0)