Skip to content

Commit 7b6c4f4

Browse files
committed
docs(dev): add copyable PHP-CS-Fixer and ESLint setup
Assisted-by: Cursor:grok-4.7 Signed-off-by: whoalin1 <whoalin414@gmail.com>
1 parent 2de9481 commit 7b6c4f4

3 files changed

Lines changed: 116 additions & 48 deletions

File tree

‎developer_manual/digging_deeper/npm.rst‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,17 @@ npm run lint (optional)
8585
-----------------------
8686

8787
Nextcloud apps that use linting tools for consistent code formatting typically add a ``lint`` script to their
88-
``package.json`` and install the appropriate `eslint config <https://www.npmjs.com/package/@nextcloud/eslint-config>`_:
88+
``package.json`` and install the appropriate `eslint config <https://www.npmjs.com/package/@nextcloud/eslint-config>`_
89+
(see the ESLint section in :doc:`../getting_started/coding_standards/javascript` for a copyable ``eslint.config.js``).
90+
91+
With the flat config format, file extensions are declared in ``eslint.config.js``, so pass directories instead of ``--ext``:
8992

9093
.. code-block:: json
9194
9295
{
9396
"scripts": {
94-
"lint": "eslint --ext .js,.vue src",
95-
"lint:fix": "eslint --ext .js,.vue src --fix"
97+
"lint": "eslint src",
98+
"lint:fix": "eslint src --fix"
9699
}
97100
}
98101

‎developer_manual/getting_started/coding_standards/javascript.rst‎

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,34 @@ General rules and advice
1111
- Nextcloud uses Vue.js for its interface, for a consistent user interface we recommend apps to also use Vue with :ref:`provided components <js-library_nextcloud-vue>`.
1212
Yet also vanilla JavaScript and HTML can be used.
1313
- We recommend using Typescript for its type checking and improved static code analysis features.
14-
- Do not create global variables, instead if needed use global namespace objects like ``OCA.YourApp.…``
14+
- Do not create global variables, instead if needed use global namespace objects like ``OCA.YourApp.…``
1515
- Use JavaScript strict mode (automatically the case when using JavaScript modules)
1616

1717
ESLint config
1818
^^^^^^^^^^^^^
1919

2020
There is a shared configuration for `eslint <https://eslint.org/>`_ that you can use to automatically format your Nextcloud apps's JavaScript and Typescript code.
2121
It consists of two parts: a `config package <https://github.com/nextcloud-libraries/eslint-config>`_ that contains the formatting preferences
22-
and a `plugin <https://github.com/nextcloud-libraries/eslint-plugin>`_ to detect deprecated and removed APIs in your code. See their readmes for instructions.
22+
and a `plugin <https://github.com/nextcloud-libraries/eslint-plugin>`_ to detect deprecated and removed APIs in your code.
23+
24+
Install the shared config (it pulls in ESLint and the Nextcloud plugin):
25+
26+
.. code-block:: bash
27+
28+
npm install --save-dev @nextcloud/eslint-config
29+
30+
Add an ``eslint.config.js`` in your app root using the flat config format (required since ``@nextcloud/eslint-config`` v9 / ESLint 9+):
31+
32+
.. code-block:: javascript
33+
34+
import { recommended } from '@nextcloud/eslint-config'
35+
36+
export default [
37+
...recommended,
38+
]
39+
40+
``recommended`` covers TypeScript and Vue 3. Alternatives such as ``recommendedJavascript`` and ``recommendedVue2`` are documented in the `config package README <https://github.com/nextcloud-libraries/eslint-config>`_.
41+
See also :ref:`app-npm` for the ``lint`` / ``lint:fix`` npm scripts.
2342

2443
Filesystem structure
2544
^^^^^^^^^^^^^^^^^^^^
@@ -79,16 +98,16 @@ Naming and casing
7998

8099
- For readability only capitalize the first letter of abbreviations like ``callHttpApi()`` instead of ``callHTTPAPI()``.
81100
- Sub-components should be prefixed.
82-
E.g. splitting a component like ``FileListEntry`` into smaller components called ``FileListEntryName``, ``FileListEntryIcon`` …
101+
E.g. splitting a component like ``FileListEntry`` into smaller components called ``FileListEntryName``, ``FileListEntryIcon`` …
83102
- Components should not have single-word names, this could conflict with current or future native HTML tags as these are always single-word.
84103
E.g. if you have a settings view, do not call it ``Settings`` but ``SettingsView`` or ``UserSettings`` etc.
85104

