@putout/printer
Advanced tools
Comparing version 1.9.0 to 1.9.1
@@ -6,3 +6,3 @@ 'use strict'; | ||
for (const [i, {value}] of tokens.entries()) { | ||
for (const {value} of tokens) { | ||
cookedTokens.push(value); | ||
@@ -13,1 +13,2 @@ } | ||
}; | ||
@@ -8,3 +8,5 @@ 'use strict'; | ||
const tokens = tokenize(ast, overrides); | ||
return printTokens(tokens); | ||
const result = printTokens(tokens); | ||
return result; | ||
}; |
@@ -6,11 +6,7 @@ 'use strict'; | ||
module.exports.parseComments = (path, {print, indent, maybe}) => { | ||
module.exports.parseLeadingComments = (path, {print, indent}) => { | ||
const {leadingComments} = path.node; | ||
if (leadingComments) | ||
parseLeadingComments(path, {print, indent, maybe}); | ||
}; | ||
function parseLeadingComments(path, {print, indent}) { | ||
const {leadingComments} = path.node; | ||
if (!leadingComments || !leadingComments.length) | ||
return; | ||
@@ -26,2 +22,3 @@ if (shouldAddNewlineBefore(path)) | ||
print.newline(); | ||
continue; | ||
@@ -41,7 +38,22 @@ } | ||
} | ||
} | ||
}; | ||
module.exports.parseTrailingComments = (path, {print}) => { | ||
const {trailingComments} = path.node; | ||
if (!trailingComments || !trailingComments.length) | ||
return; | ||
for (const {type, value} of trailingComments) { | ||
if (type === 'CommentLine') { | ||
print.space(); | ||
print(`//${value}`); | ||
continue; | ||
} | ||
} | ||
}; | ||
function shouldAddNewlineBefore(path) { | ||
return path.isStatement() && !isFirst(path); | ||
} | ||
'use strict'; | ||
const {stringify} = JSON; | ||
const {TYPES} = require('../types'); | ||
@@ -8,2 +10,3 @@ const toSnakeCase = require('just-snake-case'); | ||
LOG_ALL, | ||
LOG_TOKENS, | ||
DEBUG, | ||
@@ -23,5 +26,12 @@ } = process.env; | ||
module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => (chunk) => { | ||
module.exports.createLog = ({newline = '\n', store = createStore()} = {}) => ({type, value}) => { | ||
if (LOG_TOKENS) { | ||
console.log(codeFrameColumns(stringify({type, value}), {}, { | ||
highlightCode: true, | ||
})); | ||
return; | ||
} | ||
if (LOG_ALL) { | ||
console.log(codeFrameColumns(chunk, {}, { | ||
console.log(codeFrameColumns(value, {}, { | ||
highlightCode: true, | ||
@@ -33,3 +43,3 @@ })); | ||
if (LOG) { | ||
if (chunk === newline) { | ||
if (value === newline) { | ||
console.log(codeFrameColumns(store(), {}, { | ||
@@ -41,3 +51,3 @@ highlightCode: true, | ||
store(chunk); | ||
store(value); | ||
} | ||
@@ -44,0 +54,0 @@ }; |
@@ -23,3 +23,3 @@ 'use strict'; | ||
if (isNewLineBefore(path) && !isMarkedParentBefore(path) && !hasPrevNewline(path.parentPath)) { | ||
if (shouldAddNewlineBefore(path)) { | ||
print.breakline(); | ||
@@ -96,1 +96,5 @@ } | ||
} | ||
function shouldAddNewlineBefore(path) { | ||
return isNewLineBefore(path) && !isMarkedParentBefore(path) && !hasPrevNewline(path.parentPath); | ||
} |
'use strict'; | ||
const {entries} = Object; | ||
const isFirst = (path) => path.node === path.parentPath.node.body[0]; | ||
module.exports.ClassDeclaration = (path, {maybe, print, indent}) => { | ||
if (!isFirst(path)) { | ||
print.breakline(); | ||
} | ||
indent(); | ||
@@ -13,0 +6,0 @@ print('class '); |
@@ -7,2 +7,3 @@ 'use strict'; | ||
const {ClassDeclaration} = require('./class-declaration'); | ||
const { | ||
@@ -14,3 +15,8 @@ CallExpression, | ||
const {NewExpression} = require('./new-expression'); | ||
const {ObjectExpression} = require('./object-expression'); | ||
const { | ||
ObjectExpression, | ||
ObjectProperty, | ||
} = require('./object-expression'); | ||
const {ObjectPattern} = require('./object-pattern'); | ||
@@ -25,2 +31,3 @@ const {AssignmentExpression} = require('./assignment-expression'); | ||
const {TaggedTemplateExpression} = require('./tagged-template-expression'); | ||
const { | ||
@@ -30,2 +37,3 @@ BinaryExpression, | ||
} = require('./binary-expression'); | ||
const {ConditionalExpression} = require('./conditional-expression'); | ||
@@ -49,2 +57,3 @@ | ||
ObjectExpression, | ||
ObjectProperty, | ||
ObjectPattern, | ||
@@ -51,0 +60,0 @@ RestElement, |
@@ -6,2 +6,3 @@ 'use strict'; | ||
const isLogical = (path) => path.get('argument').isLogicalExpression(); | ||
const isForOf = (path) => { | ||
@@ -24,3 +25,2 @@ if (path.parentPath.isForOfStatement()) | ||
print('{'); | ||
maybe.print(manyLines, '\n'); | ||
@@ -41,4 +41,2 @@ | ||
const {shorthand, computed} = property.node; | ||
maybe.indent(manyLines); | ||
@@ -51,16 +49,7 @@ | ||
maybe.print(computed, '['); | ||
print(property.get('key')); | ||
maybe.print(computed, ']'); | ||
if (!shorthand) { | ||
print(': '); | ||
print(property.get('value')); | ||
} | ||
maybe.print(manyLines, ',\n'); | ||
print(property); | ||
maybe.print.newline(manyLines); | ||
} | ||
indent.dec(); | ||
maybe.indent(manyLines); | ||
@@ -71,2 +60,22 @@ print('}'); | ||
}; | ||
module.exports.ObjectProperty = (path, {print, maybe}) => { | ||
const { | ||
shorthand, | ||
computed, | ||
} = path.node; | ||
const manyLines = !isOneLine(path.parentPath); | ||
maybe.print(computed, '['); | ||
print('__key'); | ||
maybe.print(computed, ']'); | ||
if (!shorthand) { | ||
print(': '); | ||
print('__value'); | ||
} | ||
maybe.print(manyLines, ','); | ||
}; | ||
function isOneLine(path) { | ||
@@ -73,0 +82,0 @@ const isCall = path.parentPath.isCallExpression(); |
'use strict'; | ||
const isParentProgram = (path) => path.parentPath?.isProgram(); | ||
const isParentBlock = (path) => path.parentPath.isBlockStatement(); | ||
const isNext = (path) => path.getNextSibling().node; | ||
module.exports.isFirst = (path) => path.node === path.parentPath.node.body[0]; | ||
module.exports.isPrevBody = (path) => path.getPrevSibling().isBlockStatement(); | ||
module.exports.isNext = (path) => path.getNextSibling().node; | ||
module.exports.isProgramParent = (path) => path.parentPath?.isProgram(); | ||
module.exports.isNextParent = (path) => path.parentPath.getNextSibling().node; | ||
module.exports.isNextBlock = (path) => path.getNextSibling().isBlockStatement(); | ||
module.exports.isNext = isNext; | ||
module.exports.isParentProgram = isParentProgram; | ||
module.exports.isParentBlock = isParentBlock; | ||
module.exports.isLast = (path) => isParentProgram(path) && !isNext(path); | ||
@@ -10,0 +14,0 @@ module.exports.isCoupleLines = (path) => { |
@@ -7,3 +7,3 @@ 'use strict'; | ||
TemplateLiteral, | ||
NumericLiteral(path, {print}) { | ||
NumericLiteral(path, {write}) { | ||
const { | ||
@@ -14,8 +14,8 @@ raw, | ||
print(raw || extra.raw); | ||
write(raw || extra.raw); | ||
}, | ||
BooleanLiteral(path, {print}) { | ||
print(path.node.value); | ||
BooleanLiteral(path, {write}) { | ||
write(path.node.value); | ||
}, | ||
StringLiteral(path, {print}) { | ||
StringLiteral(path, {write}) { | ||
const { | ||
@@ -26,3 +26,3 @@ raw, | ||
print(raw || `'${value}'`); | ||
write(raw || `'${value}'`); | ||
}, | ||
@@ -35,5 +35,8 @@ Identifier(path, {write}) { | ||
}, | ||
NullLiteral(path, {print}) { | ||
print('null'); | ||
NullLiteral(path, {write}) { | ||
write('null'); | ||
}, | ||
MetaProperty(path, {write}) { | ||
write('import.meta'); | ||
}, | ||
}; |
@@ -8,2 +8,3 @@ 'use strict'; | ||
module.exports.markAfter = markAfter; | ||
module.exports.maybeMarkAfter = (a, path) => a && markAfter(path); | ||
@@ -10,0 +11,0 @@ function markBefore(path) { |
@@ -5,4 +5,5 @@ 'use strict'; | ||
isNext, | ||
isProgramParent, | ||
isParentProgram, | ||
} = require('../is'); | ||
const isFirstStatement = (path) => path.get('body.0')?.isStatement(); | ||
@@ -42,3 +43,3 @@ | ||
if (!isNext(path) && !isNext(path.parentPath) && isProgramParent(path.parentPath)) | ||
if (!isNext(path) && !isNext(path.parentPath) && isParentProgram(path.parentPath)) | ||
return false; | ||
@@ -45,0 +46,0 @@ |
'use strict'; | ||
const {isFirst} = require('../is'); | ||
module.exports.ExportDefaultDeclaration = (path, {print}) => { | ||
if (shouldAddNewlineBefore(path)) | ||
print.newline(); | ||
print('export default '); | ||
@@ -13,8 +8,1 @@ print('__declaration'); | ||
}; | ||
function shouldAddNewlineBefore(path) { | ||
if (isFirst(path)) | ||
return false; | ||
return true; | ||
} |
@@ -5,3 +5,2 @@ 'use strict'; | ||
markBefore, | ||
markAfter, | ||
hasPrevNewline, | ||
@@ -12,8 +11,17 @@ } = require('../mark'); | ||
isNext, | ||
isProgramParent, | ||
isNextParent, | ||
isParentProgram, | ||
isLast, | ||
isParentBlock, | ||
} = require('../is'); | ||
module.exports.ExpressionStatement = (path, {indent, print}) => { | ||
const isNextDifferent = (path) => { | ||
const next = path.getNextSibling(); | ||
if (!next.isExpressionStatement()) | ||
return true; | ||
return next.node.expression.type !== path.node.expression.type; | ||
}; | ||
module.exports.ExpressionStatement = (path, {indent, print, maybe}) => { | ||
if (isCoupleLinesExpression(path) && !isFirst(path) && shouldAddNewlineBefore(path) && !hasPrevNewline(path)) { | ||
@@ -28,4 +36,9 @@ print.breakline(); | ||
if (isNext(path) || isNextParent(path) || path.parentPath.isBlockStatement() && !isProgramParent(path)) { | ||
let wasNewline = false; | ||
if (shouldBreakline(path)) { | ||
print.newline(); | ||
maybe.indent(isNext(path)); | ||
wasNewline = true; | ||
} | ||
@@ -35,16 +48,38 @@ | ||
print.newline(); | ||
markAfter(path); | ||
maybe.markAfter(wasNewline, path); | ||
} | ||
}; | ||
function shouldAddNewLineAfter(path) { | ||
if (!isNext(path)) | ||
function shouldBreakline(path) { | ||
if (isLast(path) || isLast(path.parentPath)) | ||
return false; | ||
if (!isNext(path) && isParentBlock(path)) | ||
return false; | ||
if (path.parentPath.get('body') === path) | ||
return true; | ||
if (isStrictMode(path) || isCoupleLinesExpression(path)) | ||
return true; | ||
if (isNext(path) && isNextDifferent(path) && path.parentPath.node.body?.length > 2) | ||
return true; | ||
return false; | ||
} | ||
function shouldAddNewLineAfter(path) { | ||
if (isLast(path)) | ||
return false; | ||
if (isParentBlock(path) && !isParentProgram(path)) | ||
return true; | ||
if (isNext(path)) | ||
return true; | ||
return false; | ||
} | ||
function isCoupleLinesExpression(path) { | ||
@@ -51,0 +86,0 @@ const start = path.node.loc?.start?.line; |
@@ -7,5 +7,4 @@ 'use strict'; | ||
module.exports.IfStatement = (path, {indent, print}) => { | ||
if (!isFirst(path) && !hasPrevNewline(path)) { | ||
print.indent(); | ||
print.newline(); | ||
if (shouldAddNewlineBefore(path)) { | ||
print.linebreak(); | ||
} | ||
@@ -35,2 +34,3 @@ | ||
} else if (alternate.node) { | ||
print.breakline(); | ||
print('else'); | ||
@@ -53,1 +53,4 @@ print.newline(); | ||
function shouldAddNewlineBefore(path) { | ||
return !isFirst(path) && !hasPrevNewline(path); | ||
} |
'use strict'; | ||
const {markAfter} = require('../mark'); | ||
const {entries} = Object; | ||
@@ -30,3 +31,14 @@ | ||
print.newline(); | ||
if (shouldAddNewlineAfter(path)) { | ||
print.newline(); | ||
markAfter(path); | ||
} | ||
}; | ||
function shouldAddNewlineAfter(path) { | ||
if (path.getNextSibling().isImportDeclaration()) | ||
return false; | ||
return true; | ||
} |
@@ -6,5 +6,4 @@ 'use strict'; | ||
isCoupleLines, | ||
isNextBlock, | ||
} = require('../is'); | ||
} = require('../is'); | ||
const { | ||
@@ -30,2 +29,3 @@ hasPrevNewline, | ||
maybe.print(isParentBlock(path), ';'); | ||
let wasNewline = false; | ||
@@ -49,5 +49,2 @@ | ||
if (isNextBlock(path)) | ||
return false; | ||
if (isLast(path)) | ||
@@ -67,3 +64,2 @@ return false; | ||
} | ||
const isLast = (path) => path.parentPath.isProgram() && !isNext(path); | ||
@@ -88,11 +84,2 @@ | ||
return true; | ||
const nextPath = path.getNextSibling(); | ||
const nextNextPath = nextPath.getNextSibling(); | ||
const twoNext = nextPath.node && nextNextPath.node; | ||
if (!twoNext) | ||
return false; | ||
return true; | ||
} | ||
@@ -111,2 +98,1 @@ | ||
}; | ||
'use strict'; | ||
const {isLast} = require('../is'); | ||
module.exports.WhileStatement = (path, {print, indent}) => { | ||
@@ -18,3 +20,12 @@ print('while ('); | ||
print.linebreak(); | ||
if (shouldAddNewlineAfter(path)) { | ||
print.linebreak(); | ||
} | ||
}; | ||
function shouldAddNewlineAfter(path) { | ||
if (isLast(path)) | ||
return false; | ||
return true; | ||
} |
@@ -9,5 +9,10 @@ 'use strict'; | ||
const {TYPES} = require('../types'); | ||
const {createDebug, createLog} = require('./debug'); | ||
const {maybeFile} = require('./maybe'); | ||
const { | ||
createDebug, | ||
createLog, | ||
} = require('./debug'); | ||
const { | ||
maybeMarkAfter, | ||
@@ -17,3 +22,7 @@ maybeMarkBefore, | ||
const {parseComments} = require('./comments'); | ||
const { | ||
parseLeadingComments, | ||
parseTrailingComments, | ||
} = require('./comments'); | ||
const isString = (a) => typeof a === 'string'; | ||
@@ -37,9 +46,7 @@ const {assign} = Object; | ||
} | ||
const createAddToken = (tokens) => { | ||
const log = createLog(); | ||
return (token) => { | ||
const {value} = token; | ||
log(value); | ||
log(token); | ||
tokens.push(token); | ||
@@ -143,3 +150,3 @@ }; | ||
babelTraverse(ast, { | ||
babelTraverse(maybeFile(ast), { | ||
Program(path) { | ||
@@ -186,4 +193,5 @@ traverse(path); | ||
parseComments(path, printer); | ||
parseLeadingComments(path, printer); | ||
currentTraverse(path, printer); | ||
parseTrailingComments(path, printer); | ||
debug(path.type); | ||
@@ -220,2 +228,1 @@ } | ||
}; | ||
{ | ||
"name": "@putout/printer", | ||
"version": "1.9.0", | ||
"version": "1.9.1", | ||
"type": "commonjs", | ||
@@ -52,3 +52,2 @@ "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", | ||
"just-kebab-case": "^4.2.0", | ||
"lerna": "^6.0.1", | ||
"madrun": "^9.0.0", | ||
@@ -55,0 +54,0 @@ "mock-require": "^3.0.3", |
'use strict'; | ||
const {assign} = Object; | ||
module.exports.report = () => `Use print('__path') instead of path.get(__path)`; | ||
@@ -7,6 +9,14 @@ | ||
'print(path.get(__a))': ({__a}) => { | ||
__a.value = '__' + __a.value; | ||
const { | ||
raw, | ||
value, | ||
} = __a; | ||
assign(__a, { | ||
value: `__${value}`, | ||
raw: raw.replace(value, `__${value}`), | ||
}); | ||
return 'print(__a)'; | ||
}, | ||
}); | ||
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
52435
15
52
1504