| 1 | # fix-getters-rules |
| 2 | |
| 3 | This package contains rules definitions for the `fix-getters` tools. |
| 4 | |
| 5 | See the [workspace documentation](https://github.com/fengalin/fix-getters/blob/0.3.2/README.md) |
| 6 | for more details on `fix-getters`. |
| 7 | |
| 8 | ## Rules |
| 9 | |
| 10 | The `rules` apply to: |
| 11 | |
| 12 | * file system directory entries to decide if a file should be processed or |
| 13 | if a directory branch should be skipped. This requires `feature` **`dir-entry`** |
| 14 | (enabled by default). |
| 15 | * functions name. |
| 16 | |
| 17 | ## Feature |
| 18 | |
| 19 | * **`dir-entry`** — directory entry filtering rules. This features is enabled by |
| 20 | default. Use `default-features = false` if your use case differs. |
| 21 | |
| 22 | ## Function name rules |
| 23 | |
| 24 | The initial intent is to comply with Rust [naming conventions for getter methods](https://doc.rust-lang.org/1.0.0/style/style/naming/README.html#getter/setter-methods-%5Brfc-344%5D): |
| 25 | |
| 26 | > A method `foo(&self) -> &T` for getting the current value of the field. |
| 27 | |
| 28 | ### General rules |
| 29 | |
| 30 | These rules are based on the function name and knowledge of the return type. |
| 31 | See next chapter for `get` functions returning exactly one `bool`. |
| 32 | |
| 33 | A `get` function is considered eligible for `get` prefix removal if: |
| 34 | |
| 35 | * The function starts with `get_`. |
| 36 | * The suffix is not a Rust keyword, which would result in invalid code. |
| 37 | |
| 38 | E.g.: `get_as`, `get_false`, ... are kept as is. |
| 39 | |
| 40 | * The method would result inconsistent with other similar methods. |
| 41 | |
| 42 | E.g.: a `struct` `Value` with a `get_mut` method to get the underlying value |
| 43 | as a mutable reference, `get_optional` to get the underlying value of a type |
| 44 | in the form `Option<T>` and `get_some` to get the underlying value of a type |
| 45 | for which the value is always defined. |
| 46 | |
| 47 | See `RESERVED` in [`function.rs`](https://github.com/fengalin/fix-getters/blob/0.3.2/rules/src/function.rs). |
| 48 | |
| 49 | * The suffix is not part of a subsitution list. This includes some Rust keywords. |
| 50 | |
| 51 | E.g.: `get_type` is replaced with `type_`. |
| 52 | |
| 53 | See `EXACT_SUFFIX_SUBSTITUTES` in [`function.rs`](https://github.com/fengalin/fix-getters/blob/0.3.2/rules/src/function.rs). |
| 54 | |
| 55 | Another rule is applied to homogenize functions names in the form |
| 56 | `get_something_mut`. This rule renames both `get_something_mut` and |
| 57 | `get_mut_something` as `something_mut`. |
| 58 | |
| 59 | The `fix-getters` tools also apply selective rules based on on the function |
| 60 | signature. See the dedicated chapter in [this `README`](https://github.com/fengalin/fix-getters/blob/0.3.2/README.md#get-functions-selection). |
| 61 | |
| 62 | ### Functions returning exactly one `bool` |
| 63 | |
| 64 | Get functions returning `bool` should usually use the form `is_suffix`, which |
| 65 | reads natural when used in a condition. E.g.: |
| 66 | |
| 67 | ```rust |
| 68 | if event.is_serialized() { |
| 69 | ... |
| 70 | } |
| 71 | ``` |
| 72 | |
| 73 | The following additional rules are implemented. |
| 74 | |
| 75 | #### First token substitutions |
| 76 | |
| 77 | When the suffix starts with a verb, it's common to conjugate. E.g. |
| 78 | |
| 79 | * `get_emit_eos` -> `if element.emits_eos()`. |
| 80 | |
| 81 | `BOOL_FIRST_TOKEN_SUBSTITUTES` in [`function.rs`](https://github.com/fengalin/fix-getters/blob/0.3.2/rules/src/function.rs) |
| 82 | lists a set of verbs and the matching substitutions and also includes other |
| 83 | cases such as: |
| 84 | |
| 85 | * `get_always_...` -> `must_always_...`. |
| 86 | * `get_focus` -> `gets_focus`. |
| 87 | |
| 88 | #### Special first tokens |
| 89 | |
| 90 | Modal verbs should be kept unchanged and no `is` prefix should be used. E.g.: |
| 91 | |
| 92 | * `get_can_focus` -> `can_focus`. |
| 93 | * `get_must_...` -> `must_...`. |
| 94 | |
| 95 | This is also the case for already conjugated verbs. E.g.: |
| 96 | |
| 97 | * `get_has_...` -> `has_...`. |
| 98 | |
| 99 | See `BOOL_FIRST_TOKEN_NO_PREFIX` in [`function.rs`](https://github.com/fengalin/fix-getters/blob/0.3.2/rules/src/function.rs). |
| 100 | |
| 101 | #### Exact suffix substitutions |
| 102 | |
| 103 | In some cases, the semantic requires substitutions only if the whole suffix |
| 104 | matches a value. E.g.: |
| 105 | |
| 106 | * `get_result` -> `result`. Neither `if a.is_result()` nor `if a.results()` |
| 107 | would be suitable. |
| 108 | * `get_visibility` -> `is_visible` Neither `if a.is_visibility()` nor |
| 109 | `if a.visibility()`) would be suitable. |
| 110 | |
| 111 | See `BOOL_EXACT_SUBSTITUTES` in [`function.rs`](https://github.com/fengalin/fix-getters/blob/0.3.2/rules/src/function.rs). |
| 112 | |
| 113 | #### get_is prefix |
| 114 | |
| 115 | Finally, the `is` prefix shouldn't be repeated when already present: |
| 116 | |
| 117 | * `get_is_active` -> `is_active`. |
| 118 | |
| 119 | ### Detecting functions returning exactly one `bool` |
| 120 | |
| 121 | The return type of Rust functions is usually not explicit. When renaming the |
| 122 | `get` functions call sites (see [`fix-getters-calls`](https://github.com/fengalin/fix-getters/tree/0.3.2/fix-calls)), |
| 123 | the returned type must be inferred. The rules described in previous chapter are |
| 124 | reversed when possible and an additional heuristic is used: when the first token |
| 125 | of the `get` function suffix ends in `able`, the function is considered to be |
| 126 | returning a `bool`. E.g.: |
| 127 | |
| 128 | * `get_seekable` -> `is_seekable`. |
| 129 | |
| 130 | ## LICENSE |
| 131 | |
| 132 | This crate is licensed under either of |
| 133 | |
| 134 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or |
| 135 | http://www.apache.org/licenses/LICENSE-2.0) |
| 136 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or |
| 137 | http://opensource.org/licenses/MIT) |
| 138 | |
| 139 | at your option. |