UNPKG

@odata2ts/odata2ts

Version:

Flexible generator to produce various TypeScript artefacts (from simple model interfaces to complete odata clients) from OData metadata files

90 lines 3.29 kB
import { __awaiter } from "tslib"; import prettier from "prettier"; import { IndentationText, NewLineKind, QuoteKind } from "ts-morph"; import { BaseFormatter } from "./BaseFormatter.js"; export class PrettierFormatter extends BaseFormatter { /** * Initializes the formatter. * * @returns {Promise<PrettierFormatter>} Initialized formatter * @memberof PrettierFormatter */ init() { return __awaiter(this, void 0, void 0, function* () { const options = yield prettier.resolveConfig(this.output); if (options) { this.options = options; this.settings = { indentationText: this.convertIndentation(this.options.useTabs, this.options.tabWidth), insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: this.options.bracketSpacing !== undefined ? this.options.bracketSpacing : true, newLineKind: this.convertNewline(this.options.endOfLine), quoteKind: this.options.singleQuote ? QuoteKind.Single : QuoteKind.Double, useTrailingCommas: this.options.trailingComma === "none" ? false : true, }; } else { this.settings = {}; } return this; }); } /** * Formats a given source code. * * @abstract * @param {string} source Source code to format * @returns {string} Formatted source code * @memberof Formatter */ format(source) { return __awaiter(this, void 0, void 0, function* () { return prettier.format(source, Object.assign({ parser: "typescript" }, this.options)); }); } /** * Converts prettier indentation config to ts-morph settings. * * @private * @param {boolean | undefined} useTabs Flag, whether to use tabs or not * @param {number | undefined} tabWidth Width of a simulated tab, each number represents a space * @returns {IndentationText} Converted indentation text * @memberof PrettierFormatter */ convertIndentation(useTabs, tabWidth) { if (useTabs) return IndentationText.Tab; switch (tabWidth) { case 2: return IndentationText.TwoSpaces; case 4: return IndentationText.FourSpaces; case 8: return IndentationText.EightSpaces; default: return IndentationText.TwoSpaces; } } /** * Converts prettier newline config to ts-morph settings. * * @private * @param {string | undefined} eol prettier end-of-line config * @returns {NewLineKind | undefined} Converted new line kind * @memberof PrettierFormatter */ convertNewline(eol) { switch (eol) { case "lf": return NewLineKind.LineFeed; case "crlf": return NewLineKind.CarriageReturnLineFeed; case "cr": return NewLineKind.CarriageReturnLineFeed; case "auto": return NewLineKind.LineFeed; default: return NewLineKind.LineFeed; } } } //# sourceMappingURL=PrettierFormatter.js.map