Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def is_expression(self, mode):
return mode == MODE.Expression or mode == MODE.ForInitializer or mode == MODE.Conditional
else:
# Support preserving wrapped arrow function expressions
# a.b('c',
# () => d.e
# )
self.allow_wrap_or_preserved_newline(current_token)
# function() vs function (), typeof() vs typeof ()
# function*() vs function* (), yield*() vs yield* ()
if (
self._flags.last_token.type == TOKEN.RESERVED and (
self._flags.last_word == 'function' or self._flags.last_word == 'typeof')) or (
self._flags.last_token.text == '*' and (
self._last_last_text in [
'function', 'yield'] or (
self._flags.mode == MODE.ObjectLiteral and self._last_last_text in [
'{', ',']))):
self._output.space_before_token = self._options.space_after_anon_function
if self._flags.last_token.text == ';' or self._flags.last_token.type == TOKEN.START_BLOCK:
self.print_newline()
elif self._flags.last_token.type in [TOKEN.END_EXPR, TOKEN.START_EXPR, TOKEN.END_BLOCK, TOKEN.COMMA] or self._flags.last_token.text == '.':
# do nothing on (( and )( and ][ and ]( and .(
# TODO: Consider whether forcing this is required. Review failing
# tests when removed.
self.allow_wrap_or_preserved_newline(
current_token, current_token.newlines)
self.print_token(current_token)
self.set_mode(next_mode)
if self._options.space_in_paren:
reserved_word(current_token, 'if') and \
current_token.comments_before is None))
start = start or (self._flags.last_token.type == TOKEN.END_EXPR and (
self._previous_flags.mode == MODE.ForInitializer or self._previous_flags.mode == MODE.Conditional))
start = start or (self._flags.last_token.type == TOKEN.WORD and self._flags.mode == MODE.BlockStatement
and not self._flags.in_case
and not (current_token.text == '--' or current_token.text == '++')
and self._last_last_text != 'function'
and current_token.type != TOKEN.WORD and current_token.type != TOKEN.RESERVED)
start = start or (
self._flags.mode == MODE.ObjectLiteral and (
(self._flags.last_token.text == ':' and self._flags.ternary_depth == 0) or (
reserved_array(self._flags.last_token, ['get', 'set']))))
if (start):
self.set_mode(MODE.Statement)
self.indent()
self.handle_whitespace_and_comments(current_token, True)
# Issue #276:
# If starting a new statement with [if, for, while, do], push to a new line.
# if (a) if (b) if(c) d(); else e(); else f();
if not self.start_of_object_property():
self.allow_wrap_or_preserved_newline(
current_token, reserved_array(current_token, ['do', 'for', 'if', 'while']))
return True
else:
return False
self.handle_whitespace_and_comments(current_token)
next_mode = MODE.Expression
if current_token.text == '[':
if self._flags.last_token.type == TOKEN.WORD or self._flags.last_token.text == ')':
if reserved_array(self._flags.last_token, Tokenizer.line_starters):
self._output.space_before_token = True
self.print_token(current_token)
self.set_mode(next_mode)
self.indent()
if self._options.space_in_paren:
self._output.space_before_token = True
return
next_mode = MODE.ArrayLiteral
if self.is_array(self._flags.mode):
if self._flags.last_token.text == '[' or (
self._flags.last_token.text == ',' and (
self._last_last_text == ']' or self._last_last_text == '}')):
# ], [ goes to a new line
# }, [ goes to a new line
if not self._options.keep_array_indentation:
self.print_newline()
if self._flags.last_token.type not in [
TOKEN.START_EXPR,
TOKEN.END_EXPR,
TOKEN.WORD,
TOKEN.OPERATOR]:
self._output.space_before_token = True
self.set_mode(MODE.ObjectLiteral)
else:
self.set_mode(MODE.BlockStatement)
elif self._flags.last_token.type == TOKEN.OPERATOR and self._flags.last_token.text == '=>':
# arrow function: (param1, paramN) => { statements }
self.set_mode(MODE.BlockStatement)
elif self._flags.last_token.type in [TOKEN.EQUALS, TOKEN.START_EXPR, TOKEN.COMMA, TOKEN.OPERATOR] or \
reserved_array(self._flags.last_token, ['return', 'throw', 'import', 'default']):
# Detecting shorthand function syntax is difficult by scanning forward,
# so check the surrounding context.
# If the block is being returned, imported, export default, passed as arg,
# assigned with = or assigned in a nested object, treat as an
# ObjectLiteral.
self.set_mode(MODE.ObjectLiteral)
else:
self.set_mode(MODE.BlockStatement)
empty_braces = (next_token is not None) and \
next_token.comments_before is None and next_token.text == '}'
empty_anonymous_function = empty_braces and self._flags.last_word == 'function' and \
self._flags.last_token.type == TOKEN.END_EXPR
if self._options.brace_preserve_inline: # check for inline, set inline_frame if so
# search forward for newline wanted inside this block
index = 0
check_token = None
self._flags.inline_frame = True
do_loop = True
while (do_loop):
index += 1
check_token = self._tokens.peek(index - 1)
if check_token.newlines: