MediaWiki master
jsparse.php
Go to the documentation of this file.
1<?php
9
10// @codeCoverageIgnoreStart
11require_once __DIR__ . '/Maintenance.php';
12// @codeCoverageIgnoreEnd
13
24 public $errs = 0;
25
26 public function __construct() {
27 parent::__construct();
28 $this->addDescription( 'Validate syntax of JavaScript files' );
29 $this->addArg( 'file(s)', 'JavaScript files or "-" to read stdin', true, true );
30 }
31
32 public function execute() {
33 $files = $this->getArgs();
34
35 foreach ( $files as $filename ) {
36 $js = $filename === '-'
37 ? stream_get_contents( STDIN )
38 // phpcs:ignore Generic.PHP.NoSilencedErrors
39 : @file_get_contents( $filename );
40 if ( $js === false ) {
41 $this->output( "$filename ERROR: could not read file\n" );
42 $this->errs++;
43 continue;
44 }
45
46 try {
47 Peast\Peast::ES2017( $js )->parse();
48 } catch ( Exception $e ) {
49 $this->errs++;
50 $this->output( "$filename ERROR: " . get_class( $e ) . ": " . $e->getMessage() . "\n" );
51 continue;
52 }
53
54 $this->output( "$filename OK\n" );
55 }
56
57 if ( $this->errs > 0 ) {
58 $this->fatalError( 'Failed.' );
59 }
60 }
61}
62
63// @codeCoverageIgnoreStart
64$maintClass = JSParseHelper::class;
65require_once RUN_MAINTENANCE_IF_MAIN;
66// @codeCoverageIgnoreEnd
Ad-hoc run ResourceLoader validation for user-supplied JavaScript.
Definition jsparse.php:22
execute()
Do the actual work.
Definition jsparse.php:32
__construct()
Default constructor.
Definition jsparse.php:26
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.
output( $out, $channel=null)
Throw some output to the user.
getArgs( $offset=0)
Get arguments.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addDescription( $text)
Set the description text.
$maintClass
Definition jsparse.php:64