Skip to content

Commit

Permalink
9 - Main 1
Browse files Browse the repository at this point in the history
  • Loading branch information
b5414 committed Apr 4, 2021
1 parent f889d1d commit 1430b51
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/document/doc-builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ const softline = {type: 'line', soft: true};
const hardline = concat([hardlineWithoutBreakParent, breakParent]);
// eslint-disable-next-line prettier-internal-rules/no-doc-builder-concat
const literalline = concat([literallineWithoutBreakParent, breakParent]);
const nospline = {type: 'line', nospace: true};

const cursor = {type: 'cursor', placeholder: Symbol('cursor')};

Expand Down Expand Up @@ -269,6 +270,7 @@ module.exports = {
line,
softline,
hardline,
nospline,
literalline,
group,
conditionalGroup,
Expand Down
6 changes: 4 additions & 2 deletions src/document/doc-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ function fits(next, restCommands, width, options, hasLineSuffix, mustBeFlat) {
// fallthrough
case MODE_FLAT:
if(!doc.hard){
if(!doc.soft){
if(doc.nospace)out.push('');
else if(!doc.soft){
out.push(' ');

width -= 1;
Expand Down Expand Up @@ -472,7 +473,8 @@ function printDocToString(doc, options) {
switch(mode){
case MODE_FLAT:
if(!doc.hard){
if(!doc.soft){
if(doc.nospace)out.push('');
else if(!doc.soft){
out.push(' ');

pos += 1;
Expand Down
26 changes: 15 additions & 11 deletions src/language-js/print/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {printDanglingComments, printCommentsSeparately} = require('../../main/com
const getLast = require('../../utils/get-last');
const {getNextNonSpaceNonCommentCharacterIndex} = require('../../common/util');
const {
builders: {line, softline, group, indent, ifBreak, hardline, join, indentIfBreak},
builders: {line, softline, nospline, group, indent, ifBreak, hardline, join, indentIfBreak},
} = require('../../document');
const {
getFunctionParameters,
Expand Down Expand Up @@ -59,7 +59,7 @@ function printFunctionDeclaration(path, print, options, expandArg) {
const returnTypeDoc = printReturnType(path, print, options);
const shouldGroupParameters = shouldGroupFunctionParameters(node, returnTypeDoc);

parts.push(printFunctionTypeParameters(path, options, print), group([shouldGroupParameters ? group(parametersDoc) : parametersDoc, returnTypeDoc]), node.body ? ' ' : '', print('body'));
parts.push(printFunctionTypeParameters(path, options, print), group([shouldGroupParameters ? group(parametersDoc) : parametersDoc, returnTypeDoc]), '', print('body'));

if(options.semi && (node.declare || !node.body)){
parts.push(';');
Expand Down Expand Up @@ -110,7 +110,7 @@ function printMethodInternal(path, options, print) {
const parts = [printFunctionTypeParameters(path, options, print), group([shouldGroupParameters ? group(parametersDoc) : parametersDoc, returnTypeDoc])];

if(node.body){
parts.push(' ', print('body'));
parts.push('', print('body'));
}else{
parts.push(options.semi ? ';' : '');
}
Expand All @@ -123,7 +123,7 @@ function printArrowFunctionSignature(path, options, print, args) {
const parts = [];

if(node.async){
parts.push('async ');
parts.push('async');
}

if(shouldPrintParamsWithoutParens(path, options)){
Expand Down Expand Up @@ -158,8 +158,8 @@ function printArrowChain(path, args, signatures, shouldBreak, bodyDoc, tailNode)
const groupId = Symbol('arrow-chain');

return group([
group(indent([isCallee || isAssignmentRhs ? softline : '', group(join([' =>', line], signatures), {shouldBreak})]), {id: groupId, shouldBreak: shouldBreakBeforeChain}),
' =>',
group(indent([isCallee || isAssignmentRhs ? softline : '', group(join(['=>', line], signatures), {shouldBreak})]), {id: groupId, shouldBreak: shouldBreakBeforeChain}),
'=>',
indentIfBreak(shouldPutBodyOnSeparateLine ? indent([line, bodyDoc]) : [' ', bodyDoc], {groupId}),
isCallee ? ifBreak(softline, '', {groupId}) : '',
]);
Expand Down Expand Up @@ -202,7 +202,7 @@ function printArrowFunctionExpression(path, options, print, args) {
}

const parts = signatures;
parts.push(' =>');
parts.push('=>');

// We want to always keep these types of nodes on the same line
// as the arrow.
Expand All @@ -216,13 +216,13 @@ function printArrowFunctionExpression(path, options, print, args) {
node.body.type === 'ArrowFunctionExpression' ||
node.body.type === 'DoExpression')
){
return group([...parts, ' ', body]);
return group([...parts, '', body]);
}

// We handle sequence expressions as the body of arrows specially,
// so that the required parentheses end up on their own lines.
if(node.body.type === 'SequenceExpression'){
return group([...parts, group([' (', indent([softline, body]), softline, ')'])]);
return group([...parts, group(['(', indent([softline, body]), softline, ')'])]);
}

// if the arrow function is expanded as last argument, we are adding a
Expand All @@ -240,7 +240,10 @@ function printArrowFunctionExpression(path, options, print, args) {

return group([
...parts,
group([indent([line, shouldAddParens ? ifBreak('', '(') : '', body, shouldAddParens ? ifBreak('', ')') : '']), shouldAddSoftLine ? [ifBreak(printTrailingComma ? ',' : ''), softline] : '']),
group([
indent([nospline, shouldAddParens ? ifBreak('', '(') : '', body, shouldAddParens ? ifBreak('', ')') : '']),
shouldAddSoftLine ? [ifBreak(printTrailingComma ? ',' : ''), softline] : '',
]),
]);
}

Expand Down Expand Up @@ -308,8 +311,9 @@ function printReturnAndThrowArgument(path, options, print) {
if(returnArgumentHasLeadingComment(options, node.argument)){
parts.push([' (', indent([hardline, print('argument')]), hardline, ')']);
}else if(isBinaryish(node.argument) || node.argument.type === 'SequenceExpression'){
parts.push(group([ifBreak(' (', ' '), indent([softline, print('argument')]), softline, ifBreak(')')]));
parts.push(group([ifBreak('(', ' '), indent([softline, print('argument')]), softline, ifBreak(')')]));
}else{
// below - space: return_(); or: return_func(); or: return_'string';
parts.push(' ', print('argument'));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/language-js/print/member-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function printMemberChain(path, options, print) {
const printedGroups = groups.map(printGroup);
const oneLine = printedGroups;

const cutoff = shouldMerge ? 3 : 2;
const cutoff = 13;
const flatGroups = flat(groups);

const nodeHasComment =
Expand Down
6 changes: 3 additions & 3 deletions src/language-js/print/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const {isNonEmptyArray} = require('../../common/util');
const {
builders: {indent, join, line},
builders: {indent, join, line, nospline},
} = require('../../document');
const {isFlowAnnotationComment} = require('../utils');

Expand Down Expand Up @@ -69,10 +69,10 @@ function adjustClause(node, clause, forceSpace) {
}

if(node.type === 'BlockStatement' || forceSpace){
return [' ', clause];
return ['', clause];
}

return indent([line, clause]);
return indent([nospline, clause]);
}

module.exports = {
Expand Down
33 changes: 17 additions & 16 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,23 +383,24 @@ function printPathNoParens(path, options, print, args) {
return group(parts);
}
case 'WithStatement':
return group(['with (', print('object'), ')', adjustClause(node.body, print('body'))]);
return group(['with(', print('object'), ')', adjustClause(node.body, print('body'))]);
case 'IfStatement': {
const con = adjustClause(node.consequent, print('consequent'));
const opening = group(['if (', group([indent([softline, print('test')]), softline]), ')', con]);
const opening = group(['if(', group([indent([softline, print('test')]), softline]), ')', con]);

parts.push(opening);

if(node.alternate){
const commentOnOwnLine = hasComment(node.consequent, CommentCheckFlags.Trailing | CommentCheckFlags.Line) || needsHardlineAfterDanglingComment(node);
const elseOnSameLine = node.consequent.type === 'BlockStatement' && !commentOnOwnLine;
parts.push(elseOnSameLine ? ' ' : hardline);
parts.push(elseOnSameLine ? '' : hardline);

if(hasComment(node, CommentCheckFlags.Dangling)){
parts.push(printDanglingComments(path, options, true), commentOnOwnLine ? hardline : ' ');
}

parts.push('else', group(adjustClause(node.alternate, print('alternate'), node.alternate.type === 'IfStatement')));
const specialSpace = node.alternate.type !== 'BlockStatement' ? ' ' : '';
parts.push(`else${specialSpace}`, group(adjustClause(node.alternate, print('alternate'), node.alternate.type === 'IfStatement')));
}

return parts;
Expand All @@ -414,30 +415,30 @@ function printPathNoParens(path, options, print, args) {
const printedComments = dangling ? [dangling, softline] : '';

if(!node.init && !node.test && !node.update){
return [printedComments, group(['for (;;)', body])];
return [printedComments, group(['for(;;)', body])];
}

return [printedComments, group(['for (', group([indent([softline, print('init'), ';', line, print('test'), ';', line, print('update')]), softline]), ')', body])];
return [printedComments, group(['for(', group([indent([softline, print('init'), ';', line, print('test'), ';', line, print('update')]), softline]), ')', body])];
}
case 'WhileStatement':
return group(['while (', group([indent([softline, print('test')]), softline]), ')', adjustClause(node.body, print('body'))]);
return group(['while(', group([indent([softline, print('test')]), softline]), ')', adjustClause(node.body, print('body'))]);
case 'ForInStatement':
return group(['for (', print('left'), ' in ', print('right'), ')', adjustClause(node.body, print('body'))]);
return group(['for(', print('left'), ' in ', print('right'), ')', adjustClause(node.body, print('body'))]);

case 'ForOfStatement':
return group(['for', node.await ? ' await' : '', ' (', print('left'), ' of ', print('right'), ')', adjustClause(node.body, print('body'))]);
return group(['for', node.await ? ' await' : '', '(', print('left'), ' of ', print('right'), ')', adjustClause(node.body, print('body'))]);

case 'DoWhileStatement': {
const clause = adjustClause(node.body, print('body'));
const doBody = group(['do', clause]);
parts = [doBody];

if(node.body.type === 'BlockStatement'){
parts.push(' ');
parts.push('');
}else{
parts.push(hardline);
}
parts.push('while (', group([indent([softline, print('test')]), softline]), ')', semi);
parts.push('while(', group([indent([softline, print('test')]), softline]), ')', semi);

return parts;
}
Expand Down Expand Up @@ -470,7 +471,7 @@ function printPathNoParens(path, options, print, args) {

return [print('label'), ': ', print('body')];
case 'TryStatement':
return ['try ', print('block'), node.handler ? [' ', print('handler')] : '', node.finalizer ? [' finally ', print('finalizer')] : ''];
return ['try', print('block'), node.handler ? ['', print('handler')] : '', node.finalizer ? ['finally', print('finalizer')] : ''];
case 'CatchClause':
if(node.param){
const parameterHasComments = hasComment(
Expand All @@ -485,15 +486,15 @@ function printPathNoParens(path, options, print, args) {
);
const param = print('param');

return ['catch ', parameterHasComments ? ['(', indent([softline, param]), softline, ') '] : ['(', param, ') '], print('body')];
return ['catch', parameterHasComments ? ['(', indent([softline, param]), softline, ')'] : ['(', param, ')'], print('body')];
}

return ['catch ', print('body')];
return ['catch', print('body')];
// Note: ignoring n.lexical because it has no printing consequences.
case 'SwitchStatement':
return [
group(['switch (', indent([softline, print('discriminant')]), softline, ')']),
' {',
group(['switch(', indent([softline, print('discriminant')]), softline, ')']),
'{',
node.cases.length > 0
? indent([
hardline,
Expand Down

0 comments on commit 1430b51

Please sign in to comment.