Skip to content

Commit

Permalink
Add Copyright header, better name data keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Sep 21, 2020
1 parent 5daa269 commit eada567
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
@@ -1,3 +1,11 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import pluginTester from 'babel-plugin-tester';
import babelPluginJestHoist from '..';

Expand Down
11 changes: 7 additions & 4 deletions packages/babel-plugin-jest-hoist/src/index.ts
Expand Up @@ -276,23 +276,26 @@ export default (): PluginObj<{
jestObjExpr.replaceWith(
callExpression(this.declareJestObjGetterIdentifier(), []),
);
exprStmt.setData('shouldHoist', true);
exprStmt.setData('_jestShouldHoist', true);
}
},
},
// in `post` to make sure we come after an import transform and can unshift above the `require`s
post({path: program}: {path: NodePath<Program>}) {
program.traverse({
ExpressionStatement: exprStmt => {
if (exprStmt.getData('shouldHoist')) {
if (exprStmt.getData('_jestShouldHoist')) {
const prevSiblings = exprStmt.getAllPrevSiblings();
const unhoistedSiblings = prevSiblings
.reverse()
.filter(s => !s.getData('shouldHoist') && !s.getData('wasHoisted'));
.filter(
s =>
!s.getData('_jestShouldHoist') && !s.getData('_jestWasHoisted'),
);
const exprNode = exprStmt.node;
if (unhoistedSiblings.length) {
const hoisted = unhoistedSiblings[0].insertBefore(clone(exprNode));
hoisted[0].setData('wasHoisted', true);
hoisted[0].setData('_jestWasHoisted', true);
exprStmt.remove();
}
}
Expand Down

0 comments on commit eada567

Please sign in to comment.