Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.format = function(node) {
var braceStart = _tk.findPrev(node.startToken, _tk.isCode);
var braceEnd = _tk.findNext(node.endToken, _tk.isCode);
// handle `import foo, { lorem, ipsum } from 'lib';`
if (braceStart.value === '{' || braceStart.value === ',') {
_br.limit(braceStart, 0);
_ws.limitBefore(braceStart, braceStart.value === '{' ? 1 : 0);
_ws.limitAfter(braceStart, braceStart.value === '{' ? 'ModuleSpecifierOpeningBrace' : 1);
}
if (braceEnd.value === '}' || braceEnd.value === ',') {
_br.limit(braceEnd, 0);
var next = _tk.findNextNonEmpty(braceEnd);
_ws.limitAfter(braceEnd, next.value === ';' ? 0 : 1);
_ws.limitBefore(braceEnd, braceEnd.value === '}' ? 'ModuleSpecifierClosingBrace' : 0);
}
_br.limit(node.startToken, 0);
_br.limit(node.endToken, 0);
if (node.startToken !== node.endToken) {
// handle spaces around "as"
// eg: `import { named1 as myNamed1 } from 'lib'`
// eg: `import * as myLib from 'lib'`
_ws.limitAfter(node.startToken, 1);
_ws.limitBefore(node.endToken, 1);
}
};
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');
};
if (braceEnd.value === '}' || braceEnd.value === ',') {
_br.limit(braceEnd, 0);
var next = _tk.findNextNonEmpty(braceEnd);
_ws.limitAfter(braceEnd, next.value === ';' ? 0 : 1);
_ws.limitBefore(braceEnd, braceEnd.value === '}' ? 'ModuleSpecifierClosingBrace' : 0);
}
_br.limit(node.startToken, 0);
_br.limit(node.endToken, 0);
if (node.startToken !== node.endToken) {
// handle spaces around "as"
// eg: `import { named1 as myNamed1 } from 'lib'`
// eg: `import * as myLib from 'lib'`
_ws.limitAfter(node.startToken, 1);
_ws.limitBefore(node.endToken, 1);
}
};
exports.format = function ImportDeclaration(node) {
_br.limitAfter(node.startToken, 0);
_ws.limitAfter(node.startToken, 1);
// node.specifiers is actually handled by the ImportSpecifier hook!
if (!node.specifiers.length) return;
var fromKeyword = _tk.findPrev(node.endToken, 'from');
_br.limit(fromKeyword, 0);
_ws.limit(fromKeyword, 1);
};
exports.format = function ExportNamedDeclaration(node) {
_br.limitAfter(node.startToken, 0);
_ws.limitAfter(node.startToken, 1);
// node.specifiers is actually handled by the ExportSpecifier hook!
if (!node.specifiers.length) return;
var fromKeyword = _tk.findPrev(node.endToken, 'from');
if (fromKeyword) {
// safeguard against `export { foo, bar };` (no "from")
_br.limit(fromKeyword, 0);
_ws.limit(fromKeyword, 1);
}
};
} else if (startToken.value === 'function') {
if (node.generator) {
startToken = _tk.findNextNonEmpty(startToken);
_ws.limitBefore(startToken, 'FunctionGeneratorAsterisk');
}
_ws.limit(startToken, 'FunctionReservedWord');
}
if (_tk.isWs(node.endToken.next) &&
_tk.isSemiColon(node.endToken.next.next)) {
_tk.remove(node.endToken.next);
}
if (node.parent.type === 'CallExpression') {
_ws.limitAfter(node.endToken, 0);
}
var bodyFirstNonEmpty = _tk.findNextNonEmpty(node.body.startToken);
if (bodyFirstNonEmpty.value === '}') {
// noop
_limit.after(node.body.startToken, 0);
}
_params.format(node);
};
// handle `export {foo, bar};`
_br.limitAfter(braceEnd, 0);
} else if (node.parent.endToken !== braceEnd) {
// we don't want to limit line break for lines that contains just
// `export {foo, bar}` because that would remove undesired line breaks
limit.after(braceEnd, 'ModuleSpecifierClosingBrace');
}
}
if (node.startToken.value !== node.endToken.value) {
// handle spaces around "as"
// eg: `import { named1 as myNamed1 } from 'lib'`
// eg: `import * as myLib from 'lib'`
_br.limitAfter(node.startToken, 0);
_br.limitBefore(node.endToken, 0);
_ws.limitAfter(node.startToken, 1);
_ws.limitBefore(node.endToken, 1);
}
};
exports.format = function FunctionDeclaration(node) {
if (node.id) {
_limit.around(node.id.startToken, 'FunctionName');
}
// babel-eslint reports async functions as generators.
//
// Make sure the generator function is not an async function before removing
// the whitespace.
//
// This will prevent a function such as `async function fun() {}` being
// converted to `asyncfunction fun() {}`.
if (node.async) {
_ws.limitAfter(node.startToken, 1);
} else if (node.generator) {
var genToken = _tk.findNextNonEmpty(node.startToken);
_ws.limitBefore(genToken, 'FunctionGeneratorAsterisk');
}
_params.format(node);
_limit.around(node.body.startToken, 'FunctionDeclarationOpeningBrace');
_limit.around(node.body.endToken, 'FunctionDeclarationClosingBrace');
};
exports.format = function ReturnStatement(node) {
// need to make sure we only remove line breaks inside the node itself
// because of ASI (see #29)
var nonEmpty = _tk.findInBetween(node.startToken.next, node.endToken, _tk.isNotEmpty);
// XXX: we want to remove line breaks and white spaces inside the node, not
// using _br.limitAfter to avoid changing the program behavior (ASI)
if (nonEmpty) _tk.removeEmptyInBetween(node.startToken, nonEmpty);
_ws.limitAfter(node.startToken, 1);
if (_tk.isSemiColon(node.endToken)) {
// XXX: we want semicolon to be on same line and no whitespaces for now.
_tk.removeEmptyInBetween(_tk.findPrevNonEmpty(node.endToken), node.endToken);
}
if (node.argument) {
expressionParentheses.addSpaceInside(node.argument);
}
};
exports.format = function ConditionalExpression(node) {
// we need to grab the actual punctuators since parenthesis aren't counted
// as part of test/consequent/alternate
var questionMark = _tk.findNext(node.test.endToken, '?');
var colon = _tk.findNext(node.consequent.endToken, ':');
_ws.limitBefore(questionMark, _ws.expectedAfter('ConditionalExpressionTest'));
_ws.limitAfter(questionMark, _ws.expectedBefore('ConditionalExpressionConsequent'));
_ws.limitBefore(colon, _ws.expectedAfter('ConditionalExpressionConsequent'));
_ws.limitAfter(colon, _ws.expectedBefore('ConditionalExpressionAlternate'));
};