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."""
17 class Checker(object):
18 """Runs the Closure compiler on a given source file and returns the
21 _COMMON_CLOSURE_ARGS
= [
22 "--accept_const_keyword",
23 "--jscomp_error=accessControls",
24 "--jscomp_error=ambiguousFunctionDecl",
25 "--jscomp_error=checkStructDictInheritance",
26 "--jscomp_error=checkTypes",
27 "--jscomp_error=checkVars",
28 "--jscomp_error=constantProperty",
29 "--jscomp_error=deprecated",
30 "--jscomp_error=externsValidation",
31 "--jscomp_error=globalThis",
32 "--jscomp_error=invalidCasts",
33 "--jscomp_error=misplacedTypeAnnotation",
34 "--jscomp_error=missingProperties",
35 "--jscomp_error=missingReturn",
36 "--jscomp_error=nonStandardJsDocs",
37 "--jscomp_error=suspiciousCode",
38 "--jscomp_error=undefinedNames",
39 "--jscomp_error=undefinedVars",
40 "--jscomp_error=unknownDefines",
41 "--jscomp_error=uselessCode",
42 "--jscomp_error=visibility",
43 # TODO(dbeam): happens when the same file is <include>d multiple times.
44 "--jscomp_off=duplicate",
45 "--language_in=ECMASCRIPT5_STRICT",
46 "--summary_detail_level=3",
54 "-XX:+TieredCompilation"
59 def __init__(self
, verbose
=False):
60 current_dir
= os
.path
.join(os
.path
.dirname(__file__
))
61 self
._compiler
_jar
= os
.path
.join(current_dir
, "lib", "compiler.jar")
62 self
._runner
_jar
= os
.path
.join(current_dir
, "runner", "runner.jar")
64 self
._verbose
= verbose
67 if not self
._temp
_files
:
70 self
._debug
("Deleting temporary files: %s" % ", ".join(self
._temp
_files
))
71 for f
in self
._temp
_files
:
75 def _debug(self
, msg
, error
=False):
77 print "(INFO) %s" % msg
79 def _error(self
, msg
):
80 print >> sys
.stderr
, "(ERROR) %s" % msg
83 def _run_command(self
, cmd
):
84 """Runs a shell command.
87 cmd: A list of tokens to be joined into a shell command.
90 True if the exit code was 0, else False.
92 cmd_str
= " ".join(cmd
)
93 self
._debug
("Running command: %s" % cmd_str
)
95 devnull
= open(os
.devnull
, "w")
96 return subprocess
.Popen(
97 cmd_str
, stdout
=devnull
, stderr
=subprocess
.PIPE
, shell
=True)
99 def _check_java_path(self
):
100 """Checks that `java` is on the system path."""
101 if not self
._found
_java
:
102 proc
= self
._run
_command
(["which", "java"])
104 if proc
.returncode
== 0:
105 self
._found
_java
= True
107 self
._error
("Cannot find java (`which java` => %s)" % proc
.returncode
)
109 return self
._found
_java
111 def _run_jar(self
, jar
, args
=None):
113 self
._check
_java
_path
()
114 return self
._run
_command
(self
._JAR
_COMMAND
+ [jar
] + args
)
116 def _fix_line_number(self
, match
):
117 """Changes a line number from /tmp/file:300 to /orig/file:100.
120 match: A re.MatchObject from matching against a line number regex.
123 The fixed up /file and :line number.
125 real_file
= self
._processor
.get_file_from_line(match
.group(1))
126 return "%s:%d" % (os
.path
.abspath(real_file
.file), real_file
.line_number
)
128 def _fix_up_error(self
, error
):
129 """Filter out irrelevant errors or fix line numbers.
132 error: A Closure compiler error (2 line string with error and source).
135 The fixed up erorr string (blank if it should be ignored).
137 if " first declared in " in error
:
138 # Ignore "Variable x first declared in /same/file".
141 expanded_file
= self
._expanded
_file
142 fixed
= re
.sub("%s:(\d+)" % expanded_file
, self
._fix
_line
_number
, error
)
143 return fixed
.replace(expanded_file
, os
.path
.abspath(self
._file
_arg
))
145 def _format_errors(self
, errors
):
146 """Formats Closure compiler errors to easily spot compiler output."""
147 errors
= filter(None, errors
)
148 contents
= "\n## ".join("\n\n".join(errors
).splitlines())
149 return "## %s" % contents
if contents
else ""
151 def _create_temp_file(self
, contents
):
152 with tempfile
.NamedTemporaryFile(mode
="wt", delete
=False) as tmp_file
:
153 self
._temp
_files
.append(tmp_file
.name
)
154 tmp_file
.write(contents
)
157 def check(self
, source_file
, depends
=None, externs
=None):
158 """Closure compile a file and check for errors.
161 source_file: A file to check.
162 depends: Other files that would be included with a <script> earlier in
164 externs: @extern files that inform the compiler about custom globals.
167 (exitcode, output) The exit code of the Closure compiler (as a number)
168 and its output (as a string).
170 depends
= depends
or []
171 externs
= externs
or []
173 if not self
._check
_java
_path
():
176 self
._debug
("FILE: %s" % source_file
)
178 if source_file
.endswith("_externs.js"):
179 self
._debug
("Skipping externs: %s" % source_file
)
182 self
._file
_arg
= source_file
184 tmp_dir
= tempfile
.gettempdir()
185 rel_path
= lambda f
: os
.path
.join(os
.path
.relpath(os
.getcwd(), tmp_dir
), f
)
187 includes
= [rel_path(f
) for f
in depends
+ [source_file
]]
188 contents
= ['<include src="%s">' % i
for i
in includes
]
189 meta_file
= self
._create
_temp
_file
("\n".join(contents
))
190 self
._debug
("Meta file: %s" % meta_file
)
192 self
._processor
= processor
.Processor(meta_file
)
193 self
._expanded
_file
= self
._create
_temp
_file
(self
._processor
.contents
)
194 self
._debug
("Expanded file: %s" % self
._expanded
_file
)
196 args
= ["--js=%s" % self
._expanded
_file
]
197 args
+= ["--externs=%s" % e
for e
in externs
]
198 args_file_content
= " %s" % " ".join(self
._COMMON
_CLOSURE
_ARGS
+ args
)
199 self
._debug
("Args: %s" % args_file_content
.strip())
201 args_file
= self
._create
_temp
_file
(args_file_content
)
202 self
._debug
("Args file: %s" % args_file
)
204 runner_args
= ["--compiler-args-file=%s" % args_file
]
205 runner_cmd
= self
._run
_jar
(self
._runner
_jar
, args
=runner_args
)
206 (_
, stderr
) = runner_cmd
.communicate()
208 errors
= stderr
.strip().split("\n\n")
209 self
._debug
("Summary: %s" % errors
.pop())
211 output
= self
._format
_errors
(map(self
._fix
_up
_error
, errors
))
212 if runner_cmd
.returncode
:
213 self
._error
("Error in: %s%s" % (source_file
, "\n" + output
if output
else ""))
215 self
._debug
("Output: %s" % output
)
219 return runner_cmd
.returncode
, output
222 if __name__
== "__main__":
223 parser
= argparse
.ArgumentParser(
224 description
="Typecheck JavaScript using Closure compiler")
225 parser
.add_argument("sources", nargs
=argparse
.ONE_OR_MORE
,
226 help="Path to a source file to typecheck")
227 parser
.add_argument("-d", "--depends", nargs
=argparse
.ZERO_OR_MORE
)
228 parser
.add_argument("-e", "--externs", nargs
=argparse
.ZERO_OR_MORE
)
229 parser
.add_argument("-o", "--out_file", help="A place to output results")
230 parser
.add_argument("-v", "--verbose", action
="store_true",
231 help="Show more information as this script runs")
232 opts
= parser
.parse_args()
234 checker
= Checker(verbose
=opts
.verbose
)
235 for source
in opts
.sources
:
236 exit
, _
= checker
.check(source
, depends
=opts
.depends
, externs
=opts
.externs
)
241 out_dir
= os
.path
.dirname(opts
.out_file
)
242 if not os
.path
.exists(out_dir
):
244 # TODO(dbeam): write compiled file to |opts.out_file|.
245 open(opts
.out_file
, "w").write("")