Skip to content

Commit 98623e8

Browse files
committed
feature: @putout/printer: VariableDeclaration: beforeIf: move out
1 parent 5d21ff8 commit 98623e8

2 files changed

Lines changed: 16 additions & 31 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {types} from '@putout/babel';
2+
import {createTypeChecker} from '#type-checker';
3+
import {hasPrevNewline} from '#mark';
4+
import {callWithPrev, isFirst} from '#is';
5+
6+
const {isBlockStatement} = types;
7+
8+
export const beforeIf = createTypeChecker([
9+
['-', isFirst],
10+
['-', hasPrevNewline],
11+
['-: parentPath ->', hasPrevNewline],
12+
['+', callWithPrev(isBlockStatement)],
13+
]);

lib/tokenize/statements/variable-declaration/variable-declaration.js

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import {types} from '@putout/babel';
2-
import {hasPrevNewline} from '#mark';
31
import {createTypeChecker} from '#type-checker';
42
import {isConcatenation} from '#is-concatenation';
53
import {maybeDeclare} from '#maybe-declare';
64
import {parseLeadingComments} from '#comments';
75
import {
86
isNext,
9-
isCoupleLines,
107
isNewlineBetweenSiblings,
118
exists,
129
noTrailingComment,
@@ -17,16 +14,13 @@ import {
1714
isInsideSwitchCase,
1815
isInsideBody,
1916
callWithParent,
17+
isLast,
2018
} from '#is';
2119
import {afterIf} from './after-if.js';
2220
import {maybeSpaceAfterKeyword} from './maybe-space-after-keyword.js';
23-
24-
const {isExportDeclaration} = types;
25-
26-
const isLast = (path) => path.parentPath?.isProgram() && !isNext(path);
21+
import {beforeIf} from './before-if.js';
2722

2823
const isParentTSModuleBlock = (path) => path.parentPath.isTSModuleBlock();
29-
3024
const isInsideBlockLike = createTypeChecker([
3125
isInsideProgram,
3226
isInsideBlock,
@@ -61,7 +55,7 @@ const isNeedNewline = createTypeChecker([
6155
]);
6256

6357
export const VariableDeclaration = {
64-
beforeIf: shouldAddNewlineBefore,
58+
beforeIf,
6559
before(path, {print}) {
6660
print.breakline();
6761
},
@@ -138,25 +132,3 @@ const skipAfter = createTypeChecker([
138132
['-: parentPath -> !', isLast],
139133
['+: -> !', isInsideBlock],
140134
]);
141-
142-
function shouldAddNewlineBefore(path) {
143-
if (isFirst(path))
144-
return false;
145-
146-
if (hasPrevNewline(path))
147-
return false;
148-
149-
if (hasPrevNewline(path.parentPath))
150-
return false;
151-
152-
const prevPath = path.getPrevSibling();
153-
154-
if (prevPath.isStatement() && !prevPath.isExpressionStatement() && !prevPath.isBlockStatement())
155-
return false;
156-
157-
return !isExportDeclaration(path.parentPath) && isCoupleLines(path);
158-
}
159-
160-
function isFirst(path) {
161-
return path.node === path.parentPath.node.body?.[0];
162-
}

0 commit comments

Comments
 (0)