Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AG-13027 Add v33 api codemods #92

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/cli/src/codemods/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { expect, test } from 'vitest';

import * as lib from './lib';

const versions: Array<string> = ['31.0.0', '31.1.0', '31.2.0', '31.3.0', '32.0.0', '32.2.0'];
const versions: Array<string> = [
'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({
Expand Down
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

module.exports = [];
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scenario": {
"input": "input.js",
"output": "output.js",
"errors": "output.errors.cjs",
"warnings": "output.warnings.cjs"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './transform-grid-api-methods-v33-0';
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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<GridApiReplacement> = [
// 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<GridApiDeprecation> = [];
Original file line number Diff line number Diff line change
@@ -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 },
});
});
Original file line number Diff line number Diff line change
@@ -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<AstCliContext> = transformGridApiMethods({ replacements, deprecations });

const transform: AstTransform<AstCliContext> = function transformGridApiMethodsV33_0(babel) {
return plugin(babel);
};

export default transform;
35 changes: 35 additions & 0 deletions packages/cli/src/codemods/versions/33.0.0/README.md
Original file line number Diff line number Diff line change
@@ -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
```
16 changes: 16 additions & 0 deletions packages/cli/src/codemods/versions/33.0.0/codemod.test.ts
Original file line number Diff line number Diff line change
@@ -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 },
});
});
24 changes: 24 additions & 0 deletions packages/cli/src/codemods/versions/33.0.0/codemod.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading