Skip to content

Commit

Permalink
fix: 🐛 ignore blade style commented lines
Browse files Browse the repository at this point in the history
fixes #8
  • Loading branch information
shufo committed Jul 19, 2020
1 parent f4f5db1 commit ebd8281
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
20 changes: 20 additions & 0 deletions __tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,24 @@ describe('The blade formatter CLI', () => {
expect(longNameResponse).toEqual(data.toString('utf-8'));
});
});

test('should ignore commented lines #8', async () => {
const formatted = await cmd.execute(
path.resolve(__basedir, 'bin', 'blade-formatter'),
[
path.resolve(__basedir, '__tests__', 'fixtures', 'commented.blade.php'),
],
);

const expectedTarget = path.resolve(
__basedir,
'__tests__',
'fixtures',
'formatted.commented.blade.php',
);

fs.readFile(expectedTarget, (err, expected) => {
expect(formatted).toEqual(expected.toString('utf-8'));
});
});
});
14 changes: 14 additions & 0 deletions __tests__/fixtures/commented.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{--@foreach ($downloadOptions as $d => $dO)
<tr>
<td>{{ $dO['description'] }}</td>
<td>{{ $dO['cost'] }}</td>
<td>
<form method="POST" action="{{ route('bonusexchange', ['id' => $dO['id']]) }}">
@csrf
<button type="submit" class="btn btn-sm btn-info btn-exchange">@lang('bon.exchange')</button>
</form>
</td>
</tr>
@endforeach--}}

{{-- @json($array) --}}
14 changes: 14 additions & 0 deletions __tests__/fixtures/formatted.commented.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{--@foreach ($downloadOptions as $d => $dO)
<tr>
<td>{{ $dO['description'] }}</td>
<td>{{ $dO['cost'] }}</td>
<td>
<form method="POST" action="{{ route('bonusexchange', ['id' => $dO['id']]) }}">
@csrf
<button type="submit" class="btn btn-sm btn-info btn-exchange">@lang('bon.exchange')</button>
</form>
</td>
</tr>
@endforeach--}}

{{-- @json($array) --}}
16 changes: 7 additions & 9 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,18 @@ export default class Formatter {
this.processToken(tokenStruct, token);
}

if (this.currentIndentLevel < 0) {
throw Error(
'Parse error: cannot parse file for some reason.' +
'(e.g. inline blade comment block,' +
' unrecognized token, inline php call)',
);
}

this.insertFormattedLineToResult(originalLine);
}

insertFormattedLineToResult(originalLine) {
let calculatedIndentLevel = this.currentIndentLevel;

if (calculatedIndentLevel < 0) {
calculatedIndentLevel = 0;
}

const indentWhiteSpace = this.indentCharacter.repeat(
this.currentIndentLevel * this.indentSize,
calculatedIndentLevel * this.indentSize,
);

const formattedLine = indentWhiteSpace + originalLine;
Expand Down
22 changes: 14 additions & 8 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,19 @@ export function generateDiff(path, originalLines, formattedLines) {
}

export function prettifyPhpContentWithUnescapedTags(content) {
let prettified = _.replace(content, /\{\{--/g, '<?php /*comment*/');
prettified = _.replace(prettified, /--\}\}/g, '/*comment*/ ?>');
prettified = _.replace(prettified, /\{\{/g, '<?php /*blade*/');
prettified = _.replace(prettified, /\}\}/g, '/*blade*/ ?>');
let prettified = _.replace(content, /\{\{([^-].*?)\}\}/sg, (match, p1) => {
return '<?php /*blade*/ ' + p1 + ' /*blade*/ ?>'
});

prettified = prettier.format(prettified, {
parser: 'php',
printWidth: 1000,
singleQuote: true,
});

prettified = _.replace(prettified, /<\?php\s\/\*comment\*\//g, '{{--');
prettified = _.replace(prettified, /\/\*comment\*\/\s\?>/g, '--}}');
prettified = _.replace(prettified, /<\?php\s\/\*blade\*\/\s/g, '{{ ');
prettified = _.replace(prettified, /\/\*blade\*\/\s\?>/g, ' }}');
prettified = _.replace(prettified, /<\?php.*?\/\*blade\*\/\s(.*?)\s\/\*blade\*\/.*?\?>/sg, (match, p1) => {
return '{{ ' + p1 + ' }}';
});

return prettified;
}
Expand All @@ -109,6 +107,8 @@ export function prettifyPhpContentWithEscapedTags(content) {
export function removeSemicolon(content) {
let prettified = _.replace(content, /;\n.*!!\}/g, ' !!}');
prettified = _.replace(prettified, /;\n.*}}/g, ' }}');
prettified = _.replace(prettified, /; }}/g, ' }}');
prettified = _.replace(prettified, /; --}}/g, ' --}}');

return prettified;
}
Expand All @@ -124,6 +124,9 @@ export function formatAsPhp(content) {
export function preserveOriginalPhpTagInHtml(content) {
let prettified = _.replace(content, /<\?php/g, '/* <?phptag_start */');
prettified = _.replace(prettified, /\?>/g, '/* end_phptag?> */');
prettified = _.replace(prettified, /\{\{--(.*?)--\}\}/sg, (match, p1) => {
return '<?php /*comment*/ ?>' + p1 + '<?php /*comment*/ ?>'
});

return prettified;
}
Expand All @@ -132,6 +135,9 @@ export function revertOriginalPhpTagInHtml(content) {
let prettified = _.replace(content, /\/\* <\?phptag_start \*\//g, '<?php');
prettified = _.replace(prettified, /\/\* end_phptag\?> \*\/\s;\n/g, '?>;');
prettified = _.replace(prettified, /\/\* end_phptag\?> \*\//g, '?>');
prettified = _.replace(prettified, /<\?php.*?\/\*comment\*\/\s\?>(.*?)<\?php\s\/\*comment\*\/.*?\?>/sg, (match, p1) => {
return '{{--' + p1 + '--}}';
});

return prettified;
}
Expand Down

0 comments on commit ebd8281

Please sign in to comment.