Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
align: exclude SemicolonClassElement (#2668)
Browse files Browse the repository at this point in the history
[bugfix] `align` with option `"members"`: don't check semicolons in classes
  • Loading branch information
ajafff authored and adidahiya committed May 24, 2017
1 parent 5a85ee6 commit 6e0404a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
16 changes: 11 additions & 5 deletions src/rules/alignRule.ts
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { getNextToken, isBlockLike, isEmptyStatement } from "tsutils";
import { getNextToken, isBlockLike } from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
Expand Down Expand Up @@ -82,7 +82,7 @@ class AlignWalker extends Lint.AbstractWalker<Options> {
public walk(sourceFile: ts.SourceFile) {
const cb = (node: ts.Node): void => {
if (this.options.statements && isBlockLike(node)) {
this.checkAlignment(node.statements.filter((s) => !isEmptyStatement(s)), OPTION_STATEMENTS);
this.checkAlignment(node.statements.filter((s) => s.kind !== ts.SyntaxKind.EmptyStatement), OPTION_STATEMENTS);
} else {
switch (node.kind) {
case ts.SyntaxKind.NewExpression:
Expand Down Expand Up @@ -131,12 +131,18 @@ class AlignWalker extends Lint.AbstractWalker<Options> {
}
break;
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ClassDeclaration:
case ts.SyntaxKind.ClassExpression:
if (this.options.members) {
this.checkAlignment(
(node as ts.ClassLikeDeclaration).members.filter((m) => m.kind !== ts.SyntaxKind.SemicolonClassElement),
OPTION_MEMBERS,
);
}
break;
case ts.SyntaxKind.InterfaceDeclaration:
case ts.SyntaxKind.TypeLiteral:
if (this.options.members) {
this.checkAlignment((node as ts.ClassLikeDeclaration | ts.InterfaceDeclaration | ts.TypeLiteralNode).members,
OPTION_MEMBERS);
this.checkAlignment((node as ts.InterfaceDeclaration | ts.TypeLiteralNode).members, OPTION_MEMBERS);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion test/rules/align/members/test.ts.fix
Expand Up @@ -7,11 +7,18 @@ interface Foo {

class Foo {
bar,
baz() {}
baz() {

};
get bas() {}
foo;
}

let Bar = class {
bar,
foo
}

let foo = {
foo,
moep,
Expand Down
32 changes: 21 additions & 11 deletions test/rules/align/members/test.ts.lint
Expand Up @@ -2,42 +2,52 @@ interface Foo {
bar,
baz
bas
~~~ [members are not aligned]
~~~ [0]
foo;
~~~~ [members are not aligned]
~~~~ [0]
}

class Foo {
bar,
baz() {}
baz() {

};
get bas() {}
~~~~~~~~~~~~ [members are not aligned]
~~~~~~~~~~~~ [0]
foo;
~~~~ [members are not aligned]
~~~~ [0]
}

let Bar = class {
bar,
foo
~~~ [0]
}

let foo = {
foo,
moep,
baz,
~~~ [members are not aligned]
~~~ [0]
baz: foo,
~~~~~~~~ [members are not aligned]
~~~~~~~~ [0]
}

let {
foo,
boo,
baz,
~~~ [members are not aligned]
~~~ [0]
baz,
~~~ [members are not aligned]
~~~ [0]
} = foo;

let foo: {
foo
bar
~~~ [members are not aligned]
~~~ [0]
baz
~~~ [members are not aligned]
~~~ [0]
}

[0]: members are not aligned

0 comments on commit 6e0404a

Please sign in to comment.