Linting - dotherightthing/wpdtrt-plugin-boilerplate GitHub Wiki

Summary

DTRT WordPress Plugin Boilerplate supports code linting, both during development, and when running automated tests.

Status

  • Stable @ 1.5.0

Usage

Linters

  • SCSS (SassLint, see .sass-lint.yml)
  • JavaScript (ESLint, see .eslintrc)
  • PHP (CodeSniffer, see gulpfile.js and phpcs.xml)

PHP CodeSniffer (PHPCS)

List sniffs

To see all sniffs run by PHPCS:

$ vendor/bin/phpcs --standard=WordPress -e

or, if using phpcs.xml (see below):

$ vendor/bin/phpcs --standard=phpcs.xml -e

Source: Listing phpcs rules and excluding commenting sniff

Disable specific sniffs

This is useful during development.

In the plugin's ./phpcs.xml, which is used by Sublime Text:

<?xml version="1.0"?>
<ruleset name="DTRT WordPress">
    <description>WordPress standards with DTRT dev exclusions</description>
    <!-- The WordPress ruleset cherry picks sniffs from Generic, PEAR, PSR-2, Squiz etc -->
    <rule ref="WordPress">
        <exclude name="WordPress.Files.FileName"/>
        <exclude name="WordPress.Functions.DontExtract"/>
        <exclude name="WordPress.CSRF.NonceVerification"/>
        <exclude name="WordPress.XSS.EscapeOutput"/>
        <exclude name="WordPress.VIP.ValidatedSanitizedInput"/>
    </rule>
</ruleset>

And in the boilerplate's Gulpfile.js, which is used by Gulp (via Yarn & Travis):

.pipe(phpcs({
    bin: "vendor/bin/phpcs",
    // standard must be included and cannot reference phpcs.xml, which is ignored
    standard: "WordPress", // -Core + -Docs + -Extra + -VIP
    warningSeverity: 0, // minimum severity required to display an error or warning.
    showSniffCode: true,
    // phpcs.xml exclusions are duplicated below:
    exclude: [
        "WordPress.Files.FileName",
        "WordPress.Functions.DontExtract",
        "WordPress.CSRF.NonceVerification",
        "WordPress.XSS.EscapeOutput",
        "WordPress.VIP.ValidatedSanitizedInput"
    ]
}))

Sublime Text integration

  • SublimeLinter
  • SublimeLinter-eslint
  • SublimeLinter-json
  • SublimeLinter-phpcs
  • SublimeLinter-contrib-sass-lint

Run lints while coding in Sublime Text:

Local .sublime-project settings
{
    "folders":
	[
		{
			"path": "."
		}
	],
    "settings":
    {
 		"SublimeLinter.linters.phpcs.args":
		[
			"-s",
			"--warning-severity=0"
		],
		"SublimeLinter.linters.phpcs.disable": false,
		"SublimeLinter.linters.phpcs.standard": "${folder}/phpcs.xml"
    }
}
Global project settings

Sublime Text > Preferences > Package Settings > SublimeLinter > Settings

// SublimeLinter Settings - User
{
	"linters": {
		"jshint": {
			"disable": false,
			"selector": "*.js, gulpfile.js"
		},
		"phpcs": {
			"disable": false,
			"standard": "${folder}/phpcs.xml",
			"args": [
				"-s",
                                "--warning-severity=0"
			]
		}
	}
}

Automated running of lints

yarn run lint

This is also run during the Travis CI build.

⚠️ **GitHub.com Fallback** ⚠️