Skip to content

Commit

Permalink
Move and improve docs
Browse files Browse the repository at this point in the history
Refs: #17
  • Loading branch information
ajafff committed Jun 30, 2017
1 parent 2acf27c commit d1893dc
Show file tree
Hide file tree
Showing 16 changed files with 579 additions and 440 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_size = 4
indent_style = space
trim_trailing_whitespace = true

[{package,tslint}.json]
indent_size = 2
456 changes: 16 additions & 440 deletions README.md

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions docs/early-exit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## early-exit

Recommends to use an early exit instead of a long `if` block.
This means to `return` early from a function, `continue` early in loop, or `break` early in a switch case.

So instead of:

```ts
function f() {
if (so) {
lots;
of;
code;
}
}
```

Prefer:
```ts
function f() {
if (!so) return;
lots;
of;
code;
}
```

This rule also warns for if-else blocks where one branch is large and the other is a single line.
So instead of:

```ts
for (const x of xs) {
if (so) {
simple;
} else {
lots;
of;
code;
}
}
```

Prefer:

```ts
for (const x of xs) {
if (so) {
simple;
continue;
}

lots;
of;
code;
}
```

An options object as in `"early-exit": [true, { "max-length": 4 }}` may be provided to configure what makes a block count as "large". The default is 2 lines.
96 changes: 96 additions & 0 deletions docs/ext-curly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
## ext-curly

Enforces where to consistently use curly braces where not strictly necessary. If you only want `"always"` or `"as-needed"`, use the `curly` rule of tslint core instead.

This rule defaults to never allow curly bracen when not necessary.

Unnecessary braces can be automatically fixed.

### Options

There are several options that let you specify when you want to use curly braces for consistency. All options can be used together.

#### `"consistent"`

Enforces curly braces on *all* branches of an `if-else` when one branch needs braces.

```ts
// Not Passing
if (foo) {
if (bar)
foo(bar)
} else
baz(); // the if then branch requires braces, therefore the else branch also needs them

if (foo) foo() // this branch needs to get braces
else if (bar) bar() // this branch needs to get braces
else {}

// Passing
if (foo)
foo()

if (foo)
foo()
else
bar()

if (foo)
foo()
else if (bar)
bar()
else
baz()
```

#### `"else"`

Enforces curly braces on all branches of an `if` statement if it contains an else statement. This option is a superset of `"consistent"`.

```ts
// Not passing
if (foo) foo()
else bar() // both branches need to get braces

// Passing
if (foo)
foo()

```

#### `"braced-child"`

Enforces curly braces if the child statement has or needs curly braces. That includes `try...catch`, `switch`, `while` or `for` loops and `if` statements where at least one branch has curly braces.

```ts
// Not passing
if (foo) // if statement needs braces
switch (foo.bar) {
default:
}

while (true) // while statement needs braces
try {
doStuff()
} catch (e) {}

for (;;) // for statement needs braces
if (foo) {
foo();
bar();
} else
baz();
```

#### `"nested-if-else"`

Enforces curly braces when the nested statement is an `if` statement with an `else` branch.

```ts
// Not passing
for (;;) // for statement needs braces
if (foo)
foo();
else
baz();
```

0 comments on commit d1893dc

Please sign in to comment.