Skip to content

Add assertion whitespace lint rule #9931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2016
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
41 changes: 12 additions & 29 deletions Gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,37 +918,20 @@ gulp.task("update-sublime", "Updates the sublime plugin's tsserver", ["local", s
return gulp.src([serverFile, serverFile + ".map"]).pipe(gulp.dest("../TypeScript-Sublime-Plugin/tsserver/"));
});


const tslintRuleDir = "scripts/tslint";
const tslintRules = [
"nextLineRule",
"preferConstRule",
"booleanTriviaRule",
"typeOperatorSpacingRule",
"noInOperatorRule",
"noIncrementDecrementRule",
"objectLiteralSurroundingSpaceRule",
];
const tslintRulesFiles = tslintRules.map(function(p) {
return path.join(tslintRuleDir, p + ".ts");
});
const tslintRulesOutFiles = tslintRules.map(function(p, i) {
const pathname = path.join(builtLocalDirectory, "tslint", p + ".js");
gulp.task(pathname, false, [], () => {
const settings: tsc.Settings = getCompilerSettings({ module: "commonjs" }, /*useBuiltCompiler*/ false);
return gulp.src(tslintRulesFiles[i])
.pipe(newer(pathname))
.pipe(sourcemaps.init())
.pipe(tsc(settings))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(path.join(builtLocalDirectory, "tslint")));
});
return pathname;
gulp.task("build-rules", "Compiles tslint rules to js", () => {
const settings: tsc.Settings = getCompilerSettings({ module: "commonjs" }, /*useBuiltCompiler*/ false);
const dest = path.join(builtLocalDirectory, "tslint");
return gulp.src("scripts/tslint/**/*.ts")
.pipe(newer({
dest,
ext: ".js"
}))
.pipe(sourcemaps.init())
.pipe(tsc(settings))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(dest));
});

gulp.task("build-rules", "Compiles tslint rules to js", tslintRulesOutFiles);


function getLinterOptions() {
return {
configuration: require("./tslint.json"),
Expand Down
1 change: 1 addition & 0 deletions Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@ var tslintRules = [
"noInOperatorRule",
"noIncrementDecrementRule",
"objectLiteralSurroundingSpaceRule",
"noTypeAssertionWhitespaceRule"
];
var tslintRulesFiles = tslintRules.map(function(p) {
return path.join(tslintRuleDir, p + ".ts");
Expand Down
25 changes: 25 additions & 0 deletions scripts/tslint/noTypeAssertionWhitespaceRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as Lint from "tslint/lib/lint";
import * as ts from "typescript";


export class Rule extends Lint.Rules.AbstractRule {
public static TRAILING_FAILURE_STRING = "Excess trailing whitespace found around type assertion.";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new TypeAssertionWhitespaceWalker(sourceFile, this.getOptions()));
}
}

class TypeAssertionWhitespaceWalker extends Lint.RuleWalker {
public visitNode(node: ts.Node) {
if (node.kind === ts.SyntaxKind.TypeAssertionExpression) {
const refined = node as ts.TypeAssertion;
const leftSideWhitespaceStart = refined.type.getEnd() + 1;
const rightSideWhitespaceEnd = refined.expression.getStart();
if (leftSideWhitespaceStart !== rightSideWhitespaceEnd) {
this.addFailure(this.createFailure(leftSideWhitespaceStart, rightSideWhitespaceEnd, Rule.TRAILING_FAILURE_STRING));
}
}
super.visitNode(node);
}
}
4 changes: 2 additions & 2 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
isNameOfExportedDeclarationInNonES6Module(node.operand);

if (internalExportChanged) {
emitAliasEqual(<Identifier> node.operand);
emitAliasEqual(<Identifier>node.operand);
}

write(tokenToString(node.operator));
Expand Down Expand Up @@ -2722,7 +2722,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}
}
else if (internalExportChanged) {
emitAliasEqual(<Identifier> node.operand);
emitAliasEqual(<Identifier>node.operand);
emit(node.operand);
if (node.operator === SyntaxKind.PlusPlusToken) {
write(" += 1");
Expand Down
2 changes: 1 addition & 1 deletion src/services/formatting/rulesMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace ts.formatting {

public Initialize(rules: Rule[]) {
this.mapRowLength = SyntaxKind.LastToken + 1;
this.map = <any> new Array(this.mapRowLength * this.mapRowLength); // new Array<RulesBucket>(this.mapRowLength * this.mapRowLength);
this.map = <any>new Array(this.mapRowLength * this.mapRowLength); // new Array<RulesBucket>(this.mapRowLength * this.mapRowLength);

// This array is used only during construction of the rulesbucket in the map
const rulesBucketConstructionStateList: RulesBucketConstructionState[] = <any>new Array(this.map.length); // new Array<RulesBucketConstructionState>(this.map.length);
Expand Down
4 changes: 2 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ namespace ts {
// fall through
case SyntaxKind.VariableDeclaration:
case SyntaxKind.BindingElement: {
const decl = <VariableDeclaration> node;
const decl = <VariableDeclaration>node;
if (isBindingPattern(decl.name)) {
forEachChild(decl.name, visit);
break;
Expand Down Expand Up @@ -2040,7 +2040,7 @@ namespace ts {
function fixupCompilerOptions(options: CompilerOptions, diagnostics: Diagnostic[]): CompilerOptions {
// Lazily create this value to fix module loading errors.
commandLineOptionsStringToEnum = commandLineOptionsStringToEnum || <CommandLineOptionOfCustomType[]>filter(optionDeclarations, o =>
typeof o.type === "object" && !forEachValue(<Map<any>> o.type, v => typeof v !== "number"));
typeof o.type === "object" && !forEachValue(<Map<any>>o.type, v => typeof v !== "number"));

options = clone(options);

Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"prefer-const": true,
"no-in-operator": true,
"no-increment-decrement": true,
"object-literal-surrounding-space": true
"object-literal-surrounding-space": true,
"no-type-assertion-whitespace": true
}
}