public function execute() { if ($this->hasArg()) { $files = $this->mArgs; } else { $this->maybeHelp(true); // @todo fixme this is a lame API :) exit(1); // it should exit from the above first... } $parser = new JSParser(); foreach ($files as $filename) { wfSuppressWarnings(); $js = file_get_contents($filename); wfRestoreWarnings(); if ($js === false) { $this->output("{$filename} ERROR: could not read file\n"); $this->errs++; continue; } try { $parser->parse($js, $filename, 1); } catch (Exception $e) { $this->errs++; $this->output("{$filename} ERROR: " . $e->getMessage() . "\n"); continue; } $this->output("{$filename} OK\n"); } if ($this->errs > 0) { exit(1); } }
/** * @dataProvider provideCases * @covers JavaScriptMinifier::minify */ public function testJavaScriptMinifierOutput($code, $expectedOutput) { $minified = JavaScriptMinifier::minify($code); // JSMin+'s parser will throw an exception if output is not valid JS. // suppression of warnings needed for stupid crap wfSuppressWarnings(); $parser = new JSParser(); wfRestoreWarnings(); $parser->parse($minified, 'minify-test.js', 1); $this->assertEquals($expectedOutput, $minified, "Minified output should be in the form expected."); }
/** * Compiles code and returns it * @return string */ public function compile() { if ($this->compiled === NULL) { $parser = new JSParser(); list($ok, $result, $error) = $parser->__invoke($this->code, array('file' => $this->file)); if (!$ok) { throw new Exception("Syntax error " . ($this->file !== NULL ? " in {$this->file} " : "") . "on {$error->line}:{$error->column}, expected " . implode(', ', $error->expected)); } $compiler = new JSCompiler(); $this->compiled = $compiler->__invoke($result); } return $this->compiled; }
} } return $options; } $options = buildOptions($argv); if (!file_exists($options['-o'])) { touch($options['-o']); } if (!is_writable($options['-o'])) { die("Invalid output file name. Make sure it exists and is writable."); } $inputFiles = $options['files']; if (empty($inputFiles)) { die("You did not provide any input file."); } $poeditParser = new PoeditParser($options['-o']); $poeditParser->parse(); $errors = array(); foreach ($inputFiles as $f) { if (!is_readable($f) || !preg_match('#\\.js$#', $f)) { $errors[] = "{$f} is not a valid javascript file."; continue; } $jsparser = new JSParser($f, explode(' ', $options['-k'])); $jsStrings = $jsparser->parse(); $poeditParser->merge($jsStrings); } if (!empty($errors)) { echo "\nThe following errors occured:\n" . implode("\n", $errors) . "\n"; } $poeditParser->save();
function load() { $this->messages = array(); $this->errors = array(); $contents = file_get_contents($this->fileName); if (!$contents) { $this->errors[] = array('file' => $this->fileName, 'line' => 0, 'type' => 'ERROR', 'message' => 'Could not read file contents or file is empty'); } $this->fileContents = $contents; $parser = new JSParser(); try { $tree = $parser->parse($contents, $this->fileName, 1); $this->fileTree = $tree; $this->descend($tree); } catch (Exception $e) { // We'll save this to cache to avoid having to validate broken JS over and over... $err = $e->getMessage(); $this->errors[] = array('file' => $this->fileName, 'line' => 0, 'type' => 'ERROR', 'message' => 'Parse failed: ' . $err); return false; } return array_merge($this->messages, $this->errors); }