Skip to content

Commit e12f2d8

Browse files
authored
Add assertion whitespace lint rule (#9931)
* Add assertion whitespace lint rule * Fix typo * Add the word `Rule` to Jakefile
1 parent 670f0c9 commit e12f2d8

File tree

7 files changed

+45
-35
lines changed

7 files changed

+45
-35
lines changed

Gulpfile.ts

+12-29
Original file line numberDiff line numberDiff line change
@@ -918,37 +918,20 @@ gulp.task("update-sublime", "Updates the sublime plugin's tsserver", ["local", s
918918
return gulp.src([serverFile, serverFile + ".map"]).pipe(gulp.dest("../TypeScript-Sublime-Plugin/tsserver/"));
919919
});
920920

921-
922-
const tslintRuleDir = "scripts/tslint";
923-
const tslintRules = [
924-
"nextLineRule",
925-
"preferConstRule",
926-
"booleanTriviaRule",
927-
"typeOperatorSpacingRule",
928-
"noInOperatorRule",
929-
"noIncrementDecrementRule",
930-
"objectLiteralSurroundingSpaceRule",
931-
];
932-
const tslintRulesFiles = tslintRules.map(function(p) {
933-
return path.join(tslintRuleDir, p + ".ts");
934-
});
935-
const tslintRulesOutFiles = tslintRules.map(function(p, i) {
936-
const pathname = path.join(builtLocalDirectory, "tslint", p + ".js");
937-
gulp.task(pathname, false, [], () => {
938-
const settings: tsc.Settings = getCompilerSettings({ module: "commonjs" }, /*useBuiltCompiler*/ false);
939-
return gulp.src(tslintRulesFiles[i])
940-
.pipe(newer(pathname))
941-
.pipe(sourcemaps.init())
942-
.pipe(tsc(settings))
943-
.pipe(sourcemaps.write("."))
944-
.pipe(gulp.dest(path.join(builtLocalDirectory, "tslint")));
945-
});
946-
return pathname;
921+
gulp.task("build-rules", "Compiles tslint rules to js", () => {
922+
const settings: tsc.Settings = getCompilerSettings({ module: "commonjs" }, /*useBuiltCompiler*/ false);
923+
const dest = path.join(builtLocalDirectory, "tslint");
924+
return gulp.src("scripts/tslint/**/*.ts")
925+
.pipe(newer({
926+
dest,
927+
ext: ".js"
928+
}))
929+
.pipe(sourcemaps.init())
930+
.pipe(tsc(settings))
931+
.pipe(sourcemaps.write("."))
932+
.pipe(gulp.dest(dest));
947933
});
948934

949-
gulp.task("build-rules", "Compiles tslint rules to js", tslintRulesOutFiles);
950-
951-
952935
function getLinterOptions() {
953936
return {
954937
configuration: require("./tslint.json"),

Jakefile.js

+1
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ var tslintRules = [
990990
"noInOperatorRule",
991991
"noIncrementDecrementRule",
992992
"objectLiteralSurroundingSpaceRule",
993+
"noTypeAssertionWhitespaceRule"
993994
];
994995
var tslintRulesFiles = tslintRules.map(function(p) {
995996
return path.join(tslintRuleDir, p + ".ts");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as Lint from "tslint/lib/lint";
2+
import * as ts from "typescript";
3+
4+
5+
export class Rule extends Lint.Rules.AbstractRule {
6+
public static TRAILING_FAILURE_STRING = "Excess trailing whitespace found around type assertion.";
7+
8+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
9+
return this.applyWithWalker(new TypeAssertionWhitespaceWalker(sourceFile, this.getOptions()));
10+
}
11+
}
12+
13+
class TypeAssertionWhitespaceWalker extends Lint.RuleWalker {
14+
public visitNode(node: ts.Node) {
15+
if (node.kind === ts.SyntaxKind.TypeAssertionExpression) {
16+
const refined = node as ts.TypeAssertion;
17+
const leftSideWhitespaceStart = refined.type.getEnd() + 1;
18+
const rightSideWhitespaceEnd = refined.expression.getStart();
19+
if (leftSideWhitespaceStart !== rightSideWhitespaceEnd) {
20+
this.addFailure(this.createFailure(leftSideWhitespaceStart, rightSideWhitespaceEnd, Rule.TRAILING_FAILURE_STRING));
21+
}
22+
}
23+
super.visitNode(node);
24+
}
25+
}

src/compiler/emitter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2667,7 +2667,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
26672667
isNameOfExportedDeclarationInNonES6Module(node.operand);
26682668

26692669
if (internalExportChanged) {
2670-
emitAliasEqual(<Identifier> node.operand);
2670+
emitAliasEqual(<Identifier>node.operand);
26712671
}
26722672

26732673
write(tokenToString(node.operator));
@@ -2722,7 +2722,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
27222722
}
27232723
}
27242724
else if (internalExportChanged) {
2725-
emitAliasEqual(<Identifier> node.operand);
2725+
emitAliasEqual(<Identifier>node.operand);
27262726
emit(node.operand);
27272727
if (node.operator === SyntaxKind.PlusPlusToken) {
27282728
write(" += 1");

src/services/formatting/rulesMap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace ts.formatting {
1919

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

2424
// This array is used only during construction of the rulesbucket in the map
2525
const rulesBucketConstructionStateList: RulesBucketConstructionState[] = <any>new Array(this.map.length); // new Array<RulesBucketConstructionState>(this.map.length);

src/services/services.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ namespace ts {
10821082
// fall through
10831083
case SyntaxKind.VariableDeclaration:
10841084
case SyntaxKind.BindingElement: {
1085-
const decl = <VariableDeclaration> node;
1085+
const decl = <VariableDeclaration>node;
10861086
if (isBindingPattern(decl.name)) {
10871087
forEachChild(decl.name, visit);
10881088
break;
@@ -2040,7 +2040,7 @@ namespace ts {
20402040
function fixupCompilerOptions(options: CompilerOptions, diagnostics: Diagnostic[]): CompilerOptions {
20412041
// Lazily create this value to fix module loading errors.
20422042
commandLineOptionsStringToEnum = commandLineOptionsStringToEnum || <CommandLineOptionOfCustomType[]>filter(optionDeclarations, o =>
2043-
typeof o.type === "object" && !forEachValue(<Map<any>> o.type, v => typeof v !== "number"));
2043+
typeof o.type === "object" && !forEachValue(<Map<any>>o.type, v => typeof v !== "number"));
20442044

20452045
options = clone(options);
20462046

tslint.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"prefer-const": true,
4747
"no-in-operator": true,
4848
"no-increment-decrement": true,
49-
"object-literal-surrounding-space": true
49+
"object-literal-surrounding-space": true,
50+
"no-type-assertion-whitespace": true
5051
}
5152
}

0 commit comments

Comments
 (0)