| 1 | # htmx Coding Standards |
| 2 | |
| 3 | * General Code Style |
| 4 | * Prefer `for` loop to `forEach` (easier to debug, compresses better) |
| 5 | * Assign complex expressions to a local variable rather than using them directly in statements (easier to debug) |
| 6 | * Use `let` rather than `const` |
| 7 | * Local variables should have descriptive names in most cases. `ctx` and `elt` are acceptable. |
| 8 | * Terser does a good job of minimizing names, so there is no benefit from a size perspective to using short variable names. |
| 9 | * There is no size code benefit to naked if statements, use curlies to make debugging easier: |
| 10 | ```js |
| 11 | // terser turns these two forms into the same compressed code |
| 12 | if(bool) return; |
| 13 | if(bool) { |
| 14 | return; |
| 15 | } |
| 16 | ``` |
| 17 | * Method/Field Conventions |
| 18 | * Private methods should be prefixed with `__`. The `dist` task will replace double underscore with `#` when it builds |
| 19 | the final script. This allows us to unit test private methods. |
| 20 | * "Internal" methods should be prefixed with a `_`. These methods are _not_ guaranteed to never change, but may be useful |
| 21 | for special cases (e.g. the `quirks` htmx 2.0 compatibility extension) |
| 22 | * Public methods are forever, be very careful with them |
| 23 | * Publicly surfaced properties should not be shortened, _except_ "Configuration" which can be shortened to "Config" |
| 24 | * Architectural Style |
| 25 | * Generally all state in the trigger -> request -> swap life cycle should be stored on `ctx`. Try to avoid overwriting |
| 26 | an existing property, pick a new property name. These properties are part of the public API and *must* be documented. |
| 27 | |
| 28 | ## Testing |
| 29 | |
| 30 | Tests for htmx are organized in the following manner: |
| 31 | |
| 32 | * `/test/unit` - These tests should for the most part *directly* exercise public and private methods. Because in |
| 33 | dev private methods are just public methods that start with `__` this is easy to do. Unit tests should be created |
| 34 | after a method has stabilized and the behavior is reasonably well understood. |
| 35 | * `/test/attributes` - These are integration tests that test the full behavior of a given attribute and should do things |
| 36 | like set up a response mock using `mockResponse()`, create a live HTML button with the `createProcessedHTML` method, |
| 37 | invoke `click()` on the button, await the `"htmx:finally:request" event, and assert something about the updated DOM. |
| 38 | * `/test/end2end` - These are end-to-end tests that do not fit in the other two categories |
| 39 | * `/test/ext` - These tests are for the core extensions, which ship as part of htmx |
| 40 | |
| 41 | |
| 42 | ## AI Policy |
| 43 | |
| 44 | AI may _not_ be used to generate any significant amount of code that is added to htmx.js. It may be used to _suggest_ code, |
| 45 | but that code must be audited and every line understood by the author. |
| 46 | |
| 47 | AI _may_ be used to generate tests for htmx. These tests should follow the existing standards as much as possible and |
| 48 | should ideally be relatively small. No more than one test should be added at a time, and the test should be reviewed |
| 49 | for correctness. |
| 50 | |
| 51 | In general, try to keep any AI contributions small and well understood. |
| 52 | |
| 53 | > “A computer can never be held accountable, therefore a computer must never make a management decision.” |