1#!/usr/bin/env python
2#
3# Copyright 2007 The Closure Linter Authors. All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS-IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Automatically fix simple style guide violations."""
18
19__author__ = 'robbyw@google.com (Robert Walker)'
20
21import StringIO
22import sys
23
24import gflags as flags
25
26from closure_linter import error_fixer
27from closure_linter import runner
28from closure_linter.common import simplefileflags as fileflags
29
30FLAGS = flags.FLAGS
31flags.DEFINE_list('additional_extensions', None, 'List of additional file '
32                  'extensions (not js) that should be treated as '
33                  'JavaScript files.')
34flags.DEFINE_boolean('dry_run', False, 'Do not modify the file, only print it.')
35
36
37def main(argv=None):
38  """Main function.
39
40  Args:
41    argv: Sequence of command line arguments.
42  """
43  if argv is None:
44    argv = flags.FLAGS(sys.argv)
45
46  suffixes = ['.js']
47  if FLAGS.additional_extensions:
48    suffixes += ['.%s' % ext for ext in FLAGS.additional_extensions]
49
50  files = fileflags.GetFileList(argv, 'JavaScript', suffixes)
51
52  output_buffer = None
53  if FLAGS.dry_run:
54    output_buffer = StringIO.StringIO()
55
56  fixer = error_fixer.ErrorFixer(output_buffer)
57
58  # Check the list of files.
59  for filename in files:
60    runner.Run(filename, fixer)
61    if FLAGS.dry_run:
62      print output_buffer.getvalue()
63
64
65if __name__ == '__main__':
66  main()
67