@putout/printer
Advanced tools
Comparing version 1.5.2 to 1.5.3
@@ -32,4 +32,2 @@ 'use strict'; | ||
module.exports.maybeMarkAfter = (a, path) => a && markAfter(path); | ||
module.exports.isMarkedParentBefore = (path) => { | ||
@@ -36,0 +34,0 @@ return isMarkedBefore(path.parentPath); |
@@ -5,3 +5,3 @@ 'use strict'; | ||
module.exports.BlockStatement = (path, {write, indent, traverse, maybe}) => { | ||
module.exports.BlockStatement = (path, {indent, maybe, print}) => { | ||
const body = path.get('body'); | ||
@@ -13,20 +13,18 @@ | ||
indent.inc(); | ||
write('{'); | ||
print('{'); | ||
if (body.length > 1 || isFirstStatement(path)) | ||
write.newline(); | ||
print.newline(); | ||
body.forEach(traverse); | ||
body.forEach(print); | ||
indent.dec(); | ||
if (body.length) | ||
indent(); | ||
maybe.indent(body.length); | ||
print('}'); | ||
write('}'); | ||
if (path.parentPath.isObjectMethod()) { | ||
write(','); | ||
print(','); | ||
} | ||
const shouldAddNewLine = !isTry(path) && !/FunctionExpression/.test(path.parentPath.type); | ||
const shouldAddNewLine = !isNewLineAdded(path); | ||
@@ -36,2 +34,14 @@ maybe.print.newline(shouldAddNewLine); | ||
function isNewLineAdded(path) { | ||
const {parentPath} = path; | ||
if (isTry(path) || /FunctionExpression/.test(path.parentPath.type)) | ||
return true; | ||
if (parentPath.isIfStatement() && parentPath.get('consequent').node === path.node && parentPath.node.alternate) | ||
return true; | ||
return false; | ||
} | ||
function isTry({parentPath}) { | ||
@@ -46,1 +56,2 @@ if (parentPath.isTryStatement()) | ||
} | ||
'use strict'; | ||
const {hasPrevNewline} = require('../mark'); | ||
const {hasPrevNewline, markAfter} = require('../mark'); | ||
const {isFirst} = require('../is'); | ||
module.exports.IfStatement = (path, {write, indent, traverse, incIndent, decIndent}) => { | ||
module.exports.IfStatement = (path, {indent, print}) => { | ||
if (!isFirst(path) && !hasPrevNewline(path)) { | ||
indent(); | ||
write('\n'); | ||
print.indent(); | ||
print.newline(); | ||
} | ||
indent(); | ||
write('if ('); | ||
traverse(path.get('test')); | ||
write(')'); | ||
print('if ('); | ||
print(path.get('test')); | ||
print(')'); | ||
const consequent = path.get('consequent'); | ||
const alternate = path.get('alternate'); | ||
if (consequent.isBlockStatement()) { | ||
write(' '); | ||
traverse(consequent); | ||
return; | ||
print(' '); | ||
print(consequent); | ||
} else { | ||
print.newline(); | ||
indent.inc(); | ||
print(consequent); | ||
indent.dec(); | ||
} | ||
write('\n'); | ||
incIndent(); | ||
traverse(consequent); | ||
decIndent(); | ||
if (alternate.isBlockStatement()) { | ||
print(' else '); | ||
print(alternate); | ||
} else if (alternate.node) { | ||
print('else'); | ||
print.newline(); | ||
indent.inc(); | ||
print(alternate); | ||
indent.dec(); | ||
} | ||
const next = path.getNextSibling(); | ||
if (!next.node || next.isIfStatement() || next.isExpressionStatement() || next.isReturnStatement()) | ||
if (!next.node || next.isExpressionStatement() || next.isReturnStatement()) | ||
return; | ||
indent(); | ||
write('\n'); | ||
print.indent(); | ||
print.newline(); | ||
markAfter(path); | ||
}; | ||
@@ -10,2 +10,3 @@ 'use strict'; | ||
const TryStatements = require('./try-statements'); | ||
const {DebuggerStatement} = require('./debugger-statement'); | ||
@@ -19,2 +20,3 @@ module.exports = { | ||
ReturnStatement, | ||
DebuggerStatement, | ||
Program(path, {traverse}) { | ||
@@ -21,0 +23,0 @@ path.get('body').forEach(traverse); |
'use strict'; | ||
const {isNext, isCoupleLines} = require('../is'); | ||
const {hasPrevNewline, markAfter} = require('../mark'); | ||
const isNextAssign = (path) => { | ||
@@ -13,24 +14,26 @@ const nextPath = path.getNextSibling(); | ||
module.exports.VariableDeclaration = (path, {write, maybe, maybeIndent, traverse}) => { | ||
if (!isFirst(path) && shouldAddNewLine(path)) { | ||
write.linebreak(); | ||
} | ||
module.exports.VariableDeclaration = (path, {maybe, print}) => { | ||
if (!isFirst(path) && shouldAddNewLine(path) && !hasPrevNewline(path)) | ||
print.linebreak(); | ||
const isParentBlock = /Program|BlockStatement/.test(path.parentPath.type); | ||
maybeIndent(isParentBlock); | ||
write(`${path.node.kind} `); | ||
traverse(path.get('declarations.0.id')); | ||
maybe.indent(isParentBlock); | ||
print(`${path.node.kind} `); | ||
print(path.get('declarations.0.id')); | ||
const initPath = path.get('declarations.0.init'); | ||
maybe.write(initPath.node, ' = '); | ||
traverse(initPath); | ||
maybe.write(isParentBlock, ';\n'); | ||
maybe.print(initPath.node, ' = '); | ||
print(initPath); | ||
maybe.print(isParentBlock, ';'); | ||
maybe.print.newline(isParentBlock); | ||
const is = isNext(path) && isCoupleLines(path) && !isNextAssign(path); | ||
maybe.indent(is); | ||
maybe.write(is, '\n'); | ||
maybe.markAfter(is, path); | ||
if (is) { | ||
print.indent(); | ||
print.newline(); | ||
markAfter(path); | ||
} | ||
}; | ||
@@ -64,1 +67,2 @@ | ||
} | ||
@@ -40,3 +40,3 @@ 'use strict'; | ||
const maybeNewline = (a) => a && newline(); | ||
const maybeLinebreak = (a) => a && writeEmptyLine(); | ||
const maybeLinebreak = (a) => a && linebreak(); | ||
@@ -58,3 +58,3 @@ let i = 0; | ||
const writeEmptyLine = () => { | ||
const linebreak = () => { | ||
tokens.push({ | ||
@@ -76,3 +76,3 @@ type: TYPES.NEWLINE, | ||
newline, | ||
linebreak: writeEmptyLine, | ||
linebreak, | ||
}); | ||
@@ -116,3 +116,2 @@ const print = (maybeLine) => { | ||
traverse, | ||
writeEmptyLine, | ||
maybe, | ||
@@ -119,0 +118,0 @@ print, |
{ | ||
"name": "@putout/printer", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"type": "commonjs", | ||
@@ -5,0 +5,0 @@ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36152
40
1027