Skip to content

Commit 4acea62

Browse files
committed
fix: insert imports one line higher
1 parent cf88924 commit 4acea62

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
},
194194
"dependencies": {
195195
"@babel/runtime": "^7.8.4",
196+
"flow-parser": "^0.117.1",
196197
"jscodeshift": "^0.7.0",
197198
"jscodeshift-choose-parser": "^1.0.0",
198199
"require-glob": "^3.2.0",

src/getExistingImports.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@ export default function getExistingImports(
1313

1414
let insertLine = 0
1515

16-
j(text)
16+
let root
17+
try {
18+
root = j(text)
19+
} catch (error) {
20+
// fall back to trying with flow parser, because it tends to be
21+
// very tolerant of errors and we might at least get the import
22+
// nodes
23+
root = j.withParser('flow')(text)
24+
}
25+
root
1726
.find(j.ImportDeclaration)
1827
.forEach(({ node }: ASTPath<ImportDeclaration>): void => {
1928
if (!node) return
20-
if (node.loc) insertLine = node.loc.end.line + 1
29+
if (node.loc) insertLine = node.loc.end.line
2130
const source = node.source.value
2231
if (typeof source === 'string' && source.startsWith('@material-ui')) {
2332
result.add(source)

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3676,7 +3676,7 @@ flatted@^2.0.0:
36763676
resolved "https://registry.yarnpkg.com/flatted/-/flatted-2.0.1.tgz#69e57caa8f0eacbc281d2e2cb458d46fdb449e08"
36773677
integrity sha512-a1hQMktqW9Nmqr5aktAux3JMNqaucxGcjtjWnZLHX7yyPCmlSV3M54nGYbqT8K+0GhF3NBgmJCc3ma+WOgX8Jg==
36783678

3679-
flow-parser@0.*:
3679+
flow-parser@0.*, flow-parser@^0.117.1:
36803680
version "0.117.1"
36813681
resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.117.1.tgz#ad52b866c16fe36a084a73056909731c9d538bdb"
36823682
integrity sha512-ybDdkcZOFw8rkpoLGzNH5xoKp4TrL5Kikrov0K/IFawNC6K8S3czWzxi+e1NJydgNHFZEKrrTcsO/9ftrQToHA==

0 commit comments

Comments
 (0)