Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Runs Closure compiler on JavaScript files to check for errors and produce | 6 """Runs Closure compiler on JavaScript files to check for errors and produce |
| 7 minified output.""" | 7 minified output.""" |
| 8 | 8 |
| 9 import argparse | 9 import argparse |
| 10 import os | 10 import os |
| 11 import re | 11 import re |
| 12 import subprocess | 12 import subprocess |
| 13 import sys | 13 import sys |
| 14 import tempfile | 14 import tempfile |
| 15 | 15 |
| 16 import build.inputs | 16 import build.inputs |
| 17 import processor | 17 import processor |
| 18 import error_filter | 18 import error_filter |
| 19 | 19 |
|
Dan Beam
2015/05/12 04:55:55
extra \n (\n\n between file-level globals)
Jeremy Klein
2015/05/12 05:46:07
Done.
| |
| 20 _CURRENT_DIR = os.path.join(os.path.dirname(__file__)) | |
| 21 | |
| 20 | 22 |
| 21 class Checker(object): | 23 class Checker(object): |
| 22 """Runs the Closure compiler on given source files to typecheck them | 24 """Runs the Closure compiler on given source files to typecheck them |
| 23 and produce minified output.""" | 25 and produce minified output.""" |
| 24 | 26 |
| 27 _COMMON_JSCOMP_ERRORS = [ | |
| 28 "accessControls", | |
| 29 "ambiguousFunctionDecl", | |
| 30 "checkStructDictInheritance", | |
| 31 "checkTypes", | |
| 32 "checkVars", | |
| 33 "constantProperty", | |
| 34 "deprecated", | |
| 35 "externsValidation", | |
| 36 "globalThis", | |
| 37 "invalidCasts", | |
| 38 "missingProperties", | |
| 39 "missingReturn", | |
| 40 "nonStandardJsDocs", | |
| 41 "suspiciousCode", | |
| 42 "undefinedNames", | |
| 43 "undefinedVars", | |
| 44 "unknownDefines", | |
| 45 "uselessCode", | |
| 46 "visibility", | |
| 47 ] | |
| 48 | |
| 49 # Extra @jsDocAnnotations used when compiling polymer code. | |
| 50 _POLYMER_EXTRA_ANNOTATIONS = [ | |
| 51 "attribute", | |
| 52 "status", | |
| 53 "element", | |
| 54 "homepage", | |
| 55 "submodule", | |
| 56 "group", | |
| 57 ] | |
| 58 | |
| 25 _COMMON_CLOSURE_ARGS = [ | 59 _COMMON_CLOSURE_ARGS = [ |
| 26 "--accept_const_keyword", | 60 "--accept_const_keyword", |
| 27 "--jscomp_error=accessControls", | |
| 28 "--jscomp_error=ambiguousFunctionDecl", | |
| 29 "--jscomp_error=checkStructDictInheritance", | |
| 30 "--jscomp_error=checkTypes", | |
| 31 "--jscomp_error=checkVars", | |
| 32 "--jscomp_error=constantProperty", | |
| 33 "--jscomp_error=deprecated", | |
| 34 "--jscomp_error=externsValidation", | |
| 35 "--jscomp_error=globalThis", | |
| 36 "--jscomp_error=invalidCasts", | |
| 37 "--jscomp_error=missingProperties", | |
| 38 "--jscomp_error=missingReturn", | |
| 39 "--jscomp_error=nonStandardJsDocs", | |
| 40 "--jscomp_error=suspiciousCode", | |
| 41 "--jscomp_error=undefinedNames", | |
| 42 "--jscomp_error=undefinedVars", | |
| 43 "--jscomp_error=unknownDefines", | |
| 44 "--jscomp_error=uselessCode", | |
| 45 "--jscomp_error=visibility", | |
| 46 "--language_in=ECMASCRIPT5_STRICT", | 61 "--language_in=ECMASCRIPT5_STRICT", |
| 47 "--summary_detail_level=3", | 62 "--summary_detail_level=3", |
| 48 "--compilation_level=SIMPLE_OPTIMIZATIONS", | 63 "--compilation_level=SIMPLE_OPTIMIZATIONS", |
| 49 "--source_map_format=V3", | 64 "--source_map_format=V3", |
| 65 "--polymer_pass", | |
| 66 ] + ["--jscomp_error=%s" % err for err in _COMMON_JSCOMP_ERRORS] + [ | |
| 67 "--extra_annotation_name=%s" % a for a in _POLYMER_EXTRA_ANNOTATIONS | |
| 50 ] | 68 ] |
|
Dan Beam
2015/05/12 04:55:55
make it prettier
] + [
"--jscomp_error=%s" % er
Jeremy Klein
2015/05/12 05:46:08
Done.
| |
| 51 | 69 |
| 52 # These are the extra flags used when compiling in strict mode. | 70 # These are the extra flags used when compiling in strict mode. |
| 53 # Flags that are normally disabled are turned on for strict mode. | 71 # Flags that are normally disabled are turned on for strict mode. |
| 54 _STRICT_CLOSURE_ARGS = [ | 72 _STRICT_CLOSURE_ARGS = [ |
| 55 "--jscomp_error=reportUnknownTypes", | 73 "--jscomp_error=reportUnknownTypes", |
| 56 "--jscomp_error=duplicate", | 74 "--jscomp_error=duplicate", |
| 57 "--jscomp_error=misplacedTypeAnnotation", | 75 "--jscomp_error=misplacedTypeAnnotation", |
| 58 ] | 76 ] |
| 59 | 77 |
| 60 _DISABLED_CLOSURE_ARGS = [ | 78 _DISABLED_CLOSURE_ARGS = [ |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 74 ] | 92 ] |
| 75 | 93 |
| 76 _MAP_FILE_FORMAT = "%s.map" | 94 _MAP_FILE_FORMAT = "%s.map" |
| 77 | 95 |
| 78 def __init__(self, verbose=False, strict=False): | 96 def __init__(self, verbose=False, strict=False): |
| 79 """ | 97 """ |
| 80 Args: | 98 Args: |
| 81 verbose: Whether this class should output diagnostic messages. | 99 verbose: Whether this class should output diagnostic messages. |
| 82 strict: Whether the Closure Compiler should be invoked more strictly. | 100 strict: Whether the Closure Compiler should be invoked more strictly. |
| 83 """ | 101 """ |
| 84 current_dir = os.path.join(os.path.dirname(__file__)) | 102 self._runner_jar = os.path.join(_CURRENT_DIR, "runner", "runner.jar") |
| 85 self._runner_jar = os.path.join(current_dir, "runner", "runner.jar") | |
| 86 self._temp_files = [] | 103 self._temp_files = [] |
| 87 self._verbose = verbose | 104 self._verbose = verbose |
| 88 self._strict = strict | 105 self._strict = strict |
| 89 self._error_filter = error_filter.PromiseErrorFilter() | 106 self._error_filter = error_filter.PromiseErrorFilter() |
| 90 | 107 |
| 91 def _nuke_temp_files(self): | 108 def _nuke_temp_files(self): |
| 92 """Deletes any temp files this class knows about.""" | 109 """Deletes any temp files this class knows about.""" |
| 93 if not self._temp_files: | 110 if not self._temp_files: |
| 94 return | 111 return |
| 95 | 112 |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 382 help="Show more information as this script runs") | 399 help="Show more information as this script runs") |
| 383 parser.add_argument("--strict", action="store_true", | 400 parser.add_argument("--strict", action="store_true", |
| 384 help="Enable strict type checking") | 401 help="Enable strict type checking") |
| 385 parser.add_argument("--success-stamp", | 402 parser.add_argument("--success-stamp", |
| 386 help="Timestamp file to update upon success") | 403 help="Timestamp file to update upon success") |
| 387 | 404 |
| 388 parser.set_defaults(single_file=True, strict=False) | 405 parser.set_defaults(single_file=True, strict=False) |
| 389 opts = parser.parse_args() | 406 opts = parser.parse_args() |
| 390 | 407 |
| 391 depends = opts.depends or [] | 408 depends = opts.depends or [] |
| 392 externs = opts.externs or set() | 409 externs = set(opts.externs) or set() |
| 410 | |
| 411 polymer_externs = os.path.join(_CURRENT_DIR, os.pardir, 'polymer', | |
|
Dan Beam
2015/05/12 04:55:55
hack: if you use os.path.dirname(_CURRENT_DIR) ins
Jeremy Klein
2015/05/12 05:46:08
Done.
| |
| 412 'v0_8', 'components-chromium', | |
| 413 'polymer-externs', 'polymer.externs.js') | |
| 414 externs.add(os.path.abspath(polymer_externs)) | |
| 393 | 415 |
| 394 if opts.out_file: | 416 if opts.out_file: |
| 395 out_dir = os.path.dirname(opts.out_file) | 417 out_dir = os.path.dirname(opts.out_file) |
| 396 if not os.path.exists(out_dir): | 418 if not os.path.exists(out_dir): |
| 397 os.makedirs(out_dir) | 419 os.makedirs(out_dir) |
| 398 | 420 |
| 399 checker = Checker(verbose=opts.verbose, strict=opts.strict) | 421 checker = Checker(verbose=opts.verbose, strict=opts.strict) |
| 400 if opts.single_file: | 422 if opts.single_file: |
| 401 for source in opts.sources: | 423 for source in opts.sources: |
| 402 depends, externs = build.inputs.resolve_recursive_dependencies( | 424 depends, externs = build.inputs.resolve_recursive_dependencies( |
| 403 source, depends, externs) | 425 source, depends, externs) |
| 404 found_errors, _ = checker.check(source, out_file=opts.out_file, | 426 found_errors, _ = checker.check(source, out_file=opts.out_file, |
| 405 depends=depends, externs=externs, | 427 depends=depends, externs=externs, |
| 406 output_wrapper=opts.output_wrapper) | 428 output_wrapper=opts.output_wrapper) |
| 407 if found_errors: | 429 if found_errors: |
| 408 sys.exit(1) | 430 sys.exit(1) |
| 409 else: | 431 else: |
| 410 found_errors, stderr = checker.check_multiple( | 432 found_errors, stderr = checker.check_multiple( |
| 411 opts.sources, | 433 opts.sources, |
| 412 out_file=opts.out_file, | 434 out_file=opts.out_file, |
| 413 output_wrapper=opts.output_wrapper) | 435 output_wrapper=opts.output_wrapper) |
| 414 if found_errors: | 436 if found_errors: |
| 415 print stderr | 437 print stderr |
| 416 sys.exit(1) | 438 sys.exit(1) |
| 417 | 439 |
| 418 if opts.success_stamp: | 440 if opts.success_stamp: |
| 419 with open(opts.success_stamp, "w"): | 441 with open(opts.success_stamp, "w"): |
| 420 os.utime(opts.success_stamp, None) | 442 os.utime(opts.success_stamp, None) |
| OLD | NEW |