86105
.. list-table:: Use camelCase for functions, methods, properties, and variables
87106
:widths: 50 50
88107
:header-rows: 1
89108

90-
* - ✅ Do
91-
- ❌ Don't
109+
* - ✅ Do
110+
- ❌ Don't
92111
* -
93112
.. code-block:: javascript
94113
@@ -111,8 +130,8 @@ Naming and casing
111130
:widths: 50 50
112131
:header-rows: 1
113132

114-
* - ✅ Do
115-
- ❌ Don't
133+
* - ✅ Do
134+
- ❌ Don't
116135
* -
117136
.. code-block:: javascript
118137
@@ -141,8 +160,8 @@ Semicolons
141160
:widths: 50 50
142161
:header-rows: 1
143162

144-
* - ✅ Do
145-
- ❌ Don't
163+
* - ✅ Do
164+
- ❌ Don't
146165
* -
147166
.. code-block:: javascript
148167
@@ -170,8 +189,8 @@ Strings
170189
:widths: 50 50
171190
:header-rows: 1
172191

173-
* - ✅ Do
174-
- ❌ Don't
192+
* - ✅ Do
193+
- ❌ Don't
175194
* -
176195
.. code-block:: javascript
177196
@@ -186,8 +205,8 @@ Strings
186205
:widths: 50 50
187206
:header-rows: 1
188207

189-
* - ✅ Do
190-
- ❌ Don't
208+
* - ✅ Do
209+
- ❌ Don't
191210
* -
192211
.. code-block:: javascript
193212
@@ -205,8 +224,8 @@ Arrays
205224
:widths: 50 50
206225
:header-rows: 1
207226

208-
* - ✅ Do
209-
- ❌ Don't
227+
* - ✅ Do
228+
- ❌ Don't
210229
* -
211230
.. code-block:: javascript
212231
@@ -225,8 +244,8 @@ Arrays
225244
:widths: 50 50
226245
:header-rows: 1
227246

228-
* - ✅ Do
229-
- ❌ Don't
247+
* - ✅ Do
248+
- ❌ Don't
230249
* -
231250
.. code-block:: javascript
232251
@@ -280,8 +299,8 @@ Functions
280299
:widths: 50 50
281300
:header-rows: 1
282301

283-
* - ✅ Do
284-
- ❌ Don't
302+
* - ✅ Do
303+
- ❌ Don't
285304
* -
286305
.. code-block:: javascript
287306
@@ -296,8 +315,8 @@ Functions
296315
:widths: 50 50
297316
:header-rows: 1
298317

299-
* - ✅ Do
300-
- ❌ Don't
318+
* - ✅ Do
319+
- ❌ Don't
301320
* -
302321
.. code-block:: javascript
303322
@@ -351,8 +370,8 @@ Functions
351370
:widths: 50 50
352371
:header-rows: 1
353372

354-
* - ✅ Do
355-
- ❌ Don't
373+
* - ✅ Do
374+
- ❌ Don't
356375
* -
357376
.. code-block:: javascript
358377
@@ -390,8 +409,8 @@ Functions
390409
:widths: 50 50
391410
:header-rows: 1
392411

393-
* - ✅ Do
394-
- ❌ Don't
412+
* - ✅ Do
413+
- ❌ Don't
395414
* -
396415
.. code-block:: javascript
397416
@@ -433,8 +452,8 @@ Functions
433452
:widths: 50 50
434453
:header-rows: 1
435454

436-
* - ✅ Do
437-
- ❌ Don't
455+
* - ✅ Do
456+
- ❌ Don't
438457
* -
439458
.. code-block:: javascript
440459
@@ -456,8 +475,8 @@ Functions
456475
:widths: 50 50
457476
:header-rows: 1
458477

459-
* - ✅ Do
460-
- ❌ Don't
478+
* - ✅ Do
479+
- ❌ Don't
461480
* -
462481
.. code-block:: javascript
463482
@@ -492,8 +511,8 @@ Objects
492511
:widths: 50 50
493512
:header-rows: 1
494513

