diff --git a/src/rules/terNewlineAfterVarRule.ts b/src/rules/terNewlineAfterVarRule.ts index f5a1757..e5fdf0d 100644 --- a/src/rules/terNewlineAfterVarRule.ts +++ b/src/rules/terNewlineAfterVarRule.ts @@ -93,8 +93,12 @@ class RuleWalker extends Lint.AbstractWalker { const nextSibling: ts.Node|void = getNextSiblingNode(node); const nextSiblingKind: number|void = nextSibling && nextSibling.kind; - // prevent a conflict with "eofline" rule - if (!nextSibling || nextSiblingKind !== ts.SyntaxKind.EndOfFileToken) { + if ( + !(isNewLineAlwaysRequired && nextSiblingKind === ts.SyntaxKind.VariableStatement) && + + // prevent a conflict with "eofline" rule + !(nextSibling && nextSiblingKind === ts.SyntaxKind.EndOfFileToken) + ) { const isNewLineRequired: boolean = isNewLineAlwaysRequired && nextSiblingKind !== ts.SyntaxKind.VariableStatement; const unexpectedLineFixes: Lint.Replacement[] = []; const expectedLineFixes: Lint.Replacement[] = []; diff --git a/src/test/rules/terNewlineAfterVarRuleTests.ts b/src/test/rules/terNewlineAfterVarRuleTests.ts index 75792d9..6eb8c11 100644 --- a/src/test/rules/terNewlineAfterVarRuleTests.ts +++ b/src/test/rules/terNewlineAfterVarRuleTests.ts @@ -20,6 +20,29 @@ function expecting (errors: ['expectedBlankLine' | 'unexpectedBlankLine', number } ruleTester.addTestGroup('always', 'should always require an empty line after variable declarations ', [ + { + code: dedent` + const foo1 = 1; + + const bar1 = 2; // Inline comment + + const foo2 = 3; /* Multiline + comment */ + + const bar2 = 4; + `, + output: dedent` + const foo1 = 1; + + const bar1 = 2; // Inline comment + + const foo2 = 3; /* Multiline + comment */ + + const bar2 = 4; + `, + options: [] + }, { code: dedent` var greet = "hello,", @@ -227,6 +250,31 @@ ruleTester.addTestGroup('always', 'should always require an empty line after var ]); ruleTester.addTestGroup('never', 'should disallow empty lines after variable declarations ', [ + { + code: dedent` + const foo1 = 1; + + const bar1 = 2; // Inline comment + + const foo2 = 3; /* Multiline + comment */ + + const bar2 = 4; + `, + output: dedent` + const foo1 = 1; + const bar1 = 2; // Inline comment + const foo2 = 3; /* Multiline + comment */ + const bar2 = 4; + `, + options: ['never'], + errors: expecting([ + ['unexpectedBlankLine', 1], + ['unexpectedBlankLine', 3], + ['unexpectedBlankLine', 5] + ]) + }, { code: dedent` var greet = "hello,",