Skip to content

Commit b24c217

Browse files
committed
Add JS coding standards
1 parent 7d91629 commit b24c217

File tree

4 files changed

+166
-34
lines changed

4 files changed

+166
-34
lines changed

source/coding_standards/global.rst

+38-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ Global Coding standards
44
Indentation
55
-----------
66

7-
- 3 spaces
8-
- Max line width: 100
7+
Line length SHOULD NOT exceed 100 characters but there is no hard limit.
8+
9+
Line indentation MUST be 3 spaces.
10+
911

1012
.. code-block:: php
1113
@@ -88,3 +90,37 @@ Variables and Constants
8890
$users_groups = ['glpi', 'glpi2', 'glpi3'];
8991
9092
$CFG_GLPI = [];
93+
94+
Checking standards
95+
------------------
96+
97+
In order to check some stabdards are respected, we provide some custom `PHP CodeSniffer <http://pear.php.net/package/PHP_CodeSniffer>`_ rules. From the GLPI directory, just run:
98+
99+
.. code-block:: bash
100+
101+
phpcs --standard=vendor/glpi-project/coding-standard/GlpiStandard/ inc/ front/ ajax/ tests/
102+
103+
If you want to check the coding standard for the main GLPI codebase, you can use the provided Composer script.
104+
105+
.. code-block:: bash
106+
107+
composer run-script cs
108+
109+
If the above commands do not provide any output, then, all is OK :)
110+
111+
An example error output would looks like:
112+
113+
.. code-block:: bash
114+
115+
phpcs --standard=vendor/glpi-project/coding-standard/GlpiStandard/ inc/ front/ ajax/ tests/
116+
117+
FILE: /var/www/webapps/glpi/tests/HtmlTest.php
118+
----------------------------------------------------------------------
119+
FOUND 3 ERRORS AFFECTING 3 LINES
120+
----------------------------------------------------------------------
121+
40 | ERROR | [x] Line indented incorrectly; expected 3 spaces, found
122+
| | 4
123+
59 | ERROR | [x] Line indented incorrectly; expected 3 spaces, found
124+
| | 4
125+
64 | ERROR | [x] Line indented incorrectly; expected 3 spaces, found
126+
| | 4

source/coding_standards/index.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ GLPI has a general set of coding standards and then additional requirements for
77
:maxdepth: 2
88

99
global.rst
10-
php.rst
10+
php.rst
11+
javascript.rst
+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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.

source/coding_standards/php.rst

+5-31
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,11 @@ Examples:
336336
Files
337337
-----
338338

339-
* Name in lower case.
340-
* Maximum line length: 100 characters
341-
* Indentation: 3 spaces
339+
Names MUST contain only lowercase characters and MUST have the ".class.php" ending in order for the GLPI autoloader to load them.
340+
341+
PHP files for tests MAY contain uppercase characters and a simple ".php" extension.
342+
343+
For plugins, the `setup` and `hook` files MUST be named "setup.php" and "hook.php" respectively.
342344

343345
Database queries
344346
----------------
@@ -364,31 +366,3 @@ Database queries
364366
$query = "INSERT INTO `glpi_alerts`
365367
(`itemtype`, `items_id`, `type`, `date`) // put field's names to avoid mistakes when names of fields change
366368
VALUE ('contract', '5', '2', NOW())";
367-
368-
Checking standards
369-
------------------
370-
371-
In order to check some stabdards are respected, we provide some custom `PHP CodeSniffer <http://pear.php.net/package/PHP_CodeSniffer>`_ rules. From the GLPI directory, just run:
372-
373-
.. code-block:: bash
374-
375-
phpcs --standard=vendor/glpi-project/coding-standard/GlpiStandard/ inc/ front/ ajax/ tests/
376-
377-
If the above command does not provide any output, then, all is OK :)
378-
379-
An example error output would looks like:
380-
381-
.. code-block:: bash
382-
383-
phpcs --standard=vendor/glpi-project/coding-standard/GlpiStandard/ inc/ front/ ajax/ tests/
384-
385-
FILE: /var/www/webapps/glpi/tests/HtmlTest.php
386-
----------------------------------------------------------------------
387-
FOUND 3 ERRORS AFFECTING 3 LINES
388-
----------------------------------------------------------------------
389-
40 | ERROR | [x] Line indented incorrectly; expected 3 spaces, found
390-
| | 4
391-
59 | ERROR | [x] Line indented incorrectly; expected 3 spaces, found
392-
| | 4
393-
64 | ERROR | [x] Line indented incorrectly; expected 3 spaces, found
394-
| | 4

0 commit comments

Comments
 (0)