Skip to content

Commit

Permalink
fix: 🐛 support case insensitive directive
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Aug 4, 2020
1 parent 31cc1af commit fbb3a6d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
38 changes: 38 additions & 0 deletions __tests__/formatter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,44 @@ describe('formatter', () => {
});
});

test('directive token should case insensitive', () => {
const content = [
`<div>`,
`@Section('foo')`,
`@section('bar')`,
`@if($user)`,
`{{ $user->name }}`,
`@foreach($users as $user)`,
`{{ $user->id }}`,
`@endForeach`,
`@endIf`,
`@endSection`,
`</div>`,
``,
].join('\n');

const expected = [
`<div>`,
` @Section('foo')`,
` @section('bar')`,
` @if ($user)`,
` {{ $user->name }}`,
` @foreach ($users as $user)`,
` {{ $user->id }}`,
` @endForeach`,
` @endIf`,
` @endSection`,
`</div>`,
``,
].join('\n');

return formatter()
.formatContent(content)
.then(function (result) {
assert.equal(result, expected);
});
});

test('multiple section directive test', function () {
const content = [
`<div>`,
Expand Down
4 changes: 2 additions & 2 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default class Formatter {
}

if (_.includes(phpKeywordEndTokens, token)) {
if (_.last(this.stack) !== '@hasSection') {
if (_.last(this.stack) !== '@hassection') {
this.stack.pop();
return;
}
Expand Down Expand Up @@ -164,7 +164,7 @@ export default class Formatter {
return;
}

this.processKeyword(token);
this.processKeyword(token.toLowerCase());
}

processTokenizeResult(tokenizeLineResult, originalLine) {
Expand Down
2 changes: 1 addition & 1 deletion src/indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const indentStartTokens = [
'@foreach',
'@forelse',
'@guest',
'@hasSection',
'@hassection',
'@if',
'@isset',
'@permission',
Expand Down
4 changes: 2 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ export function preserveDirectives(content) {
`(${phpKeywordStartTokens.join(
'|',
)})([\\s]*?)\\(((?:[^)(]+|\\((?:[^)(]+|\\([^)(]*\\))*\\))*)\\)`,
'gs',
'gis',
);
return _.replace(res, regex, (match, p1, p2, p3) => {
return `<beautify start="${p1}${p2}" exp="^^^${p3}^^^">`;
});
})
.then((res) => {
const regex = new RegExp(`(${phpKeywordEndTokens.join('|')})`, 'gs');
const regex = new RegExp(`(${phpKeywordEndTokens.join('|')})`, 'gis');
return _.replace(res, regex, (match, p1) => {
return `</beautify end="${p1}">`;
});
Expand Down

0 comments on commit fbb3a6d

Please sign in to comment.