Skip to content

Commit 6cb31a0

Browse files
committed
fix(injector): insert import after the first one
Insert the prop-types import after the first import found to avoid issues with comment flags
1 parent 9d4dfd1 commit 6cb31a0

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Diff for: src/injector.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,20 @@ function plugin(propTypes: t.ProgramNode, options: InjectOptions = {}): babel.Pl
6060
exit(path) {
6161
if (alreadyImported || !needImport) return;
6262

63-
const x = babel.template.ast(`import ${importName} from 'prop-types'`);
64-
65-
path.node.body = [x as babel.types.ImportDeclaration, ...path.node.body];
63+
const propTypesImport = babel.template.ast(
64+
`import ${importName} from 'prop-types'`,
65+
) as babel.types.ImportDeclaration;
66+
67+
const firstImport = path
68+
.get('body')
69+
.find(nodePath => babelTypes.isImportDeclaration(nodePath.node));
70+
71+
// Insert import after the first one to avoid issues with comment flags
72+
if (firstImport) {
73+
firstImport.insertAfter(propTypesImport);
74+
} else {
75+
path.node.body = [propTypesImport, ...path.node.body];
76+
}
6677
},
6778
},
6879
FunctionDeclaration(path) {

0 commit comments

Comments
 (0)