Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion transforms/React-PropTypes-to-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ module.exports = function(file, api, options) {
return target;
}

function hasPropTypesImport(j, root) {
return root
.find(j.ImportDeclaration, {
source: {value: 'prop-types'}
})
.length > 0;
}

function hasPropTypesRequire(j, root) {
return root.find(j.CallExpression, {
callee: {name: 'require'},
arguments: {0: {value: 'prop-types'}}
}).length > 0;
}

// React.PropTypes
const isReactPropTypes = path => (
path.node.name === 'PropTypes' &&
Expand All @@ -68,7 +83,9 @@ module.exports = function(file, api, options) {
// Program uses ES import syntax
function useImportSyntax(j, root) {
return root
.find(j.ImportDeclaration)
.find(j.ImportDeclaration, {
importKind: 'value'
})
.length > 0;
}

Expand All @@ -82,6 +99,12 @@ module.exports = function(file, api, options) {
// If any PropTypes references exist, add a 'prop-types' import (or require)
function addPropTypesImport(j, root) {
if (useImportSyntax(j, root)) {
// Handle cases where 'prop-types' already exists;
// eg the file has already been codemodded but more React.PropTypes were added.
if (hasPropTypesImport(j, root)) {
return;
}

const path = findImportAfterPropTypes(j, root);
if (path) {
const importStatement = j.importDeclaration(
Expand All @@ -93,6 +116,12 @@ module.exports = function(file, api, options) {
}
}

// Handle cases where 'prop-types' already exists;
// eg the file has already been codemodded but more React.PropTypes were added.
if (hasPropTypesRequire(j, root)) {
return;
}

const path = findRequireAfterPropTypes(j, root);
if (path) {
const requireStatement = useVar(j, root)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import PropTypes from 'prop-types';
import React from 'react';

const object = PropTypes.object;
const string = React.PropTypes.string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import PropTypes from 'prop-types';
import React from 'react';

const object = PropTypes.object;
const string = PropTypes.string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const PropTypes = require('prop-types');
const React = require('React');

const object = PropTypes.object;
const string = React.PropTypes.string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const PropTypes = require('prop-types');
const React = require('React');

const object = PropTypes.object;
const string = PropTypes.string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const React = require('React');

const {PropTypes} = React;

import type {SomeType} from 'SomeModule';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const PropTypes = require('prop-types');
const React = require('React');

import type {SomeType} from 'SomeModule';
3 changes: 3 additions & 0 deletions transforms/__tests__/React-PropTypes-to-prop-types-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
'use strict';

const tests = [
'already-migrated-import',
'already-migrated-require',
'assigned-from-react-var',
'assigned-to-var-with-different-name',
'default-and-named-import',
'default-import',
'destructured-proptypes-import',
'import-alias',
'import-flow-type-with-require',
'mixed-import-and-require',
'mixed-import-and-require-2',
'named-parameters',
Expand Down