1 | "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
2 |
|
3 | var _types = require('../parser/tokenizer/types');
|
4 |
|
5 |
|
6 | var _Transformer = require('./Transformer'); var _Transformer2 = _interopRequireDefault(_Transformer);
|
7 |
|
8 | const JEST_GLOBAL_NAME = "jest";
|
9 | const HOISTED_METHODS = ["mock", "unmock", "enableAutomock", "disableAutomock"];
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | class JestHoistTransformer extends _Transformer2.default {
|
20 | __init() {this.hoistedFunctionNames = []}
|
21 |
|
22 | constructor(
|
23 | rootTransformer,
|
24 | tokens,
|
25 | nameManager,
|
26 | importProcessor,
|
27 | ) {
|
28 | super();this.rootTransformer = rootTransformer;this.tokens = tokens;this.nameManager = nameManager;this.importProcessor = importProcessor;JestHoistTransformer.prototype.__init.call(this);;
|
29 | }
|
30 |
|
31 | process() {
|
32 | if (
|
33 | this.tokens.currentToken().scopeDepth === 0 &&
|
34 | this.tokens.matches4(_types.TokenType.name, _types.TokenType.dot, _types.TokenType.name, _types.TokenType.parenL) &&
|
35 | this.tokens.identifierName() === JEST_GLOBAL_NAME
|
36 | ) {
|
37 |
|
38 |
|
39 | if (_optionalChain([this, 'access', _ => _.importProcessor, 'optionalAccess', _2 => _2.getGlobalNames, 'call', _3 => _3(), 'optionalAccess', _4 => _4.has, 'call', _5 => _5(JEST_GLOBAL_NAME)])) {
|
40 | return false;
|
41 | }
|
42 | return this.extractHoistedCalls();
|
43 | }
|
44 |
|
45 | return false;
|
46 | }
|
47 |
|
48 | getHoistedCode() {
|
49 | if (this.hoistedFunctionNames.length > 0) {
|
50 |
|
51 |
|
52 | return this.hoistedFunctionNames.map((name) => `${name}();`).join("");
|
53 | }
|
54 | return "";
|
55 | }
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | extractHoistedCalls() {
|
66 |
|
67 |
|
68 | this.tokens.removeToken();
|
69 |
|
70 |
|
71 | let followsNonHoistedJestCall = false;
|
72 |
|
73 |
|
74 | while (this.tokens.matches3(_types.TokenType.dot, _types.TokenType.name, _types.TokenType.parenL)) {
|
75 | const methodName = this.tokens.identifierNameAtIndex(this.tokens.currentIndex() + 1);
|
76 | const shouldHoist = HOISTED_METHODS.includes(methodName);
|
77 | if (shouldHoist) {
|
78 |
|
79 |
|
80 | const hoistedFunctionName = this.nameManager.claimFreeName("__jestHoist");
|
81 | this.hoistedFunctionNames.push(hoistedFunctionName);
|
82 | this.tokens.replaceToken(`function ${hoistedFunctionName}(){${JEST_GLOBAL_NAME}.`);
|
83 | this.tokens.copyToken();
|
84 | this.tokens.copyToken();
|
85 | this.rootTransformer.processBalancedCode();
|
86 | this.tokens.copyExpectedToken(_types.TokenType.parenR);
|
87 | this.tokens.appendCode(";}");
|
88 | followsNonHoistedJestCall = false;
|
89 | } else {
|
90 |
|
91 | if (followsNonHoistedJestCall) {
|
92 |
|
93 |
|
94 |
|
95 | this.tokens.copyToken();
|
96 | } else {
|
97 |
|
98 |
|
99 | this.tokens.replaceToken(`${JEST_GLOBAL_NAME}.`);
|
100 | }
|
101 | this.tokens.copyToken();
|
102 | this.tokens.copyToken();
|
103 | this.rootTransformer.processBalancedCode();
|
104 | this.tokens.copyExpectedToken(_types.TokenType.parenR);
|
105 | followsNonHoistedJestCall = true;
|
106 | }
|
107 | }
|
108 |
|
109 | return true;
|
110 | }
|
111 | } exports.default = JestHoistTransformer;
|