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 1: Line 1:
{{Work in progress}}
{{Work in progress}}
In Moodle development code linters are used to help ensure consistent [[Coding_style|coding conventions]] and help prevent common errors in code.  
In Moodle development code linters are used to help ensure consistent [[Coding_style|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 [https://css-tricks.com/stylelint/#article-header-id-0 mistake-preventing machine].


= Linters =
= Linters =
Line 8: Line 11:


=== Rules ===
=== Rules ===
The rules defined as part of the [https://moodle.org/plugins/local_codechecker Code-checker plugin].
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) ==


Javascript code is linted with [http://eslint.org eslint].
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.
Linting is enforced in the Javascript build process for both AMD and YUI modules when using `grunt js`.


=== Rules ===
=== Rules ===
Line 24: Line 27:
=== Auto fixing ===
=== Auto fixing ===


Many eslint rules come with auto fixes  
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 ===

Revision as of 15:59, 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