|
| 1 | +JavaScript Coding standards |
| 2 | +=========================== |
| 3 | + |
| 4 | +ECMAScript Level |
| 5 | +---------------- |
| 6 | + |
| 7 | +All JavaScript code should be written to support the target ECMAScript version specified for the version of GLPI you are writing the code for. |
| 8 | +This version can be found in the `.eslintrc.json` file in the `parserOptions.ecmaVersion` property. |
| 9 | + |
| 10 | +For reference, versions of GLPI before 9.5.0 had a target ECMAScript version of 5 due to its support of Internet Explorer 11. |
| 11 | +Starting with GLPI 9.5.0, the target version was bumped to 6 (2015). |
| 12 | + |
| 13 | +Functions |
| 14 | +--------- |
| 15 | + |
| 16 | +Function names must be written in *camelCaps*: |
| 17 | + |
| 18 | +.. code-block:: JavaScript |
| 19 | +
|
| 20 | + function getUserName(a, b = 'foo') { |
| 21 | + //do something here! |
| 22 | + } |
| 23 | +
|
| 24 | + class ExampleClass { |
| 25 | +
|
| 26 | + getUserName(a, b = 'foo') { |
| 27 | + //do something here! |
| 28 | + } |
| 29 | + } |
| 30 | +
|
| 31 | +Space after opening parenthesis and before closing parenthesis are forbidden. For parameters which have a default value, add a space before and after the equals sign. |
| 32 | + |
| 33 | +All functions MUST have a JSDoc block especially if it has parameters or is not a simple getter/setter. |
| 34 | + |
| 35 | +Classes |
| 36 | +------- |
| 37 | + |
| 38 | +If you are writing code for versions of GLPI before 9.5.0, you may not use classes as class support was added in EMCAScript 6. |
| 39 | + |
| 40 | +Class names must be written in `PascalCase`. |
| 41 | + |
| 42 | +..code-block:: JavaScript |
| 43 | + |
| 44 | + class ExampleClass { |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + class ExampleClass extends BaseClass { |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | +In general, you should create a new file for each class. |
| 53 | + |
| 54 | +Comments |
| 55 | +-------- |
| 56 | + |
| 57 | +For simple or one-line comments, use `//`. |
| 58 | + |
| 59 | +For multi-line or JSDoc comments, use '/** */' |
| 60 | +
|
| 61 | +Function JSDoc blocks MUST contain: |
| 62 | + |
| 63 | +- Description |
| 64 | +- Parameter types and descriptions |
| 65 | +- Return type |
| 66 | + |
| 67 | +Function JSDoc blocks SHOULD contain: |
| 68 | + |
| 69 | +- The version of GLPI or the plugin that the function was introduced |
| 70 | + |
| 71 | +.. code-block:: JavaScript |
| 72 | +
|
| 73 | + /** |
| 74 | + * Short description (single line) |
| 75 | + * |
| 76 | + * This is an optional longer description that can span |
| 77 | + * multiple lines. |
| 78 | + * |
| 79 | + * @since 10.0.0 |
| 80 | + * |
| 81 | + * @param {{}} a First parameter description |
| 82 | + * @param {string} b Second parameter description |
| 83 | + * |
| 84 | + * @returns {string} |
| 85 | + */ |
| 86 | + function getUserName(a, b = 'foo') { |
| 87 | +
|
| 88 | + } |
| 89 | +
|
| 90 | +JSDoc sections MUST be in the following order with an empty line between them: |
| 91 | + |
| 92 | +- Short description (one line) |
| 93 | +- Long description |
| 94 | +- '@deprecated' |
| 95 | +- '@since' |
| 96 | +- '@param' |
| 97 | +- '@return' |
| 98 | +- '@see' |
| 99 | +- '@throw' |
| 100 | +- '@todo' |
| 101 | + |
| 102 | +Variable types |
| 103 | +-------------- |
| 104 | + |
| 105 | +When type hinting in JSDoc blocks, you MAY use any of the native types or class names. |
| 106 | + |
| 107 | +If you want to create custom types/object shapes without creating a class, you MAY define a new type using the JSDoc '@typedef' tag. |
| 108 | + |
| 109 | +Quoting Strings |
| 110 | +--------------- |
| 111 | + |
| 112 | +Using single quotes for simple strings is preferred. If you are writing for GLPI 9.5.0 or higher, you may use the ECMAScript 6 template literals feature. |
| 113 | + |
| 114 | +When your strings contain HTML content, you SHOULD use template literals (if possible). This lets you format your HTML across multiple lines for better readability and easily inject variable values without concatenation. |
| 115 | + |
| 116 | +Files |
| 117 | +----- |
| 118 | + |
| 119 | +Regular JavaScript files MUST have only lowercase characters in the name. If using modules, the file name MAY have captialized characters. |
| 120 | + |
| 121 | +JavaScript files for tests MAY contain uppercase characters. |
0 commit comments