~malept/python-jsutils/trunk

« back to all changes in this revision

Viewing changes to javascript/jslint.py

  • Committer: Mark Lee
  • Date: 2009-04-17 23:06:08 UTC
  • Revision ID: bzr@lazymalevolence.com-20090417230608-5v3j10i315w2tteo
Initial commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2009 Mark Lee <python-jsutils@lazymalevolence.com>
 
2
#
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
#     http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
 
14
 
 
15
'''The interface for handling calls to the JSLint application.
 
16
 
 
17
.. moduleauthor:: Mark Lee <python-jsutils@lazymalevolence.com>
 
18
'''
 
19
 
 
20
import sys
 
21
from spidermonkey import Runtime
 
22
 
 
23
 
 
24
class JSLint:
 
25
    '''Checks JavaScript code for errors and code style violations, using
 
26
       Douglas Crockford's JSLint code.
 
27
 
 
28
    :ivar args: The arguments passed to the JSLint application, controlling
 
29
                what rules it uses.
 
30
    :type args: dict
 
31
    :ivar context: The JavaScript context.
 
32
    :type context: :class:`spidermonkey.Context`
 
33
    '''
 
34
 
 
35
    def __init__(self, path, args):
 
36
        '''Initializes the JSLint object.
 
37
 
 
38
        :param path: The full path to the ``fulljslint.js`` script.
 
39
        :type path: string
 
40
        :param args: The arguments passed to the JSLint application,
 
41
                     controlling what rules it uses.
 
42
        :type args: dict
 
43
        '''
 
44
        self.context = Runtime().new_context()
 
45
        self.args = args
 
46
        script = open(path).read()
 
47
        self.context.execute(script)
 
48
 
 
49
    def run(self, filename):
 
50
        '''Runs JSLint on the file specified.
 
51
 
 
52
        :param filename: If the value is ``'-'``, standard input is used.
 
53
        :type filename: string
 
54
        :raise: :exc:`IOError` if the file cannot be opened.
 
55
        :return: True on success, or a list of errors on failure. See the
 
56
                 JSLint documentation in its source code for details on how
 
57
                 the error objects are structured.
 
58
        :rtype: bool or list
 
59
        '''
 
60
        data = sys.stdin.read() if filename == '-' else open(filename).read()
 
61
        self.context.add_global('data', data)
 
62
        self.context.add_global('args', self.args)
 
63
        result = self.context.execute('JSLINT(data, args)')
 
64
        if result:
 
65
            return True
 
66
        else:
 
67
            errors = []
 
68
            for error in self.context.execute('JSLINT.errors'):
 
69
                if error is None:
 
70
                    continue
 
71
                error.line += 1
 
72
                errors.append(error)
 
73
            return errors