mirror of
https://github.com/swc-project/swc.git
synced 2024-12-26 07:02:28 +03:00
feat(es/jest): Support chaining of jest function calls (#6747)
**Related issue:** - Closes https://github.com/swc-project/swc/issues/6540.
This commit is contained in:
parent
1638105865
commit
72fb606eb2
23
crates/swc/tests/fixture/jest/issue-6540/input/.swcrc
Normal file
23
crates/swc/tests/fixture/jest/issue-6540/input/.swcrc
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"jsc": {
|
||||||
|
"parser": {
|
||||||
|
"syntax": "typescript",
|
||||||
|
"tsx": false,
|
||||||
|
"dynamicImport": true,
|
||||||
|
"decorators": true
|
||||||
|
},
|
||||||
|
"transform": {
|
||||||
|
"legacyDecorator": true,
|
||||||
|
"hidden": {
|
||||||
|
"jest": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"target": "es2016",
|
||||||
|
"loose": false,
|
||||||
|
"externalHelpers": false,
|
||||||
|
"keepClassNames": false
|
||||||
|
},
|
||||||
|
"module": {
|
||||||
|
"type": "commonjs"
|
||||||
|
}
|
||||||
|
}
|
7
crates/swc/tests/fixture/jest/issue-6540/input/index.ts
Normal file
7
crates/swc/tests/fixture/jest/issue-6540/input/index.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import foo from "./foo";
|
||||||
|
|
||||||
|
jest.mock("./foo").mock("./bar");
|
||||||
|
|
||||||
|
test("Foo is a mock", () => {
|
||||||
|
expect(jest.isMockFunction(foo)).toBe(true);
|
||||||
|
});
|
10
crates/swc/tests/fixture/jest/issue-6540/output/index.ts
Normal file
10
crates/swc/tests/fixture/jest/issue-6540/output/index.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
"use strict";
|
||||||
|
jest.mock("./foo").mock("./bar");
|
||||||
|
Object.defineProperty(exports, "__esModule", {
|
||||||
|
value: true
|
||||||
|
});
|
||||||
|
const _interopRequireDefault = require("@swc/helpers/lib/_interop_require_default.js").default;
|
||||||
|
const _foo = /*#__PURE__*/ _interopRequireDefault(require("./foo"));
|
||||||
|
test("Foo is a mock", ()=>{
|
||||||
|
expect(jest.isMockFunction(_foo.default)).toBe(true);
|
||||||
|
});
|
@ -44,14 +44,13 @@ impl Jest {
|
|||||||
prop: MemberProp::Ident(prop),
|
prop: MemberProp::Ident(prop),
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
) => match &*callee.obj {
|
) => {
|
||||||
Expr::Ident(i)
|
if is_jest(&callee.obj) && HOIST_METHODS.contains(&*prop.sym) {
|
||||||
if i.sym == *"jest" && HOIST_METHODS.contains(&*prop.sym) =>
|
|
||||||
{
|
|
||||||
hoisted.push(T::from_stmt(stmt))
|
hoisted.push(T::from_stmt(stmt))
|
||||||
|
} else {
|
||||||
|
new.push(T::from_stmt(stmt));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => new.push(T::from_stmt(stmt)),
|
|
||||||
},
|
|
||||||
_ => new.push(T::from_stmt(stmt)),
|
_ => new.push(T::from_stmt(stmt)),
|
||||||
},
|
},
|
||||||
_ => new.push(T::from_stmt(stmt)),
|
_ => new.push(T::from_stmt(stmt)),
|
||||||
@ -80,3 +79,15 @@ impl VisitMut for Jest {
|
|||||||
self.visit_mut_stmt_like(stmts)
|
self.visit_mut_stmt_like(stmts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_jest(e: &Expr) -> bool {
|
||||||
|
match e {
|
||||||
|
Expr::Ident(i) => i.sym == *"jest",
|
||||||
|
Expr::Member(MemberExpr { obj, .. }) => is_jest(obj),
|
||||||
|
Expr::Call(CallExpr {
|
||||||
|
callee: Callee::Expr(callee),
|
||||||
|
..
|
||||||
|
}) => is_jest(callee),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user