Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
JSParseHelper
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
42
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Maintenance
20 */
21
22require_once __DIR__ . '/Maintenance.php';
23
24/**
25 * Ad-hoc run ResourceLoader validation for user-supplied JavaScript.
26 *
27 * Matches the behaviour of ResourceLoader\Module::validateScriptFile, currently
28 * powered by the the Peast library.
29 *
30 * @ingroup Maintenance
31 */
32class JSParseHelper extends Maintenance {
33    public $errs = 0;
34
35    public function __construct() {
36        parent::__construct();
37        $this->addDescription( 'Validate syntax of JavaScript files' );
38        $this->addArg( 'file(s)', 'JavaScript files or "-" to read stdin', true );
39    }
40
41    public function execute() {
42        $files = $this->getArgs();
43
44        foreach ( $files as $filename ) {
45            $js = $filename === '-'
46                ? stream_get_contents( STDIN )
47                // phpcs:ignore Generic.PHP.NoSilencedErrors
48                : @file_get_contents( $filename );
49            if ( $js === false ) {
50                $this->output( "$filename ERROR: could not read file\n" );
51                $this->errs++;
52                continue;
53            }
54
55            try {
56                Peast\Peast::ES2016( $js )->parse();
57            } catch ( Exception $e ) {
58                $this->errs++;
59                $this->output( "$filename ERROR: " . get_class( $e ) . ": " . $e->getMessage() . "\n" );
60                continue;
61            }
62
63            $this->output( "$filename OK\n" );
64        }
65
66        if ( $this->errs > 0 ) {
67            $this->fatalError( 'Failed.' );
68        }
69    }
70}
71
72$maintClass = JSParseHelper::class;
73require_once RUN_MAINTENANCE_IF_MAIN;