Skip to content

Commit

Permalink
Add PHP 5.2 Compatibility linting pre-commit hook (#9908)
Browse files Browse the repository at this point in the history
* Add PHP Compatibility pre-commit hook
  • Loading branch information
brbrr committed Jul 18, 2018
1 parent ce014ac commit 99c2308
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
19 changes: 14 additions & 5 deletions bin/pre-commit-hook.js
Expand Up @@ -4,19 +4,28 @@ const execSync = require( 'child_process' ).execSync;
const spawnSync = require( 'child_process' ).spawnSync;
const chalk = require( 'chalk' );

const files = execSync( 'git diff --cached --name-only --diff-filter=ACM' )
const gitFiles = execSync( 'git diff --cached --name-only --diff-filter=ACM' )
.toString()
.split( '\n' )
.map( name => name.trim() )
.filter( name => name.endsWith( '.js' ) || name.endsWith( '.jsx' ) );
.map( name => name.trim() );
const jsFiles = gitFiles.filter( name => name.endsWith( '.js' ) || name.endsWith( '.jsx' ) );
const phpFiles = gitFiles.filter( name => name.endsWith( '.php' ) );

// linting should happen after formatting
const lintResult = spawnSync( 'eslint-eslines', [ ...files, '--', '--diff=index' ], {
const jsLintResult = spawnSync( 'eslint-eslines', [ ...jsFiles, '--', '--diff=index' ], {
shell: true,
stdio: 'inherit',
} );

if ( lintResult.status ) {
let phpLintResult;
if ( phpFiles.length > 0 ) {
phpLintResult = spawnSync( 'composer', [ 'php:5.2-compatibility', ...phpFiles, ], {
shell: true,
stdio: 'inherit',
} );
}

if ( jsLintResult.status || ( phpLintResult && phpLintResult.status ) ) {
console.log(
chalk.red( 'COMMIT ABORTED:' ),
'The linter reported some problems. ' +
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -17,7 +17,7 @@
"wimg/php-compatibility": "^8.1"
},
"scripts": {
"php:5.2-compatibility": "composer install && vendor/bin/phpcs -p --runtime-set testVersion '5.2-' --standard=PHPCompatibility --ignore=docker,tools,tests,node_modules,vendor --extensions=php .",
"php:5.2-compatibility": "composer install && vendor/bin/phpcs -p --runtime-set testVersion '5.2-' --standard=PHPCompatibility --ignore=docker,tools,tests,node_modules,vendor --extensions=php",
"php:lint": "composer install && vendor/bin/phpcs -p"
}
}
5 changes: 2 additions & 3 deletions docs/development-environment.md
Expand Up @@ -177,16 +177,15 @@ $ yarn php:lint
We have a handy `composer` script that will just run the PHP CodeSniffer `PHPCompatibility` ruleset checking for code not compatible with PHP 5.2

```sh
$ composer php:5.2-compatibility
$ composer php:5.2-compatibility .
```

_There's also a handy `yarn php:5.2-compatibility` that will run `composer php:5.2-compatibility` if you prefer_.

```sh
$ yarn php:5.2-compatibility
$ yarn php:5.2-compatibility .
```


### Linting Jetpack's JavaScript

`yarn lint` will check syntax and style in the following JavaScript pieces:
Expand Down

0 comments on commit 99c2308

Please sign in to comment.