| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
require_once __DIR__ . '/Maintenance.php'; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class JSParseHelper extends Maintenance { |
| 35 |
|
| 36 |
public $errs = 0; |
| 37 |
|
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
$this->addDescription( 'Validate syntax of JavaScript files' ); |
| 41 |
$this->addArg( 'file(s)', 'JavaScript files or "-" to read stdin', true, true ); |
| 42 |
} |
| 43 |
|
| 44 |
public function execute() { |
| 45 |
$files = $this->getArgs(); |
| 46 |
|
| 47 |
foreach ( $files as $filename ) { |
| 48 |
$js = $filename === '-' |
| 49 |
? stream_get_contents( STDIN ) |
| 50 |
|
| 51 |
: @file_get_contents( $filename ); |
| 52 |
if ( $js === false ) { |
| 53 |
$this->output( "$filename ERROR: could not read file\n" ); |
| 54 |
$this->errs++; |
| 55 |
continue; |
| 56 |
} |
| 57 |
|
| 58 |
try { |
| 59 |
Peast\Peast::ES2016( $js )->parse(); |
| 60 |
} catch ( Exception $e ) { |
| 61 |
$this->errs++; |
| 62 |
$this->output( "$filename ERROR: " . get_class( $e ) . ": " . $e->getMessage() . "\n" ); |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
$this->output( "$filename OK\n" ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( $this->errs > 0 ) { |
| 70 |
$this->fatalError( 'Failed.' ); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
$maintClass = JSParseHelper::class; |
| 77 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 78 |
|