This repository has been archived by the owner on Sep 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
394ae32
commit bf773c9
Showing
10 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
test/react-remove-prop-types-import/from-prop-types/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
5 changes: 5 additions & 0 deletions
5
test/react-remove-prop-types-import/from-prop-types/output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
}; |
7 changes: 7 additions & 0 deletions
7
test/react-remove-prop-types-import/from-react-multi-named-import/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/react-remove-prop-types-import/from-react-multi-named-import/output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
test/react-remove-prop-types-import/from-react-simple/input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
5 changes: 5 additions & 0 deletions
5
test/react-remove-prop-types-import/from-react-simple/output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters