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
Line 8: Line 8:
== PHP (PHP_CodeSniffer) ==
== PHP (PHP_CodeSniffer) ==


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]. 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.
 
==== 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.


== Javascript (ESLint) ==
== Javascript (ESLint) ==

Revision as of 16:09, 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. 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. ESlint is run as part of the build process with grunt. When building Javascript with `grunt js` AMD and YUI modules are linted and lint errors will cause the build to fail. By default, lint warnings are not displayed, but they can be displayed with the flag `--show-lint-warnings`.

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. 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 directly from your editor.

In some cases it is desirable to disable eslint rules for a certain section of code or a particular rule. It is possible to do this with inline comments like the following /* eslint-disable no-alert */ alert('foo'); /* eslint-enable */

alert('foo'); // eslint-disable-line no-alert

For full details, see the eslint documentation. Note: this should be used sparingly and usually accompanied with a comment justifying the rationale for ignoring the lint.


CSS/SCSS/Less (stylelint)

CSS code is linted by stylelint.


Linting in your editor

Atom

Eclipse

PHPStorm

Vim

Visual Studio Code