Skip to content

Commit

Permalink
fix(transformers): use Array.sort in hoisting transformer (#3498)
Browse files Browse the repository at this point in the history
fixes #3476
  • Loading branch information
benasher44 committed May 6, 2022
1 parent 8e1e8dd commit e400a6e
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/transformers/hoist-jest.ts
Expand Up @@ -87,21 +87,12 @@ export function factory({ configSet }: TsCompilerInstance) {
return statements
}

const pivot = statements[0]
const leftPart: _ts.Statement[] = []
const rightPart: _ts.Statement[] = []
for (let i = 1; i < statements.length; i++) {
const currentStatement = statements[i]
if (isJestGlobalImport(currentStatement)) {
leftPart.push(currentStatement)
} else {
isHoistableStatement(currentStatement) && !isHoistableStatement(pivot) && !isJestGlobalImport(pivot)
? leftPart.push(currentStatement)
: rightPart.push(currentStatement)
}
}

return sortStatements(leftPart).concat(pivot, sortStatements(rightPart))
return statements.sort((stmtA, stmtB) =>
isJestGlobalImport(stmtA) ||
(isHoistableStatement(stmtA) && !isHoistableStatement(stmtB) && !isJestGlobalImport(stmtB))
? -1
: 1,
)
}

const createVisitor = (ctx: _ts.TransformationContext, _: _ts.SourceFile) => {
Expand Down

0 comments on commit e400a6e

Please sign in to comment.