Skip to content

Commit 6dd901a

Browse files
authored
Explicit coding rules (#1772)
## Context Some coding styles are unwritten and not covered by eslint. ## Proposed solution Explicit them for now, let's discuss on them. Once this PR is merged, we will be able to later work on writing Eslint rules when possible and remove the associated coding styles from the documentation. ## Has this been tested? <!-- Put an `x` in the box that applies: --> - [ ] 👍 yes, I added tests to the test suite - [ ] 💭 no, because this PR is a draft and still needs work - [x] 🙅 no, because this is not relevant here - [ ] 🙋 no, because I need help <!-- Detail how we can help you -->
1 parent 8515342 commit 6dd901a

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

documentation/develop.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,139 @@ $ yarn start:debug
110110

111111
And start using your nodejs debugger client (like the Chrome Devtools). See https://nodejs.org/en/docs/guides/debugging-getting-started#inspector-clients
112112

113+
114+
## Coding Rules
115+
116+
Most of the coding rules are already enforced by the Eslint tool. You can run it using the `yarn lint:ci` command
117+
and you may `yarn lint:fix` to make eslint try to fix them for you.
118+
119+
There are still few rules that are not yet configured in Eslint (we plan to add them):
120+
121+
### Regarding the imports
122+
- The import should be gathered by these blocks following this order:
123+
- The modules of the projects
124+
- The contribs and the native libraries (ideally the later prefixed with `node:`);
125+
- Separate the blocks with a new line
126+
```ts
127+
// ✅ Good
128+
import {
129+
ExternalStorage,
130+
joinKeySegments,
131+
} from 'app/server/lib/ExternalStorage';
132+
import {MemoryWritableStream} from 'app/server/utils/streams';
133+
134+
import * as fse from 'fs-extra';
135+
import * as stream from 'node:stream';
136+
import * as path from 'path';
137+
138+
// ❌ Bad
139+
import {
140+
ExternalStorage,
141+
joinKeySegments,
142+
} from 'app/server/lib/ExternalStorage';
143+
import * as path from 'path';
144+
import * as fse from 'fs-extra';
145+
import * as stream from 'node:stream';
146+
import {MemoryWritableStream} from 'app/server/utils/streams';
147+
148+
```
149+
- Their paths should be absolute (ie. start with `app/`, `test/`, ... and not with `./`) and should not contain the extension:
150+
151+
```ts
152+
// ✅ Good
153+
import {Sessions} from 'app/server/lib/Sessions';
154+
import {TcpForwarder} from 'test/server/tcpForwarder';
155+
import * as testUtils from 'test/server/testUtils';
156+
157+
// ❌ Bad
158+
import {Sessions} from '../../app/server/lib/Sessions'
159+
import {TcpForwarder} from './tcpForwarder';
160+
import * as testUtils from 'test/server/testUtils.ts';
161+
```
162+
163+
164+
- They should be alphabetically sorted by the order of the import path within their blocks:
165+
```ts
166+
// ✅ Good
167+
168+
import {
169+
ExternalStorage,
170+
joinKeySegments,
171+
} from 'app/server/lib/ExternalStorage';
172+
import {drainWhenSettled, MemoryWritableStream} from 'app/server/utils/streams';
173+
174+
// ❌ Bad (`app/server/utils` comes after `apps/server/lib`)
175+
176+
import {drainWhenSettled, MemoryWritableStream} from 'app/server/utils/streams';
177+
import {
178+
ExternalStorage,
179+
joinKeySegments,
180+
} from 'app/server/lib/ExternalStorage';
181+
```
182+
183+
### Spacing
184+
185+
```ts
186+
// ✅ Good :
187+
// - no space after the function name (`foo`)
188+
// - one space before the opening curly brace (`{`);
189+
// - no space before the colon (`:`)
190+
// - one space after the colon (`:`)
191+
// - one space around the equal (`=`) sign
192+
193+
function foo(varName: Type) {
194+
const myOtherVar = varName;
195+
}
196+
197+
// ❌ Bad
198+
function foo (varName :Type){
199+
const myOtherVar=varName;
200+
}
201+
```
202+
203+
### Strings
204+
205+
```ts
206+
import {makeT, t} from 'app/client/lib/localization';
207+
208+
const t = makeT('MyModule');
209+
210+
// ✅ Good :
211+
// - Following local rules: either choose simple quotes or double quotes
212+
// - use the `t()` function (client-side) or `req.t()` function (server-side) for the texts that will be displayed to the user and therefore needs localization
213+
// - pass arguments to the `t()` function, does not use concatenation for the localizers
214+
// - does not concatenate nor use the template strings for the `t()` function, but do as below
215+
// - consistency of the use of type of quotes locally (`"` or `'`) within a same function, except for concatenation where template strings are allowed
216+
217+
function foo() {
218+
const str1 = "foo";
219+
const str2 = "bar";
220+
const shortLocalizedStr = t("Lorem Ipsum");
221+
const result = someFunction();
222+
// Passing arguments
223+
const localizedStrWithPlaceholder = t("Your response: {{result}}", {result});
224+
// Because we use i18next-scanner to collect the translated strings, we have to
225+
// use either simple or double quotes and end lines with backslashes (`\`) to insert a new line.
226+
// Unfortunately this library does not support template strings nor concatenation.
227+
// See: https://github.com/i18next/i18next-scanner/issues/35
228+
const localizedStr = t("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. \
229+
Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. \
230+
Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi.");
231+
}
232+
233+
// ❌ Bad
234+
function foo() {
235+
const str1 = 'foo';
236+
const str2 = "bar";
237+
const localizedStrWithPlaceholder = t("Your response: " + someFunction());
238+
// These concatenations will cause our tool to collect string miss this translation key.
239+
const localizedStr = t("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. " +
240+
"Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. " +
241+
"Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi.");
242+
}
243+
```
244+
245+
113246
## Run tests
114247

115248
You may run the tests using one of these commands:

0 commit comments

Comments
 (0)