From ce8b9b9976f5f0980ae05640c604625c90c2cfaa Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Thu, 3 Oct 2024 10:50:44 -0400 Subject: [PATCH] fix(helpers): get moveSpecifiers to handle cjs/dynamic imports correctly (#784) --- .../src/rules/helpers/helpers.js | 9 +++++++-- .../dualListSelector-deprecated.test.ts | 8 ++++---- .../dualListSelectorNext-promoted.test.ts | 8 ++++---- .../rules/v6/modalDeprecated/modal-deprecated.test.ts | 8 ++++---- .../v6/modalNextPromoted/modalNext-promoted.test.ts | 8 ++++---- .../src/rules/v6/tileDeprecated/tile-deprecated.test.ts | 4 ++-- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/helpers.js b/packages/eslint-plugin-pf-codemods/src/rules/helpers/helpers.js index 7111ee6cd..23f2cf85e 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/helpers.js +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/helpers.js @@ -40,15 +40,20 @@ export function moveSpecifiers( // expecting @patternfly/{package} or // @patternfly/{package}/{designator} where designator is deprecated const toParts = toPackage.split('/'); + const parentSourceValue = firstSpecifier?.parent?.source?.value; + const modulePaths = ['esm', 'js', 'dynamic'].map((path) => `dist/${path}`); + const isDistImport = modulePaths.some((path) => + parentSourceValue?.includes(path) + ); if ( - !firstSpecifier?.parent?.source?.value?.includes('dist/esm') || + !isDistImport || toParts[0] !== '@patternfly' ) { return; } - const fromParts = firstSpecifier.parent.source.value.split('/'); + const fromParts = parentSourceValue.split('/'); //expecting @patternfly/{package}/dist/esm/components/{Component}/index.js //needing toPath to look like fromPath with the designator before /components if (toParts.length === 3) { diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/dualListSelectorDeprecated/dualListSelector-deprecated.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/dualListSelectorDeprecated/dualListSelector-deprecated.test.ts index c99e30cee..36a97f56d 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/dualListSelectorDeprecated/dualListSelector-deprecated.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/dualListSelectorDeprecated/dualListSelector-deprecated.test.ts @@ -69,14 +69,14 @@ specifiersToMove.forEach((specifier) => { invalidTests.push( createInvalidTest( `import { ${specifier} } from '@patternfly/react-core/dist/js/components/Modal/index.js';`, - `import {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`, + `import {\n\t${specifier}\n} from '@patternfly/react-core/dist/js/deprecated/components/Modal/index.js';`, [{ message: errorMessage, type: "ImportDeclaration" }] ) ); invalidTests.push( createInvalidTest( `import { ${specifier} } from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`, - `import {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`, + `import {\n\t${specifier}\n} from '@patternfly/react-core/dist/dynamic/deprecated/components/Modal/index.js';`, [{ message: errorMessage, type: "ImportDeclaration" }] ) ); @@ -97,14 +97,14 @@ specifiersToMove.forEach((specifier) => { invalidTests.push( createInvalidTest( `export { ${specifier} } from '@patternfly/react-core/dist/js/components/Modal/index.js';`, - `export {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`, + `export {\n\t${specifier}\n} from '@patternfly/react-core/dist/js/deprecated/components/Modal/index.js';`, [{ message: errorMessage, type: "ExportNamedDeclaration" }] ) ); invalidTests.push( createInvalidTest( `export { ${specifier} } from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`, - `export {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`, + `export {\n\t${specifier}\n} from '@patternfly/react-core/dist/dynamic/deprecated/components/Modal/index.js';`, [{ message: errorMessage, type: "ExportNamedDeclaration" }] ) ); diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/dualListSelectorNextPromoted/dualListSelectorNext-promoted.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/dualListSelectorNextPromoted/dualListSelectorNext-promoted.test.ts index eb877d5ee..824bf32f5 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/dualListSelectorNextPromoted/dualListSelectorNext-promoted.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/dualListSelectorNextPromoted/dualListSelectorNext-promoted.test.ts @@ -64,14 +64,14 @@ specifiersToMove.forEach((specifier) => { invalidTests.push( createInvalidTest( `import { ${specifier} } from '@patternfly/react-core/dist/js/next/components/Modal/index.js';`, - `import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`, + `import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/js/components/Modal/index.js';`, [{ message: errorMessage, type: "ImportDeclaration" }] ) ); invalidTests.push( createInvalidTest( `import { ${specifier} } from '@patternfly/react-core/dist/dynamic/next/components/Modal/index.js';`, - `import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`, + `import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`, [{ message: errorMessage, type: "ImportDeclaration" }] ) ); @@ -92,14 +92,14 @@ specifiersToMove.forEach((specifier) => { invalidTests.push( createInvalidTest( `export { ${specifier} } from '@patternfly/react-core/dist/js/next/components/Modal/index.js';`, - `export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`, + `export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/js/components/Modal/index.js';`, [{ message: errorMessage, type: "ExportNamedDeclaration" }] ) ); invalidTests.push( createInvalidTest( `export { ${specifier} } from '@patternfly/react-core/dist/dynamic/next/components/Modal/index.js';`, - `export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`, + `export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`, [{ message: errorMessage, type: "ExportNamedDeclaration" }] ) ); diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/modalDeprecated/modal-deprecated.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/modalDeprecated/modal-deprecated.test.ts index f0cc4dc58..ae3fca6d0 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/modalDeprecated/modal-deprecated.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/modalDeprecated/modal-deprecated.test.ts @@ -71,14 +71,14 @@ specifiersToMove.forEach((specifier) => { invalidTests.push( createInvalidTest( `import { ${specifier} } from '@patternfly/react-core/dist/js/components/Modal/index.js';`, - `import {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`, + `import {\n\t${specifier}\n} from '@patternfly/react-core/dist/js/deprecated/components/Modal/index.js';`, [{ message: errorMessage, type: "ImportDeclaration" }] ) ); invalidTests.push( createInvalidTest( `import { ${specifier} } from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`, - `import {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`, + `import {\n\t${specifier}\n} from '@patternfly/react-core/dist/dynamic/deprecated/components/Modal/index.js';`, [{ message: errorMessage, type: "ImportDeclaration" }] ) ); @@ -99,14 +99,14 @@ specifiersToMove.forEach((specifier) => { invalidTests.push( createInvalidTest( `export { ${specifier} } from '@patternfly/react-core/dist/js/components/Modal/index.js';`, - `export {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`, + `export {\n\t${specifier}\n} from '@patternfly/react-core/dist/js/deprecated/components/Modal/index.js';`, [{ message: errorMessage, type: "ExportNamedDeclaration" }] ) ); invalidTests.push( createInvalidTest( `export { ${specifier} } from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`, - `export {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`, + `export {\n\t${specifier}\n} from '@patternfly/react-core/dist/dynamic/deprecated/components/Modal/index.js';`, [{ message: errorMessage, type: "ExportNamedDeclaration" }] ) ); diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/modalNextPromoted/modalNext-promoted.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/modalNextPromoted/modalNext-promoted.test.ts index 1bb7ba9b8..ecf05015c 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/modalNextPromoted/modalNext-promoted.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/modalNextPromoted/modalNext-promoted.test.ts @@ -57,14 +57,14 @@ specifiersToMove.forEach((specifier) => { invalidTests.push( createInvalidTest( `import { ${specifier} } from '@patternfly/react-core/dist/js/next/components/Modal/index.js';`, - `import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`, + `import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/js/components/Modal/index.js';`, [{ message: errorMessage, type: "ImportDeclaration" }] ) ); invalidTests.push( createInvalidTest( `import { ${specifier} } from '@patternfly/react-core/dist/dynamic/next/components/Modal/index.js';`, - `import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`, + `import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`, [{ message: errorMessage, type: "ImportDeclaration" }] ) ); @@ -85,14 +85,14 @@ specifiersToMove.forEach((specifier) => { invalidTests.push( createInvalidTest( `export { ${specifier} } from '@patternfly/react-core/dist/js/next/components/Modal/index.js';`, - `export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`, + `export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/js/components/Modal/index.js';`, [{ message: errorMessage, type: "ExportNamedDeclaration" }] ) ); invalidTests.push( createInvalidTest( `export { ${specifier} } from '@patternfly/react-core/dist/dynamic/next/components/Modal/index.js';`, - `export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`, + `export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`, [{ message: errorMessage, type: "ExportNamedDeclaration" }] ) ); diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/tileDeprecated/tile-deprecated.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/tileDeprecated/tile-deprecated.test.ts index c090dd63d..7aed6423b 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/tileDeprecated/tile-deprecated.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/tileDeprecated/tile-deprecated.test.ts @@ -40,7 +40,7 @@ ruleTester.run('tile-deprecated', rule, { }, { code: `import { Tile } from '@patternfly/react-core/dist/js/components/Tile/index.js';`, - output: `import {\n\tTile\n} from '@patternfly/react-core/deprecated';`, + output: `import {\n\tTile\n} from '@patternfly/react-core/dist/js/deprecated/components/Tile/index.js';`, errors: [ { message: `Tile has been deprecated. Running the fix flag will update your imports to our deprecated package, but we suggest using Card instead.`, @@ -50,7 +50,7 @@ ruleTester.run('tile-deprecated', rule, { }, { code: `import { Tile } from '@patternfly/react-core/dist/dynamic/components/Tile/index.js';`, - output: `import {\n\tTile\n} from '@patternfly/react-core/deprecated';`, + output: `import {\n\tTile\n} from '@patternfly/react-core/dist/dynamic/deprecated/components/Tile/index.js';`, errors: [ { message: `Tile has been deprecated. Running the fix flag will update your imports to our deprecated package, but we suggest using Card instead.`,