Skip to content

Commit

Permalink
Format args in call token
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Quadflieg committed Sep 1, 2021
1 parent 7c3d764 commit 16e8b13
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import {
previousNormalAttributeToken,
previousTagToken,
previousTypeAttributeToken,
splitArguments,
unwrapLineFeeds
} from './utils/common';
import { getScriptParserName } from './utils/script-mime-types';
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/options/attributeSeparator/always/formatted.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/options/attributeSeparator/as-needed/formatted.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 16e8b13

Please sign in to comment.