token: A token in the token stream before any goog.provide tokens.
Returns:
- A tuple containing the first provide token in the token stream and a list
- of provided objects sorted alphabetically. For example:
-
- (JavaScriptToken, ['object.a', 'object.b', ...])
+ The first provide token in the token stream.
None is returned if all goog.provide statements are already sorted.
"""
provide_strings = self._GetRequireOrProvideTokenStrings(provide_tokens)
sorted_provide_strings = sorted(provide_strings)
if provide_strings != sorted_provide_strings:
- return [provide_tokens[0], sorted_provide_strings]
+ return provide_tokens[0]
return None
def CheckRequires(self, token):
token: A token in the token stream before any goog.require tokens.
Returns:
- A tuple containing the first require token in the token stream and a list
- of required dependencies sorted alphabetically. For example:
-
- (JavaScriptToken, ['object.a', 'object.b', ...])
+ The first require token in the token stream.
None is returned if all goog.require statements are already sorted.
"""
require_strings = self._GetRequireOrProvideTokenStrings(require_tokens)
sorted_require_strings = sorted(require_strings)
if require_strings != sorted_require_strings:
- return (require_tokens[0], sorted_require_strings)
+ return require_tokens[0]
return None
def FixProvides(self, token):
first_token = tokens[0]
last_token = tokens[-1]
i = last_token
- while i != first_token:
+ while i != first_token and i is not None:
if i.type is Type.BLANK_LINE:
tokenutil.DeleteToken(i)
i = i.previous
for i in tokens_to_delete:
tokenutil.DeleteToken(i)
+ # Save token to rest of file. Sorted token will be inserted before this.
+ rest_of_file = tokens_map[strings[-1]][-1].next
+
# Re-add all tokens in the map in alphabetical order.
insert_after = tokens[0].previous
for string in sorted_strings:
for i in tokens_map[string]:
- tokenutil.InsertTokenAfter(i, insert_after)
- insert_after = i
+ if rest_of_file:
+ tokenutil.InsertTokenBefore(i, rest_of_file)
+ else:
+ tokenutil.InsertTokenAfter(i, insert_after)
+ insert_after = i
def _GetRequireOrProvideTokens(self, token, token_string):
"""Gets all goog.provide or goog.require tokens in the given token stream.
if token.type == Type.IDENTIFIER:
if token.string == token_string:
tokens.append(token)
- elif token.string not in ['goog.require', 'goog.provide']:
- # The goog.provide and goog.require identifiers are at the top of the
- # file. So if any other identifier is encountered, return.
+ elif token.string not in [
+ 'goog.provide', 'goog.require', 'goog.setTestOnly']:
+ # These 3 identifiers are at the top of the file. So if any other
+ # identifier is encountered, return.
break
token = token.next
"""
token_strings = []
for token in tokens:
- name = tokenutil.Search(token, Type.STRING_TEXT).string
- token_strings.append(name)
+ if not token.is_deleted:
+ name = tokenutil.GetStringAfterToken(token)
+ token_strings.append(name)
return token_strings
def _GetTokensMap(self, tokens):
"""
tokens_map = {}
for token in tokens:
- object_name = tokenutil.Search(token, Type.STRING_TEXT).string
+ object_name = tokenutil.GetStringAfterToken(token)
# If the previous line starts with a comment, presume that the comment
# relates to the goog.require or goog.provide and keep them together when
# sorting.
first_token = token
previous_first_token = tokenutil.GetFirstTokenInPreviousLine(first_token)
- while previous_first_token.IsAnyType(Type.COMMENT_TYPES):
+ while (previous_first_token and
+ previous_first_token.IsAnyType(Type.COMMENT_TYPES)):
first_token = previous_first_token
previous_first_token = tokenutil.GetFirstTokenInPreviousLine(
first_token)
token_list.append(last_token)
return token_list
+
+ def GetFixedRequireString(self, token):
+ """Get fixed/sorted order of goog.require statements.
+
+ Args:
+ token: The first token in the token stream.
+
+ Returns:
+ A string for correct sorted order of goog.require.
+ """
+ return self._GetFixedRequireOrProvideString(
+ self._GetRequireOrProvideTokens(token, 'goog.require'))
+
+ def GetFixedProvideString(self, token):
+ """Get fixed/sorted order of goog.provide statements.
+
+ Args:
+ token: The first token in the token stream.
+
+ Returns:
+ A string for correct sorted order of goog.provide.
+ """
+ return self._GetFixedRequireOrProvideString(
+ self._GetRequireOrProvideTokens(token, 'goog.provide'))
+
+ def _GetFixedRequireOrProvideString(self, tokens):
+ """Sorts goog.provide or goog.require statements.
+
+ Args:
+ tokens: A list of goog.provide or goog.require tokens in the order they
+ appear in the token stream. i.e. the first token in this list must
+ be the first goog.provide or goog.require token.
+
+ Returns:
+ A string for sorted goog.require or goog.provide statements
+ """
+
+ # A map from required/provided object name to tokens that make up the line
+ # it was on, including any comments immediately before it or after it on the
+ # same line.
+ tokens_map = self._GetTokensMap(tokens)
+ sorted_strings = sorted(tokens_map.keys())
+
+ new_order = ''
+ for string in sorted_strings:
+ for i in tokens_map[string]:
+ new_order += i.string
+ if i.IsLastInLine():
+ new_order += '\n'
+
+ return new_order