Skip to content

Commit 9cd1f78

Browse files
authored
Merge pull request microsoft#9332 from Microsoft/object-whitespace-rule
Object whitespace rule
2 parents 2bc35c1 + 8cc8293 commit 9cd1f78

8 files changed

Lines changed: 112 additions & 67 deletions

File tree

Gulpfile.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ for (const i in libraryTargets) {
229229
gulp.task(target, false, [], function() {
230230
return gulp.src(sources)
231231
.pipe(newer(target))
232-
.pipe(concat(target, {newLine: ""}))
232+
.pipe(concat(target, { newLine: "" }))
233233
.pipe(gulp.dest("."));
234234
});
235235
}
@@ -580,7 +580,7 @@ gulp.task(run, false, [servicesFile], () => {
580580
.pipe(newer(run))
581581
.pipe(sourcemaps.init())
582582
.pipe(tsc(settings))
583-
.pipe(sourcemaps.write(".", {includeContent: false, sourceRoot: "../../"}))
583+
.pipe(sourcemaps.write(".", { includeContent: false, sourceRoot: "../../" }))
584584
.pipe(gulp.dest("."));
585585
});
586586

@@ -746,7 +746,7 @@ gulp.task("runtests",
746746
const nodeServerOutFile = "tests/webTestServer.js";
747747
const nodeServerInFile = "tests/webTestServer.ts";
748748
gulp.task(nodeServerOutFile, false, [servicesFile], () => {
749-
const settings: tsc.Settings = getCompilerSettings({module: "commonjs"}, /*useBuiltCompiler*/ true);
749+
const settings: tsc.Settings = getCompilerSettings({ module: "commonjs" }, /*useBuiltCompiler*/ true);
750750
return gulp.src(nodeServerInFile)
751751
.pipe(newer(nodeServerOutFile))
752752
.pipe(sourcemaps.init())
@@ -771,7 +771,7 @@ gulp.task("browserify", "Runs browserify on run.js to produce a file suitable fo
771771
next(undefined, file);
772772
});
773773
}))
774-
.pipe(sourcemaps.write(".", {includeContent: false, sourceRoot: "../../"}))
774+
.pipe(sourcemaps.write(".", { includeContent: false, sourceRoot: "../../" }))
775775
.pipe(gulp.dest("."));
776776
});
777777

