Skip to content

Commit bdcc191

Browse files
committed
Merge pull request #29 from AlexHaxe/LeftCurly
added support for nlow to LeftCurlyCheck
2 parents e97340e + 423296b commit bdcc191

File tree

2 files changed

+102
-13
lines changed

2 files changed

+102
-13
lines changed

checkstyle/checks/LeftCurlyCheck.hx

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class LeftCurlyCheck extends Check {
2626

2727
public static inline var EOL:String = "eol";
2828
public static inline var NL:String = "nl";
29+
public static inline var NLOW:String = "nlow";
2930

3031
public var tokens:Array<String>;
3132
public var option:String;
@@ -101,7 +102,7 @@ class LeftCurlyCheck extends Check {
101102
switch (field.kind) {
102103
case FFun(f):
103104
if (!hasToken(FUNCTION)) return;
104-
checkBlocks(f.expr);
105+
checkBlocks(f.expr, isFieldWrapped(field));
105106
default:
106107
}
107108
}
@@ -140,25 +141,25 @@ class LeftCurlyCheck extends Check {
140141
checkBlocks(f.expr);
141142
case EFor(it, expr):
142143
if (!hasToken(FOR)) return;
143-
checkBlocks(expr);
144+
checkBlocks(expr, isWrapped(it));
144145
case EIf(econd, eif, eelse):
145146
if (!hasToken(IF)) return;
146-
checkBlocks(eif);
147+
checkBlocks(eif, isWrapped(econd));
147148
checkBlocks(eelse);
148149
case EWhile(econd, expr, _):
149150
if (!hasToken(WHILE)) return;
150-
checkBlocks(expr);
151+
checkBlocks(expr, isWrapped(econd));
151152
case ESwitch(expr, cases, edef):
152153
if (!hasToken(SWITCH)) return;
153154
var firstCase:Expr = edef;
154155
if (cases.length > 0) {
155156
firstCase = cases[0].values[0];
156157
}
157158
if (firstCase == null) {
158-
checkLinesBetween(e.pos.min, e.pos.max, e.pos);
159+
checkLinesBetween(e.pos.min, e.pos.max, isWrapped(expr), e.pos);
159160
return;
160161
}
161-
checkLinesBetween(expr.pos.max, firstCase.pos.min, e.pos);
162+
checkLinesBetween(expr.pos.max, firstCase.pos.min, isWrapped(expr), e.pos);
162163
case ETry(expr, catches):
163164
if (!hasToken(TRY)) return;
164165
checkBlocks(expr);
@@ -170,30 +171,53 @@ class LeftCurlyCheck extends Check {
170171
});
171172
}
172173

