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

Added "check-preblock" option to whitespace rule #2002

Merged
merged 2 commits into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/rules/whitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const OPTION_MODULE = "check-module";
const OPTION_SEPARATOR = "check-separator";
const OPTION_TYPE = "check-type";
const OPTION_TYPECAST = "check-typecast";
const OPTION_PREBLOCK = "check-preblock";

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
Expand All @@ -42,13 +43,14 @@ export class Rule extends Lint.Rules.AbstractRule {
* \`"check-module"\` checks for whitespace in import & export statements.
* \`"check-separator"\` checks for whitespace after separator tokens (\`,\`/\`;\`).
* \`"check-type"\` checks for whitespace before a variable type specification.
* \`"check-typecast"\` checks for whitespace between a typecast and its target.`,
* \`"check-typecast"\` checks for whitespace between a typecast and its target.
* \`"check-preblock"\` checks for whitespace before the opening brace of a block`,
options: {
type: "array",
items: {
type: "string",
enum: ["check-branch", "check-decl", "check-operator", "check-module",
"check-separator", "check-type", "check-typecast"],
"check-separator", "check-type", "check-typecast", "check-preblock"],
},
minLength: 0,
maxLength: 7,
Expand Down Expand Up @@ -87,7 +89,7 @@ class WhitespaceWalker extends Lint.SkippableTokenAwareRuleWalker {
if (tokenKind === ts.SyntaxKind.WhitespaceTrivia || tokenKind === ts.SyntaxKind.NewLineTrivia) {
prevTokenShouldBeFollowedByWhitespace = false;
} else if (prevTokenShouldBeFollowedByWhitespace) {
this.addFailureAt(startPos, 1, Rule.FAILURE_STRING);
this.addMissingWhitespaceErrorAt(startPos);
prevTokenShouldBeFollowedByWhitespace = false;
}

Expand Down Expand Up @@ -153,6 +155,13 @@ class WhitespaceWalker extends Lint.SkippableTokenAwareRuleWalker {
super.visitBinaryExpression(node);
}

protected visitBlock(block: ts.Block) {
if (this.hasOption(OPTION_PREBLOCK)) {
this.checkForTrailingWhitespace(block.getFullStart());
}
super.visitBlock(block);
}

// check for spaces between ternary operator symbols
public visitConditionalExpression(node: ts.ConditionalExpression) {
if (this.hasOption(OPTION_OPERATOR)) {
Expand Down Expand Up @@ -260,7 +269,12 @@ class WhitespaceWalker extends Lint.SkippableTokenAwareRuleWalker {
if (nextTokenType !== ts.SyntaxKind.WhitespaceTrivia
&& nextTokenType !== ts.SyntaxKind.NewLineTrivia
&& nextTokenType !== ts.SyntaxKind.EndOfFileToken) {
this.addFailureAt(position, 1, Rule.FAILURE_STRING);
this.addMissingWhitespaceErrorAt(position);
}
}

private addMissingWhitespaceErrorAt(position: number) {
const fix = this.createFix(this.appendText(position, " "));
this.addFailureAt(position, 1, Rule.FAILURE_STRING, fix);
}
}
26 changes: 26 additions & 0 deletions test/rules/whitespace/all/test.js.lint
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,29 @@ export function each(obj, iterator, context) {

export {each as forEach};
import "libE";

function foobar(){}
~ [missing whitespace]

function foorbar()
{}

if (){
~ [missing whitespace]
//
} else{}
~ [missing whitespace]

if ()
{}
else
{}

/* some comment */{
// some code with a preceding comment
}

{
const foo = 123;
// code that just wants to be encapsulated in a block scope
}
94 changes: 94 additions & 0 deletions test/rules/whitespace/all/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import ast = AST;
module M {
export var ast = AST;

var x: number;

var y = (x === 10) ? 1 : 2;

var zz = (y === 4);

var z = y;

var a, b;

switch (x) {
case 1: break;
default: break;
}

for (x = 1; x < 2; ++x) {
goto: console.log("hi");
}

while (i < 1) {
++i;
}

var q;
q.forEach(() => 3);
q.forEach(() => {
return 3;
});

var r: () => string;
var s: new () => string;
var a = <number> "10";
var a = <number> "10";
}

var a;

export = a;

a.then(() => {
return 1;
}).if(() => {
return 1;
});

var name = "something";
var test = `
<sl-property-group label=${name} Axes">
<div class="repeat"
`;

import { importA } from "libA";
import { importB } from "libB";
import {importC} from "libC";
import moduleD, {importD} from "libD";

export default 123;

const D = 123;
export { D as default }; // failure

export function each(obj, iterator, context) {
//
}

export {each as forEach};
import "libE";

function foobar() {}

function foorbar()
{}

if () {
//
} else {}

if ()
{}
else
{}

/* some comment */{
// some code with a preceding comment
}

{
const foo = 123;
// code that just wants to be encapsulated in a block scope
}
32 changes: 30 additions & 2 deletions test/rules/whitespace/all/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ module M {
~ [missing whitespace]
}

for(x = 1;x <2; ++x) {
for(x = 1;x <2; ++x){
~ [missing whitespace]
~ [missing whitespace]
~ [missing whitespace]
~ [missing whitespace]
goto:console.log("hi");
~ [missing whitespace]
}

while(i < 1) {
while(i < 1){
~ [missing whitespace]
~ [missing whitespace]
++i;
}

Expand Down Expand Up @@ -108,3 +110,29 @@ export function each(obj, iterator, context) {

export {each as forEach};
import "libE";

function foobar(){}
~ [missing whitespace]

function foorbar()
{}

if (){
~ [missing whitespace]
//
} else{}
~ [missing whitespace]

if ()
{}
else
{}

/* some comment */{
// some code with a preceding comment
}

{
const foo = 123;
// code that just wants to be encapsulated in a block scope
}
2 changes: 2 additions & 0 deletions test/rules/whitespace/all/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"check-decl",
"check-operator",
"check-module",
"check-preblock",
"check-separator",
"check-type",
"check-typecast"
Expand All @@ -16,6 +17,7 @@
"check-decl",
"check-operator",
"check-module",
"check-preblock",
"check-separator",
"check-type",
"check-typecast"
Expand Down
23 changes: 23 additions & 0 deletions test/rules/whitespace/none/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,26 @@ export function each(obj, iterator, context) {

export {each as forEach};
import "libE";

function foobar(){}

function foorbar()
{}

if (){
//
} else{}

if ()
{}
else
{}

/* some comment */{
// some code with a preceding comment
}

{
const foo = 123;
// code that just wants to be encapsulated in a block scope
}