UNPKG

17.7 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*Copyright (c) 2015, Robert Binna <r.binna@synedra.com>
8
9 Permission to use, copy, modify, and/or distribute this software for any
10 purpose with or without fee is hereby granted, provided that the above
11 copyright notice and this permission notice appear in all copies.
12
13 THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.*/
20
21var _Templates = require('./Templates.js');
22
23var _RewireHelpers = require('./RewireHelpers.js');
24
25var _babelTypes = require('babel-types');
26
27var t = _interopRequireWildcard(_babelTypes);
28
29function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33var RewireState = function () {
34 function RewireState(scope) {
35 _classCallCheck(this, RewireState);
36
37 this.isES6Module = false;
38 this.hasES6Export = false;
39 this.hasES6DefaultExport = false;
40 this.nodesToAppendToProgramBody = [];
41 this.hasCommonJSExport = false;
42 this.accessors = {};
43 this.trackedIdentfiers = {};
44 this.isWildcardImport = {};
45 this.ignoredIdentifiers = [];
46 this.updateableVariables = {};
47 this.rewiredDataIdentifier = scope.generateUidIdentifier('__RewiredData__');
48 this.originalVariableAccessorIdentifier = scope.generateUidIdentifier('__get_original__');
49 this.originalVariableSetterIdentifier = scope.generateUidIdentifier('__set_original__');
50 this.updateOperationIdentifier = scope.generateUidIdentifier('__update_operation__');
51 this.assignmentOperationIdentifier = scope.generateUidIdentifier('__assign__');
52 this.typeofOriginalExportVariable = scope.generateUidIdentifier('typeOfOriginalExport');
53
54 this.universalAccessors = {
55 __get__: (0, _RewireHelpers.noRewire)(scope.generateUidIdentifier('__get__')),
56 __set__: (0, _RewireHelpers.noRewire)(scope.generateUidIdentifier('__set__')),
57 __reset__: (0, _RewireHelpers.noRewire)(scope.generateUidIdentifier('__reset__')),
58 __with__: (0, _RewireHelpers.noRewire)(scope.generateUidIdentifier('__with__')),
59 __RewireAPI__: (0, _RewireHelpers.noRewire)(scope.generateUidIdentifier('__RewireAPI__')),
60 __assignOperation: (0, _RewireHelpers.noRewire)(scope.generateUidIdentifier('__assign__'))
61 };
62 }
63
64 _createClass(RewireState, [{
65 key: 'appendToProgramBody',
66 value: function appendToProgramBody(nodes) {
67 if (!Array.isArray(nodes)) {
68 nodes = [nodes];
69 }
70 this.nodesToAppendToProgramBody = this.nodesToAppendToProgramBody.concat(nodes);
71 }
72 }, {
73 key: 'prependToProgramBody',
74 value: function prependToProgramBody(nodes) {
75 if (!Array.isArray(nodes)) {
76 nodes = [nodes];
77 }
78 this.nodesToAppendToProgramBody = nodes.concat(this.nodesToAppendToProgramBody);
79 }
80 }, {
81 key: 'ensureAccessor',
82 value: function ensureAccessor(variableName) {
83 var isWildcardImport = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
84
85 if (!this.accessors[variableName]) {
86 this.accessors[variableName] = true;
87 this.addTrackedIdentifier(variableName, isWildcardImport);
88 }
89
90 return this.accessors[variableName];
91 }
92 }, {
93 key: 'addTrackedIdentifier',
94 value: function addTrackedIdentifier(variableName) {
95 var isWildcardImport = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
96
97 this.isWildcardImport[variableName] = isWildcardImport;
98 return this.trackedIdentfiers[variableName] = true;
99 }
100 }, {
101 key: 'hasTrackedIdentifier',
102 value: function hasTrackedIdentifier(variableName) {
103 return !!this.trackedIdentfiers[variableName];
104 }
105 }, {
106 key: 'addUpdateableVariable',
107 value: function addUpdateableVariable(variableName) {
108 this.updateableVariables[variableName] = true;
109 this.ensureAccessor(variableName);
110 }
111 }, {
112 key: 'setIgnoredIdentifiers',
113 value: function setIgnoredIdentifiers(ignoredIdentifiers) {
114 this.ignoredIdentifiers = ignoredIdentifiers || [];
115 }
116 }, {
117 key: 'prependUniversalAccessors',
118 value: function prependUniversalAccessors(scope) {
119 var hasWildcardImport = Object.keys(this.isWildcardImport).some(function (propertyName) {
120 return this.isWildcardImport[propertyName];
121 }.bind(this));
122 var filterWildcardImportIdentifier = hasWildcardImport && (0, _RewireHelpers.noRewire)(scope.generateUidIdentifier('__filterWildcardImport__')) || null;
123
124 var originalAccessor = t.functionDeclaration(this.originalVariableAccessorIdentifier, [t.identifier('variableName')], t.blockStatement([t.switchStatement(t.identifier('variableName'), Object.keys(this.accessors).map(function (identifierName) {
125 var accessOriginalVariable = (0, _RewireHelpers.noRewire)(t.identifier(identifierName));
126
127 if (this.isWildcardImport[identifierName]) {
128 accessOriginalVariable = t.callExpression(filterWildcardImportIdentifier, [accessOriginalVariable]);
129 }
130
131 return t.switchCase(t.stringLiteral(identifierName), [t.returnStatement(accessOriginalVariable)]);
132 }, this)), t.returnStatement((0, _RewireHelpers.noRewire)(t.identifier('undefined')))]));
133
134 var valueVariable = scope.generateUidIdentifier('value');
135 var originalSetter = t.functionDeclaration(this.originalVariableSetterIdentifier, [t.identifier('variableName'), valueVariable], t.blockStatement([t.switchStatement(t.identifier('variableName'), Object.keys(this.updateableVariables).map(function (identifierName) {
136 return t.switchCase(t.stringLiteral(identifierName), [t.returnStatement(t.assignmentExpression('=', (0, _RewireHelpers.noRewire)(t.identifier(identifierName)), valueVariable))]);
137 })), t.returnStatement((0, _RewireHelpers.noRewire)(t.identifier('undefined')))]));
138
139 this.prependToProgramBody((0, _Templates.universalAccesorsTemplate)({
140 ORIGINAL_VARIABLE_ACCESSOR_IDENTIFIER: this.originalVariableAccessorIdentifier,
141 ORIGINAL_VARIABLE_SETTER_IDENTIFIER: this.originalVariableSetterIdentifier,
142 ASSIGNMENT_OPERATION_IDENTIFIER: this.assignmentOperationIdentifier,
143 UPDATE_OPERATION_IDENTIFIER: this.updateOperationIdentifier,
144 ORIGINAL_ACCESSOR: originalAccessor,
145 ORIGINAL_SETTER: originalSetter,
146 UNIVERSAL_GETTER_ID: this.getUniversalGetterID(),
147 UNIVERSAL_SETTER_ID: this.getUniversalSetterID(),
148 UNIVERSAL_RESETTER_ID: this.getUniversalResetterID(),
149 UNIVERSAL_WITH_ID: this.getUniversalWithID(),
150 API_OBJECT_ID: this.getAPIObjectID(),
151 REWIRED_DATA_IDENTIFIER: this.rewiredDataIdentifier
152 }));
153
154 if (hasWildcardImport) {
155 this.appendToProgramBody((0, _Templates.filterWildcardImportTemplate)({
156 FILTER_WILDCARD_IMPORT_IDENTIFIER: filterWildcardImportIdentifier
157 }));
158 }
159 }
160 }, {
161 key: 'appendExports',
162 value: function appendExports() {
163 if (this.isES6Module && (!this.hasCommonJSExport || this.hasES6Export)) {
164 this.appendToProgramBody(this.generateNamedExports());
165
166 if (!this.hasES6DefaultExport) {
167 this.appendToProgramBody(t.exportDefaultDeclaration(this.getAPIObjectID()));
168 }
169 } else if (!this.isES6Module || !this.hasES6Export && this.hasCommonJSExport) {
170 var commonJSExport = t.memberExpression(t.identifier('module'), t.identifier('exports'), false);
171 this.enrichExport(commonJSExport);
172 }
173 }
174 }, {
175 key: 'enrichExport',
176 value: function enrichExport(exportValue) {
177 this.appendToProgramBody((0, _Templates.enrichExportTemplate)({
178 TYPEOFORIGINALEXPORTVARIABLE: this.getTypeOfOriginalExportVariable(),
179 UNIVERSAL_GETTER_ID: this.getUniversalGetterID(),
180 UNIVERSAL_SETTER_ID: this.getUniversalSetterID(),
181 UNIVERSAL_RESETTER_ID: this.getUniversalResetterID(),
182 UNIVERSAL_WITH_ID: this.getUniversalWithID(),
183 API_OBJECT_ID: this.getAPIObjectID(),
184 EXPORT_VALUE: exportValue
185 }));
186 }
187 }, {
188 key: 'generateNamedExports',
189 value: function generateNamedExports() {
190 return t.exportNamedDeclaration(null, [t.exportSpecifier(this.getUniversalGetterID(), t.identifier('__get__')), t.exportSpecifier(this.getUniversalGetterID(), t.identifier('__GetDependency__')), t.exportSpecifier(this.getUniversalSetterID(), t.identifier('__Rewire__')), t.exportSpecifier(this.getUniversalSetterID(), t.identifier('__set__')), t.exportSpecifier(this.getUniversalResetterID(), t.identifier('__ResetDependency__')), t.exportSpecifier(this.getAPIObjectID(), t.identifier('__RewireAPI__'))]);
191 }
192 }, {
193 key: 'containsDependenciesToRewire',
194 value: function containsDependenciesToRewire() {
195 return Object.keys(this.accessors).length > 0 || Object.keys(this.updateableVariables).length > 0;
196 }
197 }, {
198 key: 'getUniversalGetterID',
199 value: function getUniversalGetterID() {
200 return this.universalAccessors['__get__'];
201 }
202 }, {
203 key: 'getUpdateOperationID',
204 value: function getUpdateOperationID() {
205 return this.updateOperationIdentifier;
206 }
207 }, {
208 key: 'getAssignmentOperationID',
209 value: function getAssignmentOperationID() {
210 return this.assignmentOperationIdentifier;
211 }
212 }, {
213 key: 'getUniversalSetterID',
214 value: function getUniversalSetterID() {
215 return this.universalAccessors['__set__'];
216 }
217 }, {
218 key: 'getUniversalResetterID',
219 value: function getUniversalResetterID() {
220 return this.universalAccessors['__reset__'];
221 }
222 }, {
223 key: 'getUniversalWithID',
224 value: function getUniversalWithID() {
225 return this.universalAccessors['__with__'];
226 }
227 }, {
228 key: 'getAPIObjectID',
229 value: function getAPIObjectID() {
230 return this.universalAccessors['__RewireAPI__'];
231 }
232 }, {
233 key: 'getTypeOfOriginalExportVariable',
234 value: function getTypeOfOriginalExportVariable() {
235 return this.typeofOriginalExportVariable;
236 }
237 }]);
238
239 return RewireState;
240}();
241
242exports.default = RewireState;
243;
\No newline at end of file