- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1
 
Pattern Matching #22
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
          
     Open
      
      
            rattrayalex
  wants to merge
  7
  commits into
  master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
match
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Pattern Matching #22
Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      64bd12b
              
                Pattern Matching WIP
              
              
                rattrayalex a4f700c
              
                wip
              
              
                rattrayalex 4c3d2df
              
                wip more tests
              
              
                rattrayalex 5395193
              
                Replace PlaceholderExpression with it
              
              
                rattrayalex 8cebb95
              
                Disallow object and array literals, cleanup hasProps
              
              
                rattrayalex 350454b
              
                Prevent sibling 'it'
              
              
                rattrayalex 1e720c1
              
                Allow sibling matches to use it
              
              
                rattrayalex 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
    
  
  
    
              
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| match x: | ||
| | []: | ||
| "empty" | ||
| | [ a, b ]: | ||
| a - b | ||
| | [ a, b = 2 ]: | ||
| a + b - 2 | ||
| | [ a, ...b ]: | ||
| b.concat(a) | ||
| | [ | ||
| [ | ||
| b | ||
| d = 'e' | ||
| ] | ||
| [ g, , h ] | ||
| ...j | ||
| ]: | ||
| [b, d, g, ...j].join('') | 
  
    
      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,92 @@ | ||
| assert.equal( | ||
| "empty", | ||
| match []: | ||
| | []: | ||
| "empty" | ||
| ) | ||
| assert.equal( | ||
| "empty", | ||
| match []: | ||
| | []: | ||
| "empty" | ||
| ) | ||
| assert.equal( | ||
| undefined, | ||
| match []: | ||
| | [a]: | ||
| a + 1 | ||
| ) | ||
| assert.equal( | ||
| undefined, | ||
| match [1]: | ||
| | [a, b]: | ||
| a + b | ||
| ) | ||
| assert.equal( | ||
| 5, | ||
| match [1]: | ||
| | [a, b = 4]: | ||
| a + b | ||
| ) | ||
| assert.equal( | ||
| 3, | ||
| match [1, 2]: | ||
| | [a, b]: | ||
| a + b | ||
| ) | ||
| assert.equal( | ||
| 4, | ||
| match [1, 2, 3]: | ||
| | [a,, b]: | ||
| a + b | ||
| ) | ||
| assert.equal( | ||
| undefined, | ||
| match [1, 2]: | ||
| | [a,, b]: | ||
| a + b | ||
| ) | ||
| assert.deepEqual( | ||
| [2, 3, 1], | ||
| match [1, 2, 3]: | ||
| | [a, ...b]: | ||
| b.concat(a) | ||
| ) | ||
| assert.deepEqual( | ||
| [1, 4], | ||
| match [4]: | ||
| | [a, b = 1, ...c]: | ||
| c.concat([b, a]) | ||
| ) | ||
| assert.deepEqual( | ||
| [6, 7, 5, 4], | ||
| match [4, 5, 6, 7]: | ||
| | [a, b = 1, ...c]: | ||
| c.concat([b, a]) | ||
| ) | ||
| assert.deepEqual( | ||
| [1, 2, 4, 6, 7, 8], | ||
| match [[1], [4, 5, 6], 7, 8]: | ||
| | [ | ||
| [ | ||
| b | ||
| d = 2 | ||
| ] | ||
| [ g, , h ] | ||
| ...j | ||
| ]: | ||
| [b, d, g, h, ...j] | ||
| ) | ||
| assert.deepEqual( | ||
| undefined, | ||
| match [[1], [4, 5], 7, 8]: | ||
| | [ | ||
| [ | ||
| b | ||
| d = 2 | ||
| ] | ||
| [ g, , h ] | ||
| ...j | ||
| ]: | ||
| [b, d, g, h, ...j] | ||
| ) | 
  
    
      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,27 @@ | ||
| function _hasLength(arr, minLength, maxLength) { | ||
| minLength = minLength || 0; | ||
| maxLength = maxLength != null ? maxLength : Number.MAX_SAFE_INTEGER; | ||
| return arr != null && typeof arr !== "function" && arr.length === arr.length | 0 && arr.length >= minLength && arr.length <= maxLength; | ||
| } | ||
| 
     | 
||
| if (_hasLength(x, 0, 0)) { | ||
| const [] = x; | ||
| 
     | 
||
| "empty"; | ||
| } else if (_hasLength(x, 2, 2)) { | ||
| const [a, b] = x; | ||
| 
     | 
||
| a - b; | ||
| } else if (_hasLength(x, 1, 2)) { | ||
| const [a, b = 2] = x; | ||
| 
     | 
||
| a + b - 2; | ||
| } else if (_hasLength(x, 1)) { | ||
| const [a, ...b] = x; | ||
| 
     | 
||
| b.concat(a); | ||
| } else if (_hasLength(x, 2) && _hasLength(x[0], 1, 2) && _hasLength(x[1], 3, 3)) { | ||
| const [[b, d = 'e'], [g,, h], ...j] = x; | ||
| 
     | 
||
| [b, d, g, ...j].join(''); | ||
| } | 
  
    
      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,14 @@ | ||
| match x: | ||
| | { a }: | ||
| a | ||
| | { a, b }: | ||
| a + b | ||
| | { a, b = 1 }: | ||
| a + b | ||
| | { a, b: { ba, bb = 1 }, c: { ca, cb: { cba } } }: | ||
| a + ba + bb + ca + cba | ||
| | 1 or 2 with { a, b: { c } }: | ||
| a + c | ||
| | { a: { b: { c } } = otherObj }: | ||
| c | ||
| //TODO: | { a, ...b }: b | 
      
      Oops, something went wrong.
        
    
  
  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.
I liked this way of doing helpers a lot, so I tried to adopt it. When I applied it to my local build of
lscdiagI got some very strange code output in the generated helpers. Took me a while to figure out why -- turns out that minifiers mangle these things and thenfn.toString()brings in the minified code.We could just say we don't care about that, but I think live compilers and minifiers are good things generally, whereas this, while neat, could just be replaced with a plain string passed to babel-template. I recommend the latter approach.
Uh oh!
There was an error while loading. Please reload this page.
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.
ah, yeah, that's why. I kind of don't mind – it's babel helping make sure that I'm writing cross-browser code. But yes, if you see the tests, the output isn't the exact same as what I wrote.
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.
This doesn't sound quite right... the transform being run here is just the one specified in
.babelrcfor the plugin, which is justenv: { node: 4 }-- that's definitely not cross browser code.I think having cross-browser code is a good point, but might be better to paste the helper into babel-repl, set the env to Netscape Navigator 1.0, and paste the output into a plain string.
Alternatively, an extra build step could compile the helpers with a different env? But that seems like overtooling.
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.
My current implementation of
hasPropsandhasLengthdoesn't change due to this factor, which I think is desirable, so I think I'll leave this as-is for now, hope that's okThere 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.
Sorry, I hadn't read that comment very carefully – I did check in the babel repl that with an ie<6 target the output doesn't change