@workday/canvas-kit-codemod 6.0.0-alpha.0-next.36 → 6.0.0-alpha.0-next.37
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/dist/es6/v7/compoundBanner.d.ts +3 -0
- package/dist/es6/v7/compoundBanner.d.ts.map +1 -0
- package/dist/es6/v7/compoundBanner.js +133 -0
- package/dist/es6/v7/utils/getImportRenameMap.d.ts +7 -0
- package/dist/es6/v7/utils/getImportRenameMap.d.ts.map +1 -0
- package/dist/es6/v7/utils/getImportRenameMap.js +49 -0
- package/package.json +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compoundBanner.d.ts","sourceRoot":"","sources":["../../../lib/v7/compoundBanner.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,QAAQ,EACR,OAAO,EAUR,MAAM,aAAa,CAAC;AAQrB,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,UAuM7E"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { getImportRenameMap } from './utils/getImportRenameMap';
|
|
2
|
+
function filterDefined(input) {
|
|
3
|
+
return !!input;
|
|
4
|
+
}
|
|
5
|
+
export default function transformer(file, api, options) {
|
|
6
|
+
var j = api.jscodeshift;
|
|
7
|
+
var root = j(file.source);
|
|
8
|
+
var _a = getImportRenameMap(j, root, '@workday/canvas-kit-react/banner'), containsCanvasImports = _a.containsCanvasImports, importMap = _a.importMap, styledMap = _a.styledMap;
|
|
9
|
+
if (!containsCanvasImports) {
|
|
10
|
+
return file.source;
|
|
11
|
+
}
|
|
12
|
+
root
|
|
13
|
+
.find(j.JSXElement, function (value) {
|
|
14
|
+
return value.openingElement.name.type === 'JSXIdentifier' &&
|
|
15
|
+
(value.openingElement.name.name === importMap.Banner ||
|
|
16
|
+
value.openingElement.name.name === styledMap.Banner);
|
|
17
|
+
})
|
|
18
|
+
.forEach(function (nodePath) {
|
|
19
|
+
var _a, _b;
|
|
20
|
+
// bail early if we find a `Banner.Label` as a child
|
|
21
|
+
for (var _i = 0, _c = nodePath.value.children || []; _i < _c.length; _i++) {
|
|
22
|
+
var child = _c[_i];
|
|
23
|
+
if (child.type === 'JSXElement' &&
|
|
24
|
+
child.openingElement.name.type === 'JSXMemberExpression' &&
|
|
25
|
+
child.openingElement.name.object.type === 'JSXIdentifier' &&
|
|
26
|
+
child.openingElement.name.object.name === 'Banner' &&
|
|
27
|
+
child.openingElement.name.property.type === 'JSXIdentifier' &&
|
|
28
|
+
child.openingElement.name.property.name === 'Label') {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
var findAttribute = function (name) { return function (item) {
|
|
33
|
+
if (item.type === 'JSXAttribute' &&
|
|
34
|
+
item.name.type === 'JSXIdentifier' &&
|
|
35
|
+
item.name.name === name) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
return false;
|
|
39
|
+
}; };
|
|
40
|
+
var attributes = nodePath.value.openingElement.attributes;
|
|
41
|
+
if (attributes) {
|
|
42
|
+
var label = attributes.find(findAttribute('label'));
|
|
43
|
+
var actionText = attributes.find(findAttribute('actionText'));
|
|
44
|
+
var errorIndex_1 = attributes.findIndex(findAttribute('error'));
|
|
45
|
+
var variantIndex_1 = attributes.findIndex(findAttribute('variant'));
|
|
46
|
+
if (errorIndex_1 >= 0 && ((_a = nodePath.value.openingElement.attributes) === null || _a === void 0 ? void 0 : _a[errorIndex_1])) {
|
|
47
|
+
var errorExpression = nodePath.value.openingElement.attributes[errorIndex_1].value.expression;
|
|
48
|
+
var getNameToBoolean = function (name) {
|
|
49
|
+
return name === 'Error' ? j.booleanLiteral(true) : j.booleanLiteral(false);
|
|
50
|
+
};
|
|
51
|
+
var setHasError = function (hasErrorValue) {
|
|
52
|
+
if (nodePath.value.openingElement.attributes) {
|
|
53
|
+
nodePath.value.openingElement.attributes[errorIndex_1] = j.jsxAttribute(j.jsxIdentifier('hasError'), j.jsxExpressionContainer(hasErrorValue));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
if (errorExpression.property) {
|
|
57
|
+
var previousExpression = errorExpression;
|
|
58
|
+
var newValue = getNameToBoolean(previousExpression.property.name);
|
|
59
|
+
setHasError(newValue);
|
|
60
|
+
}
|
|
61
|
+
if (errorExpression.test) {
|
|
62
|
+
var previousExpression = errorExpression;
|
|
63
|
+
var newValue = j.conditionalExpression(previousExpression.test, getNameToBoolean(previousExpression.consequent.property.name), getNameToBoolean(previousExpression.alternate.property.name));
|
|
64
|
+
setHasError(newValue);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (variantIndex_1 >= 0 && ((_b = nodePath.value.openingElement.attributes) === null || _b === void 0 ? void 0 : _b[variantIndex_1])) {
|
|
68
|
+
var stickyExpression = nodePath.value.openingElement.attributes[variantIndex_1].value.expression;
|
|
69
|
+
var getNameToBoolean = function (name) {
|
|
70
|
+
return name === 'Sticky' ? j.booleanLiteral(true) : j.booleanLiteral(false);
|
|
71
|
+
};
|
|
72
|
+
var setIsSticky = function (hasStickyValue) {
|
|
73
|
+
if (nodePath.value.openingElement.attributes) {
|
|
74
|
+
nodePath.value.openingElement.attributes[variantIndex_1] = j.jsxAttribute(j.jsxIdentifier('isSticky'), j.jsxExpressionContainer(hasStickyValue));
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
if (stickyExpression.property) {
|
|
78
|
+
var previousExpression = stickyExpression;
|
|
79
|
+
var newValue = getNameToBoolean(previousExpression.property.name);
|
|
80
|
+
setIsSticky(newValue);
|
|
81
|
+
}
|
|
82
|
+
if (stickyExpression.test) {
|
|
83
|
+
var previousExpression = stickyExpression;
|
|
84
|
+
var newValue = j.conditionalExpression(previousExpression.test, getNameToBoolean(previousExpression.consequent.property.name), getNameToBoolean(previousExpression.alternate.property.name));
|
|
85
|
+
setIsSticky(newValue);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// remove these attributes from the list - we'll add them as sub components
|
|
89
|
+
nodePath.value.openingElement.attributes = attributes.filter(function (item) {
|
|
90
|
+
if (item.type === 'JSXAttribute' &&
|
|
91
|
+
item.name.type === 'JSXIdentifier' &&
|
|
92
|
+
['label', 'actionText'].includes(item.name.name)) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
});
|
|
97
|
+
var BannerIconJSX = j.jsxMemberExpression(j.jsxIdentifier(importMap.Banner), j.jsxIdentifier('Icon'));
|
|
98
|
+
var BannerLabelJSX = j.jsxMemberExpression(j.jsxIdentifier(importMap.Banner), j.jsxIdentifier('Label'));
|
|
99
|
+
var BannerActionTextJSX = j.jsxMemberExpression(j.jsxIdentifier(importMap.Banner), j.jsxIdentifier('ActionText'));
|
|
100
|
+
var convertAttributeToChildren = function (input) {
|
|
101
|
+
var _a;
|
|
102
|
+
switch ((_a = input.value) === null || _a === void 0 ? void 0 : _a.type) {
|
|
103
|
+
case 'JSXExpressionContainer':
|
|
104
|
+
// unwrap JSXElements
|
|
105
|
+
if (input.value.expression.type === 'JSXElement') {
|
|
106
|
+
return [input.value.expression];
|
|
107
|
+
}
|
|
108
|
+
return [input.value];
|
|
109
|
+
default:
|
|
110
|
+
return [input.value];
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
var createSelfClosingTag = function (Tag) {
|
|
114
|
+
var OpeningTag = j.jsxOpeningElement(Tag);
|
|
115
|
+
OpeningTag.selfClosing = true;
|
|
116
|
+
return j.jsxElement(OpeningTag);
|
|
117
|
+
};
|
|
118
|
+
nodePath.value.openingElement.selfClosing = false;
|
|
119
|
+
// Add subcomponents back as children
|
|
120
|
+
nodePath.value.children = [
|
|
121
|
+
createSelfClosingTag(BannerIconJSX),
|
|
122
|
+
!!label
|
|
123
|
+
? j.jsxElement(j.jsxOpeningElement(BannerLabelJSX), j.jsxClosingElement(BannerLabelJSX), convertAttributeToChildren(label))
|
|
124
|
+
: createSelfClosingTag(BannerLabelJSX),
|
|
125
|
+
!!actionText
|
|
126
|
+
? j.jsxElement(j.jsxOpeningElement(BannerActionTextJSX), j.jsxClosingElement(BannerActionTextJSX), convertAttributeToChildren(actionText))
|
|
127
|
+
: createSelfClosingTag(BannerActionTextJSX),
|
|
128
|
+
].filter(filterDefined);
|
|
129
|
+
nodePath.value.closingElement = j.jsxClosingElement(nodePath.value.openingElement.name);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return root.toSource();
|
|
133
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Collection, JSCodeshift } from 'jscodeshift';
|
|
2
|
+
export declare function getImportRenameMap(j: JSCodeshift, root: Collection<any>, packageName?: string): {
|
|
3
|
+
containsCanvasImports: boolean;
|
|
4
|
+
importMap: Record<string, string>;
|
|
5
|
+
styledMap: Record<string, string>;
|
|
6
|
+
};
|
|
7
|
+
//# sourceMappingURL=getImportRenameMap.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getImportRenameMap.d.ts","sourceRoot":"","sources":["../../../../lib/v7/utils/getImportRenameMap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAqB,WAAW,EAAiB,MAAM,aAAa,CAAC;AAEvF,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,WAAW,SAAK;;;;EAwDzF"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export function getImportRenameMap(j, root, packageName) {
|
|
2
|
+
if (packageName === void 0) { packageName = ''; }
|
|
3
|
+
var containsCanvasImports = false;
|
|
4
|
+
// build import name remapping - in case someone renamed imports...
|
|
5
|
+
// i.e. `import { IconButton as StyledIconButton } ...`
|
|
6
|
+
var importMap = {};
|
|
7
|
+
var styledMap = {};
|
|
8
|
+
root.find(j.ImportDeclaration, function (node) {
|
|
9
|
+
// imports our module
|
|
10
|
+
var value = node.source.value;
|
|
11
|
+
if (typeof value === 'string' &&
|
|
12
|
+
(value === '@workday/canvas-kit-react' || value === packageName)) {
|
|
13
|
+
containsCanvasImports = true;
|
|
14
|
+
(node.specifiers || []).forEach(function (specifier) {
|
|
15
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
16
|
+
if (!specifier.local || specifier.local.name === specifier.imported.name) {
|
|
17
|
+
importMap[specifier.imported.name] = specifier.imported.name;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
importMap[specifier.imported.name] = specifier.local.name;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
root
|
|
27
|
+
.find(j.CallExpression, function (node) {
|
|
28
|
+
if (node.callee.type === 'Identifier' &&
|
|
29
|
+
node.callee.name === 'styled' &&
|
|
30
|
+
node.arguments[0].type === 'Identifier') {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
})
|
|
35
|
+
.forEach(function (nodePath) {
|
|
36
|
+
// const StyledName = styled(OriginalName)({})
|
|
37
|
+
// const StyledName = styled(OriginalName)`` // Tagged template
|
|
38
|
+
if ((nodePath.parent.value.type === 'CallExpression' ||
|
|
39
|
+
nodePath.parent.value.type === 'TaggedTemplateExpression') &&
|
|
40
|
+
nodePath.parent.parent.value.type === 'VariableDeclarator' &&
|
|
41
|
+
nodePath.parent.parent.value.id.type === 'Identifier') {
|
|
42
|
+
var styledName = nodePath.parent.parent.value.id.name;
|
|
43
|
+
if (nodePath.value.arguments[0].type === 'Identifier') {
|
|
44
|
+
styledMap[nodePath.value.arguments[0].name] = styledName;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
return { containsCanvasImports: containsCanvasImports, importMap: importMap, styledMap: styledMap };
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@workday/canvas-kit-codemod",
|
|
3
3
|
"author": "Workday, Inc. (https://www.workday.com)",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
|
-
"version": "6.0.0-alpha.0-next.
|
|
5
|
+
"version": "6.0.0-alpha.0-next.37+4f33e928",
|
|
6
6
|
"description": "A collection of codemods for use on Workday Canvas Kit packages.",
|
|
7
7
|
"main": "dist/es6/index.js",
|
|
8
8
|
"sideEffects": false,
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"test": "TZ=UTC jest -c ../../jest.config.js",
|
|
40
40
|
"typecheck:src": "tsc -p . --noEmit --incremental false"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "4f33e928c2e93b179997eaf8c9c6b40b87c10580"
|
|
43
43
|
}
|