Skip to content

Commit

Permalink
fix: 🐛 do nothing if something goes wrong while formatting
Browse files Browse the repository at this point in the history
Closes #128
  • Loading branch information
shufo committed Nov 3, 2020
1 parent 1d2fbd7 commit d46c7e6
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 8 deletions.
40 changes: 40 additions & 0 deletions __tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,44 @@ describe('The blade formatter CLI', () => {

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

test('show error message if unexpected syntax', async () => {
const cmdResult = await spawnSync(
'/bin/cat',
[
'__tests__/fixtures/escaped_bug.blade.php',
'|',
'./bin/blade-formatter',
'--stdin',
],
{ stdio: 'pipe', shell: true },
).stdout.toString();

expect(cmdResult).toEqual(expect.stringContaining("Can't format blade"));
});

test('Do nothing if something goes wrong #128', async () => {
const originalContent = fs.readFileSync(
path.resolve(__basedir, '__tests__', 'fixtures', 'escaped_bug.blade.php'),
);

await cmd.execute(
path.resolve(__basedir, 'bin', 'blade-formatter'),
[
path.resolve(
__basedir,
'__tests__',
'fixtures',
'escaped_bug.blade.php',
),
],
'-w',
);

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

expect(originalContent).toEqual(result);
});
});
54 changes: 54 additions & 0 deletions __tests__/fixtures/escaped_bug.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<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>
4 changes: 1 addition & 3 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ export default class Formatter {
.formatAsPhp(content)
.then((formattedAsPhp) => this.formatAsHtml(formattedAsPhp))
.then((formattedAsHtml) => this.formatAsBlade(formattedAsHtml))
.then((formattedAsBlade) => {
return formattedAsBlade;
});
.then((formattedResult) => util.checkResult(formattedResult));
}

formatAsHtml(data) {
Expand Down
4 changes: 3 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export class BladeFormatter {

format(content, opts = {}) {
const options = this.options || opts;
return new Formatter(options).formatContent(content);
return new Formatter(options)
.formatContent(content)
.catch((err) => console.log(err));
}

async formatFromCLI() {
Expand Down
32 changes: 28 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ export function preserveDirectives(content) {
'gis',
);
return _.replace(res, regex, (match, p1, p2, p3) => {
return `<beautify start="${p1}${p2}" exp="^^^${p3}^^^">`;
return `<beautifyTag start="${p1}${p2}" exp="^^^${p3}^^^">`;
});
})
.then((res) => {
const regex = new RegExp(`(${phpKeywordEndTokens.join('|')})`, 'gis');
return _.replace(res, regex, (match, p1) => {
return `</beautify end="${p1}">`;
return `</beautifyTag end="${p1}">`;
});
});
}
Expand All @@ -233,14 +233,14 @@ export function revertDirectives(content) {
.then((res) => {
return _.replace(
res,
/<beautify.*?start="(.*?)".*?exp="\^\^\^(.*?)\^\^\^">/gs,
/<beautifyTag.*?start="(.*?)".*?exp="\^\^\^(.*?)\^\^\^">/gs,
(match, p1, p2) => {
return `${p1}(${p2})`;
},
);
})
.then((res) => {
return _.replace(res, /<\/beautify end="(.*?)">/gs, (match, p1) => {
return _.replace(res, /<\/beautifyTag end="(.*?)">/gs, (match, p1) => {
return `${p1}`;
});
});
Expand All @@ -253,3 +253,27 @@ export function printDescription() {
process.stdout.write(chalk.bold.red('Errors: E\n'));
process.stdout.write(chalk.bold('Not Changed: ') + chalk.bold.green('.\n'));
}

const escapeTags = [
'/\\*\\* phptag_start \\*\\*/',
'/\\*\\* end_phptag \\*\\*/',
'/\\*escaped\\*/',
'__BLADE__;',
'/\\* blade_comment_start \\*/',
'/\\* blade_comment_end \\*/',
'beautifyTag',
];

export function checkResult(formatted) {
if (new RegExp(escapeTags.join('|')).test(formatted)) {
throw new Error(
[
`Can't format blade: something goes wrong.`,
// eslint-disable-next-line max-len
`Please check if template is too complicated or not. Or simplify template might solves issue.`,
].join('\n'),
);
}

return formatted;
}

0 comments on commit d46c7e6

Please sign in to comment.