Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions tools/flir-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# flir migration rules on sample calls

*Handbook: [`architecture/r-layer/conventions/`](/handbook/architecture/r-layer/conventions/README.md) is the node this folder serves.*

`calls.R` holds old-style calls to `degree()`, `sample_gnp()`, `simplify()` and `clique_size_counts()`,
each with a comment saying what the rules should do with it.
`rules/` holds the rules `tools/generate-flir-rules.R` writes for those four functions.

To see the rules at work, rewrite `calls.R` in place from the repository root and read the diff:

```r
flir::fix(
"tools/flir-demo/calls.R",
linters = list.files("tools/flir-demo/rules", full.names = TRUE)
)
```

```sh
git diff tools/flir-demo/calls.R
git checkout tools/flir-demo/calls.R
```

`flir::lint()` with the same arguments lists the matches without changing the file.

After a change to the generator or its template, regenerate the rules before running flir again:

```sh
Rscript tools/generate-flir-rules.R tools/flir-demo/rules degree sample_gnp simplify clique_size_counts
```
49 changes: 49 additions & 0 deletions tools/flir-demo/calls.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Old-style calls to igraph functions whose arguments moved behind `...` in
# igraph 3.0.0. Each comment says what the rules should do with the call.

library(igraph)
library(magrittr)

g <- make_ring(10)

# degree(): old formals graph, v, mode, loops, normalized

# by position: mode, loops and normalized all get named
degree(g, V(g), mode = "out", loops = FALSE, normalized = TRUE)
# by position, only mode
degree(g, 1:3, mode = "in")
# by an abbreviation of the old name
degree(g, mode = "out", loops = FALSE)
# already new style: left alone
degree(g, mode = "out")
# piped: the graph comes from the left-hand side
g |> degree(V(g), mode = "out")
g %>% degree(V(g), mode = "out")
# magrittr with a `.` placeholder: the graph is not first, so positions are
# as written
g %>% degree(., V(.), mode = "out")

# Left to the runtime recovery, not rewritten by position:
# a named argument shifts the unnamed ones
degree(g, v = 1:3, "out", TRUE)
# an empty slot
degree(g, , "out")
# `...` of a wrapper
my_degree <- function(...) degree(g, ..., "out")

# sample_gnp(): old formals n, p, directed, loops
sample_gnp(10, 0.2, directed = TRUE, loops = FALSE)
sample_gnp(10, 0.2, directed = TRUE)

# simplify(): old formals graph, remove.multiple, remove.loops, edge.attr.comb
# edge.attr.comb is renamed edge_attr_combine
simplify(g, TRUE, TRUE, edge_attr_combine = "first")
simplify(g, edge_attr_combine = "sum")
igraph::simplify(g, edge_attr_combine = "sum")

