Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option componentPrefix #902

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ Configuration file will like below:
"sortTailwindcssClasses": true,
"sortHtmlAttributes": "none",
"noPhpSyntaxCheck": false,
"noSingleQuote": false
"noSingleQuote": false,
"componentPrefix"": ["x-", "livewire:"]
}
```

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@
"default": false,
"markdownDescription": "Use double quotes instead of single quotes for php expression."
},
"bladeFormatter.format.componentPrefix": {
"type": "string",
"default": "x-,livewire",
"markdownDescription": "Specify custom prefixes for component names. This changes the format rules applied to custom components e.g. preserve style in attributes."
},
"bladeFormatter.misc.dontShowNewVersionMessage": {
"type": "boolean",
"default": false,
Expand Down
4 changes: 4 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export const formatFromCommand = async (
sortTailwindcssClasses: extConfig.sortTailwindcssClasses,
sortHtmlAttributes: extConfig.sortHtmlAttributes ?? "none",
noMultipleEmptyLines: extConfig.noMultipleEmptyLines,
noPhpSyntaxCheck: extConfig.noPhpSyntaxCheck,
indentInnerHtml: extConfig.indentInnerHtml,
noSingleQuote: extConfig.noSingleQuote,
componentPrefix: extConfig.componentPrefix,
...runtimeConfig,
};
const originalText = editor.document.getText();
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export function activate(context: ExtensionContext) {
noPhpSyntaxCheck: extConfig.noPhpSyntaxCheck,
indentInnerHtml: extConfig.indentInnerHtml,
noSingleQuote: extConfig.noSingleQuote,
componentPrefix: extConfig.componentPrefix.split(",") ?? null,
...runtimeConfig, // override all settings by runtime config
...tailwindConfig,
};
Expand Down
2 changes: 2 additions & 0 deletions src/runtimeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface RuntimeConfig {
noMultipleEmptyLines?: boolean;
noPhpSyntaxCheck?: boolean;
noSingleQuote?: boolean;
componentPrefix?: string[];
}

const configFileNames = [".bladeformatterrc.json", ".bladeformatterrc"];
Expand Down Expand Up @@ -67,6 +68,7 @@ export function readRuntimeConfig(filePath: string): RuntimeConfig | undefined {
noMultipleEmptyLines: { type: "boolean" },
noPhpSyntaxCheck: { type: "boolean" },
noSingleQuote: { type: "boolean" },
componentPrefix: { elements: { type: "string" } },
},
additionalProperties: true,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"componentPrefix": ["foo"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<foo:button :key="$foo->bar">
</foo:button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<foo:button :key="$foo->bar">
</foo:button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<foo:button :key="$foo->bar">
</foo:button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<foo:button :key="$foo->bar">
</foo:button>
19 changes: 19 additions & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,25 @@ suite("Extension Test Suite", () => {
await config.update("noSingleQuote", undefined, true);
});

test("Should format file with runtime config / componentPrefix", async function (this: any) {
this.timeout(20000);
await formatSameAsBladeFormatter(
"withConfig/componentPrefix/index.blade.php",
"withConfig/componentPrefix/formatted.index.blade.php",
);
});

test("Should format file without runtime config / componentPrefix", async function (this: any) {
this.timeout(20000);
const config = vscode.workspace.getConfiguration("bladeFormatter.format");
await config.update("componentPrefix", "foo");
await formatSameAsBladeFormatter(
"withoutConfig/componentPrefix/index.blade.php",
"withoutConfig/componentPrefix/formatted.index.blade.php",
);
await config.update("componentPrefix", "x-,livewire");
});

test("Format command exists in command list", async () => {
const commands = await vscode.commands.getCommands();
assert(commands.includes(ExtensionConstants.formatCommandKey));
Expand Down
Loading