3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
7 '''Uses the closure compiler to check syntax and semantics of a js module
15 _SCRIPT_DIR
= os
.path
.dirname(os
.path
.abspath(__file__
))
16 _CHROME_SOURCE_DIR
= os
.path
.normpath(
18 _SCRIPT_DIR
, *[os
.path
.pardir
] * 6))
21 _CLOSURE_COMPILER_JAR
= os
.path
.join(
22 _CHROME_SOURCE_DIR
, 'third_party', 'closure_compiler', 'compiler',
25 # List of compilation errors to enable with the --jscomp_errors flag.
26 _JSCOMP_ERRORS
= [ 'accessControls', 'checkTypes', 'checkVars', 'invalidCasts',
27 'missingProperties', 'undefinedNames', 'undefinedVars',
30 _java_executable
= 'java'
34 print >>sys
.stderr
, msg
38 def _ExecuteCommand(args
, ignore_exit_status
=False):
40 return subprocess
.check_output(args
, stderr
=subprocess
.STDOUT
)
41 except subprocess
.CalledProcessError
as e
:
42 if ignore_exit_status
and e
.returncode
> 0:
44 _Error('%s\nCommand \'%s\' returned non-zero exit status %d' %
45 (e
.output
, ' '.join(e
.cmd
), e
.returncode
))
46 except (OSError, IOError) as e
:
47 _Error('Error executing %s: %s' % (_java_executable
, str(e
)))
51 global _java_executable
52 java_home
= os
.environ
.get('JAVAHOME')
53 if java_home
is not None:
54 _java_executable
= os
.path
.join(java_home
, 'bin', 'java')
55 output
= _ExecuteCommand([_java_executable
, '-version'])
56 match
= re
.search(r
'version "(?:\d+)\.(\d+)', output
)
57 if match
is None or int(match
.group(1)) < 7:
58 _Error('Java 7 or later is required: \n%s' % output
)
63 def RunCompiler(js_files
, externs
=[]):
64 args
= [_java_executable
, '-jar', _CLOSURE_COMPILER_JAR
]
65 args
.extend(['--compilation_level', 'SIMPLE_OPTIMIZATIONS'])
66 args
.extend(['--jscomp_error=%s' % error
for error
in _JSCOMP_ERRORS
])
67 args
.extend(['--language_in', 'ECMASCRIPT5'])
68 args
.extend(['--externs=%s' % extern
for extern
in externs
])
69 args
.extend(['--js=%s' % js
for js
in js_files
])
70 args
.extend(['--js_output_file', '/dev/null'])
71 output
= _ExecuteCommand(args
, ignore_exit_status
=True)
72 success
= len(output
) == 0
73 return success
, output