Fix names of parsercache_selective_* stats
[mediawiki.git] / maintenance / jsparse.php
blob777e5b5d010154720b5fcfda8393dd0d0d951b91
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup Maintenance
22 // @codeCoverageIgnoreStart
23 require_once __DIR__ . '/Maintenance.php';
24 // @codeCoverageIgnoreEnd
26 /**
27 * Ad-hoc run ResourceLoader validation for user-supplied JavaScript.
29 * Matches the behaviour of ResourceLoader\Module::validateScriptFile, currently
30 * powered by the the Peast library.
32 * @ingroup Maintenance
34 class JSParseHelper extends Maintenance {
35 /** @var int */
36 public $errs = 0;
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 );
44 public function execute() {
45 $files = $this->getArgs();
47 foreach ( $files as $filename ) {
48 $js = $filename === '-'
49 ? stream_get_contents( STDIN )
50 // phpcs:ignore Generic.PHP.NoSilencedErrors
51 : @file_get_contents( $filename );
52 if ( $js === false ) {
53 $this->output( "$filename ERROR: could not read file\n" );
54 $this->errs++;
55 continue;
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;
66 $this->output( "$filename OK\n" );
69 if ( $this->errs > 0 ) {
70 $this->fatalError( 'Failed.' );
75 // @codeCoverageIgnoreStart
76 $maintClass = JSParseHelper::class;
77 require_once RUN_MAINTENANCE_IF_MAIN;
78 // @codeCoverageIgnoreEnd