Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Linting: Difference between revisions

From MoodleDocs
No edit summary
No edit summary
Line 10: Line 10:
PHP code is linted by [https://github.com/squizlabs/PHP_CodeSniffer PHP_CodeSniffer].
PHP code is linted by [https://github.com/squizlabs/PHP_CodeSniffer PHP_CodeSniffer].


=== Rules ===
==== Rules ====  
The rules defined as part of the [https://moodle.org/plugins/local_codechecker Code-checker plugin]. There are plans (CONTRIB-6209) to move this into a more distributable form.
The rules defined as part of the [https://moodle.org/plugins/local_codechecker Code-checker plugin]. There are plans (CONTRIB-6209) to move this into a more distributable form.


Line 17: Line 17:
Javascript code is linted with [http://eslint.org eslint] since Moodle 3.2.
Javascript code is linted with [http://eslint.org eslint] since Moodle 3.2.


=== grunt js ====
==== grunt js ====


Linting is enforced in the Javascript build process for both AMD and YUI modules when using `grunt js`.
Linting is enforced in the Javascript build process for both AMD and YUI modules when using `grunt js`.


=== Rules ===
==== Rules ====


ESlint rules enabled for Moodle are defined within [https://github.com/moodle/moodle/blob/master/.eslintrc .eslintrc] and the eslint website provides [http://eslint.org/docs/rules/ good documentation] on each rules' purpose and examples of correct and incorrect code for each rule.
ESlint rules enabled for Moodle are defined within [https://github.com/moodle/moodle/blob/master/.eslintrc .eslintrc] and the eslint website provides [http://eslint.org/docs/rules/ good documentation] on each rules' purpose and examples of correct and incorrect code for each rule.


=== Auto fixing ===
==== Auto fixing ====


Many eslint rules come with auto fixes which allows eslint to fix many problem in place, you can run this fix using the CLI tool  `eslint --fix /path/to/file.js` or some editor plugins such as [https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint Visual Studio Code] are able to auto fix in place.
Many eslint rules come with auto fixes which allows eslint to fix many problem in place, you can run this fix using the CLI tool  `eslint --fix /path/to/file.js` or some editor plugins such as [https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint Visual Studio Code] are able to auto fix in place.


=== Ignores ===
==== Ignores ====




Line 36: Line 36:
CSS code is linted by [http://stylelint.io stylelint].
CSS code is linted by [http://stylelint.io stylelint].


=== grunt css ====
==== grunt css =====


==== Rules ====


=== Rules ===
==== Ignores ====
 
=== Ignores ===


= Linting in your editor =
= Linting in your editor =

Revision as of 16:00, 2 December 2016

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.

In Moodle development code linters are used to help ensure consistent coding conventions and help prevent common errors in code.

Motivation

Linting is used to ensure consistent coding style, detect common errors and enforce best practices. At it's best linting helps developers learn, with editor integration and fast feedback meaning that human code reviews can focus on the non-trivial details while linter can be the tireless mistake-preventing machine.

Linters

PHP (PHP_CodeSniffer)

PHP code is linted by PHP_CodeSniffer.

Rules

The rules defined as part of the Code-checker plugin. There are plans (CONTRIB-6209) to move this into a more distributable form.

Javascript (ESLint)

Javascript code is linted with eslint since Moodle 3.2.

grunt js

Linting is enforced in the Javascript build process for both AMD and YUI modules when using `grunt js`.

Rules

ESlint rules enabled for Moodle are defined within .eslintrc and the eslint website provides good documentation on each rules' purpose and examples of correct and incorrect code for each rule.

Auto fixing

Many eslint rules come with auto fixes which allows eslint to fix many problem in place, you can run this fix using the CLI tool `eslint --fix /path/to/file.js` or some editor plugins such as Visual Studio Code are able to auto fix in place.

Ignores

CSS/SCSS/Less (stylelint)

CSS code is linted by stylelint.

grunt css =

Rules

Ignores

Linting in your editor

Atom

Eclipse

PHPStorm

Vim

Visual Studio Code