@atlaskit/codemod-cli 0.27.2 → 0.27.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/cjs/main.js +85 -33
- package/dist/cjs/presets/index.js +2 -1
- package/dist/cjs/presets/remove-token-fallbacks/remove-token-fallbacks.js +225 -0
- package/dist/cjs/presets/remove-token-fallbacks/types.js +5 -0
- package/dist/cjs/presets/remove-token-fallbacks/utils/all-tokens.js +44 -0
- package/dist/cjs/presets/remove-token-fallbacks/utils/color-utils.js +93 -0
- package/dist/cjs/presets/remove-token-fallbacks/utils/get-team-info.js +51 -0
- package/dist/cjs/presets/remove-token-fallbacks/utils/normalize-values.js +113 -0
- package/dist/cjs/presets/remove-token-fallbacks/utils/remove-unused-imports.js +61 -0
- package/dist/cjs/presets/remove-token-fallbacks/utils/remove-unused-variables.js +37 -0
- package/dist/cjs/presets/remove-token-fallbacks/utils/reporter.js +310 -0
- package/dist/cjs/presets/remove-token-fallbacks/utils/token-processor.js +632 -0
- package/dist/cjs/presets/remove-token-fallbacks/utils/update-comments.js +58 -0
- package/dist/es2019/main.js +24 -0
- package/dist/es2019/presets/index.js +2 -1
- package/dist/es2019/presets/remove-token-fallbacks/remove-token-fallbacks.js +130 -0
- package/dist/es2019/presets/remove-token-fallbacks/types.js +1 -0
- package/dist/es2019/presets/remove-token-fallbacks/utils/all-tokens.js +30 -0
- package/dist/es2019/presets/remove-token-fallbacks/utils/color-utils.js +84 -0
- package/dist/es2019/presets/remove-token-fallbacks/utils/get-team-info.js +22 -0
- package/dist/es2019/presets/remove-token-fallbacks/utils/normalize-values.js +104 -0
- package/dist/es2019/presets/remove-token-fallbacks/utils/remove-unused-imports.js +51 -0
- package/dist/es2019/presets/remove-token-fallbacks/utils/remove-unused-variables.js +31 -0
- package/dist/es2019/presets/remove-token-fallbacks/utils/reporter.js +118 -0
- package/dist/es2019/presets/remove-token-fallbacks/utils/token-processor.js +377 -0
- package/dist/es2019/presets/remove-token-fallbacks/utils/update-comments.js +46 -0
- package/dist/esm/main.js +85 -33
- package/dist/esm/presets/index.js +2 -1
- package/dist/esm/presets/remove-token-fallbacks/remove-token-fallbacks.js +215 -0
- package/dist/esm/presets/remove-token-fallbacks/types.js +1 -0
- package/dist/esm/presets/remove-token-fallbacks/utils/all-tokens.js +38 -0
- package/dist/esm/presets/remove-token-fallbacks/utils/color-utils.js +86 -0
- package/dist/esm/presets/remove-token-fallbacks/utils/get-team-info.js +44 -0
- package/dist/esm/presets/remove-token-fallbacks/utils/normalize-values.js +107 -0
- package/dist/esm/presets/remove-token-fallbacks/utils/remove-unused-imports.js +55 -0
- package/dist/esm/presets/remove-token-fallbacks/utils/remove-unused-variables.js +31 -0
- package/dist/esm/presets/remove-token-fallbacks/utils/reporter.js +302 -0
- package/dist/esm/presets/remove-token-fallbacks/utils/token-processor.js +625 -0
- package/dist/esm/presets/remove-token-fallbacks/utils/update-comments.js +51 -0
- package/dist/types/presets/index.d.ts +1 -0
- package/dist/types/presets/remove-token-fallbacks/remove-token-fallbacks.d.ts +29 -0
- package/dist/types/presets/remove-token-fallbacks/types.d.ts +39 -0
- package/dist/types/presets/remove-token-fallbacks/utils/all-tokens.d.ts +1 -0
- package/dist/types/presets/remove-token-fallbacks/utils/color-utils.d.ts +3 -0
- package/dist/types/presets/remove-token-fallbacks/utils/get-team-info.d.ts +8 -0
- package/dist/types/presets/remove-token-fallbacks/utils/normalize-values.d.ts +8 -0
- package/dist/types/presets/remove-token-fallbacks/utils/remove-unused-imports.d.ts +2 -0
- package/dist/types/presets/remove-token-fallbacks/utils/remove-unused-variables.d.ts +2 -0
- package/dist/types/presets/remove-token-fallbacks/utils/reporter.d.ts +4 -0
- package/dist/types/presets/remove-token-fallbacks/utils/token-processor.d.ts +30 -0
- package/dist/types/presets/remove-token-fallbacks/utils/update-comments.d.ts +2 -0
- package/dist/types-ts4.5/presets/index.d.ts +1 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/remove-token-fallbacks.d.ts +29 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/types.d.ts +39 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/all-tokens.d.ts +1 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/color-utils.d.ts +3 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/get-team-info.d.ts +8 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/normalize-values.d.ts +8 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/remove-unused-imports.d.ts +2 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/remove-unused-variables.d.ts +2 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/reporter.d.ts +4 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/token-processor.d.ts +30 -0
- package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/update-comments.d.ts +2 -0
- package/package.json +8 -3
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.TokenProcessor = void 0;
|
|
8
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
9
|
+
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
10
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
11
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
12
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
13
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
14
|
+
var _promises = _interopRequireDefault(require("fs/promises"));
|
|
15
|
+
var _path = _interopRequireDefault(require("path"));
|
|
16
|
+
var _chalk = _interopRequireDefault(require("chalk"));
|
|
17
|
+
var _normalizeValues2 = require("./normalize-values");
|
|
18
|
+
var _updateComments = require("./update-comments");
|
|
19
|
+
var _excluded = ["shouldLog"];
|
|
20
|
+
/* eslint-disable no-console */
|
|
21
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
|
|
22
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
23
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
24
|
+
var TokenProcessor = exports.TokenProcessor = /*#__PURE__*/function () {
|
|
25
|
+
function TokenProcessor(j, options, fileInfo, source, rootDir, details, tokenMap, teamInfo) {
|
|
26
|
+
(0, _classCallCheck2.default)(this, TokenProcessor);
|
|
27
|
+
(0, _defineProperty2.default)(this, "logMessages", []);
|
|
28
|
+
(0, _defineProperty2.default)(this, "possibleExtensions", ['.ts', '.tsx', '.js', '.jsx']);
|
|
29
|
+
this.j = j;
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.fileInfo = fileInfo;
|
|
32
|
+
this.source = source;
|
|
33
|
+
this.rootDir = rootDir;
|
|
34
|
+
this.details = details;
|
|
35
|
+
this.tokenMap = tokenMap;
|
|
36
|
+
this.teamInfo = teamInfo;
|
|
37
|
+
}
|
|
38
|
+
return (0, _createClass2.default)(TokenProcessor, [{
|
|
39
|
+
key: "processAndLogSingleToken",
|
|
40
|
+
value: function () {
|
|
41
|
+
var _processAndLogSingleToken = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(callPath) {
|
|
42
|
+
var _callPath$node$loc;
|
|
43
|
+
var line, _yield$this$processSi, shouldLog, rest, coloredPath, coloredLine;
|
|
44
|
+
return _regenerator.default.wrap(function _callee$(_context) {
|
|
45
|
+
while (1) switch (_context.prev = _context.next) {
|
|
46
|
+
case 0:
|
|
47
|
+
line = (_callPath$node$loc = callPath.node.loc) === null || _callPath$node$loc === void 0 ? void 0 : _callPath$node$loc.start.line;
|
|
48
|
+
_context.next = 3;
|
|
49
|
+
return this.processSingleToken(callPath);
|
|
50
|
+
case 3:
|
|
51
|
+
_yield$this$processSi = _context.sent;
|
|
52
|
+
shouldLog = _yield$this$processSi.shouldLog;
|
|
53
|
+
rest = (0, _objectWithoutProperties2.default)(_yield$this$processSi, _excluded);
|
|
54
|
+
if (!(this.options.silent || !shouldLog)) {
|
|
55
|
+
_context.next = 8;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
return _context.abrupt("return", rest);
|
|
59
|
+
case 8:
|
|
60
|
+
coloredPath = _chalk.default.blue(this.fileInfo.path);
|
|
61
|
+
coloredLine = line ? ": ".concat(_chalk.default.green(line)) : '';
|
|
62
|
+
console.log("".concat(coloredPath).concat(coloredLine, ": ").concat(this.logMessages.join(' | '), "\n----------------------------------------"));
|
|
63
|
+
return _context.abrupt("return", rest);
|
|
64
|
+
case 12:
|
|
65
|
+
case "end":
|
|
66
|
+
return _context.stop();
|
|
67
|
+
}
|
|
68
|
+
}, _callee, this);
|
|
69
|
+
}));
|
|
70
|
+
function processAndLogSingleToken(_x) {
|
|
71
|
+
return _processAndLogSingleToken.apply(this, arguments);
|
|
72
|
+
}
|
|
73
|
+
return processAndLogSingleToken;
|
|
74
|
+
}()
|
|
75
|
+
}, {
|
|
76
|
+
key: "logVerbose",
|
|
77
|
+
value: function logVerbose(message) {
|
|
78
|
+
if (this.options.verbose) {
|
|
79
|
+
this.log(message);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}, {
|
|
83
|
+
key: "log",
|
|
84
|
+
value: function log(message) {
|
|
85
|
+
this.logMessages.push(message);
|
|
86
|
+
}
|
|
87
|
+
}, {
|
|
88
|
+
key: "logError",
|
|
89
|
+
value: function logError(message) {
|
|
90
|
+
this.log(_chalk.default.red(message));
|
|
91
|
+
}
|
|
92
|
+
}, {
|
|
93
|
+
key: "processSingleToken",
|
|
94
|
+
value: function () {
|
|
95
|
+
var _processSingleToken = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(callPath) {
|
|
96
|
+
var _callPath$node$loc2;
|
|
97
|
+
var args, line, tokenKey, isSkipped, tokenValue, _ref, rawFallbackValue, fallbackValue, resolvedImportDeclaration, resolvedLocalVarDeclaration, _normalizeValues, difference, isAcceptableDifference, tokenLogValue, fallbackLogValue, normalizedTokenValue, normalizedFallbackValue, areEqual, logData, fallbackRemoved, importDeclaration, localVarDeclaration, message;
|
|
98
|
+
return _regenerator.default.wrap(function _callee2$(_context2) {
|
|
99
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
100
|
+
case 0:
|
|
101
|
+
args = callPath.node.arguments;
|
|
102
|
+
line = (_callPath$node$loc2 = callPath.node.loc) === null || _callPath$node$loc2 === void 0 ? void 0 : _callPath$node$loc2.start.line;
|
|
103
|
+
if (!(args.length < 2)) {
|
|
104
|
+
_context2.next = 5;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
this.logVerbose(_chalk.default.yellow('Skipped token call without fallback'));
|
|
108
|
+
return _context2.abrupt("return", {
|
|
109
|
+
shouldLog: false,
|
|
110
|
+
fallbackRemoved: false,
|
|
111
|
+
resolvedImportDeclaration: undefined,
|
|
112
|
+
resolvedLocalVarDeclaration: undefined
|
|
113
|
+
});
|
|
114
|
+
case 5:
|
|
115
|
+
tokenKey = this.getTokenKey(args[0]);
|
|
116
|
+
if (tokenKey) {
|
|
117
|
+
_context2.next = 8;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
return _context2.abrupt("return", {
|
|
121
|
+
shouldLog: true,
|
|
122
|
+
fallbackRemoved: false,
|
|
123
|
+
resolvedImportDeclaration: undefined,
|
|
124
|
+
resolvedLocalVarDeclaration: undefined
|
|
125
|
+
});
|
|
126
|
+
case 8:
|
|
127
|
+
isSkipped = tokenKey.startsWith('elevation.shadow') || tokenKey.startsWith('font.body') || tokenKey.startsWith('font.heading');
|
|
128
|
+
tokenValue = isSkipped ? '' : this.tokenMap[tokenKey];
|
|
129
|
+
this.logVerbose("Token value from tokenMap: ".concat(_chalk.default.magenta(tokenValue), " for key: ").concat(_chalk.default.yellow(tokenKey)));
|
|
130
|
+
if (!isSkipped) {
|
|
131
|
+
_context2.next = 15;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
_context2.t0 = {
|
|
135
|
+
rawFallbackValue: 'N/A',
|
|
136
|
+
fallbackValue: undefined,
|
|
137
|
+
resolvedImportDeclaration: undefined,
|
|
138
|
+
resolvedLocalVarDeclaration: undefined
|
|
139
|
+
};
|
|
140
|
+
_context2.next = 18;
|
|
141
|
+
break;
|
|
142
|
+
case 15:
|
|
143
|
+
_context2.next = 17;
|
|
144
|
+
return this.getFallbackValue(args[1]);
|
|
145
|
+
case 17:
|
|
146
|
+
_context2.t0 = _context2.sent;
|
|
147
|
+
case 18:
|
|
148
|
+
_ref = _context2.t0;
|
|
149
|
+
rawFallbackValue = _ref.rawFallbackValue;
|
|
150
|
+
fallbackValue = _ref.fallbackValue;
|
|
151
|
+
resolvedImportDeclaration = _ref.resolvedImportDeclaration;
|
|
152
|
+
resolvedLocalVarDeclaration = _ref.resolvedLocalVarDeclaration;
|
|
153
|
+
_normalizeValues = (0, _normalizeValues2.normalizeValues)(tokenKey, tokenValue, fallbackValue), difference = _normalizeValues.difference, isAcceptableDifference = _normalizeValues.isAcceptableDifference, tokenLogValue = _normalizeValues.tokenLogValue, fallbackLogValue = _normalizeValues.fallbackLogValue, normalizedTokenValue = _normalizeValues.normalizedTokenValue, normalizedFallbackValue = _normalizeValues.normalizedFallbackValue;
|
|
154
|
+
areEqual = normalizedTokenValue === normalizedFallbackValue;
|
|
155
|
+
logData = {
|
|
156
|
+
teamInfo: this.teamInfo,
|
|
157
|
+
filePath: this.fileInfo.path,
|
|
158
|
+
lineNumber: line || -1,
|
|
159
|
+
tokenKey: tokenKey,
|
|
160
|
+
rawFallbackValue: rawFallbackValue,
|
|
161
|
+
resolvedTokenValue: tokenValue,
|
|
162
|
+
resolvedFallbackValue: fallbackValue !== null && fallbackValue !== void 0 ? fallbackValue : '',
|
|
163
|
+
difference: difference
|
|
164
|
+
};
|
|
165
|
+
fallbackRemoved = false;
|
|
166
|
+
if (areEqual || isAcceptableDifference || this.options.forceUpdate) {
|
|
167
|
+
this.log(_chalk.default.green(areEqual ? 'Token value and fallback value are equal, removing fallback' : 'Token value and fallback value are within acceptable difference threshold, removing fallback'));
|
|
168
|
+
args.pop();
|
|
169
|
+
this.details.replaced.push(logData);
|
|
170
|
+
fallbackRemoved = true;
|
|
171
|
+
importDeclaration = resolvedImportDeclaration;
|
|
172
|
+
localVarDeclaration = resolvedLocalVarDeclaration;
|
|
173
|
+
} else {
|
|
174
|
+
message = normalizedFallbackValue === undefined ? "Fallback value could not be resolved" : "Values mismatched significantly";
|
|
175
|
+
this.logError(message);
|
|
176
|
+
if (this.options.addEslintComments) {
|
|
177
|
+
(0, _updateComments.addOrUpdateEslintIgnoreComment)(this.j, tokenValue, fallbackValue, callPath);
|
|
178
|
+
}
|
|
179
|
+
this.details.notReplaced.push(logData);
|
|
180
|
+
}
|
|
181
|
+
this.log("Token: ".concat(_chalk.default.yellow(tokenKey), ", Raw fallback: ").concat(_chalk.default.yellow(rawFallbackValue), ", Resolved token value: ").concat(tokenLogValue, ", Resolved fallback value: ").concat(fallbackLogValue));
|
|
182
|
+
return _context2.abrupt("return", {
|
|
183
|
+
shouldLog: true,
|
|
184
|
+
fallbackRemoved: fallbackRemoved,
|
|
185
|
+
resolvedImportDeclaration: importDeclaration,
|
|
186
|
+
resolvedLocalVarDeclaration: localVarDeclaration
|
|
187
|
+
});
|
|
188
|
+
case 30:
|
|
189
|
+
case "end":
|
|
190
|
+
return _context2.stop();
|
|
191
|
+
}
|
|
192
|
+
}, _callee2, this);
|
|
193
|
+
}));
|
|
194
|
+
function processSingleToken(_x2) {
|
|
195
|
+
return _processSingleToken.apply(this, arguments);
|
|
196
|
+
}
|
|
197
|
+
return processSingleToken;
|
|
198
|
+
}()
|
|
199
|
+
}, {
|
|
200
|
+
key: "getTokenKey",
|
|
201
|
+
value: function getTokenKey(arg) {
|
|
202
|
+
if (arg.type === 'StringLiteral') {
|
|
203
|
+
var tokenKey = arg.value;
|
|
204
|
+
this.logVerbose("Determined token key as literal: ".concat(_chalk.default.yellow(tokenKey)));
|
|
205
|
+
return tokenKey;
|
|
206
|
+
} else {
|
|
207
|
+
this.logError("The first argument of token function is not a string literal");
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}, {
|
|
212
|
+
key: "getFallbackValue",
|
|
213
|
+
value: function () {
|
|
214
|
+
var _getFallbackValue = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(fallbackValueNode) {
|
|
215
|
+
return _regenerator.default.wrap(function _callee3$(_context3) {
|
|
216
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
217
|
+
case 0:
|
|
218
|
+
_context3.t0 = fallbackValueNode.type;
|
|
219
|
+
_context3.next = _context3.t0 === 'StringLiteral' ? 3 : _context3.t0 === 'Identifier' ? 4 : _context3.t0 === 'MemberExpression' ? 5 : _context3.t0 === 'TemplateLiteral' ? 6 : 7;
|
|
220
|
+
break;
|
|
221
|
+
case 3:
|
|
222
|
+
return _context3.abrupt("return", this.processFallbackAsStringLiteral(fallbackValueNode));
|
|
223
|
+
case 4:
|
|
224
|
+
return _context3.abrupt("return", this.processFallbackAsIdentifier(fallbackValueNode));
|
|
225
|
+
case 5:
|
|
226
|
+
return _context3.abrupt("return", this.processFallbackAsMemberExpression(fallbackValueNode));
|
|
227
|
+
case 6:
|
|
228
|
+
return _context3.abrupt("return", this.processFallbackAsTemplateLiteral(fallbackValueNode));
|
|
229
|
+
case 7:
|
|
230
|
+
return _context3.abrupt("return", {
|
|
231
|
+
fallbackValue: undefined,
|
|
232
|
+
rawFallbackValue: '',
|
|
233
|
+
resolvedImportDeclaration: undefined,
|
|
234
|
+
resolvedLocalVarDeclaration: undefined
|
|
235
|
+
});
|
|
236
|
+
case 8:
|
|
237
|
+
case "end":
|
|
238
|
+
return _context3.stop();
|
|
239
|
+
}
|
|
240
|
+
}, _callee3, this);
|
|
241
|
+
}));
|
|
242
|
+
function getFallbackValue(_x3) {
|
|
243
|
+
return _getFallbackValue.apply(this, arguments);
|
|
244
|
+
}
|
|
245
|
+
return getFallbackValue;
|
|
246
|
+
}()
|
|
247
|
+
}, {
|
|
248
|
+
key: "processFallbackAsStringLiteral",
|
|
249
|
+
value: function processFallbackAsStringLiteral(fallbackValueNode) {
|
|
250
|
+
var fallbackValue = fallbackValueNode.value;
|
|
251
|
+
this.logVerbose("Fallback value is a literal: ".concat(_chalk.default.yellow(fallbackValue)));
|
|
252
|
+
return {
|
|
253
|
+
fallbackValue: fallbackValue,
|
|
254
|
+
rawFallbackValue: fallbackValue,
|
|
255
|
+
resolvedImportDeclaration: undefined,
|
|
256
|
+
resolvedLocalVarDeclaration: undefined
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}, {
|
|
260
|
+
key: "processFallbackAsIdentifier",
|
|
261
|
+
value: function () {
|
|
262
|
+
var _processFallbackAsIdentifier = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(fallbackValueNode) {
|
|
263
|
+
var variableName, variableNameForLog, fallbackValue, localVarDeclaration, resolvedImportDeclaration, resolvedLocalVarDeclaration, init, importDeclaration, importSource;
|
|
264
|
+
return _regenerator.default.wrap(function _callee4$(_context4) {
|
|
265
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
266
|
+
case 0:
|
|
267
|
+
variableName = fallbackValueNode.name;
|
|
268
|
+
variableNameForLog = "".concat(_chalk.default.yellow(variableName));
|
|
269
|
+
this.logVerbose("Fallback is an identifier: ".concat(_chalk.default.yellow(variableName), ", attempting to resolve..."));
|
|
270
|
+
|
|
271
|
+
// Check for local variable declaration
|
|
272
|
+
localVarDeclaration = this.source.find(this.j.VariableDeclarator, {
|
|
273
|
+
id: {
|
|
274
|
+
name: variableName
|
|
275
|
+
}
|
|
276
|
+
}).at(0);
|
|
277
|
+
if (!localVarDeclaration.size()) {
|
|
278
|
+
_context4.next = 9;
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
init = localVarDeclaration.get().value.init;
|
|
282
|
+
if (init.type === 'Literal' || init.type === 'StringLiteral') {
|
|
283
|
+
fallbackValue = init.value;
|
|
284
|
+
resolvedLocalVarDeclaration = localVarDeclaration.paths()[0];
|
|
285
|
+
this.logVerbose("Resolved fallback value from local variable: ".concat(_chalk.default.yellow(fallbackValue), " for identifier: ").concat(variableNameForLog));
|
|
286
|
+
}
|
|
287
|
+
_context4.next = 19;
|
|
288
|
+
break;
|
|
289
|
+
case 9:
|
|
290
|
+
// Check for named import
|
|
291
|
+
importDeclaration = this.source.find(this.j.ImportDeclaration).filter(this.createImportFilter(variableName)).at(0);
|
|
292
|
+
if (!importDeclaration.size()) {
|
|
293
|
+
_context4.next = 18;
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
importSource = importDeclaration.get().value.source.value;
|
|
297
|
+
_context4.next = 14;
|
|
298
|
+
return this.resolveValueFromImport(this.rootDir, importSource, undefined, variableName);
|
|
299
|
+
case 14:
|
|
300
|
+
fallbackValue = _context4.sent;
|
|
301
|
+
if (fallbackValue !== undefined) {
|
|
302
|
+
this.logVerbose("Resolved fallback value from import: ".concat(_chalk.default.yellow(fallbackValue), " for identifier: ").concat(variableNameForLog));
|
|
303
|
+
resolvedImportDeclaration = importDeclaration.paths()[0];
|
|
304
|
+
}
|
|
305
|
+
_context4.next = 19;
|
|
306
|
+
break;
|
|
307
|
+
case 18:
|
|
308
|
+
this.logVerbose(_chalk.default.red("Could not resolve fallback value for identifier: ".concat(variableNameForLog, ": it's neither a local variable nor an import")));
|
|
309
|
+
case 19:
|
|
310
|
+
return _context4.abrupt("return", {
|
|
311
|
+
rawFallbackValue: variableName,
|
|
312
|
+
fallbackValue: fallbackValue,
|
|
313
|
+
resolvedImportDeclaration: resolvedImportDeclaration,
|
|
314
|
+
resolvedLocalVarDeclaration: resolvedLocalVarDeclaration
|
|
315
|
+
});
|
|
316
|
+
case 20:
|
|
317
|
+
case "end":
|
|
318
|
+
return _context4.stop();
|
|
319
|
+
}
|
|
320
|
+
}, _callee4, this);
|
|
321
|
+
}));
|
|
322
|
+
function processFallbackAsIdentifier(_x4) {
|
|
323
|
+
return _processFallbackAsIdentifier.apply(this, arguments);
|
|
324
|
+
}
|
|
325
|
+
return processFallbackAsIdentifier;
|
|
326
|
+
}()
|
|
327
|
+
}, {
|
|
328
|
+
key: "processFallbackAsMemberExpression",
|
|
329
|
+
value: function () {
|
|
330
|
+
var _processFallbackAsMemberExpression = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(fallbackValueNode) {
|
|
331
|
+
var objectName, propertyName, fallbackValue, rawFallbackValue, resolvedImportDeclaration, importDeclaration, importSource;
|
|
332
|
+
return _regenerator.default.wrap(function _callee5$(_context5) {
|
|
333
|
+
while (1) switch (_context5.prev = _context5.next) {
|
|
334
|
+
case 0:
|
|
335
|
+
if (fallbackValueNode.object.type === 'Identifier') {
|
|
336
|
+
objectName = fallbackValueNode.object.name;
|
|
337
|
+
}
|
|
338
|
+
if (fallbackValueNode.property.type === 'Identifier') {
|
|
339
|
+
propertyName = fallbackValueNode.property.name;
|
|
340
|
+
} else if (fallbackValueNode.property.type === 'Literal' && typeof fallbackValueNode.property.value === 'string') {
|
|
341
|
+
propertyName = fallbackValueNode.property.value;
|
|
342
|
+
}
|
|
343
|
+
if (!(!objectName || !propertyName)) {
|
|
344
|
+
_context5.next = 5;
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
this.logError("Could not determine object and property names from member expression: ".concat(_chalk.default.yellow(fallbackValueNode)));
|
|
348
|
+
return _context5.abrupt("return", {
|
|
349
|
+
rawFallbackValue: '',
|
|
350
|
+
fallbackValue: fallbackValue,
|
|
351
|
+
resolvedImportDeclaration: undefined,
|
|
352
|
+
resolvedLocalVarDeclaration: undefined
|
|
353
|
+
});
|
|
354
|
+
case 5:
|
|
355
|
+
rawFallbackValue = "".concat(objectName, ".").concat(propertyName);
|
|
356
|
+
this.logVerbose("Fallback is a member expression: ".concat(_chalk.default.yellow(rawFallbackValue), ", attempting to resolve..."));
|
|
357
|
+
// Find the import statement for the object
|
|
358
|
+
importDeclaration = this.source.find(this.j.ImportDeclaration).filter(this.createImportFilter(objectName)).at(0);
|
|
359
|
+
if (!importDeclaration.size()) {
|
|
360
|
+
_context5.next = 16;
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
importSource = importDeclaration.get().value.source.value;
|
|
364
|
+
_context5.next = 12;
|
|
365
|
+
return this.resolveValueFromImport(this.rootDir, importSource, objectName, propertyName);
|
|
366
|
+
case 12:
|
|
367
|
+
fallbackValue = _context5.sent;
|
|
368
|
+
if (fallbackValue !== undefined) {
|
|
369
|
+
resolvedImportDeclaration = importDeclaration.paths()[0];
|
|
370
|
+
this.logVerbose("Resolved fallback value from member expression: ".concat(_chalk.default.yellow(fallbackValue)));
|
|
371
|
+
}
|
|
372
|
+
_context5.next = 17;
|
|
373
|
+
break;
|
|
374
|
+
case 16:
|
|
375
|
+
this.logError("Could not find import for member expression: ".concat(_chalk.default.yellow(rawFallbackValue)));
|
|
376
|
+
case 17:
|
|
377
|
+
return _context5.abrupt("return", {
|
|
378
|
+
rawFallbackValue: rawFallbackValue,
|
|
379
|
+
fallbackValue: fallbackValue,
|
|
380
|
+
resolvedImportDeclaration: resolvedImportDeclaration,
|
|
381
|
+
resolvedLocalVarDeclaration: undefined
|
|
382
|
+
});
|
|
383
|
+
case 18:
|
|
384
|
+
case "end":
|
|
385
|
+
return _context5.stop();
|
|
386
|
+
}
|
|
387
|
+
}, _callee5, this);
|
|
388
|
+
}));
|
|
389
|
+
function processFallbackAsMemberExpression(_x5) {
|
|
390
|
+
return _processFallbackAsMemberExpression.apply(this, arguments);
|
|
391
|
+
}
|
|
392
|
+
return processFallbackAsMemberExpression;
|
|
393
|
+
}()
|
|
394
|
+
}, {
|
|
395
|
+
key: "processFallbackAsTemplateLiteral",
|
|
396
|
+
value: function () {
|
|
397
|
+
var _processFallbackAsTemplateLiteral = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(fallbackValueNode) {
|
|
398
|
+
var expressions, rawFallbackValue, fallbackValue, quasis, exprValue, expression, resolvedImportDeclaration, resolvedLocalVarDeclaration, result, _result;
|
|
399
|
+
return _regenerator.default.wrap(function _callee6$(_context6) {
|
|
400
|
+
while (1) switch (_context6.prev = _context6.next) {
|
|
401
|
+
case 0:
|
|
402
|
+
expressions = fallbackValueNode.expressions;
|
|
403
|
+
rawFallbackValue = '';
|
|
404
|
+
quasis = fallbackValueNode.quasis;
|
|
405
|
+
if (!(expressions.length !== 1 || quasis.length !== 2)) {
|
|
406
|
+
_context6.next = 6;
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
this.logError("Unsupported template literal structure");
|
|
410
|
+
return _context6.abrupt("return", {
|
|
411
|
+
rawFallbackValue: rawFallbackValue,
|
|
412
|
+
fallbackValue: fallbackValue,
|
|
413
|
+
resolvedImportDeclaration: undefined,
|
|
414
|
+
resolvedLocalVarDeclaration: undefined
|
|
415
|
+
});
|
|
416
|
+
case 6:
|
|
417
|
+
expression = expressions[0];
|
|
418
|
+
if (!(expression.type === 'Identifier')) {
|
|
419
|
+
_context6.next = 16;
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
_context6.next = 10;
|
|
423
|
+
return this.processFallbackAsIdentifier(expression);
|
|
424
|
+
case 10:
|
|
425
|
+
result = _context6.sent;
|
|
426
|
+
exprValue = result.fallbackValue;
|
|
427
|
+
resolvedImportDeclaration = result.resolvedImportDeclaration;
|
|
428
|
+
resolvedLocalVarDeclaration = result.resolvedLocalVarDeclaration;
|
|
429
|
+
_context6.next = 22;
|
|
430
|
+
break;
|
|
431
|
+
case 16:
|
|
432
|
+
if (!(expression.type === 'MemberExpression')) {
|
|
433
|
+
_context6.next = 22;
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
_context6.next = 19;
|
|
437
|
+
return this.processFallbackAsMemberExpression(expression);
|
|
438
|
+
case 19:
|
|
439
|
+
_result = _context6.sent;
|
|
440
|
+
exprValue = _result.fallbackValue;
|
|
441
|
+
resolvedImportDeclaration = _result.resolvedImportDeclaration;
|
|
442
|
+
case 22:
|
|
443
|
+
if (exprValue !== undefined) {
|
|
444
|
+
rawFallbackValue = "".concat(quasis[0].value.raw, "${").concat(exprValue, "}").concat(quasis[1].value.raw);
|
|
445
|
+
fallbackValue = "".concat(quasis[0].value.cooked).concat(exprValue).concat(quasis[1].value.cooked);
|
|
446
|
+
this.logVerbose("Resolved fallback value from template literal: ".concat(_chalk.default.yellow(fallbackValue)));
|
|
447
|
+
}
|
|
448
|
+
return _context6.abrupt("return", {
|
|
449
|
+
rawFallbackValue: rawFallbackValue,
|
|
450
|
+
fallbackValue: fallbackValue,
|
|
451
|
+
resolvedImportDeclaration: resolvedImportDeclaration,
|
|
452
|
+
resolvedLocalVarDeclaration: resolvedLocalVarDeclaration
|
|
453
|
+
});
|
|
454
|
+
case 24:
|
|
455
|
+
case "end":
|
|
456
|
+
return _context6.stop();
|
|
457
|
+
}
|
|
458
|
+
}, _callee6, this);
|
|
459
|
+
}));
|
|
460
|
+
function processFallbackAsTemplateLiteral(_x6) {
|
|
461
|
+
return _processFallbackAsTemplateLiteral.apply(this, arguments);
|
|
462
|
+
}
|
|
463
|
+
return processFallbackAsTemplateLiteral;
|
|
464
|
+
}()
|
|
465
|
+
}, {
|
|
466
|
+
key: "tryResolveModulePath",
|
|
467
|
+
value: function tryResolveModulePath(moduleName) {
|
|
468
|
+
try {
|
|
469
|
+
var resolvedPath = require.resolve(moduleName, {
|
|
470
|
+
paths: [this.rootDir]
|
|
471
|
+
});
|
|
472
|
+
this.logVerbose("Resolved module path: ".concat(_chalk.default.green(resolvedPath), " for ").concat(_chalk.default.cyan(moduleName)));
|
|
473
|
+
return resolvedPath;
|
|
474
|
+
} catch (error) {
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}, {
|
|
479
|
+
key: "tryResolveLocalPath",
|
|
480
|
+
value: function () {
|
|
481
|
+
var _tryResolveLocalPath = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee7(currentDir, importPath) {
|
|
482
|
+
var _iterator, _step, ext, potentialPath;
|
|
483
|
+
return _regenerator.default.wrap(function _callee7$(_context7) {
|
|
484
|
+
while (1) switch (_context7.prev = _context7.next) {
|
|
485
|
+
case 0:
|
|
486
|
+
_iterator = _createForOfIteratorHelper(this.possibleExtensions);
|
|
487
|
+
_context7.prev = 1;
|
|
488
|
+
_iterator.s();
|
|
489
|
+
case 3:
|
|
490
|
+
if ((_step = _iterator.n()).done) {
|
|
491
|
+
_context7.next = 17;
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
ext = _step.value;
|
|
495
|
+
potentialPath = _path.default.resolve(currentDir, "".concat(importPath).concat(ext));
|
|
496
|
+
_context7.prev = 6;
|
|
497
|
+
_context7.next = 9;
|
|
498
|
+
return _promises.default.access(potentialPath);
|
|
499
|
+
case 9:
|
|
500
|
+
this.logVerbose("Resolved file path locally: ".concat(_chalk.default.green(potentialPath)));
|
|
501
|
+
return _context7.abrupt("return", potentialPath);
|
|
502
|
+
case 13:
|
|
503
|
+
_context7.prev = 13;
|
|
504
|
+
_context7.t0 = _context7["catch"](6);
|
|
505
|
+
case 15:
|
|
506
|
+
_context7.next = 3;
|
|
507
|
+
break;
|
|
508
|
+
case 17:
|
|
509
|
+
_context7.next = 22;
|
|
510
|
+
break;
|
|
511
|
+
case 19:
|
|
512
|
+
_context7.prev = 19;
|
|
513
|
+
_context7.t1 = _context7["catch"](1);
|
|
514
|
+
_iterator.e(_context7.t1);
|
|
515
|
+
case 22:
|
|
516
|
+
_context7.prev = 22;
|
|
517
|
+
_iterator.f();
|
|
518
|
+
return _context7.finish(22);
|
|
519
|
+
case 25:
|
|
520
|
+
return _context7.abrupt("return", null);
|
|
521
|
+
case 26:
|
|
522
|
+
case "end":
|
|
523
|
+
return _context7.stop();
|
|
524
|
+
}
|
|
525
|
+
}, _callee7, this, [[1, 19, 22, 25], [6, 13]]);
|
|
526
|
+
}));
|
|
527
|
+
function tryResolveLocalPath(_x7, _x8) {
|
|
528
|
+
return _tryResolveLocalPath.apply(this, arguments);
|
|
529
|
+
}
|
|
530
|
+
return tryResolveLocalPath;
|
|
531
|
+
}()
|
|
532
|
+
}, {
|
|
533
|
+
key: "resolveValueFromImport",
|
|
534
|
+
value: function () {
|
|
535
|
+
var _resolveValueFromImport = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee8(currentDir, importPath, objectName, propertyOrVariableName) {
|
|
536
|
+
var filePath, fileContent, source, imports, matchingImport, _importDecl$source$va, importDecl, newImportPath, varDeclaration, init;
|
|
537
|
+
return _regenerator.default.wrap(function _callee8$(_context8) {
|
|
538
|
+
while (1) switch (_context8.prev = _context8.next) {
|
|
539
|
+
case 0:
|
|
540
|
+
filePath = this.tryResolveModulePath(importPath);
|
|
541
|
+
if (filePath) {
|
|
542
|
+
_context8.next = 5;
|
|
543
|
+
break;
|
|
544
|
+
}
|
|
545
|
+
_context8.next = 4;
|
|
546
|
+
return this.tryResolveLocalPath(currentDir, importPath);
|
|
547
|
+
case 4:
|
|
548
|
+
filePath = _context8.sent;
|
|
549
|
+
case 5:
|
|
550
|
+
if (filePath) {
|
|
551
|
+
_context8.next = 8;
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
this.logError("File not found for import path: ".concat(_chalk.default.cyan(importPath), " in directory: ").concat(_chalk.default.blue(this.rootDir)));
|
|
555
|
+
return _context8.abrupt("return", undefined);
|
|
556
|
+
case 8:
|
|
557
|
+
this.logVerbose("Reading file: ".concat(_chalk.default.green(filePath)));
|
|
558
|
+
_context8.next = 11;
|
|
559
|
+
return _promises.default.readFile(filePath, 'utf-8');
|
|
560
|
+
case 11:
|
|
561
|
+
fileContent = _context8.sent;
|
|
562
|
+
source = this.j(fileContent);
|
|
563
|
+
if (!objectName) {
|
|
564
|
+
_context8.next = 20;
|
|
565
|
+
break;
|
|
566
|
+
}
|
|
567
|
+
// Check if the object is imported from another module
|
|
568
|
+
imports = source.find(this.j.ImportDeclaration);
|
|
569
|
+
matchingImport = imports.filter(this.createImportFilter(objectName)).at(0);
|
|
570
|
+
if (!matchingImport.size()) {
|
|
571
|
+
_context8.next = 20;
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
importDecl = matchingImport.get().node;
|
|
575
|
+
newImportPath = (_importDecl$source$va = importDecl.source.value) === null || _importDecl$source$va === void 0 ? void 0 : _importDecl$source$va.toString();
|
|
576
|
+
return _context8.abrupt("return", this.resolveValueFromImport(_path.default.dirname(filePath), newImportPath, objectName, propertyOrVariableName));
|
|
577
|
+
case 20:
|
|
578
|
+
// If not imported, check for variable declaration
|
|
579
|
+
varDeclaration = source.find(this.j.VariableDeclarator, {
|
|
580
|
+
id: {
|
|
581
|
+
name: propertyOrVariableName
|
|
582
|
+
}
|
|
583
|
+
}).at(0);
|
|
584
|
+
if (varDeclaration.size()) {
|
|
585
|
+
_context8.next = 24;
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
this.logError("Variable declaration not found for ".concat(_chalk.default.yellow(propertyOrVariableName), " in file: ").concat(_chalk.default.green(filePath)));
|
|
589
|
+
return _context8.abrupt("return", undefined);
|
|
590
|
+
case 24:
|
|
591
|
+
init = varDeclaration.get().value.init;
|
|
592
|
+
if (!(init.type === 'Literal' || init.type === 'StringLiteral' || init.type === 'NumericLiteral')) {
|
|
593
|
+
_context8.next = 29;
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
return _context8.abrupt("return", init.value);
|
|
597
|
+
case 29:
|
|
598
|
+
this.logError("Unhandled init type ".concat(init.type, " for variable: ").concat(_chalk.default.yellow(propertyOrVariableName), " in file: ").concat(_chalk.default.green(filePath)));
|
|
599
|
+
return _context8.abrupt("return", undefined);
|
|
600
|
+
case 31:
|
|
601
|
+
case "end":
|
|
602
|
+
return _context8.stop();
|
|
603
|
+
}
|
|
604
|
+
}, _callee8, this);
|
|
605
|
+
}));
|
|
606
|
+
function resolveValueFromImport(_x9, _x10, _x11, _x12) {
|
|
607
|
+
return _resolveValueFromImport.apply(this, arguments);
|
|
608
|
+
}
|
|
609
|
+
return resolveValueFromImport;
|
|
610
|
+
}()
|
|
611
|
+
}, {
|
|
612
|
+
key: "createImportFilter",
|
|
613
|
+
value: function createImportFilter(targetName) {
|
|
614
|
+
return function (path) {
|
|
615
|
+
var _path$node$specifiers;
|
|
616
|
+
return ((_path$node$specifiers = path.node.specifiers) === null || _path$node$specifiers === void 0 ? void 0 : _path$node$specifiers.some(function (specifier) {
|
|
617
|
+
var _specifier$local, _specifier$local2, _specifier$local3;
|
|
618
|
+
switch (specifier.type) {
|
|
619
|
+
case 'ImportNamespaceSpecifier':
|
|
620
|
+
return ((_specifier$local = specifier.local) === null || _specifier$local === void 0 ? void 0 : _specifier$local.name) === targetName;
|
|
621
|
+
case 'ImportDefaultSpecifier':
|
|
622
|
+
return ((_specifier$local2 = specifier.local) === null || _specifier$local2 === void 0 ? void 0 : _specifier$local2.name) === targetName;
|
|
623
|
+
case 'ImportSpecifier':
|
|
624
|
+
return ((_specifier$local3 = specifier.local) === null || _specifier$local3 === void 0 ? void 0 : _specifier$local3.name) === targetName || specifier.imported.name === targetName;
|
|
625
|
+
default:
|
|
626
|
+
return false;
|
|
627
|
+
}
|
|
628
|
+
})) === true;
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
}]);
|
|
632
|
+
}();
|