|
| 1 | +# TSR Javascript Style Guide |
| 2 | + |
| 3 | +This guide represents Two Story Robot's approach to Javascript. You can use this |
| 4 | +document for internal reference or to share with others. |
| 5 | + |
| 6 | +Thanks to [AirBNB](AirBNB) for heavily inspiring this document. |
| 7 | + |
| 8 | +## How to Update This |
| 9 | + |
| 10 | +To update this document, make a pull request with your recommended changes. The |
| 11 | +pull request must receive a thumbs up emoji (👍) from at least half the active |
| 12 | +dev team prior to merging. The pull request itself can be used for discussion, |
| 13 | +comments, and iteration on the proposed change. |
| 14 | + |
| 15 | +Rules here should be 1) actionable and 2) clearly testable in a PR review. |
| 16 | + |
| 17 | +## Style Prescedence |
| 18 | + |
| 19 | +- A project's established style convention takes prescedence over this guide. |
| 20 | + - Why? Many of our projects predate this guide, we may be experimenting with |
| 21 | + a new style, or we may be working on code maintained by someone else. |
| 22 | +- Don't include rules in this guide that could be covered by eslint or prettier |
| 23 | + configurations. |
| 24 | + - Why? This lets prettier and eslint worry about the minute details of |
| 25 | + standardizing our code, and keeps this guide focused on high-level patterns |
| 26 | + and things we can't programmatically capture. |
| 27 | + |
| 28 | +## Automatic Styling |
| 29 | + |
| 30 | +- Use prettier with the configuration from @twostoryrobot/prettier-config |
| 31 | +- Use eslint with the configuration from @twostoryrobot/eslint-config |
| 32 | +- `npm test` should validate both prettier and eslint in a CI environment |
| 33 | + - You can use [jest-runner-eslint] to run eslint within jest |
| 34 | + - You can use [is-ci-cli] to automatically run either prettier + jest or jest |
| 35 | + interactive |
| 36 | + |
| 37 | +## General |
| 38 | + |
| 39 | +- Use a line length of 80 characters. |
| 40 | +- Use camelCase when naming objects, functions, and instances. |
| 41 | + |
| 42 | +```jsx |
| 43 | +// bad |
| 44 | +const UsTaxes = 1000 |
| 45 | +const playerID = 10 |
| 46 | +const OBJEcttsssss = {} |
| 47 | +const this_is_my_object = {} |
| 48 | +function getUNESCOProperties() {} |
| 49 | + |
| 50 | +// good |
| 51 | +const usTaxes = 1000 |
| 52 | +const playerId = 10 |
| 53 | +const thisIsMyObject = {} |
| 54 | +function getUnescoProperties() {} |
| 55 | +``` |
| 56 | + |
| 57 | +- Use PascalCase when naming constructors, classes, and components. |
| 58 | + |
| 59 | +```jsx |
| 60 | +// bad |
| 61 | +const myContainer = styled.div` |
| 62 | + color: blue; |
| 63 | +` |
| 64 | + |
| 65 | +// good |
| 66 | +const MyContainer = styled.div` |
| 67 | + color: blue; |
| 68 | +` |
| 69 | +``` |
| 70 | + |
| 71 | +- Group your shorthand properties at the end of your object declaration. |
| 72 | + |
| 73 | +```jsx |
| 74 | +const anakinSkywalker = 'Anakin Skywalker' |
| 75 | +const lukeSkywalker = 'Luke Skywalker' |
| 76 | + |
| 77 | +// bad |
| 78 | +const obj = { |
| 79 | + anakinSkywalker, |
| 80 | + episodeOne: 1, |
| 81 | + twoJediWalkIntoACantina: 2, |
| 82 | + lukeSkywalker, |
| 83 | + episodeThree: 3, |
| 84 | + mayTheFourth: 4, |
| 85 | +} |
| 86 | + |
| 87 | +// good |
| 88 | +const obj = { |
| 89 | + episodeOne: 1, |
| 90 | + twoJediWalkIntoACantina: 2, |
| 91 | + episodeThree: 3, |
| 92 | + mayTheFourth: 4, |
| 93 | + anakinSkywalker, |
| 94 | + lukeSkywalker |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +- Use function declarations instead of function expressions. |
| 99 | + - Why? This improves readability. |
| 100 | + - An exception to this is arrow functions, which must be assigned to |
| 101 | + variables. |
| 102 | + |
| 103 | +```jsx |
| 104 | +// bad |
| 105 | +const foo = function() { |
| 106 | + // ... |
| 107 | +} |
| 108 | + |
| 109 | +// good |
| 110 | +function foo() { |
| 111 | + // ... |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +- Never declare functions inside blocks. This might work but is invalid JS. |
| 116 | + |
| 117 | +```jsx |
| 118 | +// bad |
| 119 | +if (currentUser) { |
| 120 | + function test() { |
| 121 | + console.log('Nope.') |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// good |
| 126 | +let test |
| 127 | +if (currentUser) { |
| 128 | + test = () => { |
| 129 | + console.log('Yup.') |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +- Always put default parameters last. |
| 135 | + |
| 136 | +```jsx |
| 137 | +// bad |
| 138 | +function handleThings(opts = {}, name) { |
| 139 | + // ... |
| 140 | +} |
| 141 | + |
| 142 | +// good |
| 143 | +function handleThings(name, opts = {}) { |
| 144 | + // ... |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +- You may optionally uppercase a constant only if it (1) is a const and (2) the |
| 149 | + programmer can trust it (and its nested properties) to never change. |
| 150 | + - Why? This is an additional tool to assist in situations where the |
| 151 | + programmer would be unsure if a variable might ever change. |
| 152 | + UPPERCASE_VARIABLES are letting the programmer know that they can trust |
| 153 | + the variable (and its properties) not to change. |
| 154 | + |
| 155 | +```jsx |
| 156 | +// bad |
| 157 | +export const THING_TO_BE_CHANGED = 'should obviously not be uppercased' |
| 158 | + |
| 159 | +// bad |
| 160 | +export let REASSIGNABLE_VARIABLE = 'do not use let with uppercase variables' |
| 161 | + |
| 162 | +// allowed but does not supply semantic value |
| 163 | +export const apiKey = 'SOMEKEY' |
| 164 | + |
| 165 | +// better in most cases |
| 166 | +export const API_KEY = 'SOMEKEY' |
| 167 | +``` |
| 168 | + |
| 169 | +## Filenames |
| 170 | + |
| 171 | +- Store source code in a subdirectory called `/src/`. |
| 172 | +- Store components in a subdirectory called `/src/components/`. |
| 173 | +- A base filename should exactly match the name of its default export if one is |
| 174 | + present. |
| 175 | +- Use PascalCase for files that export a component, class, or constructor. |
| 176 | +- Use camelCase for files that export functions, variables, instances, etc. |
| 177 | +- Use the `.stories.js` and `test.js` extensions for stories and tests where |
| 178 | + applicable. |
| 179 | + |
| 180 | +```jsx |
| 181 | +// bad |
| 182 | +./MyComponent.js |
| 183 | +./test/MyComponent.js |
| 184 | +./stories/MyComponent.js |
| 185 | + |
| 186 | +// good |
| 187 | +./MyComponent.js |
| 188 | +./MyComponent.stories.js |
| 189 | +./MyComponent.test.js |
| 190 | + |
| 191 | +// good |
| 192 | +./useMyHook.js |
| 193 | +./useMyHook.test.js |
| 194 | +``` |
| 195 | + |
| 196 | +## Modules |
| 197 | + |
| 198 | +- Always use modules (import/export) over a non-standard module system. |
| 199 | +- In modules with a single export, prefer default export over named export. |
| 200 | +- Put all imports above non-import statements. |
| 201 | + |
| 202 | +## Metadetails |
| 203 | + |
| 204 | +- Provide a README that lists all environment variables and their descriptions. |
| 205 | +- Private projects should have `UNLICENSED` and `private` in their |
| 206 | + `package.json`. |
| 207 | +- Public projects should have `MIT` in their `package.json` with the license in |
| 208 | + `LICENSE.txt`. |
| 209 | +- Projects should specify an `engine` field with the minimum required node |
| 210 | + version. |
| 211 | + - You can use the version you started the project with. |
| 212 | + |
| 213 | +## React |
| 214 | + |
| 215 | +- We initialize our web applications with create-react-app. |
| 216 | + - Next.js and Gatsby are also acceptable. |
| 217 | +- Use hooks instead of other state management. |
| 218 | + |
| 219 | +## Component Driven Development |
| 220 | + |
| 221 | +- Practice CDD with Storybook in React applications. |
| 222 | +- All component stories should have a `Default` state. |
| 223 | +- Put spaces in story component names for consistency with story parsing. |
| 224 | + |
| 225 | +```graphql |
| 226 | +// bad |
| 227 | +export default { |
| 228 | + title: 'pages/BrowsePage', |
| 229 | +} |
| 230 | + |
| 231 | +// good |
| 232 | +export default { |
| 233 | + title: 'pages/Browse Page' |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +## GraphQL |
| 238 | + |
| 239 | +- Write root-level Mutations and Queries verb-first. |
| 240 | + |
| 241 | +```graphql |
| 242 | +// bad |
| 243 | +Mutation { |
| 244 | + userCreate |
| 245 | + address |
| 246 | +} |
| 247 | + |
| 248 | +// bad |
| 249 | +Query { |
| 250 | + comments |
| 251 | + comment |
| 252 | +} |
| 253 | + |
| 254 | +// good |
| 255 | +Mutation { |
| 256 | + createUser |
| 257 | + likePost |
| 258 | + updateComment |
| 259 | +} |
| 260 | + |
| 261 | +// good |
| 262 | +Query { |
| 263 | + getComments |
| 264 | + getUser |
| 265 | +} |
| 266 | +``` |
| 267 | + |
| 268 | +- Field names should use camelCase. |
| 269 | +- Type names should use PascalCase. |
| 270 | +- Enum values should be ALL_CAPS and enum names should use PascalCase. |
| 271 | + |
| 272 | +## GitHub |
| 273 | + |
| 274 | +- Use separate repositories for frontend and backend applications. |
| 275 | + - e.g.: repo-web, repo-mobile, and repo-api |
| 276 | +- Projects should follow Git flow, and have both a **master** and **dev** |
| 277 | + branch. |
| 278 | + - Open source apps or small projects can use a single **master** branch. |
| 279 | +- Both **dev** and **master** should be protected branches. |
| 280 | + - Status checks must pass before merging. |
| 281 | + - Require branches are up-to-date. |
| 282 | + |
| 283 | +## CI / CD |
| 284 | + |
| 285 | +- Use dependabot for dependency management. |
| 286 | +- Configure CI / CD using Semaphore 2.0 or Github Actions. |
| 287 | + |
| 288 | +## Testing |
| 289 | + |
| 290 | +- Write tests. |
| 291 | +- Use Jest for unit / integration tests. |
| 292 | +- Use Cypress for e2e tests. |
| 293 | +- Follow the [testing trophy] methodology. |
| 294 | + |
| 295 | +[AirBNB]: https://github.com/airbnb/javascript |
| 296 | +[jest-runner-eslint]: https://www.npmjs.com/package/jest-runner-eslint |
| 297 | +[is-ci-cli]: https://www.npmjs.com/package/is-ci-cli |
| 298 | +[testing trophy]: https://kentcdodds.com/blog/write-tests |
0 commit comments