Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/document/transformOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export interface TransformOptions {
safeWrappingJsAttributes: string[],
includeJsAttributes: string[],
excludeJsAttributes: string[],
normalizeInlineSlotNames: boolean,
}
10 changes: 6 additions & 4 deletions src/document/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ export class Transformer {
],
includeJsAttributes: [
'^x-'
]
],
normalizeInlineSlotNames: true,
}

constructor(doc: BladeDocument) {
Expand Down Expand Up @@ -615,9 +616,10 @@ export class Transformer {
const open = `<x-${slug}`,
close = `</x-${slug}`,
inlineName = component.name?.inlineName as string,
replace = `<x-slot:${inlineName}`;
result = StringUtilities.safeReplaceAllInString(result, close, '</x-slot');
result = StringUtilities.safeReplaceAllInString(result, open, replace);
replaceOpen = `<x-slot:${inlineName}`,
replaceClose = this.transformOptions.normalizeInlineSlotNames ? `</x-slot` : `</x-slot:${inlineName}`;
result = StringUtilities.safeReplaceAllInString(result, close, replaceClose);
result = StringUtilities.safeReplaceAllInString(result, open, replaceOpen);
});

return result;
Expand Down
13 changes: 10 additions & 3 deletions src/formatting/optionDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const defaultSettings: FormattingOptions = {
],
safeWrappingJsAttributes: [
'^x-data',
]
],
normalizeInlineSlotNames: true,
};

export { defaultSettings };
Expand Down Expand Up @@ -156,7 +157,8 @@ export function parseBladeConfigObject(configObject: any): FormattingOptions {
formatJsAttributes = defaultSettings.formatJsAttributes,
includeJsAttributes = defaultSettings.includeJsAttributes,
excludeJsAttributes = defaultSettings.excludeJsAttributes,
safeWrappingJsAttributes = defaultSettings.safeWrappingJsAttributes;
safeWrappingJsAttributes = defaultSettings.safeWrappingJsAttributes,
normalizeInlineSlotNames = defaultSettings.normalizeInlineSlotNames;

if (typeof configObject.ignoreDirectives !== 'undefined' && configObject.ignoreDirectives !== null) {
ignoreDirectives = configObject.ignoreDirectives as string[];
Expand Down Expand Up @@ -245,6 +247,10 @@ export function parseBladeConfigObject(configObject: any): FormattingOptions {
if (typeof configObject.safeWrappingJsAttributes !== 'undefined') {
safeWrappingJsAttributes = configObject.safeWrappingJsAttributes as string[];
}

if (typeof configObject.normalizeInlineSlotNames !== 'undefined') {
normalizeInlineSlotNames = configObject.normalizeInlineSlotNames as boolean;
}

if (spacesAfterDirective < 0) {
spacesAfterDirective = 0;
Expand Down Expand Up @@ -289,7 +295,8 @@ export function parseBladeConfigObject(configObject: any): FormattingOptions {
formatJsAttributes: formatJsAttributes,
includeJsAttributes: includeJsAttributes,
excludeJsAttributes: excludeJsAttributes,
safeWrappingJsAttributes: safeWrappingJsAttributes
safeWrappingJsAttributes: safeWrappingJsAttributes,
normalizeInlineSlotNames: normalizeInlineSlotNames
};
}

Expand Down
36 changes: 36 additions & 0 deletions src/test/formatter_component_tags.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert';
import { formatBladeString, formatBladeStringWithPint } from '../formatting/prettier/utils.js';
import { defaultSettings } from '../formatting/optionDiscovery.js';

suite('Component Tags', () => {
test('it formats simple component tags', async () => {
Expand Down Expand Up @@ -58,6 +59,41 @@ suite('Component Tags', () => {
</x-slot>`
);
});

test('it does not normalize closing tag name', async () => {
assert.strictEqual(
(await formatBladeString(`<x:slot:name

param="value">
<p>Content</p>
</x:slot:name>`, {...defaultSettings, normalizeInlineSlotNames: false})).trim(),
`<x-slot:name param="value">
<p>Content</p>
</x-slot:name>`
);

assert.strictEqual(
(await formatBladeString(`<x-slot:name

param="value">
<p>Content</p>
</x-slot:name>`, {...defaultSettings, normalizeInlineSlotNames: false})).trim(),
`<x-slot:name param="value">
<p>Content</p>
</x-slot:name>`
);

assert.strictEqual(
(await formatBladeString(`<x-slot:[name]

param="value">
<p>Content</p>
</x-slot:[name]>`, {...defaultSettings, normalizeInlineSlotNames: false})).trim(),
`<x-slot:[name] param="value">
<p>Content</p>
</x-slot:[name]>`
);
});

test('it normalizes names', async () => {
assert.strictEqual(
Expand Down
36 changes: 36 additions & 0 deletions src/test/formatter_pint_component_tags.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'assert';
import { formatBladeStringWithPint } from '../formatting/prettier/utils.js';
import { defaultSettings } from '../formatting/optionDiscovery.js';

suite('Pint Transformer: Component Tags', () => {
test('pint: it formats simple component tags', async () => {
Expand Down Expand Up @@ -58,6 +59,41 @@ suite('Pint Transformer: Component Tags', () => {
</x-slot>`
);
});

test('pint: it does not normalize inline name pairs', async () => {
assert.strictEqual(
(await formatBladeStringWithPint(`<x:slot:name

param="value">
<p>Content</p>
</x:slot:name>`, {...defaultSettings, normalizeInlineSlotNames: false})).trim(),
`<x-slot:name param="value">
<p>Content</p>
</x-slot:name>`
);

assert.strictEqual(
(await formatBladeStringWithPint(`<x:slot:[name]

param="value">
<p>Content</p>
</x:slot:[name]>`, {...defaultSettings, normalizeInlineSlotNames: false})).trim(),
`<x-slot:[name] param="value">
<p>Content</p>
</x-slot:[name]>`
);

assert.strictEqual(
(await formatBladeStringWithPint(`<x-slot:[name]

param="value">
<p>Content</p>
</x-slot:[name]>`, {...defaultSettings, normalizeInlineSlotNames: false})).trim(),
`<x-slot:[name] param="value">
<p>Content</p>
</x-slot:[name]>`
);
});

test('pint: it normalizes names', async () => {
assert.strictEqual(
Expand Down