Skip to content

Commit 59c91b5

Browse files
✨ Add fixer for Delimiter spacing
1 parent d40e960 commit 59c91b5

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

TwigCS/Ruleset/Generic/DelimiterSpacingSniff.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,19 @@ public function processStart(int $tokenPosition, array $tokens)
6262
}
6363

6464
if (1 !== $count) {
65-
$this->addMessage(
65+
$fix = $this->addFixableMessage(
6666
$this::MESSAGE_TYPE_ERROR,
6767
sprintf('Expecting 1 whitespace after "%s"; found %d', $token->getValue(), $count),
6868
$token
6969
);
70+
71+
if ($fix) {
72+
if (0 === $count) {
73+
$this->fixer->addContent($tokenPosition, ' ');
74+
} else {
75+
$this->fixer->replaceToken($tokenPosition + 1, ' ');
76+
}
77+
}
7078
}
7179
}
7280

@@ -92,11 +100,19 @@ public function processEnd(int $tokenPosition, array $tokens)
92100
}
93101

94102
if (1 !== $count) {
95-
$this->addMessage(
103+
$fix = $this->addFixableMessage(
96104
$this::MESSAGE_TYPE_ERROR,
97105
sprintf('Expecting 1 whitespace before "%s"; found %d', $token->getValue(), $count),
98106
$token
99107
);
108+
109+
if ($fix) {
110+
if (0 === $count) {
111+
$this->fixer->addContentBefore($tokenPosition, ' ');
112+
} else {
113+
$this->fixer->replaceToken($tokenPosition - 1, ' ');
114+
}
115+
}
100116
}
101117
}
102118
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{{ foo }}
2+
{# comment #}
3+
{% if foo %}{% endif %}
4+
5+
{{- foo -}}
6+
{#- comment -#}
7+
{%- if foo -%}{%- endif -%}
8+
9+
{{
10+
shouldNotCareAboutNewLine
11+
}}
12+
{%- if foo -%}{%- endif -%}
13+
14+
{{ foo({'bar': {'baz': 'shouldNotCareAboutDoubleHashes'}}) }}

0 commit comments

Comments
 (0)