UNPKG

3.34 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7
8var _assert = _interopRequireDefault(require("assert"));
9
10var _Plugin = _interopRequireDefault(require("./Plugin"));
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14function _default({
15 types
16}) {
17 let plugins = null; // Only for test
18
19 global.__clearBabelAntdPlugin = () => {
20 plugins = null;
21 };
22
23 function applyInstance(method, args, context) {
24 var _iteratorNormalCompletion = true;
25 var _didIteratorError = false;
26 var _iteratorError = undefined;
27
28 try {
29 for (var _iterator = plugins[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
30 const plugin = _step.value;
31
32 if (plugin[method]) {
33 plugin[method].apply(plugin, [...args, context]);
34 }
35 }
36 } catch (err) {
37 _didIteratorError = true;
38 _iteratorError = err;
39 } finally {
40 try {
41 if (!_iteratorNormalCompletion && _iterator.return != null) {
42 _iterator.return();
43 }
44 } finally {
45 if (_didIteratorError) {
46 throw _iteratorError;
47 }
48 }
49 }
50 }
51
52 const Program = {
53 enter(path, {
54 opts = {}
55 }) {
56 // Init plugin instances once.
57 if (!plugins) {
58 if (Array.isArray(opts)) {
59 plugins = opts.map(({
60 libraryName,
61 libraryDirectory,
62 style,
63 styleLibraryDirectory,
64 customStyleName,
65 camel2DashComponentName,
66 camel2UnderlineComponentName,
67 fileName,
68 customName,
69 transformToDefaultImport
70 }, index) => {
71 (0, _assert.default)(libraryName, 'libraryName should be provided');
72 return new _Plugin.default(libraryName, libraryDirectory, style, styleLibraryDirectory, customStyleName, camel2DashComponentName, camel2UnderlineComponentName, fileName, customName, transformToDefaultImport, types, index);
73 });
74 } else {
75 (0, _assert.default)(opts.libraryName, 'libraryName should be provided');
76 plugins = [new _Plugin.default(opts.libraryName, opts.libraryDirectory, opts.style, opts.styleLibraryDirectory, opts.customStyleName, opts.camel2DashComponentName, opts.camel2UnderlineComponentName, opts.fileName, opts.customName, opts.transformToDefaultImport, types)];
77 }
78 }
79
80 applyInstance('ProgramEnter', arguments, this); // eslint-disable-line
81 },
82
83 exit() {
84 applyInstance('ProgramExit', arguments, this); // eslint-disable-line
85 }
86
87 };
88 const methods = ['ImportDeclaration', 'CallExpression', 'MemberExpression', 'Property', 'VariableDeclarator', 'ArrayExpression', 'LogicalExpression', 'ConditionalExpression', 'IfStatement', 'ExpressionStatement', 'ReturnStatement', 'ExportDefaultDeclaration', 'BinaryExpression', 'NewExpression', 'ClassDeclaration'];
89 const ret = {
90 visitor: {
91 Program
92 }
93 };
94
95 for (var _i = 0; _i < methods.length; _i++) {
96 const method = methods[_i];
97
98 ret.visitor[method] = function () {
99 // eslint-disable-line
100 applyInstance(method, arguments, ret.visitor); // eslint-disable-line
101 };
102 }
103
104 return ret;
105}
\No newline at end of file