Skip to content

Avoid checking for hard-coded "." in token text #81

Closed

Description

@krlmlr

https://github.com/krlmlr/styler/pull/80/files#diff-abef16e00aa68f1dab0e93325dd84b74R3

styler:::create_tree("a %>% b")
#> 1 ROOT (token: short_text [lag_newlines/spaces] {id})
#> 2  °--expr:  [0/0] {7}                               
#> 3      ¦--expr:  [0/1] {3}                           
#> 4      ¦   °--SYMBOL: a [0/0] {1}                    
#> 5      ¦--SPECIAL-PIPE: %>% [0/1] {2}                
#> 6      °--expr:  [0/0] {6}                           
#> 7          °--SYMBOL: b [0/0] {4}
styler:::create_tree("a %>% b()")
#>                                              levelName
#> 1  ROOT (token: short_text [lag_newlines/spaces] {id})
#> 2   °--expr:  [0/0] {11}                              
#> 3       ¦--expr:  [0/1] {3}                           
#> 4       ¦   °--SYMBOL: a [0/0] {1}                    
#> 5       ¦--SPECIAL-PIPE: %>% [0/1] {2}                
#> 6       °--expr:  [0/0] {9}                           
#> 7           ¦--expr:  [0/0] {6}                       
#> 8           ¦   °--SYMBOL_FUNCTION_CALL: b [0/0] {4}  
#> 9           ¦--'(': ( [0/0] {5}                       
#> 10          °--')': ) [0/0] {7}

The first needs to be transformed to the second example, can be detected by working on the level of the nested parse data that contains the SPECIAL-PIPE symbol and looking into the children (one level deep).

Activity

  1. lorenzwalthert commented on Sep 22, 2017

    @lorenzwalthert
    Collaborator

    If I remember correctly, the reason why we did check for a hardcoded . is because we want the following to prevent from happening:
    Turning

    a %>%
      .[[1]]

    Into

    a %>%
      .()[[1]]

    If you don't like checking text of token we can probably check if the token after . is "'['".
    This seems safe as the only expression where [ follows after . I can think of is the following:

    a %>%
      .
    `[`(letters, 1)

    And that's invalid.

  2. krlmlr commented on Oct 23, 2017

    @krlmlr
    MemberAuthor

    The structure of the parse tree with .[[1]] looks sufficiently different to me:

    styler:::create_tree("a %>% .[[1]]")
    #> Warning: replacing previous import 'scales::viridis_pal' by
    #> 'viridis::viridis_pal' when loading 'DiagrammeR'
    #>                                              levelName
    #> 1  ROOT (token: short_text [lag_newlines/spaces] {id})
    #> 2   °--expr:  [0/0] {15}                              
    #> 3       ¦--expr:  [0/1] {3}                           
    #> 4       ¦   °--SYMBOL: a [0/0] {1}                    
    #> 5       ¦--SPECIAL-PIPE: %>% [0/1] {2}                
    #> 6       °--expr:  [0/0] {13}                          
    #> 7           ¦--expr:  [0/0] {6}                       
    #> 8           ¦   °--SYMBOL: . [0/0] {4}                
    #> 9           ¦--LBB: [[ [0/0] {5}                      
    #> 10          ¦--expr:  [0/0] {8}                       
    #> 11          ¦   °--NUM_CONST: 1 [0/0] {7}             
    #> 12          ¦--']': ] [0/0] {9}                       
    #> 13          °--']': ] [0/0] {12}

    We should be adding parens only if there's a single symbol at the RHS of the pipe. Can we avoid checking for "." in this case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions