UNPKG

prettier-plugin-awk

Version:

[![tests](https://github.com/Beaglefoot/prettier-plugin-awk/actions/workflows/tests.yml/badge.svg)](https://github.com/Beaglefoot/prettier-plugin-awk/actions/workflows/tests.yml) [![npm](https://img.shields.io/npm/v/prettier-plugin-awk)](https://www.npmjs

75 lines 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.withNullNodeHandler = exports.withPreservedEmptyLines = exports.withNodesSeparator = exports.separatedNodes = void 0; const prettier_1 = require("prettier"); const utils_1 = require("./utils"); const { hardline } = prettier_1.doc.builders; // These are separated with newline exports.separatedNodes = new Set([ 'if_statement', 'while_statement', 'do_while_statement', 'for_statement', 'for_in_statement', 'switch_statement', 'rule', 'func_def', ]); /** Adds an empty line before and after some statements */ function withNodesSeparator(printFn) { return function (path, options, print) { const node = path.getValue(); const result = printFn(path, options, print); const shouldPrependNewline = node.previousNamedSibling && (node.previousNamedSibling.type !== 'comment' || (0, utils_1.doesCommentBelongToNode)(node.previousNamedSibling)); const shouldAppendNewline = node.nextNamedSibling && !exports.separatedNodes.has(node.nextNamedSibling.type) && !(0, utils_1.doesCommentBelongToNode)(node.nextNamedSibling); if (exports.separatedNodes.has(node.type)) { return [ shouldPrependNewline ? hardline : '', result, shouldAppendNewline ? hardline : '', ]; } return result; }; } exports.withNodesSeparator = withNodesSeparator; /** * Preserves existing empty lines in original source code. * Multiple empty lines get replaced with a single one. */ function withPreservedEmptyLines(printFn) { const nodeTypesToExclude = new Set(['rule', 'block', 'func_def']); return function (path, options, print) { const node = path.getValue(); const result = printFn(path, options, print); if (nodeTypesToExclude.has(node.type)) return result; if (!exports.separatedNodes.has(node.type) && node.previousNamedSibling && !exports.separatedNodes.has(node.previousNamedSibling.type)) { const currentLine = node.startPosition.row; const previousLine = node.previousNamedSibling.endPosition.row; if (currentLine - previousLine > 1) { return [hardline, result]; } } return result; }; } exports.withPreservedEmptyLines = withPreservedEmptyLines; /** This printer wrapper must be the outer one */ function withNullNodeHandler(printFn) { return function (path, options, print) { const node = path.getValue(); if (node === null) return ''; const result = printFn(path, options, print); return result; }; } exports.withNullNodeHandler = withNullNodeHandler; //# sourceMappingURL=wrappers.js.map