From 1f512ee93e0ecb7831d38bf30efb540b119bb187 Mon Sep 17 00:00:00 2001 From: Steph Meslin-Weber Date: Fri, 15 Nov 2024 11:06:36 +0000 Subject: [PATCH] AG-13294 - add framework tests, make rule more specific to cellRendererParams --- .../input.component.html | 12 ++++ .../input.component.ts | 27 +++++++ .../output.component.html | 12 ++++ .../output.component.ts | 30 ++++++++ .../output.errors.cjs | 1 + .../output.warnings.cjs | 1 + .../sparkline-angular-external/scenario.json | 16 +++++ .../input.component.ts | 40 +++++++++++ .../output.component.ts | 43 ++++++++++++ .../output.errors.cjs | 1 + .../output.warnings.cjs | 1 + .../sparkline-angular-inline/scenario.json | 8 +++ .../frameworks/sparkline-jsx/input.jsx | 68 ++++++++++++++++++ .../sparkline-jsx/output.errors.cjs | 1 + .../frameworks/sparkline-jsx/output.jsx | 70 +++++++++++++++++++ .../sparkline-jsx/output.warnings.cjs | 1 + .../frameworks/sparkline-jsx/scenario.json | 8 +++ .../frameworks/sparkline-vue-sfc/input.vue | 34 +++++++++ .../sparkline-vue-sfc/output.errors.cjs | 1 + .../frameworks/sparkline-vue-sfc/output.vue | 37 ++++++++++ .../sparkline-vue-sfc/output.warnings.cjs | 1 + .../sparkline-vue-sfc/scenario.json | 8 +++ .../scenarios/sparkline-types/input.js | 17 +++-- .../scenarios/sparkline-types/output.js | 21 ++++-- .../migrate-sparklines-options/match-utils.ts | 11 ++- .../migrate-sparklines-options.ts | 63 +++++------------ .../transform-utils.ts | 24 +++++++ 27 files changed, 498 insertions(+), 59 deletions(-) create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/input.component.html create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/input.component.ts create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.component.html create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.component.ts create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.errors.cjs create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.warnings.cjs create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/scenario.json create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/input.component.ts create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.component.ts create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.errors.cjs create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.warnings.cjs create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/scenario.json create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/input.jsx create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.errors.cjs create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.jsx create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.warnings.cjs create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/scenario.json create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/input.vue create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.errors.cjs create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.vue create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.warnings.cjs create mode 100644 packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/scenario.json diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/input.component.html b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/input.component.html new file mode 100644 index 00000000..6fb22115 --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/input.component.html @@ -0,0 +1,12 @@ +
+ + +
diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/input.component.ts b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/input.component.ts new file mode 100644 index 00000000..aefe3ca8 --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/input.component.ts @@ -0,0 +1,27 @@ +// @ts-nocheck +import { ColDef, ColGroupDef, GridReadyEvent } from '@ag-grid-community/core'; +import { AgGridAngular } from '@ag-grid-community/angular'; +import { HttpClient } from '@angular/common/http'; +import { Component, ViewChild } from '@angular/core'; +import { IOlympicData } from './interfaces'; + +@Component({ + selector: 'my-app', + templateUrl: './component.html', +}) +export class AppComponent { + @ViewChild(AgGridAngular) private grid!: AgGridAngular; + public columnDefs: (ColDef | ColGroupDef)[] = [{field: 'test', cellRenderer: 'agSparklineRenderer', cellRendererParams: {sparklineOptions: {type: 'column'}}}]; + public rowData!: IOlympicData[]; + + constructor(private http: HttpClient) { + } + + onGridReady(params: GridReadyEvent) { + this.http + .get('https://www.ag-grid.com/example-assets/olympic-winners.json') + .subscribe((data) => { + this.rowData = data; + }); + } +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.component.html b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.component.html new file mode 100644 index 00000000..6fb22115 --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.component.html @@ -0,0 +1,12 @@ +
+ + +
diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.component.ts b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.component.ts new file mode 100644 index 00000000..46b355aa --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.component.ts @@ -0,0 +1,30 @@ +// @ts-nocheck +import { ColDef, ColGroupDef, GridReadyEvent } from '@ag-grid-community/core'; +import { AgGridAngular } from '@ag-grid-community/angular'; +import { HttpClient } from '@angular/common/http'; +import { Component, ViewChild } from '@angular/core'; +import { IOlympicData } from './interfaces'; + +@Component({ + selector: 'my-app', + templateUrl: './component.html', +}) +export class AppComponent { + @ViewChild(AgGridAngular) private grid!: AgGridAngular; + public columnDefs: (ColDef | ColGroupDef)[] = [{field: 'test', cellRenderer: 'agSparklineRenderer', cellRendererParams: {sparklineOptions: { + type: "bar", + direction: "vertical" + }}}]; + public rowData!: IOlympicData[]; + + constructor(private http: HttpClient) { + } + + onGridReady(params: GridReadyEvent) { + this.http + .get('https://www.ag-grid.com/example-assets/olympic-winners.json') + .subscribe((data) => { + this.rowData = data; + }); + } +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.errors.cjs b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.errors.cjs new file mode 100644 index 00000000..e0a30c5d --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.errors.cjs @@ -0,0 +1 @@ +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.warnings.cjs b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.warnings.cjs new file mode 100644 index 00000000..e0a30c5d --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/output.warnings.cjs @@ -0,0 +1 @@ +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/scenario.json b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/scenario.json new file mode 100644 index 00000000..a9ed2899 --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-external/scenario.json @@ -0,0 +1,16 @@ +{ + "scenario": { + "input": "input.component.ts", + "output": "output.component.ts", + "errors": "output.errors.cjs", + "warnings": "output.warnings.cjs" + }, + "files": { + "input": { + "component.html": "input.component.html" + }, + "expected": { + "component.html": "output.component.html" + } + } +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/input.component.ts b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/input.component.ts new file mode 100644 index 00000000..1be391b7 --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/input.component.ts @@ -0,0 +1,40 @@ +// @ts-nocheck +import { AdvancedFilterModel, ColDef, ColGroupDef, GridReadyEvent } from '@ag-grid-community/core'; +import { AgGridAngular } from '@ag-grid-community/angular'; +import { HttpClient } from '@angular/common/http'; +import { Component, ViewChild } from '@angular/core'; +import { IOlympicData } from './interfaces'; + +@Component({ + selector: 'my-app', + template: `
+ + +
`, +}) +export class AppComponent { + @ViewChild(AgGridAngular) private grid!: AgGridAngular; + public columnDefs: (ColDef | ColGroupDef)[] = [{ + field: 'test', cellRenderer: 'agSparklineRenderer', cellRendererParams: {sparklineOptions: {type: 'column'}} + }]; + public rowData!: IOlympicData[]; + + constructor(private http: HttpClient) { + } + + onGridReady(params: GridReadyEvent) { + this.http + .get('https://www.ag-grid.com/example-assets/olympic-winners.json') + .subscribe((data) => { + this.rowData = data; + }); + } +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.component.ts b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.component.ts new file mode 100644 index 00000000..b55ddd70 --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.component.ts @@ -0,0 +1,43 @@ +// @ts-nocheck +import { AdvancedFilterModel, ColDef, ColGroupDef, GridReadyEvent } from '@ag-grid-community/core'; +import { AgGridAngular } from '@ag-grid-community/angular'; +import { HttpClient } from '@angular/common/http'; +import { Component, ViewChild } from '@angular/core'; +import { IOlympicData } from './interfaces'; + +@Component({ + selector: 'my-app', + template: `
+ + +
`, +}) +export class AppComponent { + @ViewChild(AgGridAngular) private grid!: AgGridAngular; + public columnDefs: (ColDef | ColGroupDef)[] = [{ + field: 'test', cellRenderer: 'agSparklineRenderer', cellRendererParams: {sparklineOptions: { + type: "bar", + direction: "vertical" + }} + }]; + public rowData!: IOlympicData[]; + + constructor(private http: HttpClient) { + } + + onGridReady(params: GridReadyEvent) { + this.http + .get('https://www.ag-grid.com/example-assets/olympic-winners.json') + .subscribe((data) => { + this.rowData = data; + }); + } +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.errors.cjs b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.errors.cjs new file mode 100644 index 00000000..e0a30c5d --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.errors.cjs @@ -0,0 +1 @@ +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.warnings.cjs b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.warnings.cjs new file mode 100644 index 00000000..e0a30c5d --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/output.warnings.cjs @@ -0,0 +1 @@ +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/scenario.json b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/scenario.json new file mode 100644 index 00000000..c31eff9c --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-angular-inline/scenario.json @@ -0,0 +1,8 @@ +{ + "scenario": { + "input": "input.component.ts", + "output": "output.component.ts", + "errors": "output.errors.cjs", + "warnings": "output.warnings.cjs" + } +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/input.jsx b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/input.jsx new file mode 100644 index 00000000..bf463ccb --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/input.jsx @@ -0,0 +1,68 @@ +import { AgGridReact } from '@ag-grid-community/react'; + +function MyComponent(props) { + + const shouldntChange0 = { + sparklineOptions: { + type: 'column' + } + } + + const anotherColumn = { + field: 'test', + cellRenderer: 'agSparklineCellRenderer', + cellRendererParams: { + sparklineOptions: { + type: 'column' + } + } + } + + return ( +
+ +
+ ); +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.errors.cjs b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.errors.cjs new file mode 100644 index 00000000..e0a30c5d --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.errors.cjs @@ -0,0 +1 @@ +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.jsx b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.jsx new file mode 100644 index 00000000..40e4fe0f --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.jsx @@ -0,0 +1,70 @@ +import { AgGridReact } from '@ag-grid-community/react'; + +function MyComponent(props) { + + const shouldntChange0 = { + sparklineOptions: { + type: 'column' + } + } + + const anotherColumn = { + field: 'test', + cellRenderer: 'agSparklineCellRenderer', + cellRendererParams: { + sparklineOptions: { + type: "bar", + direction: "vertical" + } + } + } + + return ( + (
+ +
) + ); +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.warnings.cjs b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.warnings.cjs new file mode 100644 index 00000000..e0a30c5d --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/output.warnings.cjs @@ -0,0 +1 @@ +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/scenario.json b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/scenario.json new file mode 100644 index 00000000..28038495 --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-jsx/scenario.json @@ -0,0 +1,8 @@ +{ + "scenario": { + "input": "input.jsx", + "output": "output.jsx", + "errors": "output.errors.cjs", + "warnings": "output.warnings.cjs" + } +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/input.vue b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/input.vue new file mode 100644 index 00000000..1875acb9 --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/input.vue @@ -0,0 +1,34 @@ + + + diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.errors.cjs b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.errors.cjs new file mode 100644 index 00000000..e0a30c5d --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.errors.cjs @@ -0,0 +1 @@ +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.vue b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.vue new file mode 100644 index 00000000..669eef5b --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.vue @@ -0,0 +1,37 @@ + + + diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.warnings.cjs b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.warnings.cjs new file mode 100644 index 00000000..e0a30c5d --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/output.warnings.cjs @@ -0,0 +1 @@ +module.exports = []; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/scenario.json b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/scenario.json new file mode 100644 index 00000000..c54482b4 --- /dev/null +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/frameworks/sparkline-vue-sfc/scenario.json @@ -0,0 +1,8 @@ +{ + "scenario": { + "input": "input.vue", + "output": "output.vue", + "errors": "output.errors.cjs", + "warnings": "output.warnings.cjs" + } +} diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/sparkline-types/input.js b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/sparkline-types/input.js index c5cca975..a4fb525d 100644 --- a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/sparkline-types/input.js +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/sparkline-types/input.js @@ -1,13 +1,15 @@ const line = { - sparklineOptions: { - type: 'line' + cellRendererParams: { + sparklineOptions: { + type: 'line' + } } } const area = { - sparklineOptions: { + cellRendererParams: {sparklineOptions: { type: 'area' - } + }} } const bar = { @@ -17,6 +19,13 @@ const bar = { } const column = { + cellRendererParams: { + sparklineOptions: { + type: 'column' + } + } +} +const shouldntChange0 = { sparklineOptions: { type: 'column' } diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/sparkline-types/output.js b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/sparkline-types/output.js index 837e245e..b1b2f622 100644 --- a/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/sparkline-types/output.js +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/__fixtures__/scenarios/sparkline-types/output.js @@ -1,13 +1,15 @@ const line = { - sparklineOptions: { - type: 'line' + cellRendererParams: { + sparklineOptions: { + type: 'line' + } } } const area = { - sparklineOptions: { + cellRendererParams: {sparklineOptions: { type: 'area' - } + }} } const bar = { @@ -17,9 +19,16 @@ const bar = { } const column = { + cellRendererParams: { + sparklineOptions: { + type: "bar", + direction: "vertical" + } + } +} +const shouldntChange0 = { sparklineOptions: { - type: "bar", - direction: "vertical" + type: 'column' } } diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/match-utils.ts b/packages/cli/src/codemods/transforms/migrate-sparklines-options/match-utils.ts index fcfb2d5e..1a586590 100644 --- a/packages/cli/src/codemods/transforms/migrate-sparklines-options/match-utils.ts +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/match-utils.ts @@ -4,7 +4,10 @@ import * as t from '@babel/types'; export type OrderedRecord = Record; export type ComplexTransform = { - matchOn: OrderedRecord; + matchOn: OrderedRecord< + string, + (SegmentMatchFunction | SegmentMatchFunction[])[] + >; transformer: (matches: OrderedRecord) => void; }; @@ -170,6 +173,10 @@ export function objectExpression(params: ObjectPropertyParams) { }); } +export function object({ name }: { name: string }) { + return [objectProperty({ name }), objectExpression({ name })]; +} + export function objectProperty(params: ObjectExpressionParams) { return tag('ObjectProperty', function (path: TypedNodePath): SegmentMatchResult { const matchPath = path.isObjectProperty() ? path : undefined; @@ -236,7 +243,7 @@ const allOrNothing = (results: any[], conditions: any[]) => results.length === conditions.length ? results.reverse() : undefined; export function match(path: NodePath | null | undefined, segments: SegmentMatchFunction[]) { - const conditions = [...segments].reverse(); + const conditions = segments.flat().reverse(); const stack = []; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/migrate-sparklines-options.ts b/packages/cli/src/codemods/transforms/migrate-sparklines-options/migrate-sparklines-options.ts index a2487d85..1a2f130a 100644 --- a/packages/cli/src/codemods/transforms/migrate-sparklines-options/migrate-sparklines-options.ts +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/migrate-sparklines-options.ts @@ -1,73 +1,42 @@ -import { NodePath, type AstCliContext, type AstTransform } from '@ag-grid-devtools/ast'; +import { type AstCliContext, type AstTransform } from '@ag-grid-devtools/ast'; import * as m from './match-utils'; import * as t from '@babel/types'; import * as v from './visitor-utils'; -import { removeImports, createImport } from './transform-utils'; +import { mergeImports, mergeTypecasts } from './transform-utils'; export const c2bTransform: m.ComplexTransform = { matchOn: { columnSparkline: [ - m.objectExpression({ name: 'sparklineOptions' }), + m.object({ name: 'cellRendererParams' }), + m.object({ name: 'sparklineOptions' }), m.objectProperty({ name: 'type', value: 'column' }), ], }, transformer: (matches: Record) => { const { columnSparkline } = matches; - const property = columnSparkline[1]!.path; + const property = columnSparkline[4]!.path; property.replaceWith(t.objectProperty(t.identifier('type'), t.stringLiteral('bar'))); property.insertAfter(t.objectProperty(t.identifier('direction'), t.stringLiteral('vertical'))); }, }; -export const importTransformer: m.ComplexTransform = { - matchOn: { - importDeclaration: [ - m.importDeclaration({ - some: [ - 'AreaSparklineOptions', - 'BarSparklineOptions', - 'ColumnSparklineOptions', - 'LineSparklineOptions', - ], - }), - ], - }, - transformer: (matches: Record) => { - const { importDeclaration } = matches; - const importParams = (importDeclaration[0] as m.ImportDeclarationMatchResult)!; - const path = importParams.path as NodePath; +const transform: AstTransform = function migrateSparklinesOptions(_babel) { + const oldOptionNames = [ + 'AreaSparklineOptions', + 'BarSparklineOptions', + 'ColumnSparklineOptions', + 'LineSparklineOptions', + ]; - removeImports(path, importParams.some!); - path.insertAfter(createImport(['AgSparklineOptions'], 'ag-charts-types')); - }, -}; + const newOptionName = 'AgSparklineOptions'; + const newPackage = 'ag-charts-types'; -export const typecastTransformer: m.ComplexTransform = { - matchOn: { - sparklineTypecast: [ - m.typeReference({ - names: [ - 'AreaSparklineOptions', - 'BarSparklineOptions', - 'ColumnSparklineOptions', - 'LineSparklineOptions', - ], - }), - ], - }, - transformer: (matches: Record) => { - const sparklineTypecastPath = matches.sparklineTypecast[0]!.path; - sparklineTypecastPath.replaceWith(t.tsTypeReference(t.identifier('AgSparklineOptions'))); - }, -}; - -const transform: AstTransform = function migrateSparklinesOptions(_babel) { return { visitor: v.combineVisitors( v.createComplexVisitor(c2bTransform), - v.createComplexVisitor(importTransformer), - v.createComplexVisitor(typecastTransformer), + v.createComplexVisitor(mergeImports(oldOptionNames, newOptionName, newPackage)), + v.createComplexVisitor(mergeTypecasts(oldOptionNames, newOptionName)), ), }; }; diff --git a/packages/cli/src/codemods/transforms/migrate-sparklines-options/transform-utils.ts b/packages/cli/src/codemods/transforms/migrate-sparklines-options/transform-utils.ts index 053fa1f7..cf920aa6 100644 --- a/packages/cli/src/codemods/transforms/migrate-sparklines-options/transform-utils.ts +++ b/packages/cli/src/codemods/transforms/migrate-sparklines-options/transform-utils.ts @@ -1,5 +1,6 @@ import { NodePath } from '@ag-grid-devtools/ast'; import * as t from '@babel/types'; +import * as m from './match-utils'; export const removeImports = (path: NodePath, importsToRemove: string[]) => { const specifierPaths = path.get('specifiers') as NodePath[]; @@ -13,3 +14,26 @@ export const createImport = (importNames: string[], source: string) => { t.importSpecifier(t.identifier(importName), t.identifier(importName)); return t.importDeclaration(importNames.map(newImportMapper), t.stringLiteral(source)); }; + +export const mergeImports = (from: string[], to: string, source: string): m.ComplexTransform => ({ + matchOn: { + importDeclaration: [m.importDeclaration({ some: from })], + }, + transformer: (matches: Record) => { + const importParams = (matches.importDeclaration[0] as m.ImportDeclarationMatchResult)!; + const path = importParams.path as NodePath; + + removeImports(path, importParams.some!); + path.insertAfter(createImport([to], source)); + }, +}); + +export const mergeTypecasts = (from: string[], to: string): m.ComplexTransform => ({ + matchOn: { + typecast: [m.typeReference({ names: from })], + }, + transformer: (matches: Record) => { + const typecastPath = matches.typecast[0]!.path; + typecastPath.replaceWith(t.tsTypeReference(t.identifier(to))); + }, +});