495-
* - ✅ Do
496-
- ❌ Don't
514+
* - ✅ Do
515+
- ❌ Don't
497516
* -
498517
.. code-block:: javascript
499518
@@ -514,8 +533,8 @@ Objects
514533
:widths: 50 50
515534
:header-rows: 1
516535

517-
* - ✅ Do
518-
- ❌ Don't
536+
* - ✅ Do
537+
- ❌ Don't
519538
* -
520539
.. code-block:: javascript
521540
@@ -540,8 +559,8 @@ Objects
540559
:widths: 50 50
541560
:header-rows: 1
542561

543-
* - ✅ Do
544-
- ❌ Don't
562+
* - ✅ Do
563+
- ❌ Don't
545564
* -
546565
.. code-block:: javascript
547566
@@ -559,8 +578,8 @@ Objects
559578
:widths: 50 50
560579
:header-rows: 1
561580

562-
* - ✅ Do
563-
- ❌ Don't
581+
* - ✅ Do
582+
- ❌ Don't
564583
* -
565584
.. code-block:: javascript
566585
@@ -575,8 +594,8 @@ Objects
575594
:widths: 50 50
576595
:header-rows: 1
577596

578-
* - ✅ Do
579-
- ❌ Don't
597+
* - ✅ Do
598+
- ❌ Don't
580599
* -
581600
.. code-block:: javascript
582601
@@ -638,8 +657,8 @@ Here's why:
638657
:widths: 50 50
639658
:header-rows: 1
640659

641-
* - ✅ Do
642-
- ❌ Don't
660+
* - ✅ Do
661+
- ❌ Don't
643662
* -
644663
.. code-block:: javascript
645664
@@ -666,8 +685,8 @@ Control structures
666685
:widths: 50 50
667686
:header-rows: 1
668687

669-
* - ✅ Do
670-
- ❌ Don't
688+
* - ✅ Do
689+
- ❌ Don't
671690
* -
672691
.. code-block:: javascript
673692
@@ -696,8 +715,8 @@ Control structures
696715
:widths: 50 50
697716
:header-rows: 1
698717

699-
* - ✅ Do
700-
- ❌ Don't
718+
* - ✅ Do
719+
- ❌ Don't
701720
* -
702721
.. code-block:: javascript
703722

‎developer_manual/getting_started/coding_standards/php.rst‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,52 @@ PHP coding standards
44

55
Starting with Nextcloud 19 there is a shared `PHP Coding Standards Fixer <https://github.com/FriendsOfPhp/PHP-CS-Fixer>`_ configuration you can use to automatically format your app's source code. For full details see the `repository on GitHub <https://github.com/nextcloud/coding-standard/>`_.
66

7+
Setup
8+
-----
9+
10+
Install the shared configuration as a development dependency:
11+
12+
.. code-block:: bash
13+
14+
composer require --dev nextcloud/coding-standard
15+
16+
Create a ``.php-cs-fixer.dist.php`` in your app root:
17+
18+
.. code-block:: php
19+
20+
<?php
21+
22+
declare(strict_types=1);
23+
24+
require_once './vendor/autoload.php';
25+
26+
use Nextcloud\CodingStandard\Config;
27+
28+
$config = new Config();
29+
$config
30+
->getFinder()
31+
->ignoreVCSIgnored(true)
32+
->notPath('build')
33+
->notPath('l10n')
34+
->notPath('src')
35+
->notPath('vendor')
36+
->in(__DIR__);
37+
return $config;
38+
39+
Add convenience scripts to ``composer.json``:
40+
41+
.. code-block:: json
42+
43+
{
44+
"scripts": {
45+
"cs:check": "php-cs-fixer fix --dry-run --diff",
46+
"cs:fix": "php-cs-fixer fix"
47+
}
48+
}
49+
50+
Then run ``composer cs:check`` to list violations, or ``composer cs:fix`` to apply fixes.
51+
Exclude ``.php-cs-fixer.dist.php`` and ``.php-cs-fixer.cache`` from your release build.
52+
753
Always use::
854

955
<?php

0 commit comments

Comments
 (0)