Skip to content

Commit

Permalink
Merge pull request #484 from shufo/fix/case-directive-line-break
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo authored Feb 20, 2022
2 parents 2f195bd + 898f8ce commit bd96b47
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 14 deletions.
2 changes: 2 additions & 0 deletions __tests__/fixtures/formatted.case_directive.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
@case(1)
First case...
@break

@case(2)
Second case...
@break

@default
Default case...
@endswitch
Expand Down
129 changes: 129 additions & 0 deletions __tests__/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2164,4 +2164,133 @@ describe('formatter', () => {

await util.doubleFormatCheck(content, expected);
});

test('line break around @case, @break and @default', async () => {
const content = [
`@switch($type) @case(1) $a = 3; @break @case(2) @case(3) $a = 4; @break @default $a = null; @endswitch`,
`<div>`,
`@switch($type) @case(1) $a = 3; @break @case(2) @case(3) $a = 4; @break @default $a = null; @endswitch`,
`</div>`,
`@switch($type) @case(1) $a = 3; @break @case(2) @case(3) $a = 4; @break @default $a = null; @endswitch`,
`@section('aaa')`,
`@switch($type)`,
`@case(1)`,
`$a = 3;`,
`@break`,
``,
`@case(2)`,
`@case(3)`,
`$a = 4;`,
`@break`,
``,
`@default`,
`$a = null;`,
`@endswitch`,
`@endsection`,
`<div>`,
`@switch($i)`,
` @case(1)`,
` @switch($j)`,
` @case(1)`,
` First case...`,
` @break`,
` @case(2)`,
` Second case...`,
` @break`,
` @default`,
` Default case...`,
` @endswitch`,
` @break`,
` @case(2)`,
` hogehoge...`,
` @break`,
`@endswitch`,
`</div>`,
].join('\n');

const expected = [
`@switch($type)`,
` @case(1)`,
` $a = 3;`,
` @break`,
``,
` @case(2)`,
` @case(3)`,
` $a = 4;`,
` @break`,
``,
` @default`,
` $a = null;`,
`@endswitch`,
`<div>`,
` @switch($type)`,
` @case(1)`,
` $a = 3;`,
` @break`,
``,
` @case(2)`,
` @case(3)`,
` $a = 4;`,
` @break`,
``,
` @default`,
` $a = null;`,
` @endswitch`,
`</div>`,
`@switch($type)`,
` @case(1)`,
` $a = 3;`,
` @break`,
``,
` @case(2)`,
` @case(3)`,
` $a = 4;`,
` @break`,
``,
` @default`,
` $a = null;`,
`@endswitch`,
`@section('aaa')`,
` @switch($type)`,
` @case(1)`,
` $a = 3;`,
` @break`,
``,
` @case(2)`,
` @case(3)`,
` $a = 4;`,
` @break`,
``,
` @default`,
` $a = null;`,
` @endswitch`,
`@endsection`,
`<div>`,
` @switch($i)`,
` @case(1)`,
` @switch($j)`,
` @case(1)`,
` First case...`,
` @break`,
``,
` @case(2)`,
` Second case...`,
` @break`,
``,
` @default`,
` Default case...`,
` @endswitch`,
` @break`,
``,
` @case(2)`,
` hogehoge...`,
` @break`,
``,
` @endswitch`,
`</div>`,
``,
].join('\n');

await util.doubleFormatCheck(content, expected);
});
});
79 changes: 67 additions & 12 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class Formatter {

classes: any;

currentIndentLevel: any;
currentIndentLevel: number;

diffs: any;

Expand Down Expand Up @@ -308,6 +308,42 @@ export default class Formatter {
* @returns
*/
breakLineBeforeAndAfterDirective(content: string): string {
const unbalancedConditions = ['@case'];

_.forEach(unbalancedConditions, (directive) => {
// eslint-disable-next-line
content = _.replace(
content,
new RegExp(`(\\s*?)(${directive})(\\s*?)${nestedParenthesisRegex}(\\s*)`, 'gmi'),
(match) => {
return `\n${match.trim()}\n`;
},
);
});

// eslint-disable-next-line
content = _.replace(content, /@case\S*?\s*?@case/gim, (match) => {
return `${match.replace('\n', '')}`;
});

const unbalancedEchos = ['@break'];

_.forEach(unbalancedEchos, (directive) => {
// eslint-disable-next-line
content = _.replace(content, new RegExp(`(\\s*?)${directive}\\s*`, 'gmi'), (match) => {
return `\n${match.trim()}\n\n`;
});
});

// other directives
_.forEach(['@default'], (directive) => {
// eslint-disable-next-line
content = _.replace(content, new RegExp(`(\\s*?)${directive}\\s*`, 'gmi'), (match) => {
return `\n\n${match.trim()}\n`;
});
});

// add line break around balanced directives
const directives = _.chain(indentStartTokens)
.map((x: any) => _.replace(x, /@/, ''))
.value();
Expand Down Expand Up @@ -1036,15 +1072,24 @@ export default class Formatter {
processKeyword(token: any) {
if (_.includes(phpKeywordStartTokens, token)) {
if (_.last(this.stack) === '@case' && token === '@case') {
this.currentIndentLevel -= 1;
return;
this.decrementIndentLevel();
}

if (token === '@case') {
this.shouldBeIndent = true;
}

this.stack.push(token);
return;
}

if (_.includes(phpKeywordEndTokens, token)) {
if (token === '@break') {
this.decrementIndentLevel();
this.stack.pop();
return;
}

if (_.last(this.stack) !== '@hassection') {
this.stack.pop();
return;
Expand All @@ -1058,33 +1103,36 @@ export default class Formatter {

if (_.includes(indentStartOrElseTokens, token)) {
if (_.includes(tokenForIndentStartOrElseTokens, _.last(this.stack))) {
this.currentIndentLevel -= 1;
this.decrementIndentLevel();
this.shouldBeIndent = true;
}
}

if (_.includes(indentStartTokens, token)) {
if (_.last(this.stack) === '@section' && token === '@section') {
if (this.currentIndentLevel > 0) this.currentIndentLevel -= 1;
if (this.currentIndentLevel > 0) this.decrementIndentLevel();
this.shouldBeIndent = true;
this.stack.push(token);
} else {
this.shouldBeIndent = true;
this.stack.push(token);
}
}

if (_.includes(indentEndTokens, token)) {
if (_.last(this.stack) === '@default') {
this.currentIndentLevel -= 1;
if (token === '@endswitch' && _.last(this.stack) === '@default') {
this.decrementIndentLevel(2);
this.shouldBeIndent = false;
return;
}

this.currentIndentLevel -= 1;
this.decrementIndentLevel();
this.shouldBeIndent = false;
this.stack.pop();
}

if (_.includes(indentElseTokens, token)) {
this.currentIndentLevel -= 1;
this.decrementIndentLevel();
this.shouldBeIndent = true;
}
}
Expand Down Expand Up @@ -1147,7 +1195,7 @@ export default class Formatter {

processTokenizeResult(tokenizeLineResult: any, originalLine: any) {
if (this.shouldBeIndent) {
this.currentIndentLevel += 1;
this.incrementIndentLevel();
this.shouldBeIndent = false;
}

Expand All @@ -1170,8 +1218,7 @@ export default class Formatter {
insertFormattedLineToResult(originalLine: any) {
const originalLineWhitespaces = detectIndent(originalLine).amount;
const whitespaces = originalLineWhitespaces + this.indentSize * this.currentIndentLevel;

const formattedLine = this.indentCharacter.repeat(whitespaces < 0 ? 0 : whitespaces) + originalLine.trimLeft();
const formattedLine = this.indentCharacter.repeat(whitespaces < 0 ? 0 : whitespaces) + originalLine.trim();

// blankline
if (originalLine.length === 0) {
Expand All @@ -1190,4 +1237,12 @@ export default class Formatter {
});
}
}

incrementIndentLevel(level = 1) {
this.currentIndentLevel += level;
}

decrementIndentLevel(level = 1) {
this.currentIndentLevel -= level;
}
}
7 changes: 5 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,20 @@ export function unindent(directive: any, content: any, level: any, options: any)
}

export function preserveDirectives(content: any) {
const startTokens = _.without(phpKeywordStartTokens, '@case');
const endTokens = _.without(phpKeywordEndTokens, '@break');

return new Promise((resolve) => resolve(content))
.then((res: any) => {
const regex = new RegExp(`(${phpKeywordStartTokens.join('|')})([\\s]*?)${nestedParenthesisRegex}`, 'gis');
const regex = new RegExp(`(${startTokens.join('|')})([\\s]*?)${nestedParenthesisRegex}`, 'gis');
return _.replace(
res,
regex,
(match: any, p1: any, p2: any, p3: any) => `<beautifyTag start="${p1}${p2}" exp="^^^${_.escape(p3)}^^^">`,
);
})
.then((res: any) => {
const regex = new RegExp(`(?!end=".*)(${phpKeywordEndTokens.join('|')})(?!.*")`, 'gi');
const regex = new RegExp(`(?!end=".*)(${endTokens.join('|')})(?!.*")`, 'gi');
return _.replace(res, regex, (match: any, p1: any) => `</beautifyTag end="${p1}">`);
});
}
Expand Down

0 comments on commit bd96b47

Please sign in to comment.