Skip to content

Commit

Permalink
Ensure we don't remove types from 'prop-types' import
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 15, 2024
1 parent 29ff53e commit 40dd307
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion transforms/__tests__/deprecated-prop-types-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe("transform deprecated-prop-types-types", () => {
declare const weakValidationMap: WeakValidationMap<{}>;
`),
).toMatchInlineSnapshot(`
"import { checkPropTypes } from 'prop-types';
"import { checkPropTypes, Requireable, Validator, ValidationMap, WeakValidationMap } from 'prop-types';
import { type ReactNode } from 'react';
declare const requireable: Requireable<ReactNode>;
Expand Down
4 changes: 2 additions & 2 deletions transforms/deprecated-prop-types-types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const parseSync = require("./utils/parseSync");
const { replaceType } = require("./utils/replaceType");
const { replaceReactType } = require("./utils/replaceType");

/**
* @type {import('jscodeshift').Transform}
Expand Down Expand Up @@ -69,7 +69,7 @@ const deprecatedPropTypesTypes = (file, api) => {
"ValidationMap",
"WeakValidationMap",
]) {
const changedType = replaceType(
const changedType = replaceReactType(
j,
ast,
movedType,
Expand Down
4 changes: 2 additions & 2 deletions transforms/deprecated-react-child.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const parseSync = require("./utils/parseSync");
const { replaceType } = require("./utils/replaceType");
const { replaceReactType } = require("./utils/replaceType");

/**
* @type {import('jscodeshift').Transform}
Expand All @@ -8,7 +8,7 @@ const deprecatedReactChildTransform = (file, api) => {
const j = api.jscodeshift;
const ast = parseSync(file);

const hasChanges = replaceType(
const hasChanges = replaceReactType(
j,
ast,
"ReactChild",
Expand Down
4 changes: 2 additions & 2 deletions transforms/deprecated-react-fragment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const parseSync = require("./utils/parseSync");
const { replaceType } = require("./utils/replaceType");
const { replaceReactType } = require("./utils/replaceType");

/**
* @type {import('jscodeshift').Transform}
Expand All @@ -8,7 +8,7 @@ const deprecatedReactFragmentTransform = (file, api) => {
const j = api.jscodeshift;
const ast = parseSync(file);

const hasChanges = replaceType(
const hasChanges = replaceReactType(
j,
ast,
"ReactFragment",
Expand Down
4 changes: 2 additions & 2 deletions transforms/deprecated-react-node-array.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const parseSync = require("./utils/parseSync");
const { replaceType } = require("./utils/replaceType");
const { replaceReactType } = require("./utils/replaceType");

/**
* @type {import('jscodeshift').Transform}
Expand All @@ -8,7 +8,7 @@ const deprecatedReactNodeArrayTransform = (file, api) => {
const j = api.jscodeshift;
const ast = parseSync(file);

const hasChanges = replaceType(
const hasChanges = replaceReactType(
j,
ast,
"ReactNodeArray",
Expand Down
4 changes: 2 additions & 2 deletions transforms/deprecated-react-text.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const parseSync = require("./utils/parseSync");
const { replaceType } = require("./utils/replaceType");
const { replaceReactType } = require("./utils/replaceType");

/**
* @type {import('jscodeshift').Transform}
Expand All @@ -8,7 +8,7 @@ const deprecatedReactTextTransform = (file, api) => {
const j = api.jscodeshift;
const ast = parseSync(file);

const hasChanges = replaceType(
const hasChanges = replaceReactType(
j,
ast,
"ReactText",
Expand Down
28 changes: 16 additions & 12 deletions transforms/utils/replaceType.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { findTSTypeReferenceCollections } = require("./jscodeshift-bugfixes");
* @returns {boolean}
*/
function renameType(j, ast, sourceIdentifier, targetIdentifier) {
return replaceType(
return replaceReactType(
j,
ast,
sourceIdentifier,
Expand Down Expand Up @@ -47,7 +47,7 @@ function renameType(j, ast, sourceIdentifier, targetIdentifier) {
* @param {(sourcePath: import("jscodeshift").TSTypeReference) => import("jscodeshift").TSTypeReference | import("jscodeshift").TSUnionType} buildTargetTypeReference
* @param {string | null} addedType - `null` if no type was added
*/
function replaceType(
function replaceReactType(
j,
ast,
sourceIdentifier,
Expand All @@ -66,15 +66,19 @@ function replaceType(
(local == null || local.name === addedType)
);
});
const sourceIdentifierImports = ast.find(j.ImportSpecifier, (node) => {
const { imported, local } = node;
return (
imported.type === "Identifier" &&
imported.name === sourceIdentifier &&
// We don't support renames generally, so we don't handle them here
(local == null || local.name === sourceIdentifier)
);
});
const sourceIdentifierImports = ast
.find(j.ImportDeclaration, (declaration) => {
return declaration.source.value === "react";
})
.find(j.ImportSpecifier, (node) => {
const { imported, local } = node;
return (
imported.type === "Identifier" &&
imported.name === sourceIdentifier &&
// We don't support renames generally, so we don't handle them here
(local == null || local.name === sourceIdentifier)
);
});
if (sourceIdentifierImports.length > 0) {
hasChanges = true;

Expand Down Expand Up @@ -120,4 +124,4 @@ function replaceType(
return hasChanges;
}

module.exports = { replaceType, renameType };
module.exports = { replaceReactType, renameType };

0 comments on commit 40dd307

Please sign in to comment.