2011-07-11 21:09:45 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-07-25 19:31:06 +00:00
|
|
|
* @file
|
2011-07-11 21:09:45 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2011-07-11 21:09:45 +00:00
|
|
|
|
2012-07-25 19:31:06 +00:00
|
|
|
/**
|
2023-10-03 00:01:22 +00:00
|
|
|
* Ad-hoc run ResourceLoader validation for user-supplied JavaScript.
|
|
|
|
|
*
|
|
|
|
|
* Matches the behaviour of ResourceLoader\Module::validateScriptFile, currently
|
2023-10-03 01:22:48 +00:00
|
|
|
* powered by the the Peast library.
|
2012-07-25 19:31:06 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2011-07-11 21:09:45 +00:00
|
|
|
class JSParseHelper extends Maintenance {
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var int */
|
2012-09-14 18:57:14 +00:00
|
|
|
public $errs = 0;
|
2011-07-11 21:09:45 +00:00
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2023-10-03 00:01:22 +00:00
|
|
|
$this->addDescription( 'Validate syntax of JavaScript files' );
|
2024-08-26 09:42:37 +00:00
|
|
|
$this->addArg( 'file(s)', 'JavaScript files or "-" to read stdin', true, true );
|
2011-07-11 21:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2023-03-14 14:23:19 +00:00
|
|
|
$files = $this->getArgs();
|
2011-07-11 21:09:45 +00:00
|
|
|
|
|
|
|
|
foreach ( $files as $filename ) {
|
2023-10-03 00:01:22 +00:00
|
|
|
$js = $filename === '-'
|
|
|
|
|
? stream_get_contents( STDIN )
|
|
|
|
|
// phpcs:ignore Generic.PHP.NoSilencedErrors
|
|
|
|
|
: @file_get_contents( $filename );
|
2013-04-27 11:23:52 +00:00
|
|
|
if ( $js === false ) {
|
2011-07-11 21:09:45 +00:00
|
|
|
$this->output( "$filename ERROR: could not read file\n" );
|
|
|
|
|
$this->errs++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2023-10-03 01:22:48 +00:00
|
|
|
Peast\Peast::ES2016( $js )->parse();
|
2013-04-27 11:23:52 +00:00
|
|
|
} catch ( Exception $e ) {
|
2011-07-11 21:09:45 +00:00
|
|
|
$this->errs++;
|
2023-10-03 01:22:48 +00:00
|
|
|
$this->output( "$filename ERROR: " . get_class( $e ) . ": " . $e->getMessage() . "\n" );
|
2011-07-11 21:09:45 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->output( "$filename OK\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-27 11:23:52 +00:00
|
|
|
if ( $this->errs > 0 ) {
|
2021-08-30 18:54:47 +00:00
|
|
|
$this->fatalError( 'Failed.' );
|
2011-07-11 21:09:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = JSParseHelper::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|