Esempio n. 1
0
  def Check(self, filename, source=None):
    """Checks the file, printing warnings and errors as they are found.

    Args:
      filename: The name of the file to check.
      source: Optional. The contents of the file.  Can be either a string or
          file-like object.  If omitted, contents will be read from disk from
          the given filename.
    """

    if source is None:
      try:
        f = open(filename)
      except IOError:
        self._error_handler.HandleFile(filename, None)
        self.HandleError(errors.FILE_NOT_FOUND, 'File not found', None)
        self._error_handler.FinishFile()
        return
    else:
      if type(source) in [str, unicode]:
        f = StringIO.StringIO(source)
      else:
        f = source

    try:
      if filename.endswith('.html') or filename.endswith('.htm'):
        self.CheckLines(filename, htmlutil.GetScriptLines(f), True)
      else:
        self.CheckLines(filename, f, False)
    finally:
      f.close()
Esempio n. 2
0
def Run(filename, error_handler, source=None):
    """Tokenize, run passes, and check the given file.

  Args:
    filename: The path of the file to check
    error_handler: The error handler to report errors to.
    source: A file-like object with the file source. If omitted, the file will
      be read from the filename path.
  """
    if not source:
        try:
            source = open(filename)
        except IOError:
            error_handler.HandleFile(filename, None)
            error_handler.HandleError(
                error.Error(errors.FILE_NOT_FOUND, 'File not found'))
            error_handler.FinishFile()
            return

    if _IsHtml(filename):
        source_file = htmlutil.GetScriptLines(source)
    else:
        source_file = source

    token, tokenizer_mode = _Tokenize(source_file)

    error_handler.HandleFile(filename, token)

    # If we did not end in the basic mode, this a failed parse.
    if tokenizer_mode is not javascripttokenizer.JavaScriptModes.TEXT_MODE:
        error_handler.HandleError(
            error.Error(errors.FILE_IN_BLOCK,
                        'File ended in mode "%s".' % tokenizer_mode,
                        _GetLastNonWhiteSpaceToken(token)))

    # Run the ECMA pass
    error_token = None

    ecma_pass = ecmametadatapass.EcmaMetaDataPass()
    error_token = RunMetaDataPass(token, ecma_pass, error_handler, filename)

    is_limited_doc_check = (_IsLimitedDocCheck(filename,
                                               flags.FLAGS.limited_doc_files))

    _RunChecker(token,
                error_handler,
                is_limited_doc_check,
                is_html=_IsHtml(filename),
                stop_token=error_token)

    error_handler.FinishFile()
  def Check(self, filename):
    """Checks the file, printing warnings and errors as they are found.

    Args:
      filename: The name of the file to check.
    """
    try:
      f = open(filename)
    except IOError:
      self.__error_handler.HandleFile(filename, None)
      self.HandleError(errors.FILE_NOT_FOUND, 'File not found', None)
      self.__error_handler.FinishFile()
      return

    try:
      if filename.endswith('.html') or filename.endswith('.htm'):
        self.CheckLines(filename, htmlutil.GetScriptLines(f), True)
      else:
        self.CheckLines(filename, f, False)
    finally:
      f.close()