MediaWiki master
jsparse.php
Go to the documentation of this file.
1<?php
23
24// @codeCoverageIgnoreStart
25require_once __DIR__ . '/Maintenance.php';
26// @codeCoverageIgnoreEnd
27
38 public $errs = 0;
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Validate syntax of JavaScript files' );
43 $this->addArg( 'file(s)', 'JavaScript files or "-" to read stdin', true, true );
44 }
45
46 public function execute() {
47 $files = $this->getArgs();
48
49 foreach ( $files as $filename ) {
50 $js = $filename === '-'
51 ? stream_get_contents( STDIN )
52 // phpcs:ignore Generic.PHP.NoSilencedErrors
53 : @file_get_contents( $filename );
54 if ( $js === false ) {
55 $this->output( "$filename ERROR: could not read file\n" );
56 $this->errs++;
57 continue;
58 }
59
60 try {
61 Peast\Peast::ES2016( $js )->parse();
62 } catch ( Exception $e ) {
63 $this->errs++;
64 $this->output( "$filename ERROR: " . get_class( $e ) . ": " . $e->getMessage() . "\n" );
65 continue;
66 }
67
68 $this->output( "$filename OK\n" );
69 }
70
71 if ( $this->errs > 0 ) {
72 $this->fatalError( 'Failed.' );
73 }
74 }
75}
76
77// @codeCoverageIgnoreStart
78$maintClass = JSParseHelper::class;
79require_once RUN_MAINTENANCE_IF_MAIN;
80// @codeCoverageIgnoreEnd
Ad-hoc run ResourceLoader validation for user-supplied JavaScript.
Definition jsparse.php:36
execute()
Do the actual work.
Definition jsparse.php:46
__construct()
Default constructor.
Definition jsparse.php:40
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:78