From ebd82810ae6a552d07873ffffabf898af24f0f19 Mon Sep 17 00:00:00 2001 From: shufo Date: Sun, 19 Jul 2020 12:46:28 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20ignore=20blade=20style=20?= =?UTF-8?q?commented=20lines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #8 --- __tests__/cli.test.js | 20 +++++++++++++++++ __tests__/fixtures/commented.blade.php | 14 ++++++++++++ .../fixtures/formatted.commented.blade.php | 14 ++++++++++++ src/formatter.js | 16 ++++++-------- src/util.js | 22 ++++++++++++------- 5 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 __tests__/fixtures/commented.blade.php create mode 100644 __tests__/fixtures/formatted.commented.blade.php diff --git a/__tests__/cli.test.js b/__tests__/cli.test.js index 3037ec96..cd515820 100644 --- a/__tests__/cli.test.js +++ b/__tests__/cli.test.js @@ -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')); + }); + }); }); diff --git a/__tests__/fixtures/commented.blade.php b/__tests__/fixtures/commented.blade.php new file mode 100644 index 00000000..3276fcdd --- /dev/null +++ b/__tests__/fixtures/commented.blade.php @@ -0,0 +1,14 @@ +{{--@foreach ($downloadOptions as $d => $dO) + +{{ $dO['description'] }} +{{ $dO['cost'] }} + +
+ @csrf + +
+ + +@endforeach--}} + +{{-- @json($array) --}} diff --git a/__tests__/fixtures/formatted.commented.blade.php b/__tests__/fixtures/formatted.commented.blade.php new file mode 100644 index 00000000..15168722 --- /dev/null +++ b/__tests__/fixtures/formatted.commented.blade.php @@ -0,0 +1,14 @@ +{{--@foreach ($downloadOptions as $d => $dO) + + {{ $dO['description'] }} + {{ $dO['cost'] }} + +
+ @csrf + +
+ + +@endforeach--}} + +{{-- @json($array) --}} diff --git a/src/formatter.js b/src/formatter.js index 58faa33c..bbdc32d7 100644 --- a/src/formatter.js +++ b/src/formatter.js @@ -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; diff --git a/src/util.js b/src/util.js index eabf01d2..306b3711 100644 --- a/src/util.js +++ b/src/util.js @@ -71,10 +71,9 @@ export function generateDiff(path, originalLines, formattedLines) { } export function prettifyPhpContentWithUnescapedTags(content) { - let prettified = _.replace(content, /\{\{--/g, ''); - prettified = _.replace(prettified, /\{\{/g, ''); + let prettified = _.replace(content, /\{\{([^-].*?)\}\}/sg, (match, p1) => { + return '' + }); prettified = prettier.format(prettified, { parser: 'php', @@ -82,10 +81,9 @@ export function prettifyPhpContentWithUnescapedTags(content) { 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; } @@ -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; } @@ -124,6 +124,9 @@ export function formatAsPhp(content) { export function preserveOriginalPhpTagInHtml(content) { let prettified = _.replace(content, /<\?php/g, '/* /g, '/* end_phptag?> */'); + prettified = _.replace(prettified, /\{\{--(.*?)--\}\}/sg, (match, p1) => { + return '' + p1 + '' + }); return prettified; } @@ -132,6 +135,9 @@ export function revertOriginalPhpTagInHtml(content) { let prettified = _.replace(content, /\/\* <\?phptag_start \*\//g, ' \*\/\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; }