Skip to content

Commit

Permalink
update spaceWithinParensRule to always allow empty parens (palantir#3513
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jbsingh authored and HyphnKnight committed Apr 9, 2018
1 parent c9b4ccf commit 87910c3
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/rules/spaceWithinParensRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "space-within-parens",
description: "Enforces spaces within parentheses or disallow them.",
description: "Enforces spaces within parentheses or disallow them. Empty parentheses () are always allowed.",
hasFix: true,
optionsDescription: Lint.Utils.dedent`
You may enforce the amount of whitespace within parentheses.
Expand Down Expand Up @@ -73,9 +73,13 @@ class SpaceWithinParensWalker extends Lint.AbstractWalker<Options> {
public walk(sourceFile: ts.SourceFile) {
forEachToken(sourceFile, (token: ts.Node) => {
if (token.kind === ts.SyntaxKind.OpenParenToken) {
this.checkOpenParenToken(token);
if (sourceFile.text.charAt(token.end) !== ")") {
this.checkOpenParenToken(token);
}
} else if (token.kind === ts.SyntaxKind.CloseParenToken) {
this.checkCloseParenToken(token);
if (sourceFile.text.charAt(token.end - 2) !== "(") {
this.checkCloseParenToken(token);
}
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions test/rules/space-within-parens/force-one-space/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ export function rgb2lab( r : number, g : number, b : number ) : { L : number; a

foo( /*no param*/ );

// empty parens are always allowed
foo();

3 changes: 3 additions & 0 deletions test/rules/space-within-parens/force-one-space/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ foo(/*no param*/);
~nil [0]
~nil [0]

// empty parens are always allowed
foo();

[0]: Needs 1 whitespace within parentheses
[1]: No more than 1 whitespace within parentheses allowed
8 changes: 7 additions & 1 deletion test/rules/space-within-parens/force-two-spaces/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ if ( x === y ) {
return ( {result: true, error: ( x + y )} );
}

// be well
// expected number of spaces provided
new Foo( );

// empty parens are always allowed
new Foo();

// Missing a space
new Foo( );

10 changes: 8 additions & 2 deletions test/rules/space-within-parens/force-two-spaces/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ if (x === y) {
~ [2]
}

// be well
// expected number of spaces provided
new Foo( );

// empty parens are always allowed
new Foo();
~nil [1]

// Missing a space
new Foo( );
~nil [0]

[0]: Needs 1 whitespace within parentheses
[1]: Needs 2 whitespaces within parentheses
Expand Down
3 changes: 3 additions & 0 deletions test/rules/space-within-parens/no-space/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ switch (typeof x) {
break;
}

// empty parens are always allowed
foo();

3 changes: 3 additions & 0 deletions test/rules/space-within-parens/no-space/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ switch ( typeof x ) {
break;
}

// empty parens are always allowed
foo();

[0]: Whitespace within parentheses is not allowed

0 comments on commit 87910c3

Please sign in to comment.