Skip to content
This repository has been archived by the owner on Sep 21, 2019. It is now read-only.

Commit

Permalink
remove prop types import
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentbel committed Jan 21, 2018
1 parent 394ae32 commit bf773c9
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { reactMovePropTypesToClassTransformFactoryFactory } from './transforms/r
import { collapseIntersectionInterfacesTransformFactoryFactory } from './transforms/collapse-intersection-interfaces-transform';
import { reactRemoveStaticPropTypesMemberTransformFactoryFactory } from './transforms/react-remove-static-prop-types-member-transform';
import { reactStatelessFunctionMakePropsTransformFactoryFactory } from './transforms/react-stateless-function-make-props-transform';
import { reactRemovePropTypesImportTransformFactoryFactory } from './transforms/react-remove-prop-types-import';

export {
reactMovePropTypesToClassTransformFactoryFactory,
Expand All @@ -15,6 +16,7 @@ export {
collapseIntersectionInterfacesTransformFactoryFactory,
reactRemovePropTypesAssignmentTransformFactoryFactory,
reactRemoveStaticPropTypesMemberTransformFactoryFactory,
reactRemovePropTypesImportTransformFactoryFactory,
compile,
};

Expand All @@ -25,6 +27,7 @@ export const allTransforms = [
collapseIntersectionInterfacesTransformFactoryFactory,
reactRemovePropTypesAssignmentTransformFactoryFactory,
reactRemoveStaticPropTypesMemberTransformFactoryFactory,
reactRemovePropTypesImportTransformFactoryFactory,
];

export type TransformFactoryFactory = (typeChecker: ts.TypeChecker) => ts.TransformerFactory<ts.SourceFile>;
Expand Down
74 changes: 74 additions & 0 deletions src/transforms/react-remove-prop-types-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as ts from 'typescript';
import * as _ from 'lodash';

import * as helpers from '../helpers';

export type Factory = ts.TransformerFactory<ts.SourceFile>;

/**
* Remove `import PropTypes from 'prop-types'` Or
* `import { PropTypes } from 'react'`
*
* @example
* Before:
* import PropTypes from 'prop-types'
* import React, { PropTypes } from 'rect'
*
* After
* import React from 'rect'
*/
export function reactRemovePropTypesImportTransformFactoryFactory(typeChecker: ts.TypeChecker): Factory {
return function reactRemovePropTypesImportTransformFactory(context: ts.TransformationContext) {
return function reactRemovePropTypesImportTransform(sourceFile: ts.SourceFile) {
return ts.updateSourceFileNode(
sourceFile,
sourceFile.statements
.filter(s => {
return !(
ts.isImportDeclaration(s) &&
ts.isStringLiteral(s.moduleSpecifier) &&
s.moduleSpecifier.text === 'prop-types'
);
})
.map(updateReactImportIfNeeded),
);
};
};
}

function updateReactImportIfNeeded(statement: ts.Statement) {
if (
!ts.isImportDeclaration(statement) ||
!ts.isStringLiteral(statement.moduleSpecifier) ||
statement.moduleSpecifier.text !== 'react' ||
!statement.importClause ||
!statement.importClause.namedBindings ||
!ts.isNamedImports(statement.importClause.namedBindings)
) {
return statement;
}

const namedBindings = statement.importClause.namedBindings;
const newNamedBindingElements = namedBindings.elements.filter(elm => elm.name.text !== 'PropTypes');

if (newNamedBindingElements.length === namedBindings.elements.length) {
// Means it has no 'PropTypes' named import
return statement;
}

const newImportClause = ts.updateImportClause(
statement.importClause,
statement.importClause.name,
newNamedBindingElements.length === 0
? undefined
: ts.updateNamedImports(namedBindings, newNamedBindingElements),
);

return ts.updateImportDeclaration(
statement,
statement.decorators,
statement.modifiers,
newImportClause,
statement.moduleSpecifier,
);
}
3 changes: 2 additions & 1 deletion test/end-to-end/basic/input.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import PropTypes from 'prop-types';
import * as React from 'react';

export default class MyComponent extends React.Component {
render() {
return <div />;
}
}
}
6 changes: 6 additions & 0 deletions test/react-remove-prop-types-import/from-prop-types/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import PropTypes from 'prop-types'
import React from 'react'

export const Hello = ({ message }) => {
return <div>hello {message}</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export const Hello = ({ message }) => {
return <div>hello {message}</div>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React, { PropTypes, Component } from 'react';

export class MyComponent extends Component {
render() {
return <div>hello</div>;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React, { Component } from 'react';

export class MyComponent extends Component {
render() {
return <div>hello</div>;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React, { PropTypes } from 'react'

export const Hello = ({ message }) => {
return <div>hello {message}</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

export const Hello = ({ message }) => {
return <div>hello {message}</div>;
};
2 changes: 2 additions & 0 deletions test/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
reactRemovePropTypesAssignmentTransformFactoryFactory,
reactRemoveStaticPropTypesMemberTransformFactoryFactory,
collapseIntersectionInterfacesTransformFactoryFactory,
reactRemovePropTypesImportTransformFactoryFactory,
allTransforms,
compile,
TransformFactoryFactory
Expand All @@ -28,6 +29,7 @@ const transformToFolderMap: [string, TransformFactoryFactory[]][] = [
['react-remove-prop-types-assignment-transform', [reactRemovePropTypesAssignmentTransformFactoryFactory]],
['collapse-intersection-interfaces-transform', [collapseIntersectionInterfacesTransformFactoryFactory]],
['react-move-prop-types-to-class-transform', [reactMovePropTypesToClassTransformFactoryFactory]],
['react-remove-prop-types-import', [reactRemovePropTypesImportTransformFactoryFactory]],
['end-to-end', allTransforms],
];

Expand Down

0 comments on commit bf773c9

Please sign in to comment.