diff --git a/packages/cli/src/codemods/lib.test.ts b/packages/cli/src/codemods/lib.test.ts index 898ee48f..d15970dd 100644 --- a/packages/cli/src/codemods/lib.test.ts +++ b/packages/cli/src/codemods/lib.test.ts @@ -2,7 +2,15 @@ import { expect, test } from 'vitest'; import * as lib from './lib'; -const versions: Array = ['31.0.0', '31.1.0', '31.2.0', '31.3.0', '32.0.0', '32.2.0']; +const versions: Array = [ + '31.0.0', + '31.1.0', + '31.2.0', + '31.3.0', + '32.0.0', + '32.2.0', + '33.0.0', +]; test('module exports', () => { expect({ ...lib }).toEqual({ diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/README.md b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/README.md new file mode 100644 index 00000000..23132beb --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/README.md @@ -0,0 +1,27 @@ +# `transform-grid-api-methods-v33-0` + +> _Transform deprecated Grid API method invocations_ + +See the [`transform-grid-api-methods`](../../plugins/transform-grid-api-methods/) plugin for usage instructions. + +## Common tasks + +### Add a test case + +Create a new unit test scenario for this transform: + +``` +pnpm run task:create-test --type transform --target transform-grid-api-methods-v33-0 +``` + +### Add a new rule + +Replacement rules are specified in [`replacements.ts`](./replacements.ts) + +### Add to a codemod release + +Add this source code transformation to a codemod release: + +``` +pnpm run task:include-transform --transform transform-grid-api-methods-v33-0 +``` diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/input.js b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/input.js new file mode 100644 index 00000000..856138a2 --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/input.js @@ -0,0 +1,18 @@ +import { createGrid } from '@ag-grid-community/core'; + +const gridApi = createGrid(document.body, { + columnDefs: [], + rowData: [], +}); + +gridApi.selectAll(); +gridApi?.selectAll('api'); + +gridApi.deselectAll(); +gridApi.deselectAll('api'); + +gridApi.selectAllFiltered('api'); +gridApi?.deselectAllFiltered(); + +gridApi?.selectAllOnCurrentPage(); +gridApi.deselectAllOnCurrentPage('api'); diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/output.errors.cjs b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/output.errors.cjs new file mode 100644 index 00000000..670c6e44 --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/output.errors.cjs @@ -0,0 +1,2 @@ + +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/output.js b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/output.js new file mode 100644 index 00000000..e469c440 --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/output.js @@ -0,0 +1,18 @@ +import { createGrid } from '@ag-grid-community/core'; + +const gridApi = createGrid(document.body, { + columnDefs: [], + rowData: [], +}); + +gridApi.selectAll(); +gridApi?.selectAll("all", 'api'); + +gridApi.deselectAll(); +gridApi.deselectAll("all", 'api'); + +gridApi.selectAll("filtered", 'api'); +gridApi?.deselectAll("filtered"); + +gridApi?.selectAll("currentPage"); +gridApi.deselectAll("currentPage", 'api'); diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/output.warnings.cjs b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/output.warnings.cjs new file mode 100644 index 00000000..e0a30c5d --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/output.warnings.cjs @@ -0,0 +1 @@ +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/scenario.json b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/scenario.json new file mode 100644 index 00000000..346c1107 --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/__fixtures__/scenarios/js/scenario.json @@ -0,0 +1,8 @@ +{ + "scenario": { + "input": "input.js", + "output": "output.js", + "errors": "output.errors.cjs", + "warnings": "output.warnings.cjs" + } +} diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/index.ts b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/index.ts new file mode 100644 index 00000000..83a7c5c1 --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/index.ts @@ -0,0 +1 @@ +export { default } from './transform-grid-api-methods-v33-0'; diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/manifest.ts b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/manifest.ts new file mode 100644 index 00000000..19451da3 --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/manifest.ts @@ -0,0 +1,8 @@ +import { type TransformManifest } from '@ag-grid-devtools/types'; + +const manifest: TransformManifest = { + name: 'Transform Grid API methods v33.0', + description: 'Transform deprecated Grid API method invocations', +}; + +export default manifest; diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/replacements.ts b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/replacements.ts new file mode 100644 index 00000000..afd103a2 --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/replacements.ts @@ -0,0 +1,173 @@ +import { ast, matchNode, pattern as p, node as t, replace, template } from '@ag-grid-devtools/ast'; +import type { + GridApiDeprecation, + GridApiReplacement, +} from '../../plugins/transform-grid-api-methods'; + +export const replacements: Array = [ + // selectAll(source) + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode( + ({ api, source }) => ast.expression`${api}${apiOptionalChaining}.selectAll(${source})`, + { + api: p.expression(), + source: p.expression(), + }, + ), + template( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.selectAll(${t.stringLiteral('all')}, ${source})`, + ), + ), + ), + + // deselectAll(source) + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode( + ({ api, source }) => ast.expression`${api}${apiOptionalChaining}.deselectAll(${source})`, + { + api: p.expression(), + source: p.expression(), + }, + ), + template( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.deselectAll(${t.stringLiteral('all')}, ${source})`, + ), + ), + ), + + // selectAllFiltered(source) + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.selectAllFiltered(${source})`, + { + api: p.expression(), + source: p.expression(), + }, + ), + template( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.selectAll(${t.stringLiteral('filtered')}, ${source})`, + ), + ), + ), + + // selectAllFiltered() + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode(({ api }) => ast.expression`${api}${apiOptionalChaining}.selectAllFiltered()`, { + api: p.expression(), + }), + template( + ({ api }) => + ast.expression`${api}${apiOptionalChaining}.selectAll(${t.stringLiteral('filtered')})`, + ), + ), + ), + + // deselectAllFiltered() + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode(({ api }) => ast.expression`${api}${apiOptionalChaining}.deselectAllFiltered()`, { + api: p.expression(), + }), + template( + ({ api }) => + ast.expression`${api}${apiOptionalChaining}.deselectAll(${t.stringLiteral('filtered')})`, + ), + ), + ), + + // deselectAllFiltered(source) + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.deselectAllFiltered(${source})`, + { + api: p.expression(), + source: p.expression(), + }, + ), + template( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.deselectAll(${t.stringLiteral('filtered')}, ${source})`, + ), + ), + ), + + // selectAllOnCurrentPage(source) + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.selectAllOnCurrentPage(${source})`, + { + api: p.expression(), + source: p.expression(), + }, + ), + template( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.selectAll(${t.stringLiteral('currentPage')}, ${source})`, + ), + ), + ), + + // selectAllOnCurrentPage() + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode( + ({ api }) => ast.expression`${api}${apiOptionalChaining}.selectAllOnCurrentPage()`, + { + api: p.expression(), + }, + ), + template( + ({ api }) => + ast.expression`${api}${apiOptionalChaining}.selectAll(${t.stringLiteral('currentPage')})`, + ), + ), + ), + + // deselectAllOnCurrentPage(source) + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.deselectAllOnCurrentPage(${source})`, + { + api: p.expression(), + source: p.expression(), + }, + ), + template( + ({ api, source }) => + ast.expression`${api}${apiOptionalChaining}.deselectAll(${t.stringLiteral('currentPage')}, ${source})`, + ), + ), + ), + + // deselectAllOnCurrentPage() + ...['', '?', '!'].map((apiOptionalChaining) => + replace( + matchNode( + ({ api }) => ast.expression`${api}${apiOptionalChaining}.deselectAllOnCurrentPage()`, + { + api: p.expression(), + }, + ), + template( + ({ api }) => + ast.expression`${api}${apiOptionalChaining}.deselectAll(${t.stringLiteral('currentPage')})`, + ), + ), + ), +]; + +export const deprecations: Array = []; diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/transform-grid-api-methods-v33-0.test.ts b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/transform-grid-api-methods-v33-0.test.ts new file mode 100644 index 00000000..20847cfd --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/transform-grid-api-methods-v33-0.test.ts @@ -0,0 +1,16 @@ +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, onTestFinished, test } from 'vitest'; +import { loadTransformScenarios } from '../../test/runners/transform'; + +import transformGridApiMethodsV33_0 from './transform-grid-api-methods-v33-0'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +describe(transformGridApiMethodsV33_0, () => { + const scenariosPath = join(__dirname, './__fixtures__/scenarios'); + loadTransformScenarios(scenariosPath, { + transforms: [transformGridApiMethodsV33_0], + vitest: { describe, expect, test, onTestFinished }, + }); +}); diff --git a/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/transform-grid-api-methods-v33-0.ts b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/transform-grid-api-methods-v33-0.ts new file mode 100644 index 00000000..8f1cd55a --- /dev/null +++ b/packages/cli/src/codemods/transforms/transform-grid-api-methods-v33-0/transform-grid-api-methods-v33-0.ts @@ -0,0 +1,11 @@ +import { type AstCliContext, type AstTransform } from '@ag-grid-devtools/ast'; +import { transformGridApiMethods } from '../../plugins/transform-grid-api-methods'; +import { deprecations, replacements } from './replacements'; + +const plugin: AstTransform = transformGridApiMethods({ replacements, deprecations }); + +const transform: AstTransform = function transformGridApiMethodsV33_0(babel) { + return plugin(babel); +}; + +export default transform; diff --git a/packages/cli/src/codemods/versions/33.0.0/README.md b/packages/cli/src/codemods/versions/33.0.0/README.md new file mode 100644 index 00000000..e0a5de89 --- /dev/null +++ b/packages/cli/src/codemods/versions/33.0.0/README.md @@ -0,0 +1,35 @@ +# 33.0.0 + +Codemod for upgrading to [AG Grid v33.0.0](https://github.com/ag-grid/ag-grid/releases/tag/v33.0.0) + +## Usage + +``` +npx @ag-grid-devtools/cli migrate --to 33.0.0 +``` + +Source code transformations applied by this codemod are specified in [`transforms.ts`](./transforms.ts). + +## Common tasks + +### Add a transform + +Option 1: Create a new source code transformation to add to this codemod release version: + +``` +pnpm run task:create-transform --release 33.0.0 +``` + +Option 2: Add an existing source code transformation to this codemod release version: + +``` +pnpm run task:include-transform --version 33.0.0 +``` + +### Add a test case + +Create a new unit test scenario for this version: + +``` +pnpm run task:create-test --type version --target 33.0.0 +``` diff --git a/packages/cli/src/codemods/versions/33.0.0/__fixtures__/scenarios/.gitignore b/packages/cli/src/codemods/versions/33.0.0/__fixtures__/scenarios/.gitignore new file mode 100644 index 00000000..e69de29b diff --git a/packages/cli/src/codemods/versions/33.0.0/codemod.test.ts b/packages/cli/src/codemods/versions/33.0.0/codemod.test.ts new file mode 100644 index 00000000..35a622d1 --- /dev/null +++ b/packages/cli/src/codemods/versions/33.0.0/codemod.test.ts @@ -0,0 +1,16 @@ +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, onTestFinished, test } from 'vitest'; +import { loadCodemodExampleScenarios } from '../../test/runners/codemod'; + +import codemod from './codemod'; + +const __dirname = dirname(fileURLToPath(import.meta.url)); + +describe(codemod, () => { + const scenariosPath = join(__dirname, './__fixtures__/scenarios'); + loadCodemodExampleScenarios(scenariosPath, { + codemod, + vitest: { describe, expect, test, onTestFinished }, + }); +}); diff --git a/packages/cli/src/codemods/versions/33.0.0/codemod.ts b/packages/cli/src/codemods/versions/33.0.0/codemod.ts new file mode 100644 index 00000000..b4d32ed3 --- /dev/null +++ b/packages/cli/src/codemods/versions/33.0.0/codemod.ts @@ -0,0 +1,24 @@ +import { transformFileAst } from '@ag-grid-devtools/codemod-utils'; +import { + type Codemod, + type CodemodInput, + type CodemodOptions, + type CodemodResult, +} from '@ag-grid-devtools/types'; + +import transforms from './transforms'; + +const codemod: Codemod = function codemodV33_0_0( + file: CodemodInput, + options: CodemodOptions, +): CodemodResult { + const { path, source } = file; + const { fs, userConfig } = options; + return transformFileAst(source, transforms, { + filename: path, + fs, + userConfig, + }); +}; + +export default codemod; diff --git a/packages/cli/src/codemods/versions/33.0.0/manifest.ts b/packages/cli/src/codemods/versions/33.0.0/manifest.ts new file mode 100644 index 00000000..515046a2 --- /dev/null +++ b/packages/cli/src/codemods/versions/33.0.0/manifest.ts @@ -0,0 +1,13 @@ +import { type TransformManifest, type VersionManifest } from '@ag-grid-devtools/types'; + +import transformGridApiMethodsV33_0 from '../../transforms/transform-grid-api-methods-v33-0/manifest.ts'; + +const transforms: Array = [transformGridApiMethodsV33_0]; + +const manifest: VersionManifest = { + version: '33.0.0', + codemodPath: 'versions/33.0.0', + transforms, +}; + +export default manifest; diff --git a/packages/cli/src/codemods/versions/33.0.0/transforms.ts b/packages/cli/src/codemods/versions/33.0.0/transforms.ts new file mode 100644 index 00000000..7a4337c2 --- /dev/null +++ b/packages/cli/src/codemods/versions/33.0.0/transforms.ts @@ -0,0 +1,7 @@ +import { type AstCliContext, type AstTransform } from '@ag-grid-devtools/ast'; + +import transformGridApiMethodsV33_0 from '../../transforms/transform-grid-api-methods-v33-0'; + +const transforms: Array> = [transformGridApiMethodsV33_0]; + +export default transforms; diff --git a/packages/cli/src/codemods/versions/manifest.ts b/packages/cli/src/codemods/versions/manifest.ts index 88e7c83d..4065f321 100644 --- a/packages/cli/src/codemods/versions/manifest.ts +++ b/packages/cli/src/codemods/versions/manifest.ts @@ -12,6 +12,16 @@ import v32_0_0 from './32.0.0/manifest'; import v32_2_0 from './32.2.0/manifest'; -const versions: Array = [v31_0_0, v31_1_0, v31_2_0, v31_3_0, v32_0_0, v32_2_0]; +import v33_0_0 from './33.0.0/manifest'; + +const versions: Array = [ + v31_0_0, + v31_1_0, + v31_2_0, + v31_3_0, + v32_0_0, + v32_2_0, + v33_0_0, +]; export default versions;