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
19 * @ingroup Maintenance
22 use MediaWiki\Maintenance\Maintenance
;
24 // @codeCoverageIgnoreStart
25 require_once __DIR__
. '/Maintenance.php';
26 // @codeCoverageIgnoreEnd
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
{
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" );
61 Peast\Peast
::ES2016( $js )->parse();
62 } catch ( Exception
$e ) {
64 $this->output( "$filename ERROR: " . get_class( $e ) . ": " . $e->getMessage() . "\n" );
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