# clique_size_counts(): old formals graph, min, max, maximal
clique_size_counts(g, min = 2, max = 4, maximal = TRUE)
# `mi` binds to min, `max` to max, `maxi` to maximal
clique_size_counts(g, min = 2, max = 4, maximal = TRUE)
# `ma` is ambiguous between max and maximal: an error in R, left alone
clique_size_counts(g, ma = 4)
166 changes: 166 additions & 0 deletions tools/flir-demo/rules/migrate-3.0.0-clique_size_counts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
id: igraph-3.0.0-clique_size_counts-min
language: r
severity: warning
rule:
kind: argument
has: { field: value, pattern: $V }
any:
- # passed by name
has: { field: name, kind: identifier, regex: '^(mi)$' }
inside:
kind: arguments
inside:
kind: call
has: { field: function, regex: '^(igraph:::?)?clique_size_counts$' }
- # passed by position
not: { has: { field: name, kind: identifier } }
nthChild: { position: 2, ofRule: { kind: argument, not: { has: { field: name, kind: identifier } } } }
inside:
kind: arguments
not:
any:
- has: { kind: comma, follows: { kind: comma } }
- has: { kind: comma, follows: { regex: '^\($' } }
- has: { kind: comma, precedes: { regex: '^\)$' } }
- has: { kind: argument, has: { kind: dots } }
- has: { kind: argument, has: { field: name, regex: '^(g|gr|gra|grap|graph|m|mi|min)$' } }
inside:
kind: call
has: { field: function, regex: '^(igraph:::?)?clique_size_counts$' }
not:
inside:
kind: binary_operator
field: rhs
has: { field: operator, regex: '^(\|>|%>%)$' }
not: { has: { field: rhs, kind: call, has: { kind: arguments, has: { kind: argument, has: { field: value, kind: identifier, regex: '^[.]$' } } } } }
- # passed by position after a pipe
not: { has: { field: name, kind: identifier } }
nthChild: { position: 1, ofRule: { kind: argument, not: { has: { field: name, kind: identifier } } } }
inside:
kind: arguments
not:
any:
- has: { kind: comma, follows: { kind: comma } }
- has: { kind: comma, follows: { regex: '^\($' } }
- has: { kind: comma, precedes: { regex: '^\)$' } }
- has: { kind: argument, has: { kind: dots } }
- has: { kind: argument, has: { field: value, kind: identifier, regex: '^[._]$' } }
- has: { kind: argument, has: { field: name, regex: '^(g|gr|gra|grap|graph|m|mi|min)$' } }
inside:
kind: call
has: { field: function, regex: '^(igraph:::?)?clique_size_counts$' }
inside:
kind: binary_operator
field: rhs
has: { field: operator, regex: '^(\|>|%>%)$' }
fix: min = ~~V~~
message: igraph 3.0.0 requires `min` of `clique_size_counts()` to be passed by its full name.
---
id: igraph-3.0.0-clique_size_counts-max
language: r
severity: warning
rule:
kind: argument
has: { field: value, pattern: $V }
any:
- # passed by position
not: { has: { field: name, kind: identifier } }
nthChild: { position: 3, ofRule: { kind: argument, not: { has: { field: name, kind: identifier } } } }
inside:
kind: arguments
not:
any:
- has: { kind: comma, follows: { kind: comma } }
- has: { kind: comma, follows: { regex: '^\($' } }
- has: { kind: comma, precedes: { regex: '^\)$' } }
- has: { kind: argument, has: { kind: dots } }
- has: { kind: argument, has: { field: name, regex: '^(g|gr|gra|grap|graph|m|mi|min|ma|max)$' } }
inside:
kind: call
has: { field: function, regex: '^(igraph:::?)?clique_size_counts$' }
not:
inside:
kind: binary_operator
field: rhs
has: { field: operator, regex: '^(\|>|%>%)$' }
not: { has: { field: rhs, kind: call, has: { kind: arguments, has: { kind: argument, has: { field: value, kind: identifier, regex: '^[.]$' } } } } }
- # passed by position after a pipe
not: { has: { field: name, kind: identifier } }
nthChild: { position: 2, ofRule: { kind: argument, not: { has: { field: name, kind: identifier } } } }
inside:
kind: arguments
not:
any:
- has: { kind: comma, follows: { kind: comma } }
- has: { kind: comma, follows: { regex: '^\($' } }
- has: { kind: comma, precedes: { regex: '^\)$' } }
- has: { kind: argument, has: { kind: dots } }
- has: { kind: argument, has: { field: value, kind: identifier, regex: '^[._]$' } }
- has: { kind: argument, has: { field: name, regex: '^(g|gr|gra|grap|graph|m|mi|min|ma|max)$' } }
inside:
kind: call
has: { field: function, regex: '^(igraph:::?)?clique_size_counts$' }
inside:
kind: binary_operator
field: rhs
has: { field: operator, regex: '^(\|>|%>%)$' }
fix: max = ~~V~~
message: igraph 3.0.0 requires `max` of `clique_size_counts()` to be passed by its full name.
---
id: igraph-3.0.0-clique_size_counts-maximal
language: r
severity: warning
rule:
kind: argument
has: { field: value, pattern: $V }
any:
- # passed by name
has: { field: name, kind: identifier, regex: '^(maxi|maxim|maxima)$' }
inside:
kind: arguments
inside:
kind: call
has: { field: function, regex: '^(igraph:::?)?clique_size_counts$' }
- # passed by position
not: { has: { field: name, kind: identifier } }
nthChild: { position: 4, ofRule: { kind: argument, not: { has: { field: name, kind: identifier } } } }
inside:
kind: arguments
not:
any:
- has: { kind: comma, follows: { kind: comma } }
- has: { kind: comma, follows: { regex: '^\($' } }
- has: { kind: comma, precedes: { regex: '^\)$' } }
- has: { kind: argument, has: { kind: dots } }
- has: { kind: argument, has: { field: name, regex: '^(g|gr|gra|grap|graph|m|mi|min|ma|max|maxi|maxim|maxima|maximal)$' } }
inside:
kind: call
has: { field: function, regex: '^(igraph:::?)?clique_size_counts$' }
not:
inside:
kind: binary_operator
field: rhs
has: { field: operator, regex: '^(\|>|%>%)$' }
not: { has: { field: rhs, kind: call, has: { kind: arguments, has: { kind: argument, has: { field: value, kind: identifier, regex: '^[.]$' } } } } }
- # passed by position after a pipe
not: { has: { field: name, kind: identifier } }
nthChild: { position: 3, ofRule: { kind: argument, not: { has: { field: name, kind: identifier } } } }
inside:
kind: arguments
not:
any:
- has: { kind: comma, follows: { kind: comma } }
- has: { kind: comma, follows: { regex: '^\($' } }
- has: { kind: comma, precedes: { regex: '^\)$' } }
- has: { kind: argument, has: { kind: dots } }
- has: { kind: argument, has: { field: value, kind: identifier, regex: '^[._]$' } }
- has: { kind: argument, has: { field: name, regex: '^(g|gr|gra|grap|graph|m|mi|min|ma|max|maxi|maxim|maxima|maximal)$' } }
inside:
kind: call
has: { field: function, regex: '^(igraph:::?)?clique_size_counts$' }
inside:
kind: binary_operator
field: rhs
has: { field: operator, regex: '^(\|>|%>%)$' }
fix: maximal = ~~V~~
message: igraph 3.0.0 requires `maximal` of `clique_size_counts()` to be passed by its full name.
Loading
Loading