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-13294 - warn when bar/column sparkline migration can't be done #103

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions packages/ast/src/types/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ export interface FsContext extends TransformContext {
fs: FsUtils;
}

export interface LogOptions {
forceOutput?: boolean;
}

export interface AstCliContext extends FsContext {
warn(node: NodePath<AstNode> | null, message: string): void;
fail(node: NodePath<AstNode> | null, message: string): void;
warn(node: NodePath<AstNode> | null, message: string | Error, options?: LogOptions): void;
fail(node: NodePath<AstNode> | null, message: string | Error, options?: LogOptions): void;
}

export type AstTransformResult = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import j, { Collection } from 'jscodeshift';
import { AstCliContext, AstTransform, NodePath } from '@ag-grid-devtools/ast';
import { AstCliContext, AstTransform, AstTransformContext, NodePath } from '@ag-grid-devtools/ast';

export type JSCodeShiftTransformer = (root: Collection) => void | any;
export type JSCodeShiftTransformer = (
root: Collection,
state?: AstTransformContext<AstCliContext>,
) => void | any;

// Use https://astexplorer.net/ to iterate on your transformer
// Parser: Typescript
Expand All @@ -18,7 +21,7 @@ export const jsCodeShiftTransform = (
return (_babel) => ({
visitor: {
Program: {
exit(path: NodePath) {
exit(path: NodePath, state: AstTransformContext<AstCliContext>) {
const root: Collection<any> = j((path.hub as any).file.ast);
const getFirstNode = () => root.find(j.Program).get('body', 0).node;

Expand All @@ -28,7 +31,7 @@ export const jsCodeShiftTransform = (

// transform
for (const transform of transforms) {
transform(root);
transform(root, state);
}

// restore initial comment if any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-nocheck
import { testType } from './test-type';
const column = {
cellRendererParams: {
sparklineOptions: {
Expand Down Expand Up @@ -30,3 +31,33 @@ const shouldntChange2 = {
type: 'asdadasqda'
}
}
const typeVariable = 'column'
const shouldWarn1 = {
cellRendererParams: {
sparklineOptions: {
type: typeVariable
}
}
}
const type = 'column'
const shouldWarn2 = {
cellRendererParams: {
sparklineOptions: {
type
}
}
}
const shouldWarn3 = {
cellRendererParams: {
sparklineOptions: {
type: (() => 'column')()
}
}
}
const shouldWarn4 = {
cellRendererParams: {
sparklineOptions: {
type: testType
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-nocheck
import { testType } from './test-type';
const column = {
cellRendererParams: {
sparklineOptions: {
Expand Down Expand Up @@ -32,3 +33,33 @@ const shouldntChange2 = {
type: 'asdadasqda'
}
}
const typeVariable = 'column'
const shouldWarn1 = {
cellRendererParams: {
sparklineOptions: {
type: typeVariable
}
}
}
const type = 'column'
const shouldWarn2 = {
cellRendererParams: {
sparklineOptions: {
type
}
}
}
const shouldWarn3 = {
cellRendererParams: {
sparklineOptions: {
type: (() => 'column')()
}
}
}
const shouldWarn4 = {
cellRendererParams: {
sparklineOptions: {
type: testType
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = [
new SyntaxError(`The grid cellRendererParams sparklineOption "type" cannot be automatically migrated. Please refer to the migration guide for more details: https://ag-grid.com/javascript-data-grid/upgrading-to-ag-grid-33-0/`),
new SyntaxError(`The grid cellRendererParams sparklineOption "type" cannot be automatically migrated. Please refer to the migration guide for more details: https://ag-grid.com/javascript-data-grid/upgrading-to-ag-grid-33-0/`),
new SyntaxError(`The grid cellRendererParams sparklineOption "type" cannot be automatically migrated. Please refer to the migration guide for more details: https://ag-grid.com/javascript-data-grid/upgrading-to-ag-grid-33-0/`),
new SyntaxError(`The grid cellRendererParams sparklineOption "type" cannot be automatically migrated. Please refer to the migration guide for more details: https://ag-grid.com/javascript-data-grid/upgrading-to-ag-grid-33-0/`)
];
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"scenario": {
"input": "input.ts",
"output": "output.ts"
"output": "output.ts",
"warnings": "output.warnings.cjs"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const testType = 'column';
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ import j from 'jscodeshift';
import { JSCodeShiftTransformer } from '../../../plugins/jscodeshift';

// update bar/column types to bar with matching direction
export const columnToVerticalBarTransform: JSCodeShiftTransformer = (root) =>
export const columnToVerticalBarTransform: JSCodeShiftTransformer = (root, state) =>
root
.find(j.ObjectProperty, { key: { name: 'cellRendererParams' } })
.find(j.ObjectProperty, { key: { name: 'sparklineOptions' } })
.find(j.ObjectProperty, { key: { name: 'type' } })
.filter((path) => ['bar', 'column'].includes((path.value.value as any).value))
.forEach((path) => {
const existingType = (path.value.value as any).value;
const direction = existingType === 'column' ? 'vertical' : 'horizontal';
path.replace(j.objectProperty(j.identifier('type'), j.stringLiteral('bar')));
path.insertAfter(j.objectProperty(j.identifier('direction'), j.stringLiteral(direction)));
const typeValue = path.value.value;
const isStringLiteral = j.StringLiteral.check(typeValue);
if (isStringLiteral && ['bar', 'column'].includes(typeValue.value)) {
const existingType = typeValue.value;
const direction = existingType === 'column' ? 'vertical' : 'horizontal';
path.replace(j.objectProperty(j.identifier('type'), j.stringLiteral('bar')));
path.insertAfter(j.objectProperty(j.identifier('direction'), j.stringLiteral(direction)));
} else if (!isStringLiteral) {
state?.opts.warn(
null,
new SyntaxError(
`The grid cellRendererParams sparklineOption "type" cannot be automatically migrated. Please refer to the migration guide for more details: https://ag-grid.com/javascript-data-grid/upgrading-to-ag-grid-33-0/`,
),
{ forceOutput: true },
);
}
});
17 changes: 11 additions & 6 deletions packages/codemod-utils/src/transform/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ function transformJsFile<S>(
const transformContext: AstTransformContext<AstCliContext> = {
filename,
opts: {
warn(node: NodePath<AstNode> | null, message: string) {
warn(node: NodePath<AstNode> | null, message: string | Error, options) {
const error = createSourceCodeError(node, message);
uniqueErrors.set(error.message, { error, fatal: false });
const messageKey = `${error.message}${options?.forceOutput ? Math.random() : ''}`;
uniqueErrors.set(messageKey, { error, fatal: false });
},
fail(node: NodePath<AstNode> | null, message: string) {
fail(node: NodePath<AstNode> | null, message: string | Error, options) {
const error = createSourceCodeError(node, message);
uniqueErrors.set(error.message, { error, fatal: true });
const messageKey = `${error.message}${options?.forceOutput ? Math.random() : ''}`;
uniqueErrors.set(messageKey, { error, fatal: true });
},
fs,
userConfig,
Expand Down Expand Up @@ -99,6 +101,9 @@ function transformJsFile<S>(
};
}

function createSourceCodeError(node: NodePath<AstNode> | null, message: string): Error {
return node ? node.buildCodeFrameError(message) : new SyntaxError(message);
function createSourceCodeError(node: NodePath<AstNode> | null, message: string | Error): Error {
if (typeof message === 'string') {
return node ? node.buildCodeFrameError(message) : new SyntaxError(message);
}
return message;
}
Loading