Skip to content

Commit 638e4b7

Browse files
committed
Use regex for repacing comments content.
Use space for indents
1 parent 00b389d commit 638e4b7

File tree

2 files changed

+79
-103
lines changed

2 files changed

+79
-103
lines changed

src/compiler/commandLineParser.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -428,39 +428,15 @@ namespace ts {
428428
switch (token) {
429429
case SyntaxKind.SingleLineCommentTrivia:
430430
case SyntaxKind.MultiLineCommentTrivia:
431-
// replace comments with whitespaces to preserve original characters position
432-
output += replaceWithWhitespaces(scanner.getTokenText());
431+
// replace comments with whitespace to preserve original character positions
432+
output += scanner.getTokenText().replace(/\S/g, " ");
433433
break;
434434
default:
435435
output += scanner.getTokenText();
436436
break;
437437
}
438438
}
439439
return output;
440-
441-
function replaceWithWhitespaces(commentTokenText: string): string {
442-
let result = "";
443-
let pos = 0;
444-
let start = 0;
445-
while (pos < commentTokenText.length) {
446-
if (isLineBreak(commentTokenText.charCodeAt(pos))) {
447-
let nbCharToReplace = pos - start;
448-
result += nSpaces(nbCharToReplace);
449-
result += commentTokenText.charAt(pos);
450-
pos += 1;
451-
start = pos;
452-
}
453-
else {
454-
pos += 1;
455-
}
456-
}
457-
result += nSpaces(pos - start);
458-
return result;
459-
460-
function nSpaces(n: number): string {
461-
return new Array(n + 1).join(" ");
462-
};
463-
}
464440
}
465441

466442

Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,86 @@
11
/// <reference path="..\..\..\src\harness\harness.ts" />
22
/// <reference path="..\..\..\src\compiler\commandLineParser.ts" />
33

4-
module ts {
5-
describe('parseConfigFileTextToJson', () => {
6-
function assertParseResult(jsonText: string, expectedConfigObject: { config?: any; error?: Diagnostic }) {
7-
let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText);
8-
assert.equal(JSON.stringify(parsed), JSON.stringify(expectedConfigObject));
9-
}
10-
11-
function assertParseError(jsonText: string) {
12-
let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText);
13-
assert.isTrue(undefined === parsed.config);
14-
assert.isTrue(undefined !== parsed.error);
15-
}
4+
namespace ts {
5+
describe('parseConfigFileTextToJson', () => {
6+
function assertParseResult(jsonText: string, expectedConfigObject: { config?: any; error?: Diagnostic }) {
7+
let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText);
8+
assert.equal(JSON.stringify(parsed), JSON.stringify(expectedConfigObject));
9+
}
1610

17-
it("returns empty config for file with only whitespaces", () => {
18-
assertParseResult("", { config : {} });
19-
assertParseResult(" ", { config : {} });
20-
});
21-
22-
it("returns empty config for file with comments only", () => {
23-
assertParseResult("// Comment", { config: {} });
24-
assertParseResult("/* Comment*/", { config: {} });
25-
});
11+
function assertParseError(jsonText: string) {
12+
let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText);
13+
assert.isTrue(undefined === parsed.config);
14+
assert.isTrue(undefined !== parsed.error);
15+
}
2616

27-
it("returns empty config when config is empty object", () => {
28-
assertParseResult("{}", { config: {} });
29-
});
17+
it("returns empty config for file with only whitespaces", () => {
18+
assertParseResult("", { config : {} });
19+
assertParseResult(" ", { config : {} });
20+
});
3021

31-
it("returns config object without comments", () => {
32-
assertParseResult(
33-
`{ // Excluded files
34-
"exclude": [
35-
// Exclude d.ts
36-
"file.d.ts"
37-
]
38-
}`, { config: { exclude: ["file.d.ts"] } });
39-
40-
assertParseResult(
41-
`{
42-
/* Excluded
43-
Files
44-
*/
45-
"exclude": [
46-
/* multiline comments can be in the middle of a line */"file.d.ts"
47-
]
48-
}`, { config: { exclude: ["file.d.ts"] } });
49-
});
22+
it("returns empty config for file with comments only", () => {
23+
assertParseResult("// Comment", { config: {} });
24+
assertParseResult("/* Comment*/", { config: {} });
25+
});
5026

51-
it("keeps string content untouched", () => {
52-
assertParseResult(
53-
`{
54-
"exclude": [
55-
"xx//file.d.ts"
56-
]
57-
}`, { config: { exclude: ["xx//file.d.ts"] } });
58-
assertParseResult(
59-
`{
60-
"exclude": [
61-
"xx/*file.d.ts*/"
62-
]
63-
}`, { config: { exclude: ["xx/*file.d.ts*/"] } });
64-
});
65-
66-
it("handles escaped characters in strings correctly", () => {
67-
assertParseResult(
68-
`{
69-
"exclude": [
70-
"xx\\"//files"
71-
]
72-
}`, { config: { exclude: ["xx\"//files"] } });
73-
74-
assertParseResult(
75-
`{
76-
"exclude": [
77-
"xx\\\\" // end of line comment
78-
]
79-
}`, { config: { exclude: ["xx\\"] } });
80-
});
81-
82-
it("returns object with error when json is invalid", () => {
83-
assertParseError("invalid");
27+
it("returns empty config when config is empty object", () => {
28+
assertParseResult("{}", { config: {} });
29+
});
30+
31+
it("returns config object without comments", () => {
32+
assertParseResult(
33+
`{ // Excluded files
34+
"exclude": [
35+
// Exclude d.ts
36+
"file.d.ts"
37+
]
38+
}`, { config: { exclude: ["file.d.ts"] } });
39+
40+
assertParseResult(
41+
`{
42+
/* Excluded
43+
Files
44+
*/
45+
"exclude": [
46+
/* multiline comments can be in the middle of a line */"file.d.ts"
47+
]
48+
}`, { config: { exclude: ["file.d.ts"] } });
49+
});
50+
51+
it("keeps string content untouched", () => {
52+
assertParseResult(
53+
`{
54+
"exclude": [
55+
"xx//file.d.ts"
56+
]
57+
}`, { config: { exclude: ["xx//file.d.ts"] } });
58+
assertParseResult(
59+
`{
60+
"exclude": [
61+
"xx/*file.d.ts*/"
62+
]
63+
}`, { config: { exclude: ["xx/*file.d.ts*/"] } });
64+
});
65+
66+
it("handles escaped characters in strings correctly", () => {
67+
assertParseResult(
68+
`{
69+
"exclude": [
70+
"xx\\"//files"
71+
]
72+
}`, { config: { exclude: ["xx\"//files"] } });
73+
74+
assertParseResult(
75+
`{
76+
"exclude": [
77+
"xx\\\\" // end of line comment
78+
]
79+
}`, { config: { exclude: ["xx\\"] } });
80+
});
81+
82+
it("returns object with error when json is invalid", () => {
83+
assertParseError("invalid");
84+
});
8485
});
85-
});
8686
}

0 commit comments

Comments
 (0)