Exemple #1
0
def ParseFunctionsAndComments(source):
  """Run the tokenizer and tracker and return comments and functions found.

  Args:
    source: A source file as a string or file-like object (iterates lines).

  Returns:
    The functions and comments as a tuple.
  """
  start_token = TokenizeSourceAndRunEcmaPass(source)

  tracker = javascriptstatetracker.JavaScriptStateTracker()

  functions = []
  comments = []
  for token in start_token:
    tracker.HandleToken(token, tracker.GetLastNonSpaceToken())

    function = tracker.GetFunction()
    if function and function not in functions:
      functions.append(function)

    comment = tracker.GetDocComment()
    if comment and comment not in comments:
      comments.append(comment)

    tracker.HandleAfterToken(token)

  return functions, comments
Exemple #2
0
 def testGetClosurizedNamespace(self):
     stateTracker = javascriptstatetracker.JavaScriptStateTracker(
         ['package'])
     for identifier, expected_namespace in self.__test_cases.items():
         actual_namespace = stateTracker.GetClosurizedNamespace(identifier)
         self.assertEqual(
             expected_namespace, actual_namespace, 'expected namespace "' +
             str(expected_namespace) + '" for identifier "' +
             str(identifier) + '" but was "' + str(actual_namespace) + '"')
    def _GetInitializedNamespacesInfo(self, token, closurized_namespaces,
                                      ignored_extra_namespaces):
        """Returns a namespaces info initialized with the given token stream."""
        namespaces_info = closurizednamespacesinfo.ClosurizedNamespacesInfo(
            closurized_namespaces=closurized_namespaces,
            ignored_extra_namespaces=ignored_extra_namespaces)
        state_tracker = javascriptstatetracker.JavaScriptStateTracker()

        while token:
            namespaces_info.ProcessToken(token, state_tracker)
            token = token.next

        return namespaces_info
    def testAliasedDoctypes(self):
        """Tests that aliases are correctly expanded within type annotations."""
        start_token = testutil.TokenizeSourceAndRunEcmaPass(_TEST_ALIAS_SCRIPT)
        tracker = javascriptstatetracker.JavaScriptStateTracker()
        tracker.DocFlagPass(start_token, error_handler=None)

        alias_pass = aliaspass.AliasPass(set(['goog', 'myproject']))
        alias_pass.Process(start_token)

        flag_token = _GetTokenByLineAndString(start_token, '@type', 22)
        self.assertEquals(
            'goog.events.Event.<goog.ui.Component,Array<myproject.foo.MyClass>>',
            repr(flag_token.attached_object.jstype))
  def __init__(self, error_handler):
    """Initialize an JavaScriptStyleChecker object.

    Args:
      error_handler: Error handler to pass all errors to
    """
    checkerbase.CheckerBase.__init__(
        self,
        error_handler=error_handler,
        lint_rules=javascriptlintrules.JavaScriptLintRules(),
        state_tracker=javascriptstatetracker.JavaScriptStateTracker(
            closurized_namespaces=flags.FLAGS.closurized_namespaces),
        metadata_pass=ecmametadatapass.EcmaMetaDataPass(),
        limited_doc_files=flags.FLAGS.limited_doc_files)
Exemple #6
0
def _RunChecker(start_token, error_handler,
                limited_doc_checks, is_html,
                stop_token=None):

  state_tracker = javascriptstatetracker.JavaScriptStateTracker()

  style_checker = checker.JavaScriptStyleChecker(
      state_tracker=state_tracker,
      error_handler=error_handler)

  style_checker.Check(start_token,
                      is_html=is_html,
                      limited_doc_checks=limited_doc_checks,
                      stop_token=stop_token)
    def _GetInitializedNamespacesInfo(self, token, closurized_namespaces,
                                      ignored_extra_namespaces):
        """Returns a namespaces info initialized with the given token stream."""
        namespaces_info = closurizednamespacesinfo.ClosurizedNamespacesInfo(
            closurized_namespaces=closurized_namespaces,
            ignored_extra_namespaces=ignored_extra_namespaces)
        state_tracker = javascriptstatetracker.JavaScriptStateTracker()

        ecma_pass = ecmametadatapass.EcmaMetaDataPass()
        ecma_pass.Process(token)

        alias_pass = aliaspass.AliasPass(closurized_namespaces)
        alias_pass.Process(token)

        while token:
            namespaces_info.ProcessToken(token, state_tracker)
            token = token.next

        return namespaces_info
Exemple #8
0
    def __init__(self, error_handler):
        """Initialize an JavaScriptStyleChecker object.

    Args:
      error_handler: Error handler to pass all errors to
    """
        self._namespaces_info = None
        if flags.FLAGS.closurized_namespaces:
            self._namespaces_info = (
                closurizednamespacesinfo.ClosurizedNamespacesInfo(
                    flags.FLAGS.closurized_namespaces,
                    flags.FLAGS.ignored_extra_namespaces))

        checkerbase.CheckerBase.__init__(
            self,
            error_handler=error_handler,
            lint_rules=javascriptlintrules.JavaScriptLintRules(
                self._namespaces_info),
            state_tracker=javascriptstatetracker.JavaScriptStateTracker(),
            metadata_pass=ecmametadatapass.EcmaMetaDataPass(),
            limited_doc_files=flags.FLAGS.limited_doc_files)