Skip to content

Commit

Permalink
Allow "none" as an option for attributeSeparator
Browse files Browse the repository at this point in the history
  • Loading branch information
lehni committed Sep 27, 2020
1 parent 72f2bee commit a9e9c38
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ They should be set via `Prettier`'s `overrides` option
Example: `button(type="submit", (click)="play()", disabled)`
- `'as-needed'` -> Only add commas between attributes where required.
Example: `button(type="submit", (click)="play()" disabled)`
- `'none'` -> Never add commas between attributes.
Example: `button(type="submit" :style="styles" disabled)`

- `closingBracketPosition`
Position of closing bracket of attributes.
Expand Down
20 changes: 15 additions & 5 deletions src/options/attribute-separator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export const ATTRIBUTE_SEPARATOR_OPTION = {
value: 'as-needed',
description:
'Only add commas between attributes where required. Example: `button(type="submit", (click)="play()" disabled)`'
},
{
value: 'none',
description:
'Never add commas between attributes. Example: `button(type="submit" :style="styles" disabled)`'
}
]
};
Expand All @@ -38,20 +43,25 @@ export const PUG_ATTRIBUTE_SEPARATOR_OPTION = {
value: 'as-needed',
description:
'Only add commas between attributes where required. Example: `button(type="submit", (click)="play()" disabled)`'
},
{
value: 'none',
description:
'Never add commas between attributes. Example: `button(type="submit" :style="styles" disabled)`'
}
]
};

export type AttributeSeparator = 'always' | 'as-needed';
export type AttributeSeparator = 'always' | 'as-needed' | 'none';

export function resolveAttributeSeparatorOption(attributeSeparator: AttributeSeparator): boolean {
export function resolveAttributeSeparatorOption(attributeSeparator: AttributeSeparator): AttributeSeparator {
switch (attributeSeparator) {
case 'always':
return true;
case 'as-needed':
return false;
case 'none':
return attributeSeparator
}
throw new Error(
`Invalid option for pug attributeSeparator. Found '${attributeSeparator}'. Possible options: 'always' or 'as-needed'`
`Invalid option for pug attributeSeparator. Found '${attributeSeparator}'. Possible options: 'always', 'as-needed' or 'none'`
);
}
7 changes: 5 additions & 2 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class PugPrinter {
private readonly otherQuotes: "'" | '"';

private readonly alwaysUseAttributeSeparator: boolean;
private readonly neverUseAttributeSeparator: boolean;
private readonly closingBracketRemainsAtNewLine: boolean;
private readonly codeInterpolationOptions: Pick<RequiredOptions, 'singleQuote' | 'printWidth' | 'endOfLine'>;

Expand All @@ -119,7 +120,9 @@ export class PugPrinter {
this.indentString = options.pugUseTabs ? '\t' : ' '.repeat(options.pugTabWidth);
this.quotes = this.options.pugSingleQuote ? "'" : '"';
this.otherQuotes = this.options.pugSingleQuote ? '"' : "'";
this.alwaysUseAttributeSeparator = resolveAttributeSeparatorOption(options.attributeSeparator);
const attributeSeparator = resolveAttributeSeparatorOption(options.attributeSeparator);
this.alwaysUseAttributeSeparator = attributeSeparator === 'always';
this.neverUseAttributeSeparator = attributeSeparator === 'none';
this.closingBracketRemainsAtNewLine = resolveClosingBracketPositionOption(options.closingBracketPosition);
const codeSingleQuote = !options.pugSingleQuote;
this.codeInterpolationOptions = {
Expand Down Expand Up @@ -525,7 +528,7 @@ export class PugPrinter {
this.currentIndex
);
if (this.previousToken?.type === 'attribute' && (!this.previousAttributeRemapped || hasNormalPreviousToken)) {
if (this.alwaysUseAttributeSeparator || /^(\(|\[|:).*/.test(token.name)) {
if (!this.neverUseAttributeSeparator && (this.alwaysUseAttributeSeparator || /^(\(|\[|:).*/.test(token.name))) {
this.result += ',';
}
if (!this.wrapAttributes) {
Expand Down

0 comments on commit a9e9c38

Please sign in to comment.