1
# Copyright 2009 Mark Lee <python-jsutils@lazymalevolence.com>
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
7
# http://www.apache.org/licenses/LICENSE-2.0
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.
15
'''The interface for handling calls to the JSLint application.
17
.. moduleauthor:: Mark Lee <python-jsutils@lazymalevolence.com>
21
from spidermonkey import Runtime
25
'''Checks JavaScript code for errors and code style violations, using
26
Douglas Crockford's JSLint code.
28
:ivar args: The arguments passed to the JSLint application, controlling
31
:ivar context: The JavaScript context.
32
:type context: :class:`spidermonkey.Context`
35
def __init__(self, path, args):
36
'''Initializes the JSLint object.
38
:param path: The full path to the ``fulljslint.js`` script.
40
:param args: The arguments passed to the JSLint application,
41
controlling what rules it uses.
44
self.context = Runtime().new_context()
46
script = open(path).read()
47
self.context.execute(script)
49
def run(self, filename):
50
'''Runs JSLint on the file specified.
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.
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)')
68
for error in self.context.execute('JSLINT.errors'):