@workday/canvas-kit-codemod 6.3.0 → 7.0.0-alpha.0-next.0
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/v5/getImportRenameMap.d.ts.map +1 -1
- package/dist/es6/v5/getImportRenameMap.js +1 -0
- package/dist/es6/v5/removeButtonEnums.d.ts.map +1 -1
- package/dist/es6/v5/removeButtonEnums.js +3 -1
- package/dist/es6/v5/renamePreviewTokenImports.js +1 -1
- package/dist/es6/v6/deprecateCookieBanner.d.ts +3 -2
- package/dist/es6/v6/deprecateCookieBanner.d.ts.map +1 -1
- package/dist/es6/v6/deprecateCookieBanner.js +8 -110
- package/dist/es6/v6/deprecateHeader.d.ts +3 -2
- package/dist/es6/v6/deprecateHeader.d.ts.map +1 -1
- package/dist/es6/v6/deprecateHeader.js +5 -101
- package/dist/es6/v6/deprecatePageHeader.d.ts +3 -2
- package/dist/es6/v6/deprecatePageHeader.d.ts.map +1 -1
- package/dist/es6/v6/deprecatePageHeader.js +8 -100
- package/dist/es6/v6/index.d.ts +3 -2
- package/dist/es6/v6/index.d.ts.map +1 -1
- package/dist/es6/v6/index.js +3 -2
- package/dist/es6/v6/moveAndRenameSearchBar.d.ts +3 -2
- package/dist/es6/v6/moveAndRenameSearchBar.d.ts.map +1 -1
- package/dist/es6/v6/moveAndRenameSearchBar.js +8 -49
- package/dist/es6/v6/renameCanvasDepthValue.d.ts +3 -2
- package/dist/es6/v6/renameCanvasDepthValue.d.ts.map +1 -1
- package/dist/es6/v6/renameCanvasDepthValue.js +7 -72
- package/dist/es6/v6/utils/index.d.ts +20 -1
- package/dist/es6/v6/utils/index.d.ts.map +1 -1
- package/dist/es6/v6/utils/index.js +121 -0
- package/dist/es6/v7/compoundActionBar.d.ts +3 -0
- package/dist/es6/v7/compoundActionBar.d.ts.map +1 -0
- package/dist/es6/v7/compoundActionBar.js +42 -0
- 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/index.d.ts +3 -0
- package/dist/es6/v7/index.d.ts.map +1 -0
- package/dist/es6/v7/index.js +5 -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 +50 -0
- package/dist/es6/v7/utils/hasImportSpecifiers.d.ts +20 -0
- package/dist/es6/v7/utils/hasImportSpecifiers.d.ts.map +1 -0
- package/dist/es6/v7/utils/hasImportSpecifiers.js +46 -0
- package/index.js +16 -9
- package/package.json +7 -4
|
@@ -1,74 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
var renameMap = {
|
|
4
|
-
CanvasDepthValue: 'CanvasDepthValues',
|
|
5
|
-
};
|
|
6
|
-
export default function transformer(file, api) {
|
|
1
|
+
import { renameImports } from './utils';
|
|
2
|
+
var transform = function (file, api) {
|
|
7
3
|
var j = api.jscodeshift;
|
|
8
4
|
var root = j(file.source);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (value === mainPackage || value === tokensPackage) {
|
|
15
|
-
var importSpecifiers = nodePath.specifiers || [];
|
|
16
|
-
importSpecifiers.forEach(function (specifier) {
|
|
17
|
-
if (specifier.type === 'ImportSpecifier' && specifier.imported.name in renameMap) {
|
|
18
|
-
hasCanvasDepthValueImports = true;
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
// Failsafe to skip transforms unless a CanvasDepthValue import is detected
|
|
24
|
-
if (!hasCanvasDepthValueImports) {
|
|
25
|
-
return root.toSource();
|
|
26
|
-
}
|
|
27
|
-
// Rename CanvasDepthValue import from @workday/canvas-kit-react
|
|
28
|
-
// e.g. import { CanvasDepthValue } from '@workday/canvas-kit-react';
|
|
29
|
-
// becomes import { CanvasDepthValues } from '@workday/canvas-kit-react';
|
|
30
|
-
root.find(j.ImportDeclaration, { source: { value: mainPackage } }).forEach(function (nodePath) {
|
|
31
|
-
var importSpecifiers = nodePath.value.specifiers || [];
|
|
32
|
-
importSpecifiers.forEach(function (specifier) {
|
|
33
|
-
if (specifier.type === 'ImportSpecifier' && specifier.imported.name in renameMap) {
|
|
34
|
-
specifier.imported.name = renameMap[specifier.imported.name];
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
// Rename CanvasDepthValue import from @workday/canvas-kit-react/tokens
|
|
39
|
-
// e.g. import { CanvasDepthValue } from '@workday/canvas-kit-react/tokens';
|
|
40
|
-
// becomes import { CanvasDepthValues } from '@workday/canvas-kit-react/tokens';
|
|
41
|
-
root.find(j.ImportDeclaration, { source: { value: tokensPackage } }).forEach(function (nodePath) {
|
|
42
|
-
var importSpecifiers = nodePath.value.specifiers || [];
|
|
43
|
-
importSpecifiers.forEach(function (specifier) {
|
|
44
|
-
if (specifier.type === 'ImportSpecifier' && specifier.imported.name in renameMap) {
|
|
45
|
-
specifier.imported.name = renameMap[specifier.imported.name];
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
// Transform CanvasDepthValue type references
|
|
50
|
-
// e.g. type CustomDepthValues = CanvasDepthValue;
|
|
51
|
-
// becomes type CustomDepthValues = CanvasDepthValues;
|
|
52
|
-
root.find(j.TSTypeReference, { typeName: { type: 'Identifier' } }).forEach(function (nodePath) {
|
|
53
|
-
var identifier = nodePath.value.typeName;
|
|
54
|
-
if (identifier.name in renameMap) {
|
|
55
|
-
identifier.name = renameMap[identifier.name];
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
// Transform CanvasDepthValue type interface declaration references
|
|
59
|
-
// e.g. interface OtherCustomDepthValues extends CanvasDepthValue {};
|
|
60
|
-
// becomes interface OtherCustomDepthValues extends CanvasDepthValues {};
|
|
61
|
-
root.find(j.TSInterfaceDeclaration).forEach(function (nodePath) {
|
|
62
|
-
var _a;
|
|
63
|
-
// If the interface is extending SearchBarProps, transform the extension name to SearchFormProps
|
|
64
|
-
(_a = nodePath.node.extends) === null || _a === void 0 ? void 0 : _a.forEach(function (typeExtension) {
|
|
65
|
-
if (typeExtension.expression.type === 'Identifier') {
|
|
66
|
-
var typeName = typeExtension.expression.name;
|
|
67
|
-
if (typeExtension.expression.name in renameMap) {
|
|
68
|
-
typeExtension.expression.name = renameMap[typeName];
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
return root.toSource();
|
|
74
|
-
}
|
|
5
|
+
return renameImports(api, root, '@workday/canvas-kit-react/tokens', {
|
|
6
|
+
CanvasDepthValue: 'CanvasDepthValues',
|
|
7
|
+
}).toSource();
|
|
8
|
+
};
|
|
9
|
+
export default transform;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ImportSpecifier, ImportDefaultSpecifier, ImportNamespaceSpecifier } from 'jscodeshift';
|
|
1
|
+
import { ImportSpecifier, ImportDefaultSpecifier, ImportNamespaceSpecifier, Collection, API } from 'jscodeshift';
|
|
2
2
|
export declare type ImportSpecifierArray = (ImportSpecifier | ImportNamespaceSpecifier | ImportDefaultSpecifier)[];
|
|
3
3
|
/**
|
|
4
4
|
* Returns a filtered array of ImportSpecifiers
|
|
@@ -48,4 +48,23 @@ export declare function filterImportDefaultSpecifiers(importSpecifiers: ImportSp
|
|
|
48
48
|
* renameImportSpecifiers(pageHeaderSpecifiers, renameMap);
|
|
49
49
|
*/
|
|
50
50
|
export declare function renameImportDefaultSpecifiers(importSpecifiers: ImportDefaultSpecifier[], renameMap: RenameMap): ImportDefaultSpecifier[];
|
|
51
|
+
/**
|
|
52
|
+
* Search for import statements for a package and specific import specifiers. This
|
|
53
|
+
* is useful for codemods to skip files that don't contain import specifiers that
|
|
54
|
+
* require codemoding.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* const hasImports = hasImportSpecifiers(
|
|
58
|
+
* api,
|
|
59
|
+
* root,
|
|
60
|
+
* '@workday/canvas-kit-react/cookie-banner',
|
|
61
|
+
* ['CookieBanner', 'CookieBannerProps']
|
|
62
|
+
* )
|
|
63
|
+
*
|
|
64
|
+
* if (!hasImports) {
|
|
65
|
+
* return file.source
|
|
66
|
+
* }
|
|
67
|
+
*/
|
|
68
|
+
export declare function hasImportSpecifiers(api: API, root: Collection<any>, packageName: string | string[], specifiers: string[]): boolean;
|
|
69
|
+
export declare function renameImports(api: API, root: Collection<any>, packageName: string, specifierMap: Record<string, string>): Collection<any>;
|
|
51
70
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../lib/v6/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../lib/v6/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,wBAAwB,EACxB,UAAU,EACV,GAAG,EACJ,MAAM,aAAa,CAAC;AAErB,oBAAY,oBAAoB,GAAG,CAC/B,eAAe,GACf,wBAAwB,GACxB,sBAAsB,CACzB,EAAE,CAAC;AAEJ;;;;;;;;;;;GAWG;AACH,wBAAgB,sBAAsB,CACpC,gBAAgB,EAAE,oBAAoB,EACtC,WAAW,CAAC,EAAE,MAAM,EAAE,qBAevB;AAED,oBAAY,SAAS,GAAG;IACtB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CAAC,gBAAgB,EAAE,eAAe,EAAE,EAAE,SAAS,EAAE,SAAS,qBAQ/F;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,6BAA6B,CAC3C,gBAAgB,EAAE,oBAAoB,EACtC,WAAW,CAAC,EAAE,MAAM,EAAE,4BAevB;AAED;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAC3C,gBAAgB,EAAE,sBAAsB,EAAE,EAC1C,SAAS,EAAE,SAAS,4BASrB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,UAAU,CAAC,GAAG,CAAC,EACrB,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,EAC9B,UAAU,EAAE,MAAM,EAAE,WAkCrB;AAED,wBAAgB,aAAa,CAC3B,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,UAAU,CAAC,GAAG,CAAC,EACrB,WAAW,EAAE,MAAM,EACnB,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GACnC,UAAU,CAAC,GAAG,CAAC,CAsFjB"}
|
|
@@ -88,3 +88,124 @@ export function renameImportDefaultSpecifiers(importSpecifiers, renameMap) {
|
|
|
88
88
|
});
|
|
89
89
|
return importSpecifiers;
|
|
90
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Search for import statements for a package and specific import specifiers. This
|
|
93
|
+
* is useful for codemods to skip files that don't contain import specifiers that
|
|
94
|
+
* require codemoding.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* const hasImports = hasImportSpecifiers(
|
|
98
|
+
* api,
|
|
99
|
+
* root,
|
|
100
|
+
* '@workday/canvas-kit-react/cookie-banner',
|
|
101
|
+
* ['CookieBanner', 'CookieBannerProps']
|
|
102
|
+
* )
|
|
103
|
+
*
|
|
104
|
+
* if (!hasImports) {
|
|
105
|
+
* return file.source
|
|
106
|
+
* }
|
|
107
|
+
*/
|
|
108
|
+
export function hasImportSpecifiers(api, root, packageName, specifiers) {
|
|
109
|
+
var j = api.jscodeshift;
|
|
110
|
+
return !!root.find(j.ImportDeclaration, function (nodePath) {
|
|
111
|
+
var value = nodePath.source.value;
|
|
112
|
+
// package name was found
|
|
113
|
+
if (typeof packageName === 'string'
|
|
114
|
+
? value === packageName
|
|
115
|
+
: packageName.includes(value)) {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
// Extract the main package(s) from the import, and test specifiers against that package
|
|
119
|
+
var mainPackage = (typeof packageName === 'string' ? [packageName] : packageName).map(function (s) {
|
|
120
|
+
return s
|
|
121
|
+
.split('/')
|
|
122
|
+
.slice(0, -1)
|
|
123
|
+
.join('/');
|
|
124
|
+
});
|
|
125
|
+
if (mainPackage.includes(value)) {
|
|
126
|
+
return !!(nodePath.specifiers || []).find(function (specifier) {
|
|
127
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
128
|
+
var specifierName = specifier.imported.name;
|
|
129
|
+
return specifiers.includes(specifierName);
|
|
130
|
+
}
|
|
131
|
+
return false;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
export function renameImports(api, root, packageName, specifierMap) {
|
|
138
|
+
var j = api.jscodeshift;
|
|
139
|
+
if (!hasImportSpecifiers(api, root, packageName, Object.keys(specifierMap))) {
|
|
140
|
+
return root;
|
|
141
|
+
}
|
|
142
|
+
var mainPackage = packageName
|
|
143
|
+
.split('/')
|
|
144
|
+
.slice(0, -1)
|
|
145
|
+
.join('/');
|
|
146
|
+
// Transform import statements
|
|
147
|
+
// e.g. import { CookieBanner, CookieBannerProps } from '@workday/canvas-kit-react';
|
|
148
|
+
// becomes import { DeprecatedCookieBanner, DeprecatedCookieBannerProps } from '@workday/canvas-kit-react';
|
|
149
|
+
root.find(j.ImportDeclaration, { source: { value: mainPackage } }).forEach(function (nodePath) {
|
|
150
|
+
var _a;
|
|
151
|
+
(_a = nodePath.value.specifiers) === null || _a === void 0 ? void 0 : _a.forEach(function (specifier) {
|
|
152
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
153
|
+
if (Object.keys(specifierMap).includes(specifier.imported.name)) {
|
|
154
|
+
specifier.imported.name = specifierMap[specifier.imported.name];
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
// Transforms default imports from package
|
|
160
|
+
root.find(j.ImportDeclaration, { source: { value: packageName } }).forEach(function (nodePath) {
|
|
161
|
+
var _a;
|
|
162
|
+
(_a = nodePath.value.specifiers) === null || _a === void 0 ? void 0 : _a.forEach(function (specifier) {
|
|
163
|
+
if (specifier.type === 'ImportDefaultSpecifier') {
|
|
164
|
+
if (specifier.local && Object.keys(specifierMap).includes(specifier.local.name)) {
|
|
165
|
+
specifier.local.name = specifierMap[specifier.local.name];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
169
|
+
if (Object.keys(specifierMap).includes(specifier.imported.name)) {
|
|
170
|
+
specifier.imported.name = specifierMap[specifier.imported.name];
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
// Transform type references
|
|
176
|
+
// e.g. `type CustomProps = CookieBannerProps;` becomes `type CustomProps = DeprecatedCookieBannerProps;`
|
|
177
|
+
root.find(j.TSTypeReference, { typeName: { type: 'Identifier' } }).forEach(function (nodePath) {
|
|
178
|
+
if (nodePath.value.typeName.type === 'Identifier' &&
|
|
179
|
+
Object.keys(specifierMap).includes(nodePath.value.typeName.name)) {
|
|
180
|
+
nodePath.value.typeName.name = specifierMap[nodePath.value.typeName.name];
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
// Transform type interface declaration references
|
|
184
|
+
// e.g. `interface CustomProps extends CookieBannerProps` becomes `interface CustomProps extends DeprecatedCookieBannerProps`
|
|
185
|
+
root.find(j.TSInterfaceDeclaration).forEach(function (nodePath) {
|
|
186
|
+
var _a;
|
|
187
|
+
// If the interface is extending CookieBannerProps, transform the extension name to DeprecatedCookieBannerProps
|
|
188
|
+
(_a = nodePath.node.extends) === null || _a === void 0 ? void 0 : _a.forEach(function (typeExtension) {
|
|
189
|
+
if (typeExtension.expression.type === 'Identifier' &&
|
|
190
|
+
Object.keys(specifierMap).includes(typeExtension.expression.name)) {
|
|
191
|
+
typeExtension.expression.name = specifierMap[typeExtension.expression.name];
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
// Transform JSXElements
|
|
196
|
+
// e.g. `<CookieBanner>` becomes `<DeprecatedCookieBanner>`
|
|
197
|
+
root.find(j.JSXIdentifier).forEach(function (nodePath) {
|
|
198
|
+
if (Object.keys(specifierMap).includes(nodePath.value.name)) {
|
|
199
|
+
nodePath.node.name = specifierMap[nodePath.value.name];
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
// Transform MemberExpressions
|
|
203
|
+
// e.g. `CookieBanner.DefaultNotice` becomes `DeprecatedCookieBanner.DefaultNotice`
|
|
204
|
+
root.find(j.MemberExpression, { object: { type: 'Identifier' } }).forEach(function (nodePath) {
|
|
205
|
+
if (nodePath.value.object.type === 'Identifier' &&
|
|
206
|
+
Object.keys(specifierMap).includes(nodePath.value.object.name)) {
|
|
207
|
+
nodePath.value.object.name = specifierMap[nodePath.value.object.name];
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
return root;
|
|
211
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compoundActionBar.d.ts","sourceRoot":"","sources":["../../../lib/v7/compoundActionBar.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAyC,MAAM,aAAa,CAAC;AAM3F,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,UAkD7E"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { getImportRenameMap } from './utils/getImportRenameMap';
|
|
2
|
+
import { hasImportSpecifiers } from '../v6/utils';
|
|
3
|
+
var actionBarPackage = '@workday/canvas-kit-react/action-bar';
|
|
4
|
+
export default function transformer(file, api, options) {
|
|
5
|
+
var j = api.jscodeshift;
|
|
6
|
+
var root = j(file.source);
|
|
7
|
+
if (!hasImportSpecifiers(api, root, actionBarPackage, ['ActionBar'])) {
|
|
8
|
+
return file.source;
|
|
9
|
+
}
|
|
10
|
+
// Replace default import with named or renamed import
|
|
11
|
+
root.find(j.ImportDefaultSpecifier).forEach(function (nodePath) {
|
|
12
|
+
var _a;
|
|
13
|
+
var parent = nodePath.parent;
|
|
14
|
+
var importSource = String(parent.node.source.value);
|
|
15
|
+
var localName = (_a = nodePath.value.local) === null || _a === void 0 ? void 0 : _a.name;
|
|
16
|
+
if (actionBarPackage === importSource && localName) {
|
|
17
|
+
nodePath.replace(j.importSpecifier(j.identifier('ActionBar'), j.identifier(localName)));
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
var _a = getImportRenameMap(j, root, '@workday/canvas-kit-react/action-bar'), importMap = _a.importMap, styledMap = _a.styledMap;
|
|
21
|
+
// Remove fixed prop from ActionBar component
|
|
22
|
+
root
|
|
23
|
+
.find(j.JSXElement, function (value) {
|
|
24
|
+
return value.openingElement.name.type === 'JSXIdentifier' &&
|
|
25
|
+
(value.openingElement.name.name === importMap.ActionBar ||
|
|
26
|
+
value.openingElement.name.name === styledMap.ActionBar);
|
|
27
|
+
})
|
|
28
|
+
.forEach(function (nodePath) {
|
|
29
|
+
var attributes = nodePath.value.openingElement.attributes;
|
|
30
|
+
if (attributes) {
|
|
31
|
+
// remove these attributes from ActionBar
|
|
32
|
+
nodePath.value.openingElement.attributes = attributes.filter(function (item) {
|
|
33
|
+
return item.type === 'JSXAttribute' &&
|
|
34
|
+
item.name.type === 'JSXIdentifier' &&
|
|
35
|
+
['fixed'].includes(item.name.name)
|
|
36
|
+
? false
|
|
37
|
+
: true;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return root.toSource();
|
|
42
|
+
}
|
|
@@ -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 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/v7/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAC,MAAM,aAAa,CAAC;AAEnD,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,QAI7E"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export default function transformer(file, api, options) {
|
|
2
|
+
// These will run in order. If your transform depends on others, place yours after dependent transforms
|
|
3
|
+
// const fixes = [];
|
|
4
|
+
// return fixes.reduce((source, fix) => fix({...file, source}, api, options), file.source);
|
|
5
|
+
}
|
|
@@ -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,EAAE,WAAW,EAAiB,MAAM,aAAa,CAAC;AAEpE,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,WAAW,SAAK;;;;EAyDzF"}
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
return false;
|
|
26
|
+
});
|
|
27
|
+
root
|
|
28
|
+
.find(j.CallExpression, function (node) {
|
|
29
|
+
if (node.callee.type === 'Identifier' &&
|
|
30
|
+
node.callee.name === 'styled' &&
|
|
31
|
+
node.arguments[0].type === 'Identifier') {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
return false;
|
|
35
|
+
})
|
|
36
|
+
.forEach(function (nodePath) {
|
|
37
|
+
// const StyledName = styled(OriginalName)({})
|
|
38
|
+
// const StyledName = styled(OriginalName)`` // Tagged template
|
|
39
|
+
if ((nodePath.parent.value.type === 'CallExpression' ||
|
|
40
|
+
nodePath.parent.value.type === 'TaggedTemplateExpression') &&
|
|
41
|
+
nodePath.parent.parent.value.type === 'VariableDeclarator' &&
|
|
42
|
+
nodePath.parent.parent.value.id.type === 'Identifier') {
|
|
43
|
+
var styledName = nodePath.parent.parent.value.id.name;
|
|
44
|
+
if (nodePath.value.arguments[0].type === 'Identifier') {
|
|
45
|
+
styledMap[nodePath.value.arguments[0].name] = styledName;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return { containsCanvasImports: containsCanvasImports, importMap: importMap, styledMap: styledMap };
|
|
50
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Collection, API } from 'jscodeshift';
|
|
2
|
+
/**
|
|
3
|
+
* Search for import statements for a package and specific import specifiers. This
|
|
4
|
+
* is useful for codemods to skip files that don't contain import specifiers that
|
|
5
|
+
* require codemoding.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* const hasImports = hasImportSpecifiers(
|
|
9
|
+
* api,
|
|
10
|
+
* root,
|
|
11
|
+
* '@workday/canvas-kit-react/cookie-banner',
|
|
12
|
+
* ['CookieBanner', 'CookieBannerProps']
|
|
13
|
+
* )
|
|
14
|
+
*
|
|
15
|
+
* if (!hasImports) {
|
|
16
|
+
* return file.source
|
|
17
|
+
* }
|
|
18
|
+
*/
|
|
19
|
+
export declare function hasImportSpecifiers(api: API, root: Collection<any>, packageName: string | string[], specifiers: string[]): boolean;
|
|
20
|
+
//# sourceMappingURL=hasImportSpecifiers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hasImportSpecifiers.d.ts","sourceRoot":"","sources":["../../../../lib/v7/utils/hasImportSpecifiers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,GAAG,EAAC,MAAM,aAAa,CAAC;AAE5C;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,UAAU,CAAC,GAAG,CAAC,EACrB,WAAW,EAAE,MAAM,GAAG,MAAM,EAAE,EAC9B,UAAU,EAAE,MAAM,EAAE,WAkCrB"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Search for import statements for a package and specific import specifiers. This
|
|
3
|
+
* is useful for codemods to skip files that don't contain import specifiers that
|
|
4
|
+
* require codemoding.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const hasImports = hasImportSpecifiers(
|
|
8
|
+
* api,
|
|
9
|
+
* root,
|
|
10
|
+
* '@workday/canvas-kit-react/cookie-banner',
|
|
11
|
+
* ['CookieBanner', 'CookieBannerProps']
|
|
12
|
+
* )
|
|
13
|
+
*
|
|
14
|
+
* if (!hasImports) {
|
|
15
|
+
* return file.source
|
|
16
|
+
* }
|
|
17
|
+
*/
|
|
18
|
+
export function hasImportSpecifiers(api, root, packageName, specifiers) {
|
|
19
|
+
var j = api.jscodeshift;
|
|
20
|
+
return !!root.find(j.ImportDeclaration, function (nodePath) {
|
|
21
|
+
var value = nodePath.source.value;
|
|
22
|
+
// package name was found
|
|
23
|
+
if (typeof packageName === 'string'
|
|
24
|
+
? value === packageName
|
|
25
|
+
: packageName.includes(value)) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
// Extract the main package(s) from the import, and test specifiers against that package
|
|
29
|
+
var mainPackage = (typeof packageName === 'string' ? [packageName] : packageName).map(function (s) {
|
|
30
|
+
return s
|
|
31
|
+
.split('/')
|
|
32
|
+
.slice(0, -1)
|
|
33
|
+
.join('/');
|
|
34
|
+
});
|
|
35
|
+
if (mainPackage.includes(value)) {
|
|
36
|
+
return !!(nodePath.specifiers || []).find(function (specifier) {
|
|
37
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
38
|
+
var specifierName = specifier.imported.name;
|
|
39
|
+
return specifiers.includes(specifierName);
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
});
|
|
46
|
+
}
|
package/index.js
CHANGED
|
@@ -2,32 +2,39 @@
|
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
const {spawn} = require('child_process');
|
|
5
|
-
require('
|
|
5
|
+
const chalk = require('chalk');
|
|
6
6
|
|
|
7
7
|
const {_: commands, path} = require('yargs')
|
|
8
8
|
.scriptName('canvas-kit-codemod')
|
|
9
|
-
.usage('$0 <transform> [path]'
|
|
10
|
-
.command('v5 [path]', 'Canvas Kit v4 > v5 migration transform'
|
|
9
|
+
.usage(chalk.bold.blueBright('$0 <transform> [path]'))
|
|
10
|
+
.command('v5 [path]', chalk.gray('Canvas Kit v4 > v5 migration transform'), yargs => {
|
|
11
11
|
yargs.positional('path', {
|
|
12
12
|
type: 'string',
|
|
13
13
|
default: '.',
|
|
14
|
-
describe: 'The path to execute the transform in (recursively).'
|
|
14
|
+
describe: chalk.gray('The path to execute the transform in (recursively).'),
|
|
15
15
|
});
|
|
16
16
|
})
|
|
17
|
-
.command('v6 [path]', 'Canvas Kit v5 > v6 migration transform'
|
|
17
|
+
.command('v6 [path]', chalk.gray('Canvas Kit v5 > v6 migration transform'), yargs => {
|
|
18
18
|
yargs.positional('path', {
|
|
19
19
|
type: 'string',
|
|
20
20
|
default: '.',
|
|
21
|
-
describe: 'The path to execute the transform in (recursively).'
|
|
21
|
+
describe: chalk.gray('The path to execute the transform in (recursively).'),
|
|
22
22
|
});
|
|
23
23
|
})
|
|
24
|
-
.
|
|
24
|
+
.command('v7 [path]', chalk.gray('Canvas Kit v6 > v7 migration transform'), yargs => {
|
|
25
|
+
yargs.positional('path', {
|
|
26
|
+
type: 'string',
|
|
27
|
+
default: '.',
|
|
28
|
+
describe: chalk.gray('The path to execute the transform in (recursively).'),
|
|
29
|
+
});
|
|
30
|
+
})
|
|
31
|
+
.demandCommand(1, chalk.red.bold('You must provide a transform to apply.'))
|
|
25
32
|
.strictCommands()
|
|
26
33
|
.fail((msg, err, yargs) => {
|
|
27
34
|
if (err) {
|
|
28
35
|
throw err;
|
|
29
36
|
}
|
|
30
|
-
console.error(`\n${
|
|
37
|
+
console.error(`\n${chalk.red.bold(msg)}\n`);
|
|
31
38
|
console.error(yargs.help());
|
|
32
39
|
process.exit(1);
|
|
33
40
|
})
|
|
@@ -41,7 +48,7 @@ const {_: commands, path} = require('yargs')
|
|
|
41
48
|
const transform = commands[0];
|
|
42
49
|
console.log(transform, path);
|
|
43
50
|
|
|
44
|
-
console.log(`\nApplying ${transform} transform to '${path}'\n
|
|
51
|
+
console.log(chalk.blueBright(`\nApplying ${transform} transform to '${path}'\n`));
|
|
45
52
|
const args = `-t ${__dirname}/dist/es6/${transform} ${path} --parser tsx --extensions js,jsx,ts,tsx`.split(
|
|
46
53
|
' '
|
|
47
54
|
);
|