forked from babel/babel
/
index.js
72 lines (68 loc) 路 2.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import _helper from "../lib/index.js";
import { transformSync } from "@babel/core";
import * as t from "@babel/types";
const helper = _helper.default || _helper;
describe("@babel/helper-builder-react-jsx", () => {
// The builder-react-jsx usage in transform-react-jsx 7.9.0
// https://github.com/babel/babel/blob/v7.9.0/packages/babel-plugin-transform-react-jsx/src/transform-classic.js#L43
it("shuold pass post with plugin pass", () => {
const fn = jest.fn().mockReturnValue(t.identifier("foo"));
const visitor = helper({
post(state, pass) {
state.callee = pass.get("jsxIdentifier")();
},
});
visitor.Program = function enter(_, state) {
state.set("jsxIdentifier", fn);
};
const plugin = () => ({ visitor });
const input = `<element></element>`;
transformSync(input, {
filename: "builder-react-jsx-test.jsx",
configFile: false,
plugins: [plugin],
parserOpts: { plugins: ["jsx"] },
});
expect(fn).toBeCalledTimes(1);
});
it("shuold pass pre with plugin pass", () => {
const fn = jest.fn().mockReturnValue(t.identifier("foo"));
const visitor = helper({
pre(state, pass) {
state.callee = pass.get("jsxIdentifier")();
},
});
visitor.Program = function enter(_, state) {
state.set("jsxIdentifier", fn);
};
const plugin = () => ({ visitor });
const input = `<element></element>`;
transformSync(input, {
filename: "builder-react-jsx-test.jsx",
configFile: false,
plugins: [plugin],
parserOpts: { plugins: ["jsx"] },
});
expect(fn).toBeCalledTimes(1);
});
it("shuold pass filter with plugin pass", () => {
const fn = jest.fn().mockReturnValue(false);
const visitor = helper({
filter(_, pass) {
return pass.get("filterAll")();
},
});
visitor.Program = function enter(_, state) {
state.set("filterAll", fn);
};
const plugin = () => ({ visitor });
const input = `<element></element>`;
transformSync(input, {
filename: "builder-react-jsx-test.jsx",
configFile: false,
plugins: [plugin],
parserOpts: { plugins: ["jsx"] },
});
expect(fn).toBeCalledTimes(1);
});
});