Skip to content

Commit

Permalink
fix(Select, CustomSelect, NativeSelect): add codemods
Browse files Browse the repository at this point in the history
  • Loading branch information
EldarMuhamethanov committed Oct 30, 2024
1 parent 9cd1b75 commit e871f60
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/codemods/src/transforms/v7/common/warnSelectOnChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { API, Collection } from 'jscodeshift';
import { report } from '../../../report';

export const warnSelectOnChange = (api: API, source: Collection, componentName: string) => {
const j = api.jscodeshift;
source
.find(j.JSXOpeningElement, { name: { name: componentName } })
.find(j.JSXAttribute)
.filter((attr) => attr.node.name.name === 'onChange')
.forEach(() => {
report(
api,
`Manual changes required for ${componentName}'s "onChange" prop: need to change event argument to select value`,
);
});
};
2 changes: 2 additions & 0 deletions packages/codemods/src/transforms/v7/custom-select.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { API, FileInfo } from 'jscodeshift';
import { getImportInfo, removeProps } from '../../codemod-helpers';
import { JSCodeShiftOptions } from '../../types';
import { warnSelectOnChange } from './common/warnSelectOnChange';

export const parser = 'tsx';

Expand All @@ -19,6 +20,7 @@ export default function transformer(file: FileInfo, api: API, options: JSCodeShi
removeProps(j, api, source, localName, PROPS_TO_REMOVE, () => {
return `need to remove props ${PROPS_TO_REMOVE.join(', ')}`;
});
warnSelectOnChange(api, source, localName);

return source.toSource();
}
21 changes: 21 additions & 0 deletions packages/codemods/src/transforms/v7/native-select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { API, FileInfo } from 'jscodeshift';
import { getImportInfo } from '../../codemod-helpers';
import { JSCodeShiftOptions } from '../../types';
import { warnSelectOnChange } from './common/warnSelectOnChange';

export const parser = 'tsx';

export default function transformer(file: FileInfo, api: API, options: JSCodeShiftOptions) {
const { alias } = options;
const j = api.jscodeshift;
const source = j(file.source);
const { localName } = getImportInfo(j, file, 'NativeSelect', alias);

if (!localName) {
return source.toSource();
}

warnSelectOnChange(api, source, localName);

return source.toSource();
}
2 changes: 2 additions & 0 deletions packages/codemods/src/transforms/v7/select.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { API, FileInfo } from 'jscodeshift';
import { getImportInfo, removeProps } from '../../codemod-helpers';
import { JSCodeShiftOptions } from '../../types';
import { warnSelectOnChange } from './common/warnSelectOnChange';

export const parser = 'tsx';

Expand All @@ -19,6 +20,7 @@ export default function transformer(file: FileInfo, api: API, options: JSCodeShi
removeProps(j, api, source, localName, PROPS_TO_REMOVE, () => {
return `need to remove props ${PROPS_TO_REMOVE.join(', ')}`;
});
warnSelectOnChange(api, source, localName);

return source.toSource();
}

0 comments on commit e871f60

Please sign in to comment.