Skip to content

Commit

Permalink
feat: 🎸 add option --no-trailing-comma-php (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo authored Sep 10, 2023
1 parent 813e683 commit 9338973
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 0 deletions.
25 changes: 25 additions & 0 deletions __tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,29 @@ describe('The blade formatter CLI', () => {

expect(cmdResult).toEqual(formatted.toString('utf-8'));
});

test.concurrent('runtime config test (trailing comma php)', async () => {
const cmdResult = await cmd.execute(binPath, [
path.resolve('__tests__', 'fixtures', 'runtimeConfig', 'trailingCommaPhp', 'index.blade.php'),
]);

const formatted = fs.readFileSync(
path.resolve('__tests__', 'fixtures', 'runtimeConfig', 'trailingCommaPhp', 'formatted.index.blade.php'),
);

expect(cmdResult).toEqual(formatted.toString('utf-8'));
});

test.concurrent('cli argument test (trailing comma php)', async () => {
const cmdResult = await cmd.execute(binPath, [
'--no-trailing-comma-php',
path.resolve('__tests__', 'fixtures', 'argumentTest', 'trailingCommaPhp', 'index.blade.php'),
]);

const formatted = fs.readFileSync(
path.resolve('__tests__', 'fixtures', 'argumentTest', 'trailingCommaPhp', 'formatted.index.blade.php'),
);

expect(cmdResult).toEqual(formatted.toString('utf-8'));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{!! Form::select(
'refundType',
REFUND_TYPES,
$data->refundType ?? null,
[
'class' => 'form-control refundType',
'required' => 'required'
] + ($refundId ? ['disabled' => 'true'] : [])
) !!}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{!! Form::select(
'refundType',
REFUND_TYPES,
$data->refundType ?? null,
[
'class' => 'form-control refundType',
'required' => 'required',
] + ($refundId ? ['disabled' => 'true'] : []),
) !!}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"noTrailingCommaPhp": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{!! Form::select(
'refundType',
REFUND_TYPES,
$data->refundType ?? null,
[
'class' => 'form-control refundType',
'required' => 'required'
] + ($refundId ? ['disabled' => 'true'] : [])
) !!}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{!! Form::select(
'refundType',
REFUND_TYPES,
$data->refundType ?? null,
[
'class' => 'form-control refundType',
'required' => 'required',
] + ($refundId ? ['disabled' => 'true'] : []),
) !!}
24 changes: 24 additions & 0 deletions __tests__/fixtures/snapshots/trailing_comma_php.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
------------------------------------options----------------------------------------
{
"noTrailingCommaPhp": true
}
------------------------------------content----------------------------------------
{!! Form::select(
'refundType',
REFUND_TYPES,
$data->refundType ?? null,
[
'class' => 'form-control refundType',
'required' => 'required',
] + ($refundId ? ['disabled' => 'true'] : []),
) !!}
------------------------------------expected----------------------------------------
{!! Form::select(
'refundType',
REFUND_TYPES,
$data->refundType ?? null,
[
'class' => 'form-control refundType',
'required' => 'required'
] + ($refundId ? ['disabled' => 'true'] : [])
) !!}
12 changes: 12 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ export default async function cli() {
hidden: true,
default: true,
})
.option('no-trailing-comma-php', {
type: 'boolean',
description: 'If set to true, no trailing commas are printed for php expression.',
default: false,
})
.option('trailing-comma-php', {
type: 'boolean',
description: 'this is a workaround for combine strict && boolean option',
hidden: true,
default: true,
})
.option('progress', {
alias: 'p',
type: 'boolean',
Expand Down Expand Up @@ -194,6 +205,7 @@ export default async function cli() {
.set('noMultipleEmptyLines', !parsed.argv.multipleEmptyLines)
.set('noPhpSyntaxCheck', !parsed.argv.phpSyntaxCheck)
.set('noSingleQuote', !parsed.argv.singleQuote)
.set('noTrailingCommaPhp', !parsed.argv.trailingCommaPhp)
.value();

if (parsed.argv.stdin) {
Expand Down
1 change: 1 addition & 0 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export default class Formatter {
this.options = {
...{
noPhpSyntaxCheck: false,
trailingCommaPHP: !options.noTrailingCommaPhp,
printWidth: options.wrapLineLength || constants.defaultPrintWidth,
},
...options,
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type FormatterOption = {
noMultipleEmptyLines?: boolean;
noPhpSyntaxCheck?: boolean;
noSingleQuote?: boolean;
noTrailingCommaPhp?: boolean;
extraLiners?: string[];
};

Expand Down
2 changes: 2 additions & 0 deletions src/runtimeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface RuntimeConfig {
noMultipleEmptyLines?: boolean;
noPhpSyntaxCheck?: boolean;
noSingleQuote?: boolean;
noTrailingCommaPhp?: boolean;
extraLiners?: string[];
}

Expand Down Expand Up @@ -95,6 +96,7 @@ export async function readRuntimeConfig(filePath: string | null): Promise<Runtim
noMultipleEmptyLines: { type: 'boolean', nullable: true },
noPhpSyntaxCheck: { type: 'boolean', nullable: true },
noSingleQuote: { type: 'boolean', nullable: true },
noTrailingCommaPhp: { type: 'boolean', nullable: true },
extraLiners: { type: 'array', nullable: true, items: { type: 'string' }, default: ['head', 'body', '/html'] },
},
additionalProperties: true,
Expand Down

0 comments on commit 9338973

Please sign in to comment.