@atlaskit/codemod-cli 0.27.2 → 0.27.4

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