Merge "docs: Fix typo"
[mediawiki.git] / maintenance / jsparse.php
blob815776c203109b324247de9d19d014e83ca07ee8
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 use MediaWiki\Maintenance\Maintenance;
24 // @codeCoverageIgnoreStart
25 require_once __DIR__ . '/Maintenance.php';
26 // @codeCoverageIgnoreEnd
28 /**
29 * Ad-hoc run ResourceLoader validation for user-supplied JavaScript.
31 * Matches the behaviour of ResourceLoader\Module::validateScriptFile, currently
32 * powered by the the Peast library.
34 * @ingroup Maintenance
36 class JSParseHelper extends Maintenance {
37 /** @var int */
38 public $errs = 0;
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 );
46 public function execute() {
47 $files = $this->getArgs();
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;
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;
68 $this->output( "$filename OK\n" );
71 if ( $this->errs > 0 ) {
72 $this->fatalError( 'Failed.' );
77 // @codeCoverageIgnoreStart
78 $maintClass = JSParseHelper::class;
79 require_once RUN_MAINTENANCE_IF_MAIN;
80 // @codeCoverageIgnoreEnd