1#!/usr/bin/env python 2# 3# Copyright 2012 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"""Utility functions for testing gjslint components.""" 18 19# Allow non-Google copyright 20# pylint: disable=g-bad-file-header 21 22__author__ = ('nnaze@google.com (Nathan Naze)') 23 24import StringIO 25 26from closure_linter import ecmametadatapass 27from closure_linter import javascriptstatetracker 28from closure_linter import javascripttokenizer 29 30 31def TokenizeSource(source): 32 """Convert a source into a string of tokens. 33 34 Args: 35 source: A source file as a string or file-like object (iterates lines). 36 37 Returns: 38 The first token of the resulting token stream. 39 """ 40 41 if isinstance(source, basestring): 42 source = StringIO.StringIO(source) 43 44 tokenizer = javascripttokenizer.JavaScriptTokenizer() 45 return tokenizer.TokenizeFile(source) 46 47 48def TokenizeSourceAndRunEcmaPass(source): 49 """Tokenize a source and run the EcmaMetaDataPass on it. 50 51 Args: 52 source: A source file as a string or file-like object (iterates lines). 53 54 Returns: 55 The first token of the resulting token stream. 56 """ 57 start_token = TokenizeSource(source) 58 ecma_pass = ecmametadatapass.EcmaMetaDataPass() 59 ecma_pass.Process(start_token) 60 return start_token 61 62 63def ParseFunctionsAndComments(source): 64 """Run the tokenizer and tracker and return comments and functions found. 65 66 Args: 67 source: A source file as a string or file-like object (iterates lines). 68 69 Returns: 70 The functions and comments as a tuple. 71 """ 72 start_token = TokenizeSourceAndRunEcmaPass(source) 73 74 tracker = javascriptstatetracker.JavaScriptStateTracker() 75 76 functions = [] 77 comments = [] 78 for token in start_token: 79 tracker.HandleToken(token, tracker.GetLastNonSpaceToken()) 80 81 function = tracker.GetFunction() 82 if function and function not in functions: 83 functions.append(function) 84 85 comment = tracker.GetDocComment() 86 if comment and comment not in comments: 87 comments.append(comment) 88 89 tracker.HandleAfterToken(token) 90 91 return functions, comments 92