From 87910c37b6e2fd0321deb1b2acf1c95757e1720c Mon Sep 17 00:00:00 2001 From: jbsingh Date: Sat, 2 Dec 2017 16:50:14 -0600 Subject: [PATCH] update spaceWithinParensRule to always allow empty parens (#3513) --- src/rules/spaceWithinParensRule.ts | 10 +++++++--- .../space-within-parens/force-one-space/test.ts.fix | 3 +++ .../space-within-parens/force-one-space/test.ts.lint | 3 +++ .../space-within-parens/force-two-spaces/test.ts.fix | 8 +++++++- .../space-within-parens/force-two-spaces/test.ts.lint | 10 ++++++++-- test/rules/space-within-parens/no-space/test.ts.fix | 3 +++ test/rules/space-within-parens/no-space/test.ts.lint | 3 +++ 7 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/rules/spaceWithinParensRule.ts b/src/rules/spaceWithinParensRule.ts index d8200e9ab6e..1302f86270c 100644 --- a/src/rules/spaceWithinParensRule.ts +++ b/src/rules/spaceWithinParensRule.ts @@ -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. @@ -73,9 +73,13 @@ class SpaceWithinParensWalker extends Lint.AbstractWalker { 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); + } } }); } diff --git a/test/rules/space-within-parens/force-one-space/test.ts.fix b/test/rules/space-within-parens/force-one-space/test.ts.fix index a8a6856c9d1..542a92bfd4a 100644 --- a/test/rules/space-within-parens/force-one-space/test.ts.fix +++ b/test/rules/space-within-parens/force-one-space/test.ts.fix @@ -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(); + diff --git a/test/rules/space-within-parens/force-one-space/test.ts.lint b/test/rules/space-within-parens/force-one-space/test.ts.lint index efd9d8eb517..5ec16cbe06e 100644 --- a/test/rules/space-within-parens/force-one-space/test.ts.lint +++ b/test/rules/space-within-parens/force-one-space/test.ts.lint @@ -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 diff --git a/test/rules/space-within-parens/force-two-spaces/test.ts.fix b/test/rules/space-within-parens/force-two-spaces/test.ts.fix index 9f02e2d5bec..28b39ff3820 100644 --- a/test/rules/space-within-parens/force-two-spaces/test.ts.fix +++ b/test/rules/space-within-parens/force-two-spaces/test.ts.fix @@ -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( ); diff --git a/test/rules/space-within-parens/force-two-spaces/test.ts.lint b/test/rules/space-within-parens/force-two-spaces/test.ts.lint index a319b7ca311..268505cbf01 100644 --- a/test/rules/space-within-parens/force-two-spaces/test.ts.lint +++ b/test/rules/space-within-parens/force-two-spaces/test.ts.lint @@ -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 diff --git a/test/rules/space-within-parens/no-space/test.ts.fix b/test/rules/space-within-parens/no-space/test.ts.fix index 65d69f4c3e3..c00e7d19386 100644 --- a/test/rules/space-within-parens/no-space/test.ts.fix +++ b/test/rules/space-within-parens/no-space/test.ts.fix @@ -32,3 +32,6 @@ switch (typeof x) { break; } +// empty parens are always allowed +foo(); + diff --git a/test/rules/space-within-parens/no-space/test.ts.lint b/test/rules/space-within-parens/no-space/test.ts.lint index d5d37c70caa..fcb1215a5fa 100644 --- a/test/rules/space-within-parens/no-space/test.ts.lint +++ b/test/rules/space-within-parens/no-space/test.ts.lint @@ -44,4 +44,7 @@ switch ( typeof x ) { break; } +// empty parens are always allowed +foo(); + [0]: Whitespace within parentheses is not allowed