@vkontakte/vkui-codemods 0.0.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.
Files changed (46) hide show
  1. package/README.md +63 -0
  2. package/dist/cli.js +113 -0
  3. package/dist/codemod-helpers.js +76 -0
  4. package/dist/getAvailableCodemods.js +19 -0
  5. package/dist/helpers/logger.js +30 -0
  6. package/dist/index.js +193 -0
  7. package/dist/report.js +13 -0
  8. package/dist/transforms/accordion.js +25 -0
  9. package/dist/transforms/action-sheet-item.js +33 -0
  10. package/dist/transforms/action-sheet.js +45 -0
  11. package/dist/transforms/alert.js +48 -0
  12. package/dist/transforms/appearance-provider.js +19 -0
  13. package/dist/transforms/banner.js +22 -0
  14. package/dist/transforms/calendar-range.js +21 -0
  15. package/dist/transforms/calendar.js +23 -0
  16. package/dist/transforms/chip.js +17 -0
  17. package/dist/transforms/chips-input.js +17 -0
  18. package/dist/transforms/config-provider.js +68 -0
  19. package/dist/transforms/content-card.js +19 -0
  20. package/dist/transforms/custom-scroll-view.js +26 -0
  21. package/dist/transforms/date-input.js +25 -0
  22. package/dist/transforms/date-range-input.js +29 -0
  23. package/dist/transforms/fixed-layout.js +32 -0
  24. package/dist/transforms/forbid-imports.js +25 -0
  25. package/dist/transforms/form-item.js +23 -0
  26. package/dist/transforms/gradient-prop-change.js +29 -0
  27. package/dist/transforms/horizontal-cell-show-more.js +22 -0
  28. package/dist/transforms/modal-card.js +32 -0
  29. package/dist/transforms/modal-page-header.js +32 -0
  30. package/dist/transforms/pagination.js +31 -0
  31. package/dist/transforms/panel-header-content.js +32 -0
  32. package/dist/transforms/popover.js +118 -0
  33. package/dist/transforms/popper.js +95 -0
  34. package/dist/transforms/range-slider.js +49 -0
  35. package/dist/transforms/rich-tooltip.js +24 -0
  36. package/dist/transforms/search.js +18 -0
  37. package/dist/transforms/simple-cell.js +47 -0
  38. package/dist/transforms/split-col.js +33 -0
  39. package/dist/transforms/tappable.js +30 -0
  40. package/dist/transforms/text-tooltip.js +139 -0
  41. package/dist/transforms/tooltip.js +162 -0
  42. package/dist/transforms/typography.js +68 -0
  43. package/dist/transforms/users-stack.js +33 -0
  44. package/dist/transforms/visually-hidden-input.js +42 -0
  45. package/dist/types.js +2 -0
  46. package/package.json +44 -0
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.parser = void 0;
5
+ var codemod_helpers_1 = require("../codemod-helpers");
6
+ exports.parser = 'tsx';
7
+ var componentName = 'unstable_Popover';
8
+ var componentNameTo = 'Popover';
9
+ var ATTRIBUTE_MANIPULATOR = {
10
+ action: {
11
+ keyTo: 'trigger',
12
+ },
13
+ offsetDistance: {
14
+ keyTo: 'offsetByMainAxis',
15
+ },
16
+ offsetSkidding: {
17
+ keyTo: 'offsetByCrossAxis',
18
+ },
19
+ };
20
+ var FORCE_PORTAL = 'forcePortal';
21
+ var COMPLEX_ATTRIBUTE_MANIPULATOR = (_a = {},
22
+ _a[FORCE_PORTAL] = {
23
+ keyTo: 'usePortal',
24
+ },
25
+ _a.portalRoot = {
26
+ keyTo: 'usePortal',
27
+ },
28
+ _a);
29
+ var LEGACY_SHOWN_DELAY_PROP = 'shownDelay';
30
+ var LEGACY_HIDE_DELAY_PROP = 'hideDelay';
31
+ var NEW_HOVER_DELAY_PROP = 'hoverDelay';
32
+ function transformer(file, api, options) {
33
+ var alias = options.alias;
34
+ var j = api.jscodeshift;
35
+ var source = j(file.source);
36
+ var localName = (0, codemod_helpers_1.getImportInfo)(j, file, componentName, alias).localName;
37
+ var attributeReplacer = (0, codemod_helpers_1.createAttributeManipulator)(ATTRIBUTE_MANIPULATOR, api);
38
+ source
39
+ .find(j.ImportDeclaration)
40
+ .filter(function (path) { return path.node.source.value === alias; })
41
+ .find(j.ImportSpecifier, { imported: { name: componentName } })
42
+ .forEach(function (path) {
43
+ return j(path).replaceWith(function (path) {
44
+ return j.importSpecifier(j.identifier(componentNameTo), path.node.local);
45
+ });
46
+ });
47
+ source
48
+ .find(j.JSXOpeningElement)
49
+ .filter(function (path) { return path.value.name.type === 'JSXIdentifier' && path.value.name.name === localName; })
50
+ .find(j.JSXAttribute, function (attribute) {
51
+ return typeof attribute.name.name === 'string' ? attributeReplacer.has(attribute.name.name) : false;
52
+ })
53
+ .forEach(function (attribute) {
54
+ var attributeName = attribute.node.name.name;
55
+ var foundFix = attributeReplacer.getReplacers(attributeName);
56
+ if (foundFix && foundFix.action !== 'remove') {
57
+ var value = attribute.node.value;
58
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(foundFix.keyTo()), foundFix.valueTo(value)));
59
+ }
60
+ });
61
+ var complexAttributeReplacer = (0, codemod_helpers_1.createAttributeManipulator)(COMPLEX_ATTRIBUTE_MANIPULATOR, api);
62
+ source.findJSXElements(localName).forEach(function (element) {
63
+ j(element)
64
+ .find(j.JSXAttribute, function (attribute) {
65
+ return typeof attribute.name.name === 'string'
66
+ ? complexAttributeReplacer.has(attribute.name.name)
67
+ : false;
68
+ })
69
+ .forEach(function (attribute, _, attributes) {
70
+ var attributeName = attribute.node.name.name;
71
+ if (attributes.length === 2 && attributeName === FORCE_PORTAL) {
72
+ j(attribute).remove();
73
+ }
74
+ else {
75
+ var foundFix = complexAttributeReplacer.getReplacers(attributeName);
76
+ if (foundFix && foundFix.action !== 'remove') {
77
+ var value = attribute.node.value;
78
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(foundFix.keyTo()), foundFix.valueTo(value)));
79
+ }
80
+ }
81
+ });
82
+ // разрешаем hoverDelay
83
+ var _a = [
84
+ j(element).find(j.JSXAttribute, function (attribute) { return attribute.name.name === LEGACY_SHOWN_DELAY_PROP; }),
85
+ j(element).find(j.JSXAttribute, function (attribute) { return attribute.name.name === LEGACY_HIDE_DELAY_PROP; }),
86
+ ], shownDelayAttr = _a[0], hideDelayAttr = _a[1];
87
+ var getNumericLiteral = function (attributeCollection) {
88
+ var val;
89
+ attributeCollection.find(j.NumericLiteral).forEach(function (attribute) {
90
+ val = attribute.node;
91
+ });
92
+ return val ? val : j.numericLiteral(0);
93
+ };
94
+ if (shownDelayAttr.length === 1 && hideDelayAttr.length === 0) {
95
+ shownDelayAttr.forEach(function (attribute) {
96
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(NEW_HOVER_DELAY_PROP), j.jsxExpressionContainer(getNumericLiteral(shownDelayAttr))));
97
+ });
98
+ }
99
+ else if (shownDelayAttr.length === 0 && hideDelayAttr.length === 1) {
100
+ hideDelayAttr.forEach(function (attribute) {
101
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(NEW_HOVER_DELAY_PROP), j.jsxExpressionContainer(j.arrayExpression([j.numericLiteral(0), getNumericLiteral(hideDelayAttr)]))));
102
+ });
103
+ }
104
+ else if (shownDelayAttr.length === 1 && hideDelayAttr.length === 1) {
105
+ shownDelayAttr.forEach(function (attribute) {
106
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(NEW_HOVER_DELAY_PROP), j.jsxExpressionContainer(j.arrayExpression([
107
+ getNumericLiteral(shownDelayAttr),
108
+ getNumericLiteral(hideDelayAttr),
109
+ ]))));
110
+ });
111
+ hideDelayAttr.forEach(function (attribute) {
112
+ j(attribute).remove();
113
+ });
114
+ }
115
+ });
116
+ return source.toSource();
117
+ }
118
+ exports.default = transformer;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ var _a;
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.parser = void 0;
8
+ var chalk_1 = __importDefault(require("chalk"));
9
+ var codemod_helpers_1 = require("../codemod-helpers");
10
+ exports.parser = 'tsx';
11
+ var componentName = 'unstable_Popper';
12
+ var ATTRIBUTE_MANIPULATOR = {
13
+ offsetDistance: {
14
+ keyTo: 'offsetByMainAxis',
15
+ },
16
+ offsetSkidding: {
17
+ keyTo: 'offsetByCrossAxis',
18
+ },
19
+ renderContent: {
20
+ keyTo: 'children',
21
+ reportText: function () {
22
+ return ": ".concat(chalk_1.default.white.bgBlue('children'), " prop in ").concat(chalk_1.default.white.bgBlue('Popper'), ". You should unwraps function manually");
23
+ },
24
+ },
25
+ arrowClassName: {
26
+ keyTo: 'arrowProps',
27
+ valueTo: function (v, api) {
28
+ return v
29
+ ? api.jscodeshift.jsxExpressionContainer(api.jscodeshift.objectExpression([
30
+ api.jscodeshift.property('init', api.jscodeshift.identifier('iconClassName'), v),
31
+ ]))
32
+ : v;
33
+ },
34
+ },
35
+ onPlacementChange: {
36
+ keyTo: 'onPlacementChange',
37
+ reportText: function () {
38
+ return ": ".concat(chalk_1.default.white.bgBlue('onPlacementChange'), " prop in ").concat(chalk_1.default.white.bgBlue('Popper'), ". You should move function params from object to array arguments manually.");
39
+ },
40
+ },
41
+ };
42
+ var FORCE_PORTAL = 'forcePortal';
43
+ var COMPLEX_ATTRIBUTE_MANIPULATOR = (_a = {},
44
+ _a[FORCE_PORTAL] = {
45
+ keyTo: 'usePortal',
46
+ },
47
+ _a.portalRoot = {
48
+ keyTo: 'usePortal',
49
+ },
50
+ _a);
51
+ function transformer(file, api, options) {
52
+ var alias = options.alias;
53
+ var j = api.jscodeshift;
54
+ var source = j(file.source);
55
+ var localName = (0, codemod_helpers_1.getImportInfo)(j, file, componentName, alias).localName;
56
+ var attributeReplacer = (0, codemod_helpers_1.createAttributeManipulator)(ATTRIBUTE_MANIPULATOR, api);
57
+ source
58
+ .find(j.ImportDeclaration)
59
+ .filter(function (path) { return path.node.source.value === alias; })
60
+ .find(j.ImportSpecifier, { imported: { name: componentName } })
61
+ .forEach(function (path) {
62
+ return j(path).replaceWith(function (path) { return j.importSpecifier(j.identifier('Popper'), path.node.local); });
63
+ });
64
+ source
65
+ .find(j.JSXOpeningElement)
66
+ .filter(function (path) { return path.value.name.type === 'JSXIdentifier' && path.value.name.name === localName; })
67
+ .find(j.JSXAttribute, function (attribute) { return attributeReplacer.has(attribute.name.name); })
68
+ .forEach(function (attribute) {
69
+ var foundFix = attributeReplacer.getReplacers(attribute.node.name.name);
70
+ if (foundFix && foundFix.action !== 'remove') {
71
+ var value = attribute.node.value;
72
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(foundFix.keyTo()), foundFix.valueTo(value)));
73
+ }
74
+ });
75
+ var complexAttributeReplacer = (0, codemod_helpers_1.createAttributeManipulator)(COMPLEX_ATTRIBUTE_MANIPULATOR, api);
76
+ source.findJSXElements(localName).forEach(function (element) {
77
+ j(element)
78
+ .find(j.JSXAttribute, function (attribute) { return complexAttributeReplacer.has(attribute.name.name); })
79
+ .forEach(function (attribute, _, attributes) {
80
+ var attributeName = attribute.node.name.name;
81
+ if (attributes.length === 2 && attributeName === FORCE_PORTAL) {
82
+ j(attribute).remove();
83
+ }
84
+ else {
85
+ var foundFix = complexAttributeReplacer.getReplacers(attributeName);
86
+ if (foundFix && foundFix.action !== 'remove') {
87
+ var value = attribute.node.value;
88
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(foundFix.keyTo()), foundFix.valueTo(value)));
89
+ }
90
+ }
91
+ });
92
+ });
93
+ return source.toSource();
94
+ }
95
+ exports.default = transformer;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.parser = void 0;
13
+ exports.parser = 'tsx';
14
+ function transformer(file, api, options) {
15
+ var alias = options.alias;
16
+ var j = api.jscodeshift;
17
+ var source = j(file.source);
18
+ var localImportName = 'RangeSlider';
19
+ var needRename = true;
20
+ var componentImport = source
21
+ .find(j.ImportDeclaration)
22
+ .filter(function (path) { return path.node.source.value === alias; })
23
+ .find(j.ImportSpecifier, { imported: { name: 'RangeSlider' } });
24
+ var sliderImport = source
25
+ .find(j.ImportDeclaration)
26
+ .filter(function (path) { return path.node.source.value === alias; })
27
+ .find(j.ImportSpecifier, { imported: { name: 'Slider' } });
28
+ componentImport.forEach(function (path) {
29
+ if (path.node.local && path.node.local.name !== path.node.imported.name) {
30
+ localImportName = path.node.local.name;
31
+ needRename = false;
32
+ }
33
+ if (sliderImport.size() > 0 && needRename) {
34
+ j(path).remove();
35
+ }
36
+ else {
37
+ j(path).replaceWith(function (path) {
38
+ return j.importSpecifier(j.identifier('Slider'), needRename ? null : path.node.local);
39
+ });
40
+ }
41
+ });
42
+ source.findJSXElements(localImportName).replaceWith(function (path) {
43
+ localImportName = needRename ? 'Slider' : localImportName;
44
+ var props = path.node.openingElement.attributes;
45
+ return j.jsxElement(j.jsxOpeningElement(j.jsxIdentifier(localImportName), __spreadArray([j.jsxAttribute(j.jsxIdentifier('multiple'))], (props || []), true), path.node.closingElement ? false : true), path.node.closingElement ? j.jsxClosingElement(j.jsxIdentifier(localImportName)) : null, path.node.children);
46
+ });
47
+ return source.toSource();
48
+ }
49
+ exports.default = transformer;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.parser = void 0;
7
+ var chalk_1 = __importDefault(require("chalk"));
8
+ var codemod_helpers_1 = require("../codemod-helpers");
9
+ var report_1 = require("../report");
10
+ exports.parser = 'tsx';
11
+ function transformer(file, api, options) {
12
+ var alias = options.alias;
13
+ var j = api.jscodeshift;
14
+ var source = j(file.source);
15
+ var localName = (0, codemod_helpers_1.getImportInfo)(j, file, 'RichTooltip', alias).localName;
16
+ var richTooltipComponents = source
17
+ .find(j.JSXOpeningElement)
18
+ .filter(function (path) { return path.value.name.type === 'JSXIdentifier' && path.value.name.name === localName; });
19
+ if (richTooltipComponents.size() > 0) {
20
+ (0, report_1.report)(api, ": ".concat(chalk_1.default.white.bgBlue('RichTooltip'), " component does not exist anymore. Use Tooltip."));
21
+ }
22
+ return source.toSource();
23
+ }
24
+ exports.default = transformer;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parser = void 0;
4
+ var codemod_helpers_1 = require("../codemod-helpers");
5
+ exports.parser = 'tsx';
6
+ var RENAME_MAP = {
7
+ iconAriaLabel: 'iconLabel',
8
+ clearAriaLabel: 'clearLabel',
9
+ };
10
+ function transformer(file, api, options) {
11
+ var alias = options.alias;
12
+ var j = api.jscodeshift;
13
+ var source = j(file.source);
14
+ var localName = (0, codemod_helpers_1.getImportInfo)(j, file, 'Search', alias).localName;
15
+ (0, codemod_helpers_1.renameProp)(j, source, localName, RENAME_MAP);
16
+ return source.toSource();
17
+ }
18
+ exports.default = transformer;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.parser = void 0;
7
+ var chalk_1 = __importDefault(require("chalk"));
8
+ var codemod_helpers_1 = require("../codemod-helpers");
9
+ var report_1 = require("../report");
10
+ exports.parser = 'tsx';
11
+ function transformer(file, api, options) {
12
+ var alias = options.alias;
13
+ var j = api.jscodeshift;
14
+ var source = j(file.source);
15
+ var localName = (0, codemod_helpers_1.getImportInfo)(j, file, 'SimpleCell', alias).localName;
16
+ source
17
+ .find(j.JSXOpeningElement)
18
+ .filter(function (path) { return path.value.name.type === 'JSXIdentifier' && path.value.name.name === localName; })
19
+ .find(j.JSXAttribute)
20
+ .filter(function (attribute) {
21
+ return attribute.node.name.name === 'expandable' || attribute.node.name.name === 'disabled';
22
+ })
23
+ .forEach(function (attribute) {
24
+ if (attribute.node.name.name === 'expandable') {
25
+ var attributeValue = attribute.node.value;
26
+ if (attributeValue && attributeValue.type === 'JSXExpressionContainer') {
27
+ var expression = attributeValue.expression;
28
+ if (expression.type === 'BooleanLiteral') {
29
+ if (expression.value) {
30
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier('expandable'), j.stringLiteral('auto')));
31
+ }
32
+ else {
33
+ j(attribute).remove();
34
+ }
35
+ }
36
+ }
37
+ if (attribute.node.type === 'JSXAttribute' && !attributeValue) {
38
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier('expandable'), j.stringLiteral('auto')));
39
+ }
40
+ }
41
+ if (attribute.node.name.name === 'disabled') {
42
+ (0, report_1.report)(api, ": ".concat(chalk_1.default.white.bgBlue('disabled'), " prop in ").concat(chalk_1.default.white.bgBlue('SimpleCell'), " may be no longer needed."));
43
+ }
44
+ });
45
+ return source.toSource();
46
+ }
47
+ exports.default = transformer;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parser = void 0;
4
+ var codemod_helpers_1 = require("../codemod-helpers");
5
+ exports.parser = 'tsx';
6
+ function transformer(file, api, options) {
7
+ var alias = options.alias;
8
+ var j = api.jscodeshift;
9
+ var source = j(file.source);
10
+ var localName = (0, codemod_helpers_1.getImportInfo)(j, file, 'SplitCol', alias).localName;
11
+ source
12
+ .find(j.JSXOpeningElement)
13
+ .filter(function (path) { return path.value.name.type === 'JSXIdentifier' && path.value.name.name === localName; })
14
+ .find(j.JSXAttribute)
15
+ .filter(function (attribute) { return attribute.node.name.name === 'spaced'; })
16
+ .forEach(function (attribute) {
17
+ var node = attribute.node;
18
+ if (!node.value) {
19
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier('autoSpaced')));
20
+ }
21
+ else if (node.value.type === 'JSXExpressionContainer' &&
22
+ node.value.expression.type === 'BooleanLiteral') {
23
+ if (node.value.expression.value) {
24
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier('autoSpaced')));
25
+ }
26
+ else {
27
+ j(attribute).remove();
28
+ }
29
+ }
30
+ });
31
+ return source.toSource();
32
+ }
33
+ exports.default = transformer;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.parser = void 0;
7
+ var chalk_1 = __importDefault(require("chalk"));
8
+ var codemod_helpers_1 = require("../codemod-helpers");
9
+ var report_1 = require("../report");
10
+ exports.parser = 'tsx';
11
+ var REMOVED_PROPS = ['onEnter', 'onLeave', 'onStart', 'onEnd', 'onMove', 'stopPropagation'];
12
+ function transformer(file, api, options) {
13
+ var alias = options.alias;
14
+ var j = api.jscodeshift;
15
+ var source = j(file.source);
16
+ var localName = (0, codemod_helpers_1.getImportInfo)(j, file, 'Tappable', alias).localName;
17
+ source
18
+ .find(j.JSXOpeningElement)
19
+ .filter(function (path) { return path.value.name.type === 'JSXIdentifier' && path.value.name.name === localName; })
20
+ .find(j.JSXAttribute)
21
+ .filter(function (attribute) {
22
+ return typeof attribute.node.name.name === 'string' &&
23
+ REMOVED_PROPS.includes(attribute.node.name.name);
24
+ })
25
+ .forEach(function (attribute) {
26
+ (0, report_1.report)(api, ": ".concat(chalk_1.default.white.bgBlue(attribute.node.name.name), " prop in ").concat(chalk_1.default.white.bgBlue('Tappable'), " component is no longer available. Manual changes required."));
27
+ });
28
+ return source.toSource();
29
+ }
30
+ exports.default = transformer;
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ var _a;
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.parser = void 0;
8
+ var chalk_1 = __importDefault(require("chalk"));
9
+ var codemod_helpers_1 = require("../codemod-helpers");
10
+ exports.parser = 'tsx';
11
+ var componentName = 'unstable_TextTooltip';
12
+ var ATTRIBUTE_MANIPULATOR = {
13
+ offsetSkidding: {
14
+ keyTo: 'offsetByCrossAxis',
15
+ },
16
+ offsetDistance: {
17
+ keyTo: 'offsetByMainAxis',
18
+ },
19
+ autoUpdateOnTargetResize: {
20
+ action: 'remove',
21
+ },
22
+ getRef: {
23
+ keyTo: 'getRootRef',
24
+ },
25
+ renderContent: {
26
+ keyTo: 'children',
27
+ reportText: function () {
28
+ return ": ".concat(chalk_1.default.white.bgBlue('children'), " prop in ").concat(chalk_1.default.white.bgBlue('TextTooltip'), ". You should unwraps function manually");
29
+ },
30
+ },
31
+ customMiddlewares: {
32
+ keyTo: 'unknown',
33
+ reportText: function () {
34
+ return ": ".concat(chalk_1.default.white.bgBlue('customMiddlewares'), " prop in ").concat(chalk_1.default.white.bgBlue('TextTooltip'), ". You should unwraps function manually");
35
+ },
36
+ },
37
+ };
38
+ var FORCE_PORTAL = 'forcePortal';
39
+ var COMPLEX_ATTRIBUTE_MANIPULATOR = (_a = {},
40
+ _a[FORCE_PORTAL] = {
41
+ keyTo: 'usePortal',
42
+ },
43
+ _a.portalRoot = {
44
+ keyTo: 'usePortal',
45
+ },
46
+ _a);
47
+ var LEGACY_SHOWN_DELAY_PROP = 'shownDelay';
48
+ var LEGACY_HIDE_DELAY_PROP = 'hideDelay';
49
+ var NEW_HOVER_DELAY_PROP = 'hoverDelay';
50
+ function transformer(file, api, options) {
51
+ var alias = options.alias;
52
+ var j = api.jscodeshift;
53
+ var source = j(file.source);
54
+ var localName = (0, codemod_helpers_1.getImportInfo)(j, file, componentName, alias).localName;
55
+ var attributeReplacer = (0, codemod_helpers_1.createAttributeManipulator)(ATTRIBUTE_MANIPULATOR, api);
56
+ source
57
+ .find(j.ImportDeclaration)
58
+ .filter(function (path) { return path.node.source.value === alias; })
59
+ .find(j.ImportSpecifier, { imported: { name: componentName } })
60
+ .forEach(function (path) {
61
+ return j(path).replaceWith(function (path) { return j.importSpecifier(j.identifier('Tooltip'), path.node.local); });
62
+ });
63
+ source
64
+ .find(j.JSXOpeningElement)
65
+ .filter(function (path) { return path.value.name.type === 'JSXIdentifier' && path.value.name.name === localName; })
66
+ .find(j.JSXAttribute, function (attribute) {
67
+ return typeof attribute.name.name === 'string' ? attributeReplacer.has(attribute.name.name) : false;
68
+ })
69
+ .forEach(function (attribute) {
70
+ var attributeName = attribute.node.name.name;
71
+ var foundFix = attributeReplacer.getReplacers(attributeName);
72
+ if (foundFix) {
73
+ if (foundFix.action === 'remove') {
74
+ j(attribute).remove();
75
+ }
76
+ else {
77
+ var value = attribute.node.value;
78
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(foundFix.keyTo()), foundFix.valueTo(value)));
79
+ }
80
+ }
81
+ });
82
+ var complexAttributeReplacer = (0, codemod_helpers_1.createAttributeManipulator)(COMPLEX_ATTRIBUTE_MANIPULATOR, api);
83
+ source.findJSXElements(localName).forEach(function (element) {
84
+ j(element)
85
+ .find(j.JSXAttribute, function (attribute) {
86
+ return typeof attribute.name.name === 'string'
87
+ ? complexAttributeReplacer.has(attribute.name.name)
88
+ : false;
89
+ })
90
+ .forEach(function (attribute, _, attributes) {
91
+ var attributeName = attribute.node.name.name;
92
+ if (attributes.length === 2 && attributeName === FORCE_PORTAL) {
93
+ j(attribute).remove();
94
+ }
95
+ else {
96
+ var foundFix = complexAttributeReplacer.getReplacers(attributeName);
97
+ if (foundFix && foundFix.action !== 'remove') {
98
+ var value = attribute.node.value;
99
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(foundFix.keyTo()), foundFix.valueTo(value)));
100
+ }
101
+ }
102
+ });
103
+ // разрешаем hoverDelay
104
+ var _a = [
105
+ j(element).find(j.JSXAttribute, function (attribute) { return attribute.name.name === LEGACY_SHOWN_DELAY_PROP; }),
106
+ j(element).find(j.JSXAttribute, function (attribute) { return attribute.name.name === LEGACY_HIDE_DELAY_PROP; }),
107
+ ], shownDelayAttr = _a[0], hideDelayAttr = _a[1];
108
+ var getNumericLiteral = function (attributeCollection) {
109
+ var val;
110
+ attributeCollection.find(j.NumericLiteral).forEach(function (attribute) {
111
+ val = attribute.node;
112
+ });
113
+ return val ? val : j.numericLiteral(0);
114
+ };
115
+ if (shownDelayAttr.length === 1 && hideDelayAttr.length === 0) {
116
+ shownDelayAttr.forEach(function (attribute) {
117
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(NEW_HOVER_DELAY_PROP), j.jsxExpressionContainer(getNumericLiteral(shownDelayAttr))));
118
+ });
119
+ }
120
+ else if (shownDelayAttr.length === 0 && hideDelayAttr.length === 1) {
121
+ hideDelayAttr.forEach(function (attribute) {
122
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(NEW_HOVER_DELAY_PROP), j.jsxExpressionContainer(j.arrayExpression([j.numericLiteral(0), getNumericLiteral(hideDelayAttr)]))));
123
+ });
124
+ }
125
+ else if (shownDelayAttr.length === 1 && hideDelayAttr.length === 1) {
126
+ shownDelayAttr.forEach(function (attribute) {
127
+ j(attribute).replaceWith(j.jsxAttribute(j.jsxIdentifier(NEW_HOVER_DELAY_PROP), j.jsxExpressionContainer(j.arrayExpression([
128
+ getNumericLiteral(shownDelayAttr),
129
+ getNumericLiteral(hideDelayAttr),
130
+ ]))));
131
+ });
132
+ hideDelayAttr.forEach(function (attribute) {
133
+ j(attribute).remove();
134
+ });
135
+ }
136
+ });
137
+ return source.toSource();
138
+ }
139
+ exports.default = transformer;