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
4 # found in the LICENSE file.
6 """Runs Closure compiler on a JavaScript file to check for errors."""
20 class Checker(object):
21 """Runs the Closure compiler on a given source file to typecheck it."""
23 _COMMON_CLOSURE_ARGS
= [
24 "--accept_const_keyword",
25 "--jscomp_error=accessControls",
26 "--jscomp_error=ambiguousFunctionDecl",
27 "--jscomp_error=checkStructDictInheritance",
28 "--jscomp_error=checkTypes",
29 "--jscomp_error=checkVars",
30 "--jscomp_error=constantProperty",
31 "--jscomp_error=deprecated",
32 "--jscomp_error=externsValidation",
33 "--jscomp_error=globalThis",
34 "--jscomp_error=invalidCasts",
35 "--jscomp_error=missingProperties",
36 "--jscomp_error=missingReturn",
37 "--jscomp_error=nonStandardJsDocs",
38 "--jscomp_error=suspiciousCode",
39 "--jscomp_error=undefinedNames",
40 "--jscomp_error=undefinedVars",
41 "--jscomp_error=unknownDefines",
42 "--jscomp_error=uselessCode",
43 "--jscomp_error=visibility",
44 "--language_in=ECMASCRIPT5_STRICT",
45 "--summary_detail_level=3",
46 "--compilation_level=SIMPLE_OPTIMIZATIONS",
47 "--source_map_format=V3",
50 # These are the extra flags used when compiling in strict mode.
51 # Flags that are normally disabled are turned on for strict mode.
52 _STRICT_CLOSURE_ARGS
= [
53 "--jscomp_error=reportUnknownTypes",
54 "--jscomp_error=duplicate",
55 "--jscomp_error=misplacedTypeAnnotation",
58 _DISABLED_CLOSURE_ARGS
= [
59 # TODO(dbeam): happens when the same file is <include>d multiple times.
60 "--jscomp_off=duplicate",
61 # TODO(fukino): happens when cr.defineProperty() has a type annotation.
62 # Avoiding parse-time warnings needs 2 pass compiling. crbug.com/421562.
63 "--jscomp_off=misplacedTypeAnnotation",
71 "-XX:+TieredCompilation"
74 def __init__(self
, verbose
=False, strict
=False):
77 verbose: Whether this class should output diagnostic messages.
78 strict: Whether the Closure Compiler should be invoked more strictly.
80 current_dir
= os
.path
.join(os
.path
.dirname(__file__
))
81 self
._runner
_jar
= os
.path
.join(current_dir
, "runner", "runner.jar")
83 self
._verbose
= verbose
85 self
._error
_filter
= error_filter
.PromiseErrorFilter()
88 """Deletes any temp files this class knows about."""
89 if not self
._temp
_files
:
92 self
._log
_debug
("Deleting temp files: %s" % ", ".join(self
._temp
_files
))
93 for f
in self
._temp
_files
:
97 def _log_debug(self
, msg
, error
=False):
98 """Logs |msg| to stdout if --verbose/-v is passed when invoking this script.
101 msg: A debug message to log.
104 print "(INFO) %s" % msg
106 def _log_error(self
, msg
):
107 """Logs |msg| to stderr regardless of --flags.
110 msg: An error message to log.
112 print >> sys
.stderr
, "(ERROR) %s" % msg
114 def _common_args(self
):
115 """Returns an array of the common closure compiler args."""
117 return self
._COMMON
_CLOSURE
_ARGS
+ self
._STRICT
_CLOSURE
_ARGS
118 return self
._COMMON
_CLOSURE
_ARGS
+ self
._DISABLED
_CLOSURE
_ARGS
120 def _run_jar(self
, jar
, args
):
121 """Runs a .jar from the command line with arguments.
124 jar: A file path to a .jar file
125 args: A list of command line arguments to be passed when running the .jar.
128 (exit_code, stderr) The exit code of the command (e.g. 0 for success) and
129 the stderr collected while running |jar| (as a string).
131 shell_command
= " ".join(self
._JAR
_COMMAND
+ [jar
] + args
)
132 self
._log
_debug
("Running jar: %s" % shell_command
)
134 devnull
= open(os
.devnull
, "w")
135 kwargs
= {"stdout": devnull
, "stderr": subprocess
.PIPE
, "shell": True}
136 process
= subprocess
.Popen(shell_command
, **kwargs
)
137 _
, stderr
= process
.communicate()
138 return process
.returncode
, stderr
140 def _get_line_number(self
, match
):
141 """When chrome is built, it preprocesses its JavaScript from:
143 <include src="blah.js">
148 /* contents of blah.js inlined */
151 Because Closure Compiler requires this inlining already be done (as
152 <include> isn't valid JavaScript), this script creates temporary files to
153 expand all the <include>s.
155 When type errors are hit in temporary files, a developer doesn't know the
156 original source location to fix. This method maps from /tmp/file:300 back to
157 /original/source/file:100 so fixing errors is faster for developers.
160 match: A re.MatchObject from matching against a line number regex.
163 The fixed up /file and :line number.
165 real_file
= self
._processor
.get_file_from_line(match
.group(1))
166 return "%s:%d" % (os
.path
.abspath(real_file
.file), real_file
.line_number
)
168 def _filter_errors(self
, errors
):
169 """Removes some extraneous errors. For example, we ignore:
171 Variable x first declared in /tmp/expanded/file
173 Because it's just a duplicated error (it'll only ever show up 2+ times).
174 We also ignore Promose-based errors:
176 found : function (VolumeInfo): (Promise<(DirectoryEntry|null)>|null)
177 required: (function (Promise<VolumeInfo>): ?|null|undefined)
179 as templates don't work with Promises in all cases yet. See
180 https://github.com/google/closure-compiler/issues/715 for details.
183 errors: A list of string errors extracted from Closure Compiler output.
186 A slimmer, sleeker list of relevant errors (strings).
188 first_declared_in
= lambda e
: " first declared in " not in e
189 return self
._error
_filter
.filter(filter(first_declared_in
, errors
))
191 def _fix_up_error(self
, error
):
192 """Reverse the effects that funky <include> preprocessing steps have on
196 error: A Closure compiler error (2 line string with error and source).
199 The fixed up error string.
201 expanded_file
= self
._expanded
_file
202 fixed
= re
.sub("%s:(\d+)" % expanded_file
, self
._get
_line
_number
, error
)
203 return fixed
.replace(expanded_file
, os
.path
.abspath(self
._file
_arg
))
205 def _format_errors(self
, errors
):
206 """Formats Closure compiler errors to easily spot compiler output.
209 errors: A list of strings extracted from the Closure compiler's output.
212 A formatted output string.
214 contents
= "\n## ".join("\n\n".join(errors
).splitlines())
215 return "## %s" % contents
if contents
else ""
217 def _create_temp_file(self
, contents
):
218 """Creates an owned temporary file with |contents|.
221 content: A string of the file contens to write to a temporary file.
224 The filepath of the newly created, written, and closed temporary file.
226 with tempfile
.NamedTemporaryFile(mode
="wt", delete
=False) as tmp_file
:
227 self
._temp
_files
.append(tmp_file
.name
)
228 tmp_file
.write(contents
)
231 def _run_js_check(self
, sources
, out_file
=None, externs
=None):
232 """Check |sources| for type errors.
235 sources: Files to check.
236 externs: @extern files that inform the compiler about custom globals.
239 (errors, stderr) A parsed list of errors (strings) found by the compiler
240 and the raw stderr (as a string).
242 args
= ["--js=%s" % s
for s
in sources
]
245 args
+= ["--js_output_file=%s" % out_file
]
246 args
+= ["--create_source_map=%s.map" % out_file
]
249 args
+= ["--externs=%s" % e
for e
in externs
]
251 args_file_content
= " %s" % " ".join(self
._common
_args
() + args
)
252 self
._log
_debug
("Args: %s" % args_file_content
.strip())
254 args_file
= self
._create
_temp
_file
(args_file_content
)
255 self
._log
_debug
("Args file: %s" % args_file
)
257 runner_args
= ["--compiler-args-file=%s" % args_file
]
258 _
, stderr
= self
._run
_jar
(self
._runner
_jar
, runner_args
)
260 errors
= stderr
.strip().split("\n\n")
261 maybe_summary
= errors
.pop()
263 if re
.search(".*error.*warning.*typed", maybe_summary
):
264 self
._log
_debug
("Summary: %s" % maybe_summary
)
266 # Not a summary. Running the jar failed. Bail.
267 self
._log
_error
(stderr
)
271 return errors
, stderr
273 def check(self
, source_file
, out_file
=None, depends
=None, externs
=None):
274 """Closure compiler |source_file| while checking for errors.
277 source_file: A file to check.
278 out_file: A file where the compiled output is written to.
279 depends: Files that |source_file| requires to run (e.g. earlier <script>).
280 externs: @extern files that inform the compiler about custom globals.
283 (found_errors, stderr) A boolean indicating whether errors were found and
284 the raw Closure compiler stderr (as a string).
286 self
._log
_debug
("FILE: %s" % source_file
)
288 if source_file
.endswith("_externs.js"):
289 self
._log
_debug
("Skipping externs: %s" % source_file
)
292 self
._file
_arg
= source_file
294 cwd
, tmp_dir
= os
.getcwd(), tempfile
.gettempdir()
295 rel_path
= lambda f
: os
.path
.join(os
.path
.relpath(cwd
, tmp_dir
), f
)
297 depends
= depends
or []
298 includes
= [rel_path(f
) for f
in depends
+ [source_file
]]
299 contents
= ['<include src="%s">' % i
for i
in includes
]
300 meta_file
= self
._create
_temp
_file
("\n".join(contents
))
301 self
._log
_debug
("Meta file: %s" % meta_file
)
303 self
._processor
= processor
.Processor(meta_file
)
304 self
._expanded
_file
= self
._create
_temp
_file
(self
._processor
.contents
)
305 self
._log
_debug
("Expanded file: %s" % self
._expanded
_file
)
307 errors
, stderr
= self
._run
_js
_check
([self
._expanded
_file
],
308 out_file
=out_file
, externs
=externs
)
309 filtered_errors
= self
._filter
_errors
(errors
)
310 fixed_errors
= map(self
._fix
_up
_error
, filtered_errors
)
311 output
= self
._format
_errors
(fixed_errors
)
314 prefix
= "\n" if output
else ""
315 self
._log
_error
("Error in: %s%s%s" % (source_file
, prefix
, output
))
317 self
._log
_debug
("Output: %s" % output
)
320 return bool(fixed_errors
), stderr
322 def check_multiple(self
, sources
):
323 """Closure compile a set of files and check for errors.
326 sources: An array of files to check.
329 (found_errors, stderr) A boolean indicating whether errors were found and
330 the raw Closure Compiler stderr (as a string).
332 errors
, stderr
= self
._run
_js
_check
(sources
, [])
334 return bool(errors
), stderr
337 if __name__
== "__main__":
338 parser
= argparse
.ArgumentParser(
339 description
="Typecheck JavaScript using Closure compiler")
340 parser
.add_argument("sources", nargs
=argparse
.ONE_OR_MORE
,
341 help="Path to a source file to typecheck")
342 single_file_group
= parser
.add_mutually_exclusive_group()
343 single_file_group
.add_argument("--single-file", dest
="single_file",
345 help="Process each source file individually")
346 single_file_group
.add_argument("--no-single-file", dest
="single_file",
347 action
="store_false",
348 help="Process all source files as a group")
349 parser
.add_argument("-d", "--depends", nargs
=argparse
.ZERO_OR_MORE
)
350 parser
.add_argument("-e", "--externs", nargs
=argparse
.ZERO_OR_MORE
)
351 parser
.add_argument("-o", "--out_file",
352 help="A file where the compiled output is written to")
353 parser
.add_argument("-v", "--verbose", action
="store_true",
354 help="Show more information as this script runs")
355 parser
.add_argument("--strict", action
="store_true",
356 help="Enable strict type checking")
357 parser
.add_argument("--success-stamp",
358 help="Timestamp file to update upon success")
360 parser
.set_defaults(single_file
=True, strict
=False)
361 opts
= parser
.parse_args()
363 depends
= opts
.depends
or []
364 externs
= opts
.externs
or set()
367 out_dir
= os
.path
.dirname(opts
.out_file
)
368 if not os
.path
.exists(out_dir
):
371 checker
= Checker(verbose
=opts
.verbose
, strict
=opts
.strict
)
373 for source
in opts
.sources
:
374 depends
, externs
= build
.inputs
.resolve_recursive_dependencies(
375 source
, depends
, externs
)
376 found_errors
, _
= checker
.check(source
, out_file
=opts
.out_file
,
377 depends
=depends
, externs
=externs
)
381 found_errors
, stderr
= checker
.check_multiple(opts
.sources
)
386 if opts
.success_stamp
:
387 with
open(opts
.success_stamp
, "w"):
388 os
.utime(opts
.success_stamp
, None)