Skip to content

Commit

Permalink
Allow "none" as an option for attributeSeparator
Browse files Browse the repository at this point in the history
Closes #102
  • Loading branch information
lehni committed Sep 27, 2020
1 parent 72f2bee commit d70b260
Show file tree
Hide file tree
Showing 6 changed files with 60 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
12 changes: 12 additions & 0 deletions tests/options/attributeSeparator/none/formatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
button(type="submit" :styles="styles" @click="play()" disabled)

nav-component(locale-relative-redirect="true" highlight="home" pin="false")

.wrapper(
data-nav
data-next="Next"
data-prev="Prev"
data-slides=4
data-arr=[1, 2, 3, 4, 5, 6]
data-obj={ att: 1, attr2: 2 }
)
21 changes: 21 additions & 0 deletions tests/options/attributeSeparator/none/none.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from './../../../../src/index';

describe('Options', () => {
describe('attributeSeparator', () => {
test('should never insert commas between attributes', () => {
const expected: string = readFileSync(resolve(__dirname, 'formatted.pug'), 'utf8');
const code: string = readFileSync(resolve(__dirname, 'unformatted.pug'), 'utf8');
const actual: string = format(code, {
parser: 'pug' as any,
plugins: [plugin],
// @ts-expect-error
attributeSeparator: 'none'
});

expect(actual).toBe(expected);
});
});
});
5 changes: 5 additions & 0 deletions tests/options/attributeSeparator/none/unformatted.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
button(type="submit", :styles="styles", @click="play()", disabled)

nav-component(locale-relative-redirect="true", highlight="home", pin="false")

.wrapper(data-nav=true data-next="Next" data-prev="Prev" data-slides=4 data-arr=[1,2,3,4,5,6] data-obj={att: 1, attr2: 2})

0 comments on commit d70b260

Please sign in to comment.