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