MediaWiki master
jsparse.php
Go to the documentation of this file.
1<?php
22require_once __DIR__ . '/Maintenance.php';
23
33 public $errs = 0;
34
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Validate syntax of JavaScript files' );
38 $this->addArg( 'file(s)', 'JavaScript files or "-" to read stdin', true );
39 }
40
41 public function execute() {
42 $files = $this->getArgs();
43
44 foreach ( $files as $filename ) {
45 $js = $filename === '-'
46 ? stream_get_contents( STDIN )
47 // phpcs:ignore Generic.PHP.NoSilencedErrors
48 : @file_get_contents( $filename );
49 if ( $js === false ) {
50 $this->output( "$filename ERROR: could not read file\n" );
51 $this->errs++;
52 continue;
53 }
54
55 try {
56 Peast\Peast::ES2016( $js )->parse();
57 } catch ( Exception $e ) {
58 $this->errs++;
59 $this->output( "$filename ERROR: " . get_class( $e ) . ": " . $e->getMessage() . "\n" );
60 continue;
61 }
62
63 $this->output( "$filename OK\n" );
64 }
65
66 if ( $this->errs > 0 ) {
67 $this->fatalError( 'Failed.' );
68 }
69 }
70}
71
72$maintClass = JSParseHelper::class;
73require_once RUN_MAINTENANCE_IF_MAIN;
Ad-hoc run ResourceLoader validation for user-supplied JavaScript.
Definition jsparse.php:32
execute()
Do the actual work.
Definition jsparse.php:41
__construct()
Default constructor.
Definition jsparse.php:35
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addArg( $arg, $description, $required=true, $multi=false)
Add some args that are needed.
getArgs( $offset=0)
Get arguments.
output( $out, $channel=null)
Throw some output to the user.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
$maintClass
Definition jsparse.php:72