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

fix: 🐛 do not add line return in unescaped blade brace #217

Merged
merged 5 commits into from
Feb 23, 2021
Merged
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
38 changes: 36 additions & 2 deletions __tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe('The blade formatter CLI', () => {
expect(cmdResult).toEqual(formatted.toString('utf-8'));
});

test('show error message if unexpected syntax', async () => {
test('show not error even if line return exists after unescaped blade brace', async () => {
const cmdResult = await spawnSync(
'/bin/cat',
[
Expand All @@ -306,7 +306,16 @@ describe('The blade formatter CLI', () => {
{ stdio: 'pipe', shell: true },
).stdout.toString();

expect(cmdResult).toEqual(expect.stringContaining("Can't format blade"));
const formatted = fs.readFileSync(
path.resolve(
__basedir,
'__tests__',
'fixtures',
'formatted.escaped_bug.blade.php',
),
);

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

test('Do nothing if something goes wrong #128', async () => {
Expand Down Expand Up @@ -398,4 +407,29 @@ describe('The blade formatter CLI', () => {

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

test('unescape blade brace', async () => {
const cmdResult = await cmd.execute(
path.resolve(__basedir, 'bin', 'blade-formatter'),
[
path.resolve(
__basedir,
'__tests__',
'fixtures',
'escaped_blade_tag.blade.php',
),
],
);

const formatted = fs.readFileSync(
path.resolve(
__basedir,
'__tests__',
'fixtures',
'formatted.escaped_blade_tag.blade.php',
),
);

expect(cmdResult).toEqual(formatted.toString('utf-8'));
});
});
1 change: 1 addition & 0 deletions __tests__/fixtures/escaped_blade_tag.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@if ($achievement->user != null) {!! $achievement->user->pay2 !!} 円 @endif
3 changes: 1 addition & 2 deletions __tests__/fixtures/formatted.edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
<div></div>
<p>@lang('user.edit')</p>
</div>
{!! Form::model($user, ['route' => ['frontend.user.user.update', $user['id']], 'method' => 'put', 'class' =>
'form-horizontal', 'role' => 'form']) !!}
{!! Form::model($user, ['route' => ['frontend.user.user.update', $user['id']], 'method' => 'put', 'class' => 'form-horizontal', 'role' => 'form']) !!}
<ul class="pf-user-form">
<li>
<p>{!! Form::label('parent_id', __('user.parent')) !!}</p>
Expand Down
2 changes: 2 additions & 0 deletions __tests__/fixtures/formatted.escaped_blade_tag.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@if ($achievement->user != null)
{!! $achievement->user->pay2 !!} 円 @endif
53 changes: 53 additions & 0 deletions __tests__/fixtures/formatted.escaped_bug.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<header class="face <?= isset($face_class) ? $face_class : '' ?>">
<div class="container d-flex">
@if (isset($face_class) && $face_class === 'face-narrow')
<div class="col-md-7 face__wrapper">
@else
<div class="col-md-6 face__wrapper">
@endif
<h1 class="face__title">{{ $face_title }}</h1>
@isset($face_subtitle)
<p class="face__subtitle">{{ $face_subtitle }}</p>
@endisset
@isset($face_desc)
<span class="face__text">{!! $face_desc !!}
@endisset
@isset($face_desc_mb_hide)
<span class="face__text d-none d-lg-inline">&ThinSpace;{!! $face_desc_mb_hide !!}</span>
@endisset
</span>
@isset($face_btn_text)
<div class="face__button-box">
<a href="{{ $face_anchor or '#request-form' }}" class="btn_regular"
onclick="yaCounter.reachGoal('form'); return true;">{{ $face_btn_text }}</a>
@endisset
@isset($clutch)
<script type="text/javascript"
src="https://widget.clutch.co/static/js/widget.js">
</script>
<div class="face__clutch clutch-widget"
data-url="https://widget.clutch.co" data-widget-type="2"
data-height="50" data-clutchcompany-id="861064"
{{ $clutch === 'dark' ? 'data-darkbg="1"' : '' }}>
</div>
@endisset
@isset($face_btn_text)
</div>
@endisset
</div>
@if (isset($face_class) && $face_class === 'face-wide')
<div class="col-md-7 face__image-box">
@else
<div class="col-md-7 col-lg-6 face__image-box">
@endif
@isset($face_image)
<picture>
@isset($face_image_webp)
<source srcset="{{ $face_image_webp }}" type="image/webp">
@endisset
<img class="face__image" src="{{ $face_image }}" alt="Logo">
</picture>
@endisset
</div>
</div>
</header>
40 changes: 40 additions & 0 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class Formatter {
this.rawBlocks = [];
this.bladeComments = [];
this.bladeBraces = [];
this.rawBladeBraces = [];
this.result = [];
this.diffs = [];
}
Expand All @@ -43,8 +44,10 @@ export default class Formatter {
.formatAsPhp(content)
.then((formattedAsPhp) => this.preserveBladeComment(formattedAsPhp))
.then((formattedAsPhp) => this.preserveBladeBrace(formattedAsPhp))
.then((formattedAsPhp) => this.preserveRawBladeBrace(formattedAsPhp))
.then((formattedAsPhp) => this.formatAsHtml(formattedAsPhp))
.then((formattedAsHtml) => this.formatAsBlade(formattedAsHtml))
.then((formattedAsBlade) => this.restoreRawBladeBrace(formattedAsBlade))
.then((formattedAsBlade) => this.restoreBladeBrace(formattedAsBlade))
.then((formattedAsBlade) => this.restoreBladeComment(formattedAsBlade))
.then((formattedResult) => util.checkResult(formattedResult));
Expand Down Expand Up @@ -90,6 +93,12 @@ export default class Formatter {
});
}

async preserveRawBladeBrace(content) {
return _.replace(content, /\{!!(.*?)!!\}/gs, (_match, p1) => {
return this.storeRawBladeBrace(p1);
});
}

storeRawBlock(value) {
return this.getRawPlaceholder(this.rawBlocks.push(value) - 1);
}
Expand All @@ -104,6 +113,11 @@ export default class Formatter {
return this.getBladeBracePlaceholder(index, length + brace.length);
}

storeRawBladeBrace(value) {
const index = this.rawBladeBraces.push(value) - 1;
return this.getRawBladeBracePlaceholder(index);
}

getRawPlaceholder(replace) {
return _.replace('@__raw_block_#__@', '#', replace);
}
Expand All @@ -126,6 +140,10 @@ export default class Formatter {
return _.replace('___blade_brace_+?#___', '#', replace);
}

getRawBladeBracePlaceholder(replace) {
return _.replace('___raw_blade_brace_#___', '#', replace);
}

restorePhpBlock(content) {
return new Promise((resolve) => resolve(content)).then((res) => {
return _.replace(
Expand Down Expand Up @@ -239,6 +257,28 @@ export default class Formatter {
});
}

restoreRawBladeBrace(content) {
return new Promise((resolve) => resolve(content)).then((res) => {
return _.replace(
res,
new RegExp(`${this.getRawBladeBracePlaceholder('(\\d+)')}`, 'gms'),
(_match, p1) => {
const bladeBrace = this.rawBladeBraces[p1];

if (bladeBrace.trim() === '') {
return `{!!${bladeBrace}!!}`;
}

return `{!! ${util
.formatRawStringAsPhp(bladeBrace)
.replace(/([\n\s]*)->([\n\s]*)/gs, '->')
.trim()
.trimRight('\n')} !!}`;
},
);
});
}

formatAsBlade(content) {
const splitedLines = util.splitByLines(content);
const vsctmModule = new vsctm.VscodeTextmate(this.vsctm, this.oniguruma);
Expand Down
4 changes: 1 addition & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ export async function removeSemicolon(content) {
}

export async function formatAsPhp(content) {
return prettifyPhpContentWithEscapedTags(content)
.then(prettifyPhpContentWithUnescapedTags)
.then(removeSemicolon);
return prettifyPhpContentWithUnescapedTags(content);
}

export async function preserveOriginalPhpTagInHtml(content) {
Expand Down