module.exports = function ForInStatement(node) {
  var expressionStart = _tk.findNext(node.startToken, '(');
  var expressionEnd = _tk.findPrev(node.body.startToken, ')');

  _tk.removeInBetween(node.startToken, expressionEnd, 'LineBreak');
  _ws.aroundIfNeeded(expressionStart, 'ForInStatementExpressionOpening');
  _ws.aroundIfNeeded(expressionEnd, 'ForInStatementExpressionClosing');

  if (node.body.type === 'BlockStatement' && node.body.body.length) {
    var bodyStart = node.body.startToken;
    var bodyEnd = node.body.endToken;
    _tk.removeAdjacentBefore(bodyStart, 'LineBreak');
    _tk.removeAdjacentAfter(bodyStart, 'WhiteSpace');
    _br.aroundIfNeeded(bodyStart, 'ForInStatementOpeningBrace');
    _ws.aroundIfNeeded(bodyStart, 'ForInStatementOpeningBrace');
    _br.aroundIfNeeded(bodyEnd, 'ForInStatementClosingBrace');
    _ws.aroundIfNeeded(bodyEnd, 'ForInStatementClosingBrace');
    _ws.afterIfNeeded(expressionEnd, 'ForInStatementExpression');
  } else if (expressionEnd.next && expressionEnd.next.value !== ';') {
    _ws.afterIfNeeded(expressionEnd, 'ForInStatementExpression');
  }

  _tk.removeEmptyInBetween(node.left.endToken, node.right.startToken);
  _ws.afterIfNeeded(node.left.endToken);
  _ws.beforeIfNeeded(node.right.startToken);
};
Beispiel #2
0
function limitInBetween(location, start, end, expected) {
  var n = getDiff(start, end, expected);
  debugBetween('[limitInBetween] diff: %d', n);
  if (n) {
    _tk.removeInBetween(start, end, 'WhiteSpace');
  }
  if (n < 0) {
    _tk.removeInBetween(start, end, function(token){
      return token.type === 'LineBreak' && n++ < 0;
    });
  } else if(n > 0) {
    var target = location === 'after' ? start : end;
    var insertNextTo = _tk[location];
    while (n-- > 0) {
      insertNextTo(target, {
        type: 'LineBreak',
        value: _curOpts.value
      });
    }
  }
}
exports.format = function ClassDeclarationAndExpression(node) {
  var classKeyword = node.startToken;
  var opening = tk.findNext(node.startToken, '{');
  var closing = node.endToken;
  // yes, we remove all the line breaks and limit to a single whitespace in
  // between the words since line breaks here would increase complexity
  tk.removeInBetween(classKeyword, opening, tk.isBr);
  ws.limitAfter(classKeyword, 1);
  var extendsKeyword = tk.findInBetween(classKeyword, opening, 'extends');
  if (extendsKeyword) {
    ws.limit(extendsKeyword, 1);
  }

  limit.around(opening, 'ClassOpeningBrace');
  limit.around(closing, 'ClassClosingBrace');
};
Beispiel #4
0
module.exports = function IfStatement(node) {

  var startBody = node.consequent.startToken;
  var endBody = node.consequent.endToken;

  var conditionalStart = _tk.findPrev(node.test.startToken, '(');
  var conditionalEnd = _tk.findNext(node.test.endToken, ')');

  _tk.removeEmptyInBetween(node.startToken, conditionalStart);
  _tk.removeEmptyInBetween(conditionalEnd, startBody);

  _ws.aroundIfNeeded(conditionalStart, 'IfStatementConditionalOpening');
  _ws.aroundIfNeeded(conditionalEnd, 'IfStatementConditionalClosing');

  var alt = node.alternate;
  if (alt) {
    var elseKeyword = _tk.findPrev(alt.startToken, 'else');
    var startEmptyRemove = _tk.findPrevNonEmpty(elseKeyword);
    if (!(startEmptyRemove.type === 'Punctuator' && startEmptyRemove.value === '}')) {
      startEmptyRemove = elseKeyword;
    }
    _tk.removeEmptyInBetween(startEmptyRemove, alt.startToken);

    if (alt.type === 'IfStatement') {
      // ElseIfStatement
      _ws.before(alt.startToken);

      _br.beforeIfNeeded(alt.consequent.startToken, 'ElseIfStatementOpeningBrace');
      _br.beforeIfNeeded(alt.consequent.endToken, 'ElseIfStatementClosingBrace');
      _br.beforeIfNeeded(elseKeyword, 'ElseIfStatement');
      _br.afterIfNeeded(alt.consequent.endToken, 'ElseIfStatement');
    } else if (alt.type === 'BlockStatement') {
      // ElseStatement
      _ws.beforeIfNeeded(elseKeyword);
      _br.aroundIfNeeded(alt.startToken, 'ElseStatementOpeningBrace');
      _ws.aroundIfNeeded(alt.startToken, 'ElseStatementOpeningBrace');

      if (_br.needsBefore('ElseStatementClosingBrace')) {
        var lastNonEmpty = _tk.findPrevNonEmpty(alt.endToken);
        _tk.removeInBetween(lastNonEmpty, alt.endToken, 'WhiteSpace');
        _br.aroundIfNeeded(alt.endToken, 'ElseStatementClosingBrace');
      } else {
        _ws.aroundIfNeeded(alt.endToken, 'ElseStatementClosingBrace');
      }
      _br.beforeIfNeeded(elseKeyword, 'ElseStatement');
      _br.afterIfNeeded(alt.endToken, 'ElseStatement');
    } else {
      // ElseStatement without curly braces
      _ws.after(elseKeyword); // required
    }
  }

  // only handle braces if block statement
  if (node.consequent.type === 'BlockStatement') {
    _tk.removeEmptyInBetween(_tk.findPrevNonEmpty(endBody), endBody);

    _br.aroundIfNeeded(startBody, 'IfStatementOpeningBrace');
    _ws.aroundIfNeeded(startBody, 'IfStatementOpeningBrace');
    if (!alt) {
      _br.aroundIfNeeded(endBody, 'IfStatementClosingBrace');
    } else {
      _br.beforeIfNeeded(endBody, 'IfStatementClosingBrace');
    }
    _ws.aroundIfNeeded(endBody, 'IfStatementClosingBrace');
  }

};