173-
function checkBlocks(e:Expr) {
174+
function isFieldWrapped(field:Field):Bool {
175+
var pos1:Int = field.pos.min;
176+
var pos2:Int = pos1;
177+
switch (field.kind) {
178+
case FFun(f):
179+
if (f.expr == null) {
180+
return false;
181+
}
182+
pos2 = f.expr.pos.min;
183+
default:
184+
return false;
185+
}
186+
187+
var functionDef:String = checker.file.content.substring(pos1, pos2);
188+
return (functionDef.indexOf('\n') >= 0) ||
189+
(functionDef.indexOf('\r') >= 0);
190+
}
191+
192+
function isWrapped(e:Expr):Bool {
193+
if (e == null) return false;
194+
return (checker.getLinePos(e.pos.min).line != checker.getLinePos(e.pos.max).line);
195+
}
196+
197+
function checkBlocks(e:Expr, wrapped:Bool = false) {
174198
if ((e == null) || (e.expr == null)) return;
175199

176200
switch(e.expr) {
177201
case EBlock(_):
178202
var linePos:LinePos = checker.getLinePos(e.pos.min);
179203
var line:String = checker.lines[linePos.line];
180-
checkLeftCurly(line, e.pos);
204+
checkLeftCurly(line, wrapped, e.pos);
181205
default:
182206
}
183207
}
184208

185-
function checkLinesBetween(min:Int, max:Int, pos:Position) {
209+
function checkLinesBetween(min:Int, max:Int, wrapped:Bool = false, pos:Position) {
186210
if (isPosSuppressed(pos)) return;
187211
var bracePos:Int = checker.file.content.lastIndexOf("{", max);
188212
if (bracePos < 0 || bracePos < min) return;
189213

190214
var lineNum:Int = checker.getLinePos(bracePos).line;
191215
var line:String = checker.lines[lineNum];
192-
checkLeftCurly(line, pos);
216+
checkLeftCurly(line, wrapped, pos);
193217
}
194218

195219
@SuppressWarnings("checkstyle:BlockFormat")
196-
function checkLeftCurly(line:String, pos:Position) {
220+
function checkLeftCurly(line:String, wrapped:Bool = false, pos:Position) {
197221
var lineLength:Int = line.length;
198222

199223
// must have at least one non whitespace character before curly
@@ -205,12 +229,14 @@ class LeftCurlyCheck extends Check {
205229
try {
206230
if (curlyAtEOL) {
207231
logErrorIf ((option == NL), 'Left curly should be on new line (only whitespace before curly)', pos);
208-
logErrorIf ((option != EOL), 'Left curly unknown option ${option}', pos);
232+
logErrorIf ((option == NLOW) && wrapped, 'Left curly should be on new line (previous expression is split over muliple lines)', pos);
233+
logErrorIf ((option != EOL) && (option != NLOW), 'Left curly unknown option ${option}', pos);
209234
return;
210235
}
211236
logErrorIf ((option == EOL), 'Left curly should be at EOL (only linebreak or comment after curly)', pos);
212237
logErrorIf ((!curlyOnNL), 'Left curly should be on new line (only whitespace before curly)', pos);
213-
logErrorIf ((option != NL), 'Left curly unknown option ${option}', pos);
238+
logErrorIf ((option == NLOW) && !wrapped, 'Left curly should be at EOL (previous expression is not split over muliple lines)', pos);
239+
logErrorIf ((option != NL) && (option != NLOW), 'Left curly unknown option ${option}', pos);
214240
}
215241
catch (e:String) {
216242
// one of the error messages fired -> do nothing

test/LeftCurlyCheckTest.hx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ class LeftCurlyCheckTest extends CheckTestCase {
5353
check.option = LeftCurlyCheck.NL;
5454
assertMsg(check, LeftCurlyTests.TEST15, '');
5555
}
56+
57+
public function testNLOW() {
58+
var check = new LeftCurlyCheck();
59+
check.option = LeftCurlyCheck.NLOW;
60+
assertMsg(check, LeftCurlyTests.TEST, '');
61+
assertMsg(check, LeftCurlyTests.TEST12, '');
62+
assertMsg(check, LeftCurlyTests.TEST16, '');
63+
assertMsg(check, LeftCurlyTests.TEST17, 'Left curly should be at EOL (previous expression is not split over muliple lines)');
64+
assertMsg(check, LeftCurlyTests.TEST18, 'Left curly should be on new line (previous expression is split over muliple lines)');
65+
assertMsg(check, LeftCurlyTests.TEST19, 'Left curly should be on new line (previous expression is split over muliple lines)');
66+
}
5667
}
5768

5869
class LeftCurlyTests {
@@ -244,4 +255,56 @@ class LeftCurlyTests {
244255
}
245256
}
246257
}";
258+
259+
public static inline var TEST16:String = "
260+
class Test {
261+
public function test(val:Int,
262+
val2:Int):String
263+
{
264+
switch(val * 10 -
265+
val / 10)
266+
{
267+
case 0: // do nothing
268+
default:
269+
}
270+
}
271+
}";
272+
273+
public static inline var TEST17:String = "
274+
class Test {
275+
public function test(val:Int, val2:Int):String
276+
{
277+
switch(val * 10 - val / 10)
278+
{
279+
case 1: // do nothing
280+
default:
281+
}
282+
}
283+
}";
284+
285+
public static inline var TEST18:String = "
286+
class Test {
287+
public function test(val:Int,
288+
val2:Int):String {
289+
switch(val * 10 -
290+
val / 10)
291+
{
292+
case 0: // do nothing
293+
default:
294+
}
295+
}
296+
}";
297+
298+
public static inline var TEST19:String = "
299+
class Test {
300+
public function test(val:Int,
301+
val2:Int):String
302+
{
303+
switch(val * 10 -
304+
val / 10) {
305+
case 0: // do nothing
306+
default:
307+
}
308+
}
309+
}";
247310
}

0 commit comments

Comments
 (0)