From a57f9ef891b096a800cd9c548368e19a677b8816 Mon Sep 17 00:00:00 2001 From: Shuhei Hayashibara Date: Mon, 9 Oct 2023 21:11:59 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20option=20`--php-ve?= =?UTF-8?q?rsion`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/options.ts | 17 +++++++++++++++++ src/parser.ts | 5 ++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/options.ts b/src/options.ts index d739067..a7d39b4 100644 --- a/src/options.ts +++ b/src/options.ts @@ -84,5 +84,22 @@ export const options = { description: "If set to false, no trailing commas are printed for php expression.", since: "1.10.0", }, + phpVersion: { + type: "string", + category: "Blade", + default: "8.1", + description: "The version of PHP to use for formatting.", + since: "1.13.0", + }, }; +/** + * Parses a PHP version string and returns a floating point number. + * + * @param {string} version - The PHP version string to parse. + * @returns {number} The parsed PHP version as a floating point number. + * @since 1.0.0 + */ +export function parsePhpVersion(version: string): number { + return parseFloat(version); +} diff --git a/src/parser.ts b/src/parser.ts index 40e1dc8..215e275 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -2,12 +2,15 @@ import { Parser, ParserOptions, resolveConfigFile } from "prettier"; import { FormatterOption } from "blade-formatter"; import { Formatter } from "blade-formatter"; import path from "path"; +import { parsePhpVersion } from "./options"; export const parse = async ( text: string, parsers: { [parserName: string]: Parser }, opts: ParserOptions & FormatterOption, ) => { + const phpVersion = parsePhpVersion(opts["phpVersion"]); + const formatterOptions: FormatterOption = { indentSize: opts["tabWidth"], wrapLineLength: opts["printWidth"], @@ -25,7 +28,7 @@ export const parse = async ( noMultipleEmptyLines: true, noPhpSyntaxCheck: opts["noPhpSyntaxCheck"], noSingleQuote: !opts["singleQuote"], - noTrailingCommaPhp: !opts["trailingCommaPHP"], + noTrailingCommaPhp: phpVersion < 7.2 || !opts["trailingCommaPHP"], customHtmlAttributesOrder: opts["customHtmlAttributesOrder"], indentInnerHtml: opts["indentInnerHtml"], // @ts-ignore