Note:

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

Linting

From MoodleDocs
Revision as of 16:00, 2 December 2016 by Dan Poltawski (talk | contribs)

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