Skip to content

Commit

Permalink
Merge branch 'main' into upgrade-tstyche
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas authored Dec 23, 2024
2 parents 1ec0000 + 611d1a4 commit bfca8bf
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 195 deletions.
13 changes: 8 additions & 5 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
'eslint:recommended',
'plugin:markdown/recommended',
'plugin:import/errors',
'plugin:eslint-comments/recommended',
'plugin:@eslint-community/eslint-comments/recommended',
'plugin:unicorn/recommended',
'plugin:promise/recommended',
'plugin:prettier/recommended',
Expand Down Expand Up @@ -348,8 +348,8 @@ module.exports = {
],
rules: {
'@typescript-eslint/ban-types': 'off',
'eslint-comments/disable-enable-pair': 'off',
'eslint-comments/no-unlimited-disable': 'off',
'@eslint-community/eslint-comments/disable-enable-pair': 'off',
'@eslint-community/eslint-comments/no-unlimited-disable': 'off',
'prefer-rest-params': 'off',
'prefer-spread': 'off',
'sort-keys ': 'off',
Expand Down Expand Up @@ -478,8 +478,11 @@ module.exports = {
'default-case': 'off',
'dot-notation': 'off',
eqeqeq: ['error', 'smart'],
'eslint-comments/disable-enable-pair': ['error', {allowWholeFile: true}],
'eslint-comments/no-unused-disable': 'error',
'@eslint-community/eslint-comments/disable-enable-pair': [
'error',
{allowWholeFile: true},
],
'@eslint-community/eslint-comments/no-unused-disable': 'error',
'func-names': 'off',
'func-style': ['off', 'declaration'],
'global-require': 'off',
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [16.x, 18.x, 20.x, 22.x]
node-version: [16.x, 18.x, 20.x, 22.x, 23.x]
name: Node v${{ matrix.node-version }}
runs-on: ${{ inputs.os }}

Expand Down
232 changes: 116 additions & 116 deletions .yarn/releases/yarn-3.8.6.cjs → .yarn/releases/yarn-3.8.7.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"

yarnPath: .yarn/releases/yarn-3.8.6.cjs
yarnPath: .yarn/releases/yarn-3.8.7.cjs
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
### Fixes

- `[babel-plugin-jest-hoist]` Use `denylist` instead of the deprecated `blacklist` for Babel 8 support ([#14109](https://github.com/jestjs/jest/pull/14109))
- `[babel-plugin-jest-hoist]` Do not rely on buggy Babel behaviour ([#15415](https://github.com/jestjs/jest/pull/15415))
- `[expect]` Check error instance type for `toThrow/toThrowError` ([#14576](https://github.com/jestjs/jest/pull/14576))
- `[expect]` Improve diff for failing `expect.objectContaining` ([#15038](https://github.com/jestjs/jest/pull/15038))
- `[expect]` Use `Array.isArray` to check if an array is an `Array` ([#15101](https://github.com/jestjs/jest/pull/15101))
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@babel/preset-typescript": "^7.0.0",
"@babel/register": "^7.0.0",
"@crowdin/cli": "^4.0.0",
"@eslint-community/eslint-plugin-eslint-comments": "^4.0.0",
"@jest/globals": "workspace:*",
"@jest/test-utils": "workspace:*",
"@lerna-lite/cli": "^3.0.0",
Expand All @@ -33,7 +34,6 @@
"eslint": "^8.8.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.2.5",
"eslint-plugin-eslint-comments": "^3.1.2",
"eslint-plugin-import": "^2.6.0",
"eslint-plugin-jest": "^28.0.0",
"eslint-plugin-jsdoc": "^50.0.0",
Expand Down Expand Up @@ -204,5 +204,5 @@
"psl": "patch:psl@npm:^1.9.0#./.yarn/patches/psl-npm-1.9.0-a546edad1a.patch",
"ts-node@^10.5.0": "patch:ts-node@npm:^10.5.0#./.yarn/patches/ts-node-npm-10.9.1-6c268be7f4.patch"
},
"packageManager": "[email protected].6"
"packageManager": "[email protected].7"
}
94 changes: 41 additions & 53 deletions packages/babel-plugin-jest-hoist/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,17 @@ import type {PluginObj} from '@babel/core';
import {statement} from '@babel/template';
import type {NodePath} from '@babel/traverse';
import {
type BlockStatement,
type CallExpression,
type EmptyStatement,
type Expression,
type Identifier,
type ImportDeclaration,
type MemberExpression,
type Node,
type Program,
type Statement,
type Super,
type VariableDeclaration,
type VariableDeclarator,
callExpression,
emptyStatement,
isIdentifier,
variableDeclaration,
} from '@babel/types';
Expand Down Expand Up @@ -312,53 +309,6 @@ const extractJestObjExprIfHoistable = (expr: NodePath): JestObjInfo | null => {
return null;
};

function visitBlock(block: NodePath<BlockStatement> | NodePath<Program>) {
// use a temporary empty statement instead of the real first statement, which may itself be hoisted
const [varsHoistPoint, callsHoistPoint] = block.unshiftContainer<
BlockStatement | Program,
'body',
[EmptyStatement, EmptyStatement]
>('body', [emptyStatement(), emptyStatement()]);
block.traverse({
CallExpression: visitCallExpr,
VariableDeclarator: visitVariableDeclarator,
// do not traverse into nested blocks, or we'll hoist calls in there out to this block
denylist: ['BlockStatement'],
});
callsHoistPoint.remove();
varsHoistPoint.remove();

function visitCallExpr(callExpr: NodePath<CallExpression>) {
if (hoistedJestGetters.has(callExpr.node)) {
const mockStmt = callExpr.getStatementParent();

if (mockStmt) {
const mockStmtParent = mockStmt.parentPath;
if (mockStmtParent.isBlock()) {
const mockStmtNode = mockStmt.node;
mockStmt.remove();
callsHoistPoint.insertBefore(mockStmtNode);
}
}
}
}

function visitVariableDeclarator(varDecl: NodePath<VariableDeclarator>) {
if (hoistedVariables.has(varDecl.node)) {
// should be assert function, but it's not. So let's cast below
varDecl.parentPath.assertVariableDeclaration();

const {kind, declarations} = varDecl.parent as VariableDeclaration;
if (declarations.length === 1) {
varDecl.parentPath.remove();
} else {
varDecl.remove();
}
varsHoistPoint.insertBefore(variableDeclaration(kind, [varDecl.node]));
}
}
}

/* eslint-disable sort-keys */
export default function jestHoist(): PluginObj<{
declareJestObjGetterIdentifier: () => Identifier;
Expand Down Expand Up @@ -404,8 +354,46 @@ export default function jestHoist(): PluginObj<{
},
// in `post` to make sure we come after an import transform and can unshift above the `require`s
post({path: program}) {
visitBlock(program);
program.traverse({BlockStatement: visitBlock});
type Item = {calls: Array<Statement>; vars: Array<Statement>};

const stack: Array<Item> = [{calls: [], vars: []}];
program.traverse({
BlockStatement: {
enter() {
stack.push({calls: [], vars: []});
},
exit(path) {
const item = stack.pop()!;
path.node.body.unshift(...item.vars, ...item.calls);
},
},
CallExpression(callExpr: NodePath<CallExpression>) {
if (hoistedJestGetters.has(callExpr.node)) {
const mockStmt = callExpr.getStatementParent();

if (mockStmt && mockStmt.parentPath.isBlock()) {
stack.at(-1)!.calls.push(mockStmt.node);
mockStmt.remove();
}
}
},
VariableDeclarator(varDecl: NodePath<VariableDeclarator>) {
if (hoistedVariables.has(varDecl.node)) {
// should be assert function, but it's not. So let's cast below
varDecl.parentPath.assertVariableDeclaration();

const {kind, declarations} = varDecl.parent as VariableDeclaration;
if (declarations.length === 1) {
varDecl.parentPath.remove();
} else {
varDecl.remove();
}
stack.at(-1)!.vars.push(variableDeclaration(kind, [varDecl.node]));
}
},
});
const item = stack.pop()!;
program.node.body.unshift(...item.vars, ...item.calls);
},
};
}
Expand Down
6 changes: 6 additions & 0 deletions scripts/lintTs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ try {
'@typescript-eslint/no-redundant-type-constituents': 'off',
},
},
// {
// files: ['packages/babel-plugin-jest-hoist/src/index.ts'],
// rules: {
// '@typescript-eslint/strict-boolean-expressions': 'off',
// },
// },
],
parser: '@typescript-eslint/parser',
parserOptions: {
Expand Down
34 changes: 17 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2837,6 +2837,18 @@ __metadata:
languageName: node
linkType: hard

"@eslint-community/eslint-plugin-eslint-comments@npm:^4.0.0":
version: 4.4.1
resolution: "@eslint-community/eslint-plugin-eslint-comments@npm:4.4.1"
dependencies:
escape-string-regexp: ^4.0.0
ignore: ^5.2.4
peerDependencies:
eslint: ^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0
checksum: 32edb04a698c1b0f2d157c57d2c931811e804f9e5df4ffdcfa52334d7f170cf22a0d5531e008efcf21e22a36474e68036882c5bc25076fedc5755e3bdfe7b8e8
languageName: node
linkType: hard

"@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0":
version: 4.4.0
resolution: "@eslint-community/eslint-utils@npm:4.4.0"
Expand Down Expand Up @@ -3254,6 +3266,7 @@ __metadata:
"@babel/preset-typescript": ^7.0.0
"@babel/register": ^7.0.0
"@crowdin/cli": ^4.0.0
"@eslint-community/eslint-plugin-eslint-comments": ^4.0.0
"@jest/globals": "workspace:*"
"@jest/test-utils": "workspace:*"
"@lerna-lite/cli": ^3.0.0
Expand All @@ -3277,7 +3290,6 @@ __metadata:
eslint: ^8.8.0
eslint-config-prettier: ^9.0.0
eslint-import-resolver-typescript: ^3.2.5
eslint-plugin-eslint-comments: ^3.1.2
eslint-plugin-import: ^2.6.0
eslint-plugin-jest: ^28.0.0
eslint-plugin-jsdoc: ^50.0.0
Expand Down Expand Up @@ -8524,13 +8536,13 @@ __metadata:
linkType: hard

"cross-spawn@npm:^7.0.0, cross-spawn@npm:^7.0.2, cross-spawn@npm:^7.0.3":
version: 7.0.3
resolution: "cross-spawn@npm:7.0.3"
version: 7.0.6
resolution: "cross-spawn@npm:7.0.6"
dependencies:
path-key: ^3.1.0
shebang-command: ^2.0.0
which: ^2.0.1
checksum: 671cc7c7288c3a8406f3c69a3ae2fc85555c04169e9d611def9a675635472614f1c0ed0ef80955d5b6d4e724f6ced67f0ad1bb006c2ea643488fcfef994d7f52
checksum: 8d306efacaf6f3f60e0224c287664093fa9185680b2d195852ba9a863f85d02dcc737094c6e512175f8ee0161f9b87c73c6826034c2422e39de7d6569cf4503b
languageName: node
linkType: hard

Expand Down Expand Up @@ -9881,18 +9893,6 @@ __metadata:
languageName: node
linkType: hard

"eslint-plugin-eslint-comments@npm:^3.1.2":
version: 3.2.0
resolution: "eslint-plugin-eslint-comments@npm:3.2.0"
dependencies:
escape-string-regexp: ^1.0.5
ignore: ^5.0.5
peerDependencies:
eslint: ">=4.19.1"
checksum: c9fe273dd56699abdf7e416cfad0344eb50aa01564a5a9133e72d982defb89310bc2e9b0b148ce19c5190d7ff641223b0ba9e667a194bc48467c3dd0d471e657
languageName: node
linkType: hard

"eslint-plugin-import@npm:^2.6.0":
version: 2.29.1
resolution: "eslint-plugin-import@npm:2.29.1"
Expand Down Expand Up @@ -12215,7 +12215,7 @@ __metadata:
languageName: node
linkType: hard

"ignore@npm:^5.0.5, ignore@npm:^5.2.0, ignore@npm:^5.2.4":
"ignore@npm:^5.2.0, ignore@npm:^5.2.4":
version: 5.3.2
resolution: "ignore@npm:5.3.2"
checksum: 2acfd32a573260ea522ea0bfeff880af426d68f6831f973129e2ba7363f422923cf53aab62f8369cbf4667c7b25b6f8a3761b34ecdb284ea18e87a5262a865be
Expand Down

0 comments on commit bfca8bf

Please sign in to comment.