From 16e8b1331245b878f131490df105ea8407ab1347 Mon Sep 17 00:00:00 2001 From: Christopher Quadflieg Date: Wed, 1 Sep 2021 13:29:26 +0200 Subject: [PATCH] Format args in call token --- src/printer.ts | 13 +++++++- src/utils/common.ts | 30 +++++++++++++++++++ .../attributeSeparator/always/formatted.pug | 2 +- .../as-needed/formatted.pug | 2 +- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/printer.ts b/src/printer.ts index b1909a76..18d48c85 100644 --- a/src/printer.ts +++ b/src/printer.ts @@ -84,6 +84,7 @@ import { previousNormalAttributeToken, previousTagToken, previousTypeAttributeToken, + splitArguments, unwrapLineFeeds } from './utils/common'; import { getScriptParserName } from './utils/script-mime-types'; @@ -1499,7 +1500,17 @@ export class PugPrinter { if (args) { args = args.trim(); args = args.replace(/\s\s+/g, ' '); - result += `(${args})`; + result += `(${splitArguments(args) + .map((arg) => arg.trim()) + .map((arg) => { + if (arg[0] === '{' || arg[0] === '[') { + return this.formatDelegatePrettier(arg, '__js_expression'); + } else if (isQuoted(arg)) { + return `${this.otherQuotes}${arg.slice(1, -1)}${this.otherQuotes}`; + } + return arg; + }) + .join(', ')})`; } this.currentLineLength += result.length; this.possibleIdPosition = this.result.length + result.length; diff --git a/src/utils/common.ts b/src/utils/common.ts index 3c3d16b5..fa2313ce 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -242,6 +242,36 @@ export function makeString( return enclosingQuote + newContent + enclosingQuote; } +export function splitArguments(args: string): string[] { + const results: string[] = []; + let current: string = ''; + const block: { '{': number; '[': number } = { '{': 0, '[': 0 }; + for (const char of args) { + if (char !== ',' || block['{'] > 0 || block['['] > 0) { + current += char; + switch (char) { + case '{': + block['{']++; + break; + case '}': + block['{']--; + break; + case '[': + block['[']++; + break; + case ']': + block['[']--; + break; + } + } else { + results.push(current); + current = ''; + } + } + results.push(current); + return results; +} + /** * See [issue #9](https://github.com/prettier/plugin-pug/issues/9) for more details. * diff --git a/tests/options/attributeSeparator/always/formatted.pug b/tests/options/attributeSeparator/always/formatted.pug index 011254f8..854adce5 100644 --- a/tests/options/attributeSeparator/always/formatted.pug +++ b/tests/options/attributeSeparator/always/formatted.pug @@ -21,7 +21,7 @@ mixin anchor({ href, isExternal }) if block block -+anchor({ href: "contact/" }).extra-class(data-popup="true") ++anchor({ href: 'contact/' }).extra-class(data-popup="true") .wrapper( data-nav, diff --git a/tests/options/attributeSeparator/as-needed/formatted.pug b/tests/options/attributeSeparator/as-needed/formatted.pug index 071a534f..d6397c5f 100644 --- a/tests/options/attributeSeparator/as-needed/formatted.pug +++ b/tests/options/attributeSeparator/as-needed/formatted.pug @@ -21,7 +21,7 @@ mixin anchor({ href, isExternal }) if block block -+anchor({ href: "contact/" }).extra-class(data-popup="true") ++anchor({ href: 'contact/' }).extra-class(data-popup="true") .wrapper( data-nav