Skip to content

Commit

Permalink
curly: don't put opening curly inside comment (palantir#3473)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored Nov 11, 2017
1 parent 6d3f78e commit 5a9cacc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
9 changes: 4 additions & 5 deletions src/rules/curlyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ class CurlyWalker extends Lint.AbstractWalker<Options> {
private createMissingBraceFix(statement: ts.Statement, node: ts.IterationStatement | ts.IfStatement, sameLine: boolean) {
if (sameLine) {
return [
Lint.Replacement.appendText(statement.getStart(), "{ "),
Lint.Replacement.appendText(statement.getEnd(), " }"),
Lint.Replacement.appendText(statement.pos, " {"),
Lint.Replacement.appendText(statement.end, " }"),
];
} else {
const match = /\n([\t ])/.exec(node.getFullText(this.sourceFile)); // determine which character to use (tab or space)
Expand All @@ -164,9 +164,8 @@ class CurlyWalker extends Lint.AbstractWalker<Options> {
const maybeCarriageReturn = this.sourceFile.text[this.sourceFile.getLineEndOfPosition(node.pos) - 1] === "\r" ? "\r" : "";

return [
Lint.Replacement.appendText(
this.sourceFile.getLineEndOfPosition(statement.pos), " {"),
Lint.Replacement.appendText(statement.getEnd(), `${maybeCarriageReturn}\n${indentation}}`),
Lint.Replacement.appendText(statement.pos, " {"),
Lint.Replacement.appendText(statement.end, `${maybeCarriageReturn}\n${indentation}}`),
];
}
}
Expand Down
19 changes: 12 additions & 7 deletions test/rules/curly/defaults/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (y == 4)
console.log("y");
}

if (z == 5) // failure {
if (z == 5) { // failure
console.log("z");
}

Expand All @@ -24,19 +24,19 @@ for (j = 0; j < 1; ++j)
console.log("j");
}

for (k = 0; k < 1; ++k) // failure {
for (k = 0; k < 1; ++k) { // failure
console.log("k");
}

for (k1 in l1) { console.log("l1"); } // failure

for (k in l) // failure {
for (k in l) { // failure
console.log("l");
}

for (k3 of l3) { console.log("l3"); } // failure

for (k4 of l4) // failure {
for (k4 of l4) { // failure
console.log("l4");
}

Expand All @@ -51,7 +51,7 @@ while (n < 0)
console.log("r");
}

while (n < 0) // failure {
while (n < 0) { // failure
console.log("s");
}

Expand All @@ -71,7 +71,7 @@ do
}
while (j == 1);

do // failure {
do { // failure
console.log("o");
}
while (k == 1);
Expand All @@ -88,6 +88,11 @@ for (let x of [1, 2, 3]) {
console.log(x);
}

for (let y of [1, 2, 3]) // failure {
for (let y of [1, 2, 3]) { // failure
console.log(y);
}

if (true) { /* some
comment */
console.log(z);
}
7 changes: 7 additions & 0 deletions test/rules/curly/defaults/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,10 @@ for (let y of [1, 2, 3]) // failure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
console.log(y);
~~~~~~~~~~~~~~~~~~~ [for statements must be braced]

if (true) /* some
~~~~~~~~~~~~~~~~~
comment */
~~~~~~~~~~~~~~~~~~~~~~~
console.log(z);
~~~~~~~~~~~~~~~~~~~ [if statements must be braced]
14 changes: 7 additions & 7 deletions test/rules/curly/ignore-same-line/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (y == 4)
console.log("y");
}

if (z == 5) // failure {
if (z == 5) { // failure
console.log("z");
}

Expand All @@ -24,19 +24,19 @@ for (j = 0; j < 1; ++j)
console.log("j");
}

for (k = 0; k < 1; ++k) // failure {
for (k = 0; k < 1; ++k) { // failure
console.log("k");
}

for (k1 in l1) console.log("l1");

for (k in l) // failure {
for (k in l) { // failure
console.log("l");
}

for (k3 of l3) console.log("l3");

for (k4 of l4) // failure {
for (k4 of l4) { // failure
console.log("l4");
}

Expand All @@ -51,7 +51,7 @@ while (n < 0)
console.log("r");
}

while (n < 0) // failure {
while (n < 0) { // failure
console.log("s");
}

Expand All @@ -71,7 +71,7 @@ do
}
while (j == 1);

do // failure {
do { // failure
console.log("o");
}
while (k == 1);
Expand All @@ -88,6 +88,6 @@ for (let x of [1, 2, 3]) {
console.log(x);
}

for (let y of [1, 2, 3]) // failure {
for (let y of [1, 2, 3]) { // failure
console.log(y);
}

0 comments on commit 5a9cacc

Please sign in to comment.