@workday/canvas-kit-codemod 5.3.17 → 5.4.0-next.1

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.
@@ -1 +1 @@
1
- {"version":3,"file":"getImportRenameMap.d.ts","sourceRoot":"","sources":["../../../lib/v5/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"}
1
+ {"version":3,"file":"getImportRenameMap.d.ts","sourceRoot":"","sources":["../../../lib/v5/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"}
@@ -22,6 +22,7 @@ export function getImportRenameMap(j, root, packageName) {
22
22
  }
23
23
  });
24
24
  }
25
+ return false; // keep the loop going
25
26
  });
26
27
  root
27
28
  .find(j.CallExpression, function (node) {
@@ -1 +1 @@
1
- {"version":3,"file":"removeButtonEnums.d.ts","sourceRoot":"","sources":["../../../lib/v5/removeButtonEnums.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,QAAQ,EACR,OAAO,EAKR,MAAM,aAAa,CAAC;AAkHrB,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,UAmI7E"}
1
+ {"version":3,"file":"removeButtonEnums.d.ts","sourceRoot":"","sources":["../../../lib/v5/removeButtonEnums.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,QAAQ,EACR,OAAO,EAKR,MAAM,aAAa,CAAC;AAkHrB,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,UAqI7E"}
@@ -199,7 +199,9 @@ export default function transformer(file, api, options) {
199
199
  }
200
200
  return null;
201
201
  }
202
- root.find(j.TSTypeReference, findTypeRename).replaceWith(function (nodePath) {
202
+ root
203
+ .find(j.TSTypeReference, function (node) { return !!findTypeRename(node); })
204
+ .replaceWith(function (nodePath) {
203
205
  var node = nodePath.value;
204
206
  var matches = findTypeRename(node);
205
207
  if (matches) {
@@ -24,7 +24,7 @@ export default function transformer(file, api) {
24
24
  else {
25
25
  // `insertBefore` ensures the import order multiple specifiers from labs-react/tokens
26
26
  // are split into new imports. `insertAfter` would reverse the order, which isn't incorrect,
27
- // but probably not what you'd intuitively expect.
27
+ // but probably not what you'd intuitively expect.
28
28
  nodePath.insertBefore(j.importDeclaration([specifier], j.stringLiteral('@workday/canvas-kit-react/tokens')));
29
29
  }
30
30
  }
@@ -0,0 +1,4 @@
1
+ import { Transform } from 'jscodeshift';
2
+ declare const transform: Transform;
3
+ export default transform;
4
+ //# sourceMappingURL=deprecateCookieBanner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deprecateCookieBanner.d.ts","sourceRoot":"","sources":["../../../lib/v6/deprecateCookieBanner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAGtC,QAAA,MAAM,SAAS,EAAE,SAShB,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { renameImports } from './utils';
2
+ var transform = function (file, api) {
3
+ var j = api.jscodeshift;
4
+ var root = j(file.source);
5
+ return renameImports(api, root, '@workday/canvas-kit-react/cookie-banner', {
6
+ CookieBanner: 'DeprecatedCookieBanner',
7
+ CookieBannerProps: 'DeprecatedCookieBannerProps',
8
+ }).toSource();
9
+ };
10
+ export default transform;
@@ -0,0 +1,4 @@
1
+ import { Transform } from 'jscodeshift';
2
+ declare const transform: Transform;
3
+ export default transform;
4
+ //# sourceMappingURL=deprecateHeader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deprecateHeader.d.ts","sourceRoot":"","sources":["../../../lib/v6/deprecateHeader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAgBtC,QAAA,MAAM,SAAS,EAAE,SAWhB,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { renameImports } from './utils';
2
+ var headerRenameMap = {
3
+ DubLogoTitle: 'DeprecatedDubLogoTitle',
4
+ GlobalHeader: 'DeprecatedGlobalHeader',
5
+ Header: 'DeprecatedHeader',
6
+ HeaderHeight: 'DeprecatedHeaderHeight',
7
+ HeaderTheme: 'DeprecatedHeaderTheme',
8
+ HeaderVariant: 'DeprecatedHeaderVariant',
9
+ ThemeAttributes: 'DeprecatedHeaderThemeAttributes',
10
+ Themes: 'DeprecatedHeaderThemes',
11
+ WorkdayLogoTitle: 'DeprecatedWorkdayLogoTitle',
12
+ themes: 'deprecatedHeaderThemes',
13
+ };
14
+ var transform = function (file, api) {
15
+ var j = api.jscodeshift;
16
+ var root = j(file.source);
17
+ return renameImports(api, root, '@workday/canvas-kit-labs-react/header', headerRenameMap).toSource();
18
+ };
19
+ export default transform;
@@ -0,0 +1,4 @@
1
+ import { Transform } from 'jscodeshift';
2
+ declare const transform: Transform;
3
+ export default transform;
4
+ //# sourceMappingURL=deprecatePageHeader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deprecatePageHeader.d.ts","sourceRoot":"","sources":["../../../lib/v6/deprecatePageHeader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAGtC,QAAA,MAAM,SAAS,EAAE,SAShB,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { renameImports } from './utils';
2
+ var transform = function (file, api) {
3
+ var j = api.jscodeshift;
4
+ var root = j(file.source);
5
+ return renameImports(api, root, '@workday/canvas-kit-react/page-header', {
6
+ PageHeader: 'DeprecatedPageHeader',
7
+ PageHeaderProps: 'DeprecatedPageHeaderProps',
8
+ }).toSource();
9
+ };
10
+ export default transform;
@@ -1,3 +1,4 @@
1
- import { API, FileInfo, Options } from 'jscodeshift';
2
- export default function transformer(file: FileInfo, api: API, options: Options): void;
1
+ import { Transform } from 'jscodeshift';
2
+ declare const transform: Transform;
3
+ export default transform;
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/v6/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAC,MAAM,aAAa,CAAC;AAInD,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,OAAO,QAO7E"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../lib/v6/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAYtC,QAAA,MAAM,SAAS,EAAE,SAYhB,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -1,8 +1,34 @@
1
- // Future v6 codemods go here!
2
- export default function transformer(file, api, options) {
3
- // These will run in order. If your transform depends on others, place yours after dependent
4
- // transforms
5
- return;
6
- // const fixes = [];
7
- // return fixes.reduce((source, fix) => fix({...file, source}, api, options), file.source);
8
- }
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ // deprecate PageHeader
13
+ import deprecatePageHeader from './deprecatePageHeader';
14
+ // deprecate CookieBanner
15
+ import deprecateCookieBanner from './deprecateCookieBanner';
16
+ // move and rename SearchBar
17
+ import moveAndRenameSearchBar from './moveAndRenameSearchBar';
18
+ // deprecate header imports
19
+ import deprecateHeader from './deprecateHeader';
20
+ // rename CanvasDepthValue
21
+ import renameCanvasDepthValue from './renameCanvasDepthValue';
22
+ var transform = function (file, api, options) {
23
+ // These will run in order. If your transform depends on others, place yours after dependent transforms
24
+ var fixes = [
25
+ deprecatePageHeader,
26
+ deprecateCookieBanner,
27
+ moveAndRenameSearchBar,
28
+ // needs to run after `moveAndRenameSearchBar`
29
+ deprecateHeader,
30
+ renameCanvasDepthValue,
31
+ ];
32
+ return fixes.reduce(function (source, fix) { return fix(__assign(__assign({}, file), { source: source }), api, options); }, file.source);
33
+ };
34
+ export default transform;
@@ -0,0 +1,4 @@
1
+ import { Transform } from 'jscodeshift';
2
+ declare const transform: Transform;
3
+ export default transform;
4
+ //# sourceMappingURL=moveAndRenameSearchBar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"moveAndRenameSearchBar.d.ts","sourceRoot":"","sources":["../../../lib/v6/moveAndRenameSearchBar.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,SAAS,EAAqC,MAAM,aAAa,CAAC;AAyBnF,QAAA,MAAM,SAAS,EAAE,SAyGhB,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -0,0 +1,95 @@
1
+ var __spreadArrays = (this && this.__spreadArrays) || function () {
2
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
3
+ for (var r = Array(s), k = 0, i = 0; i < il; i++)
4
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
5
+ r[k] = a[j];
6
+ return r;
7
+ };
8
+ import { filterImportSpecifiers, renameImports, hasImportSpecifiers, } from './utils';
9
+ var mainPackage = '@workday/canvas-kit-labs-react';
10
+ var headerPackage = '@workday/canvas-kit-labs-react/header';
11
+ var searchBarRenameImports = ['SearchBar', 'SearchBarProps', 'SearchBarState'];
12
+ var searchBarImportSpecifiers = __spreadArrays(searchBarRenameImports, [
13
+ 'SearchThemeAttributes',
14
+ 'SearchThemes',
15
+ ]);
16
+ var renameMap = {
17
+ SearchBar: 'SearchForm',
18
+ SearchBarProps: 'SearchFormProps',
19
+ SearchBarState: 'SearchFormState',
20
+ };
21
+ var transform = function (file, api) {
22
+ var j = api.jscodeshift;
23
+ var root = j(file.source);
24
+ if (!hasImportSpecifiers(api, root, '@workday/canvas-kit-labs-react/header', Object.keys(renameMap))) {
25
+ return file.source;
26
+ }
27
+ function insertImportDeclarationBefore(nodePath, specifiers, sourcePath) {
28
+ nodePath.insertBefore(j.importDeclaration(specifiers, j.stringLiteral(sourcePath)));
29
+ }
30
+ var discoveredImportSpecifiers = {};
31
+ // Rename search-bar named imports from @workday/canvas-kit-labs-react
32
+ // e.g. import { SearchBar, SearchBarProps } from '@workday/canvas-kit-labs-react';
33
+ // becomes import { SearchForm, SearchFormProps } from '@workday/canvas-kit-labs-react';
34
+ root.find(j.ImportDeclaration, { source: { value: mainPackage } }).forEach(function (nodePath) {
35
+ var importSpecifiers = nodePath.value.specifiers || [];
36
+ // Filter search bar imports
37
+ var searchBarImports = filterImportSpecifiers(importSpecifiers);
38
+ // Rename search bar imports
39
+ searchBarImports.forEach(function (specifier) {
40
+ var specifierName = specifier.imported.name;
41
+ if (specifierName in renameMap) {
42
+ // if it hasn't been aliased, track it for updating JSX
43
+ if (!specifier.local || specifier.local.name === specifier.imported.name) {
44
+ discoveredImportSpecifiers[specifier.imported.name] = true;
45
+ }
46
+ specifier.imported.name = renameMap[specifierName];
47
+ }
48
+ });
49
+ });
50
+ // Handle imports from @workday/canvas-kit-react/header
51
+ // e.g import { Header, SearchBar, SearchBarProps } from '@workday/canvas-kit-labs-react/header';
52
+ // becomes import { Header } from '@workday/canvas-kit-labs-react/header';
53
+ // and import { SearchForm, SearchFormProps } from '@workday/canvas-kit-labs-react/search-form';
54
+ root.find(j.ImportDeclaration, { source: { value: headerPackage } }).forEach(function (nodePath) {
55
+ var importSpecifiers = nodePath.value.specifiers || [];
56
+ var headerSpecifiers = [];
57
+ var searchBarSpecifiers = [];
58
+ importSpecifiers.forEach(function (specifier) {
59
+ // If there is a SearchBar import specifier, keep it in an array.
60
+ // This will be used to build a new import declaration later.
61
+ if (specifier.type === 'ImportSpecifier' &&
62
+ searchBarImportSpecifiers.includes(specifier.imported.name)) {
63
+ searchBarSpecifiers.push(specifier);
64
+ }
65
+ else {
66
+ // If there is another header import specifier, keep it in a separate array.
67
+ // This will be used to build a new import declaration later.
68
+ headerSpecifiers.push(specifier);
69
+ }
70
+ });
71
+ // Remove the original import declaration
72
+ root.find(j.ImportDeclaration, { source: { value: headerPackage } }).remove();
73
+ // If SearchBar import specifiers were found, insert a new import declaration above the original import declaration
74
+ if (searchBarSpecifiers.length) {
75
+ searchBarSpecifiers.forEach(function (specifier) {
76
+ var specifierName = specifier.imported.name;
77
+ if (specifierName in renameMap) {
78
+ // if it hasn't been aliased, track it for updating JSX
79
+ if (!specifier.local || specifier.local.name === specifier.imported.name) {
80
+ discoveredImportSpecifiers[specifier.imported.name] = true;
81
+ }
82
+ specifier.imported.name = renameMap[specifierName];
83
+ }
84
+ });
85
+ insertImportDeclarationBefore(nodePath, searchBarSpecifiers, '@workday/canvas-kit-labs-react/search-form');
86
+ }
87
+ // If other header import specifiers were found, insert a new import declaration above the original import declaration
88
+ // This will replace the import declaration that was removed above
89
+ if (headerSpecifiers.length) {
90
+ insertImportDeclarationBefore(nodePath, headerSpecifiers, headerPackage);
91
+ }
92
+ });
93
+ return renameImports(api, root, '@workday/canvas-kit-labs-react/search-form', renameMap, discoveredImportSpecifiers).toSource();
94
+ };
95
+ export default transform;
@@ -0,0 +1,4 @@
1
+ import { Transform } from 'jscodeshift';
2
+ declare const transform: Transform;
3
+ export default transform;
4
+ //# sourceMappingURL=renameCanvasDepthValue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renameCanvasDepthValue.d.ts","sourceRoot":"","sources":["../../../lib/v6/renameCanvasDepthValue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAGtC,QAAA,MAAM,SAAS,EAAE,SAQhB,CAAC;AAEF,eAAe,SAAS,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { renameImports } from './utils';
2
+ var transform = function (file, api) {
3
+ var j = api.jscodeshift;
4
+ var root = j(file.source);
5
+ return renameImports(api, root, '@workday/canvas-kit-react/tokens', {
6
+ CanvasDepthValue: 'CanvasDepthValues',
7
+ }).toSource();
8
+ };
9
+ export default transform;
@@ -0,0 +1,59 @@
1
+ import { ImportSpecifier, ImportDefaultSpecifier, ImportNamespaceSpecifier, Collection, API } from 'jscodeshift';
2
+ export declare type ImportSpecifierArray = (ImportSpecifier | ImportNamespaceSpecifier | ImportDefaultSpecifier)[];
3
+ /**
4
+ * Returns a filtered array of ImportSpecifiers
5
+ * You can filter all ImportSpecifiers or additionally filter by providing an array of import names.
6
+ * @example
7
+ * root.find(j.ImportDeclaration, (nodePath: ImportDeclaration) => {
8
+ * const importSpecifiers = nodePath.specifiers || [];
9
+ * // specific named imports we're looking for
10
+ * const importNames = ['PageHeader', 'PageHeaderProps']
11
+ * const pageHeaderImports = filterImportSpecifiers(importSpecifiers, importNames);
12
+ * ...
13
+ * }
14
+ */
15
+ export declare function filterImportSpecifiers(importSpecifiers: ImportSpecifierArray, importNames?: string[]): ImportSpecifier[];
16
+ export declare type RenameMap = {
17
+ [key: string]: string;
18
+ };
19
+ /**
20
+ * Returns a filtered array of ImportDefaultSpecifiers
21
+ * You can filter all ImportDefaultSpecifiers or additionally filter by providing an array of import names.
22
+ * @example
23
+ * root.find(j.ImportDeclaration, (nodePath: ImportDeclaration) => {
24
+ * const importSpecifiers = nodePath.specifiers || [];
25
+ * // specific default imports we're looking for
26
+ * const importNames = ['PageHeader']
27
+ * const pageHeaderImports = filterImportDefaultSpecifiers(importSpecifiers, importNames);
28
+ * ...
29
+ * }
30
+ */
31
+ export declare function filterImportDefaultSpecifiers(importSpecifiers: ImportSpecifierArray, importNames?: string[]): ImportDefaultSpecifier[];
32
+ /**
33
+ * Renames import default specifiers for a given rename map
34
+ * @example
35
+ * const renameMap = { 'PageHeader': 'DeprecatedPageHeader' };
36
+ * const pageHeaderImports = filterImportDefaultSpecifiers(importSpecifiers, importNames);
37
+ * renameImportSpecifiers(pageHeaderSpecifiers, renameMap);
38
+ */
39
+ export declare function renameImportDefaultSpecifiers(importSpecifiers: ImportDefaultSpecifier[], renameMap: RenameMap): ImportDefaultSpecifier[];
40
+ /**
41
+ * Search for import statements for a package and specific import specifiers. This
42
+ * is useful for codemods to skip files that don't contain import specifiers that
43
+ * require codemoding.
44
+ *
45
+ * @example
46
+ * const hasImports = hasImportSpecifiers(
47
+ * api,
48
+ * root,
49
+ * '@workday/canvas-kit-react/cookie-banner',
50
+ * ['CookieBanner', 'CookieBannerProps']
51
+ * )
52
+ *
53
+ * if (!hasImports) {
54
+ * return file.source
55
+ * }
56
+ */
57
+ export declare function hasImportSpecifiers(api: API, root: Collection<any>, packageName: string | string[], specifiers: string[]): boolean;
58
+ export declare function renameImports(api: API, root: Collection<any>, packageName: string, specifierMap: Record<string, string>, discoveredImportSpecifiers?: Record<string, boolean>): Collection<any>;
59
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
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;;;;;;;;;;;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,EACpC,0BAA0B,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,GACvD,UAAU,CAAC,GAAG,CAAC,CA+GjB"}
@@ -0,0 +1,212 @@
1
+ /**
2
+ * Returns a filtered array of ImportSpecifiers
3
+ * You can filter all ImportSpecifiers or additionally filter by providing an array of import names.
4
+ * @example
5
+ * root.find(j.ImportDeclaration, (nodePath: ImportDeclaration) => {
6
+ * const importSpecifiers = nodePath.specifiers || [];
7
+ * // specific named imports we're looking for
8
+ * const importNames = ['PageHeader', 'PageHeaderProps']
9
+ * const pageHeaderImports = filterImportSpecifiers(importSpecifiers, importNames);
10
+ * ...
11
+ * }
12
+ */
13
+ export function filterImportSpecifiers(importSpecifiers, importNames) {
14
+ var filteredSpecifiers = [];
15
+ importSpecifiers.forEach(function (specifier) {
16
+ if (specifier.type === 'ImportSpecifier') {
17
+ if (importNames && importNames.includes(specifier.imported.name)) {
18
+ filteredSpecifiers.push(specifier);
19
+ }
20
+ else {
21
+ filteredSpecifiers.push(specifier);
22
+ }
23
+ }
24
+ });
25
+ return filteredSpecifiers;
26
+ }
27
+ /**
28
+ * Returns a filtered array of ImportDefaultSpecifiers
29
+ * You can filter all ImportDefaultSpecifiers or additionally filter by providing an array of import names.
30
+ * @example
31
+ * root.find(j.ImportDeclaration, (nodePath: ImportDeclaration) => {
32
+ * const importSpecifiers = nodePath.specifiers || [];
33
+ * // specific default imports we're looking for
34
+ * const importNames = ['PageHeader']
35
+ * const pageHeaderImports = filterImportDefaultSpecifiers(importSpecifiers, importNames);
36
+ * ...
37
+ * }
38
+ */
39
+ export function filterImportDefaultSpecifiers(importSpecifiers, importNames) {
40
+ var filteredSpecifiers = [];
41
+ importSpecifiers.forEach(function (specifier) {
42
+ var _a;
43
+ if (specifier.type === 'ImportDefaultSpecifier') {
44
+ var localName = ((_a = specifier.local) === null || _a === void 0 ? void 0 : _a.name) || '';
45
+ if (importNames && importNames.includes(localName)) {
46
+ filteredSpecifiers.push(specifier);
47
+ }
48
+ else {
49
+ filteredSpecifiers.push(specifier);
50
+ }
51
+ }
52
+ });
53
+ return filteredSpecifiers;
54
+ }
55
+ /**
56
+ * Renames import default specifiers for a given rename map
57
+ * @example
58
+ * const renameMap = { 'PageHeader': 'DeprecatedPageHeader' };
59
+ * const pageHeaderImports = filterImportDefaultSpecifiers(importSpecifiers, importNames);
60
+ * renameImportSpecifiers(pageHeaderSpecifiers, renameMap);
61
+ */
62
+ export function renameImportDefaultSpecifiers(importSpecifiers, renameMap) {
63
+ importSpecifiers.forEach(function (specifier) {
64
+ var _a;
65
+ var specifierName = (_a = specifier.local) === null || _a === void 0 ? void 0 : _a.name;
66
+ if (specifier.local && specifierName && specifierName in renameMap) {
67
+ specifier.local.name = renameMap[specifierName];
68
+ }
69
+ });
70
+ return importSpecifiers;
71
+ }
72
+ /**
73
+ * Search for import statements for a package and specific import specifiers. This
74
+ * is useful for codemods to skip files that don't contain import specifiers that
75
+ * require codemoding.
76
+ *
77
+ * @example
78
+ * const hasImports = hasImportSpecifiers(
79
+ * api,
80
+ * root,
81
+ * '@workday/canvas-kit-react/cookie-banner',
82
+ * ['CookieBanner', 'CookieBannerProps']
83
+ * )
84
+ *
85
+ * if (!hasImports) {
86
+ * return file.source
87
+ * }
88
+ */
89
+ export function hasImportSpecifiers(api, root, packageName, specifiers) {
90
+ var j = api.jscodeshift;
91
+ return !!root.find(j.ImportDeclaration, function (nodePath) {
92
+ var value = nodePath.source.value;
93
+ // package name was found
94
+ if (typeof packageName === 'string'
95
+ ? value === packageName
96
+ : packageName.includes(value)) {
97
+ return true;
98
+ }
99
+ // Extract the main package(s) from the import, and test specifiers against that package
100
+ var mainPackage = (typeof packageName === 'string' ? [packageName] : packageName).map(function (s) {
101
+ return s
102
+ .split('/')
103
+ .slice(0, -1)
104
+ .join('/');
105
+ });
106
+ if (mainPackage.includes(value)) {
107
+ return !!(nodePath.specifiers || []).find(function (specifier) {
108
+ if (specifier.type === 'ImportSpecifier') {
109
+ var specifierName = specifier.imported.name;
110
+ return specifiers.includes(specifierName);
111
+ }
112
+ return false;
113
+ });
114
+ }
115
+ return false;
116
+ });
117
+ }
118
+ export function renameImports(api, root, packageName, specifierMap, discoveredImportSpecifiers) {
119
+ if (discoveredImportSpecifiers === void 0) { discoveredImportSpecifiers = {}; }
120
+ var j = api.jscodeshift;
121
+ if (!hasImportSpecifiers(api, root, packageName, Object.keys(specifierMap))) {
122
+ return root;
123
+ }
124
+ var mainPackage = packageName
125
+ .split('/')
126
+ .slice(0, -1)
127
+ .join('/');
128
+ // Transform import statements
129
+ // e.g. import { CookieBanner, CookieBannerProps } from '@workday/canvas-kit-react';
130
+ // becomes import { DeprecatedCookieBanner, DeprecatedCookieBannerProps } from '@workday/canvas-kit-react';
131
+ // Find imports from main packages
132
+ // ex. '@workday/canvas-kit-labs-react'
133
+ root.find(j.ImportDeclaration, { source: { value: mainPackage } }).forEach(function (nodePath) {
134
+ var _a;
135
+ (_a = nodePath.value.specifiers) === null || _a === void 0 ? void 0 : _a.forEach(function (specifier) {
136
+ if (specifier.type === 'ImportSpecifier') {
137
+ if (Object.keys(specifierMap).includes(specifier.imported.name)) {
138
+ // if it hasn't been aliased, track it for updating JSX
139
+ if (!specifier.local || specifier.local.name === specifier.imported.name) {
140
+ discoveredImportSpecifiers[specifier.imported.name] = true;
141
+ }
142
+ specifier.imported.name = specifierMap[specifier.imported.name];
143
+ }
144
+ }
145
+ });
146
+ });
147
+ // Find imports from component specific packages
148
+ // ex. '@workday/canvas-kit-labs-react/header'
149
+ root.find(j.ImportDeclaration, { source: { value: packageName } }).forEach(function (nodePath) {
150
+ var _a;
151
+ (_a = nodePath.value.specifiers) === null || _a === void 0 ? void 0 : _a.forEach(function (specifier) {
152
+ // Transforms default imports
153
+ if (specifier.type === 'ImportDefaultSpecifier') {
154
+ if (specifier.local && Object.keys(specifierMap).includes(specifier.local.name)) {
155
+ discoveredImportSpecifiers[specifier.local.name] = true;
156
+ specifier.local.name = specifierMap[specifier.local.name];
157
+ }
158
+ }
159
+ // Transform named exports
160
+ if (specifier.type === 'ImportSpecifier') {
161
+ if (Object.keys(specifierMap).includes(specifier.imported.name)) {
162
+ // if it hasn't been aliased, track it for updating JSX
163
+ if (!specifier.local || specifier.local.name === specifier.imported.name) {
164
+ discoveredImportSpecifiers[specifier.imported.name] = true;
165
+ }
166
+ specifier.imported.name = specifierMap[specifier.imported.name];
167
+ }
168
+ }
169
+ });
170
+ });
171
+ // Transform type references
172
+ // e.g. `type CustomProps = CookieBannerProps;` becomes `type CustomProps = DeprecatedCookieBannerProps;`
173
+ root.find(j.TSTypeReference, { typeName: { type: 'Identifier' } }).forEach(function (nodePath) {
174
+ if (nodePath.value.typeName.type === 'Identifier' &&
175
+ Object.keys(specifierMap).includes(nodePath.value.typeName.name)) {
176
+ nodePath.value.typeName.name = specifierMap[nodePath.value.typeName.name];
177
+ }
178
+ });
179
+ // Transform type interface declaration references
180
+ // e.g. `interface CustomProps extends CookieBannerProps` becomes `interface CustomProps extends DeprecatedCookieBannerProps`
181
+ root.find(j.TSInterfaceDeclaration).forEach(function (nodePath) {
182
+ var _a;
183
+ // If the interface is extending CookieBannerProps, transform the extension name to DeprecatedCookieBannerProps
184
+ (_a = nodePath.node.extends) === null || _a === void 0 ? void 0 : _a.forEach(function (typeExtension) {
185
+ if (typeExtension.expression.type === 'Identifier' &&
186
+ Object.keys(specifierMap).includes(typeExtension.expression.name)) {
187
+ typeExtension.expression.name = specifierMap[typeExtension.expression.name];
188
+ }
189
+ });
190
+ });
191
+ // Transform JSXElements
192
+ // e.g. `<CookieBanner>` becomes `<DeprecatedCookieBanner>`
193
+ root.find(j.JSXIdentifier).forEach(function (nodePath) {
194
+ if (
195
+ // Check to see if it was imported from Canvas Kit
196
+ discoveredImportSpecifiers[nodePath.value.name] &&
197
+ Object.keys(specifierMap).includes(nodePath.value.name)) {
198
+ nodePath.node.name = specifierMap[nodePath.value.name];
199
+ }
200
+ });
201
+ // Transform MemberExpressions
202
+ // e.g. `CookieBanner.DefaultNotice` becomes `DeprecatedCookieBanner.DefaultNotice`
203
+ root.find(j.MemberExpression, { object: { type: 'Identifier' } }).forEach(function (nodePath) {
204
+ if (nodePath.value.object.type === 'Identifier' &&
205
+ // Check to see if it was imported from Canvas Kit
206
+ discoveredImportSpecifiers[nodePath.value.object.name] &&
207
+ Object.keys(specifierMap).includes(nodePath.value.object.name)) {
208
+ nodePath.value.object.name = specifierMap[nodePath.value.object.name];
209
+ }
210
+ });
211
+ return root;
212
+ }
package/index.js CHANGED
@@ -1,26 +1,33 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
 
4
- const {exec} = require('child_process');
5
- require('colors');
4
+ const {spawn} = require('child_process');
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]'.brightBlue.bold)
10
- .command('v5 [path]', 'Canvas Kit v4 > v5 migration transform'.gray, yargs => {
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).'.gray,
14
+ describe: chalk.gray('The path to execute the transform in (recursively).'),
15
15
  });
16
16
  })
17
- .demandCommand(1, 'You must provide a transform to apply.'.red.bold)
17
+ .command('v6 [path]', chalk.gray('Canvas Kit v5 > v6 migration transform'), yargs => {
18
+ yargs.positional('path', {
19
+ type: 'string',
20
+ default: '.',
21
+ describe: chalk.gray('The path to execute the transform in (recursively).'),
22
+ });
23
+ })
24
+ .demandCommand(1, chalk.red.bold('You must provide a transform to apply.'))
18
25
  .strictCommands()
19
26
  .fail((msg, err, yargs) => {
20
27
  if (err) {
21
28
  throw err;
22
29
  }
23
- console.error(`\n${msg.red.bold}\n`);
30
+ console.error(`\n${chalk.red.bold(msg)}\n`);
24
31
  console.error(yargs.help());
25
32
  process.exit(1);
26
33
  })
@@ -34,19 +41,20 @@ const {_: commands, path} = require('yargs')
34
41
  const transform = commands[0];
35
42
  console.log(transform, path);
36
43
 
37
- console.log(`\nApplying ${transform} transform to '${path}'\n`.brightBlue);
44
+ console.log(chalk.blueBright(`\nApplying ${transform} transform to '${path}'\n`));
45
+ const args = `-t ${__dirname}/dist/es6/${transform} ${path} --parser tsx --extensions js,jsx,ts,tsx`.split(
46
+ ' '
47
+ );
38
48
 
39
- exec(
40
- `jscodeshift -t ${__dirname}/dist/es6/${transform} ${path} --parser=tsx --extensions=js,jsx,ts,tsx --verbose=2`,
41
- (error, stdout, stderr) => {
42
- if (error) {
43
- console.error(`${error}\n`);
44
- return;
45
- }
46
- console.log(`${stdout}\n`);
49
+ const proc = spawn(`jscodeshift`, args);
47
50
 
48
- if (stderr) {
49
- console.error(`${stderr}\n`);
50
- }
51
- }
52
- );
51
+ let errCode = 0;
52
+ proc.stdout.on('data', data => console.log(data.toString()));
53
+ proc.stderr.on('data', data => {
54
+ errCode = 1;
55
+ console.error(data.toString());
56
+ });
57
+
58
+ proc.on('close', code => {
59
+ process.exit(code || errCode);
60
+ });
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": "5.3.17",
5
+ "version": "5.4.0-next.1+bf910ff4",
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,
@@ -29,15 +29,18 @@
29
29
  },
30
30
  "homepage": "https://github.com/Workday/canvas-kit#readme",
31
31
  "dependencies": {
32
- "colors": "^1.4.0",
33
- "jscodeshift": "^0.11.0",
32
+ "chalk": "4.1.2",
33
+ "jscodeshift": "^0.13.1",
34
34
  "yargs": "^16.2.0"
35
35
  },
36
+ "devDependencies": {
37
+ "@types/jscodeshift": "^0.11.3"
38
+ },
36
39
  "scripts": {
37
40
  "clean": "rimraf dist && rimraf .build-info && mkdirp dist",
38
41
  "build": "tsc -p tsconfig.es6.json",
39
42
  "test": "TZ=UTC jest -c ../../jest.config.js",
40
43
  "typecheck:src": "tsc -p . --noEmit --incremental false"
41
44
  },
42
- "gitHead": "0e5c733deb9079d061b788c205e73c54c55e7c58"
45
+ "gitHead": "bf910ff480fb9237ea4d70e95293de7bdffdb138"
43
46
  }