@@ -960,15 +960,16 @@ const tslintRules = [
960960
"booleanTriviaRule",
961961
"typeOperatorSpacingRule",
962962
"noInOperatorRule",
963-
"noIncrementDecrementRule"
963+
"noIncrementDecrementRule",
964+
"objectLiteralSurroundingSpaceRule",
964965
];
965966
const tslintRulesFiles = tslintRules.map(function(p) {
966967
return path.join(tslintRuleDir, p + ".ts");
967968
});
968969
const tslintRulesOutFiles = tslintRules.map(function(p, i) {
969970
const pathname = path.join(builtLocalDirectory, "tslint", p + ".js");
970971
gulp.task(pathname, false, [], () => {
971-
const settings: tsc.Settings = getCompilerSettings({module: "commonjs"}, /*useBuiltCompiler*/ false);
972+
const settings: tsc.Settings = getCompilerSettings({ module: "commonjs" }, /*useBuiltCompiler*/ false);
972973
return gulp.src(tslintRulesFiles[i])
973974
.pipe(newer(pathname))
974975
.pipe(sourcemaps.init())

Jakefile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,8 @@ var tslintRules = [
992992
"booleanTriviaRule",
993993
"typeOperatorSpacingRule",
994994
"noInOperatorRule",
995-
"noIncrementDecrementRule"
995+
"noIncrementDecrementRule",
996+
"objectLiteralSurroundingSpaceRule",
996997
];
997998
var tslintRulesFiles = tslintRules.map(function(p) {
998999
return path.join(tslintRuleDir, p + ".ts");
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 LEADING_FAILURE_STRING = "No leading whitespace found on single-line object literal.";
7+
public static TRAILING_FAILURE_STRING = "No trailing whitespace found on single-line object literal.";
8+
public static LEADING_EXCESS_FAILURE_STRING = "Excess leading whitespace found on single-line object literal.";
9+
public static TRAILING_EXCESS_FAILURE_STRING = "Excess trailing whitespace found on single-line object literal.";
10+
11+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
12+
return this.applyWithWalker(new ObjectLiteralSpaceWalker(sourceFile, this.getOptions()));
13+
}
14+
}
15+
16+
class ObjectLiteralSpaceWalker extends Lint.RuleWalker {
17+
public visitNode(node: ts.Node) {
18+
if (node.kind === ts.SyntaxKind.ObjectLiteralExpression) {
19+
const literal = node as ts.ObjectLiteralExpression;
20+
const text = literal.getText();
21+
if (text.match(/^{[^\n]+}$/g)) {
22+
if (text.charAt(1) !== " ") {
23+
const failure = this.createFailure(node.pos, node.getWidth(), Rule.LEADING_FAILURE_STRING);
24+
this.addFailure(failure);
25+
}
26+
if (text.charAt(2) === " ") {
27+
const failure = this.createFailure(node.pos + 2, 1, Rule.LEADING_EXCESS_FAILURE_STRING);
28+
this.addFailure(failure);
29+
}
30+
if (text.charAt(text.length - 2) !== " ") {
31+
const failure = this.createFailure(node.pos, node.getWidth(), Rule.TRAILING_FAILURE_STRING);
32+
this.addFailure(failure);
33+
}
34+
if (text.charAt(text.length - 3) === " ") {
35+
const failure = this.createFailure(node.pos + node.getWidth() - 3, 1, Rule.TRAILING_EXCESS_FAILURE_STRING);
36+
this.addFailure(failure);
37+
}
38+
}
39+
}
40+
super.visitNode(node);
41+
}
42+
}

src/server/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ namespace ts.server {
11231123
[CommandNames.Reload]: (request: protocol.Request) => {
11241124
const reloadArgs = <protocol.ReloadRequestArgs>request.arguments;
11251125
this.reload(reloadArgs.file, reloadArgs.tmpfile, request.seq);
1126-
return {response: { reloadFinished: true }, responseRequired: true};
1126+
return { response: { reloadFinished: true }, responseRequired: true };
11271127
},
11281128
[CommandNames.Saveto]: (request: protocol.Request) => {
11291129
const savetoArgs = <protocol.SavetoRequestArgs>request.arguments;

tests/cases/unittests/moduleResolution.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ import b = require("./moduleB.ts");
589589
const file1: File = { name: "/root/folder1/file1.ts" };
590590
const file2: File = { name: "/root/generated/folder1/file2.ts" }; // load remapped file as module
591591
const file3: File = { name: "/root/generated/folder2/file3/index.d.ts" }; // load folder a module
592-
const file4Typings: File = { name: "/root/generated/folder2/file4/package.json", content: JSON.stringify({ typings: "dist/types.d.ts" })};
592+
const file4Typings: File = { name: "/root/generated/folder2/file4/package.json", content: JSON.stringify({ typings: "dist/types.d.ts" }) };
593593
const file4: File = { name: "/root/generated/folder2/file4/dist/types.d.ts" }; // load file pointed by typings
594594
const file5: File = { name: "/root/someanotherfolder/file5/index.d.ts" }; // load remapped module from folder
595595
const file6: File = { name: "/root/node_modules/file6.ts" }; // fallback to node
@@ -957,7 +957,7 @@ import b = require("./moduleB.ts");
957957
describe("Type reference directive resolution: ", () => {
958958
function test(typesRoot: string, typeDirective: string, primary: boolean, initialFile: File, targetFile: File, ...otherFiles: File[]) {
959959
const host = createModuleResolutionHost(/*hasDirectoryExists*/ false, ...[initialFile, targetFile].concat(...otherFiles));
960-
const result = resolveTypeReferenceDirective(typeDirective, initialFile.name, {typeRoots: [typesRoot]}, host);
960+
const result = resolveTypeReferenceDirective(typeDirective, initialFile.name, { typeRoots: [typesRoot] }, host);
961961
assert(result.resolvedTypeReferenceDirective.resolvedFileName !== undefined, "expected type directive to be resolved");
962962
assert.equal(result.resolvedTypeReferenceDirective.resolvedFileName, targetFile.name, "unexpected result of type reference resolution");
963963
assert.equal(result.resolvedTypeReferenceDirective.primary, primary, "unexpected 'primary' value");
@@ -972,7 +972,7 @@ import b = require("./moduleB.ts");
972972
{
973973
const f1 = { name: "/root/src/app.ts" };
974974
const f2 = { name: "/root/src/types/lib/typings/lib.d.ts" };
975-
const package = { name: "/root/src/types/lib/package.json", content: JSON.stringify({types: "typings/lib.d.ts"}) };
975+
const package = { name: "/root/src/types/lib/package.json", content: JSON.stringify({ types: "typings/lib.d.ts" }) };
976976
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ true, f1, f2, package);
977977
}
978978
{
@@ -983,7 +983,7 @@ import b = require("./moduleB.ts");
983983
{
984984
const f1 = { name: "/root/src/app.ts" };
985985
const f2 = { name: "/root/src/node_modules/lib/typings/lib.d.ts" };
986-
const package = { name: "/root/src/node_modules/lib/package.json", content: JSON.stringify({types: "typings/lib.d.ts"}) };
986+
const package = { name: "/root/src/node_modules/lib/package.json", content: JSON.stringify({ types: "typings/lib.d.ts" }) };
987987
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
988988
}
989989
{
@@ -994,7 +994,7 @@ import b = require("./moduleB.ts");
994994
{
995995
const f1 = { name: "/root/src/app.ts" };
996996
const f2 = { name: "/root/src/node_modules/@types/lib/typings/lib.d.ts" };
997-
const package = { name: "/root/src/node_modules/@types/lib/package.json", content: JSON.stringify({types: "typings/lib.d.ts"}) };
997+
const package = { name: "/root/src/node_modules/@types/lib/package.json", content: JSON.stringify({ types: "typings/lib.d.ts" }) };
998998
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
999999
}
10001000
});
@@ -1012,7 +1012,7 @@ import b = require("./moduleB.ts");
10121012
{
10131013
const f1 = { name: "/root/src/app.ts" };
10141014
const f2 = { name: "/root/node_modules/lib/typings/lib.d.ts" };
1015-
const package = { name: "/root/node_modules/lib/package.json", content: JSON.stringify({typings: "typings/lib.d.ts"}) };
1015+
const package = { name: "/root/node_modules/lib/package.json", content: JSON.stringify({ typings: "typings/lib.d.ts" }) };
10161016
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
10171017
}
10181018
{
@@ -1023,7 +1023,7 @@ import b = require("./moduleB.ts");
10231023
{
10241024
const f1 = { name: "/root/src/app.ts" };
10251025
const f2 = { name: "/root/node_modules/@types/lib/typings/lib.d.ts" };
1026-
const package = { name: "/root/node_modules/@types/lib/package.json", content: JSON.stringify({typings: "typings/lib.d.ts"}) };
1026+
const package = { name: "/root/node_modules/@types/lib/package.json", content: JSON.stringify({ typings: "typings/lib.d.ts" }) };
10271027
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
10281028
}
10291029
});

tests/cases/unittests/session.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ namespace ts.server {
170170

171171
describe("send", () => {
172172
it("is an overrideable handle which sends protocol messages over the wire", () => {
173-
const msg = {seq: 0, type: "none"};
173+
const msg = { seq: 0, type: "none" };
174174
const strmsg = JSON.stringify(msg);
175175
const len = 1 + Utils.byteLength(strmsg, "utf8");
176176
const resultMsg = `Content-Length: ${len}\r\n\r\n${strmsg}\n`;
@@ -266,7 +266,7 @@ namespace ts.server {
266266
constructor() {
267267
super(mockHost, Utils.byteLength, process.hrtime, mockLogger);
268268
this.addProtocolHandler(this.customHandler, () => {
269-
return {response: undefined, responseRequired: true};
269+
return { response: undefined, responseRequired: true };
270270
});
271271
}
272272
send(msg: protocol.Message) {
@@ -340,7 +340,7 @@ namespace ts.server {
340340
handleRequest(msg: protocol.Request) {
341341
let response: protocol.Response;
342342
try {
343-
({response} = this.executeCommand(msg));
343+
({ response } = this.executeCommand(msg));
344344
}
345345
catch (e) {
346346
this.output(undefined, msg.command, msg.seq, e.toString());

0 commit comments

Comments
 (0)