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 authored and anh.pham committed May 18, 2022
1 parent 8fc3e07 commit c89a907
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
26 changes: 16 additions & 10 deletions e2e/transform-js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/config/__snapshots__/config-set.spec.ts.snap
Expand Up @@ -60,7 +60,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
],
}
Expand All @@ -74,7 +74,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
Object {
"factory": [Function],
Expand All @@ -93,7 +93,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
Object {
"factory": [Function],
Expand All @@ -118,7 +118,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
],
}
Expand All @@ -138,7 +138,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
],
}
Expand All @@ -152,7 +152,7 @@ Object {
Object {
"factory": [Function],
"name": "hoist-jest",
"version": 3,
"version": 4,
},
Object {
"factory": [Function],
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/hoist-jest.spec.ts
Expand Up @@ -168,7 +168,7 @@ const printer = ts.createPrinter()
describe('hoist-jest', () => {
test('should have correct transformer name and version', () => {
expect(name).toBe('hoist-jest')
expect(version).toBe(3)
expect(version).toBe(4)
})

test('should hoist correctly when not using @jest/globals', () => {
Expand Down
22 changes: 7 additions & 15 deletions src/transformers/hoist-jest.ts
Expand Up @@ -8,7 +8,7 @@ import type { TsCompilerInstance } from '../types'
* Remember to increase the version whenever transformer's content is changed. This is to inform Jest to not reuse
* the previous cache which contains old transformer's content
*/
export const version = 3
export const version = 4
// Used for constructing cache key
export const name = 'hoist-jest'

Expand Down Expand Up @@ -87,21 +87,13 @@ export function factory({ configSet }: TsCompilerInstance) {
if (statements.length <= 1) {
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 c89a907

Please sign in to comment.