Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.24% |
20 / 21 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
JSParseHelper | |
95.24% |
20 / 21 |
|
50.00% |
1 / 2 |
7 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
6.01 |
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 | |
22 | use MediaWiki\Maintenance\Maintenance; |
23 | |
24 | // @codeCoverageIgnoreStart |
25 | require_once __DIR__ . '/Maintenance.php'; |
26 | // @codeCoverageIgnoreEnd |
27 | |
28 | /** |
29 | * Ad-hoc run ResourceLoader validation for user-supplied JavaScript. |
30 | * |
31 | * Matches the behaviour of ResourceLoader\Module::validateScriptFile, currently |
32 | * powered by the the Peast library. |
33 | * |
34 | * @ingroup Maintenance |
35 | */ |
36 | class JSParseHelper extends Maintenance { |
37 | /** @var int */ |
38 | public $errs = 0; |
39 | |
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 ); |
44 | } |
45 | |
46 | public function execute() { |
47 | $files = $this->getArgs(); |
48 | |
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; |
58 | } |
59 | |
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; |
66 | } |
67 | |
68 | $this->output( "$filename OK\n" ); |
69 | } |
70 | |
71 | if ( $this->errs > 0 ) { |
72 | $this->fatalError( 'Failed.' ); |
73 | } |
74 | } |
75 | } |
76 | |
77 | // @codeCoverageIgnoreStart |
78 | $maintClass = JSParseHelper::class; |
79 | require_once RUN_MAINTENANCE_IF_MAIN; |
80 | // @codeCoverageIgnoreEnd |