- Notifications
You must be signed in to change notification settings - Fork 300
Pure component destructuring #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
keyz merged 13 commits into reactjs:master from andrewdushane:pure-component-destructuring Jul 10, 2017
Merged
Changes from all commits
Commits
Show all changes
13 commits Select commit Hold shift + click to select a range
5615873 Add description of `pure-component` transform
andrewdushane 4115b5b Basic destructuring works
andrewdushane 51fe8fb Remove duplicate variable declarations
andrewdushane 4408b4e Test various scenarios
andrewdushane 536845e Update readme with destructure option
andrewdushane 1bfa3de Undo accidental indentation change
andrewdushane 9e3e246 Made propNames be a set to make sure they are unique
a0c0489 Type Arrays to Array
jimmyhmiller c74bbae Rename option to `destructuring`
andrewdushane 50d3a56 Simplify build argument
andrewdushane 3ed45f9 Identify shadowing and do not destructure
andrewdushane 79cdcfe Retain type annotations
andrewdushane f9e7858 Change option name in readme
andrewdushane 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
There are no files selected for viewing
This file contains hidden or 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
51 changes: 51 additions & 0 deletions 51 transforms/__testfixtures__/pure-component-destructuring.input.js
This file contains hidden or 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,51 @@ | ||
| 'use strict'; | ||
| | ||
| var React = require('React'); | ||
| | ||
| const shadow = 'shadow'; | ||
| | ||
| function doSomething(props) { return props; } | ||
| | ||
| class ShouldDestsructure extends React.Component { | ||
| render() { | ||
| return <div className={this.props.foo} />; | ||
| } | ||
| } | ||
| | ||
| class ShouldDestructureAndRemoveDuplicateDeclaration extends React.Component { | ||
| render() { | ||
| const fizz = { buzz: 'buzz' }; | ||
| const bar = this.props.bar; | ||
| const baz = this.props.bizzaz; | ||
| const buzz = fizz.buzz; | ||
| return <div className={this.props.foo} bar={bar} baz={baz} buzz={buzz} />; | ||
| } | ||
| } | ||
| | ||
| class UsesThisDotProps extends React.Component { | ||
| render() { | ||
| doSomething(this.props); | ||
| return <div className={this.props.foo} />; | ||
| } | ||
| } | ||
| | ||
| class DestructuresThisDotProps extends React.Component { | ||
| // would be nice to destructure in this case | ||
| render() { | ||
| const { bar } = this.props; | ||
| return <div className={this.props.foo} bar={bar} />; | ||
| } | ||
| } | ||
| | ||
| class HasShadowProps extends React.Component { | ||
| render() { | ||
| return <div shadow={shadow} propsShadow={this.props.shadow} />; | ||
| } | ||
| } | ||
| | ||
| class PureWithTypes extends React.Component { | ||
| props: { foo: string }; | ||
| render() { | ||
| return <div className={this.props.foo} />; | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions 50 transforms/__testfixtures__/pure-component-destructuring.output.js
This file contains hidden or 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,50 @@ | ||
| 'use strict'; | ||
| | ||
| var React = require('React'); | ||
| | ||
| const shadow = 'shadow'; | ||
| | ||
| function doSomething(props) { return props; } | ||
| | ||
| function ShouldDestsructure( | ||
| { | ||
| foo, | ||
| }, | ||
| ) { | ||
| return <div className={foo} />; | ||
| } | ||
| | ||
| function ShouldDestructureAndRemoveDuplicateDeclaration( | ||
| { | ||
| bar, | ||
| bizzaz, | ||
| foo, | ||
| }, | ||
| ) { | ||
| const fizz = { buzz: 'buzz' }; | ||
| const baz = bizzaz; | ||
| const buzz = fizz.buzz; | ||
| return <div className={foo} bar={bar} baz={baz} buzz={buzz} />; | ||
| } | ||
| | ||
| function UsesThisDotProps(props) { | ||
| doSomething(props); | ||
| return <div className={props.foo} />; | ||
| } | ||
| | ||
| function DestructuresThisDotProps(props) { | ||
| const { bar } = props; | ||
| return <div className={props.foo} bar={bar} />; | ||
| } | ||
| | ||
| function HasShadowProps(props) { | ||
| return <div shadow={shadow} propsShadow={props.shadow} />; | ||
| } | ||
| | ||
| function PureWithTypes( | ||
| { | ||
| foo: string, | ||
| }, | ||
| ) { | ||
| return <div className={foo} />; | ||
| } |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this approach (adding names to the set while replacing) is efficient, it introduces some potential shadowing issues. For example:
Input:
Output:
I think a better way here is to scan the whole function twice (using
forEach), once for identifiers and once forpropproperty accesses; then we can compare the two sets to see if it's possible to apply destructuring here.