Skip to content

Commit 00b389d

Browse files
committed
New commit using TS scanner.
This commit uses TS scanner and replaces comments token text by whitespaces to preserve orginal positions.
1 parent 5f81ba1 commit 00b389d

File tree

2 files changed

+40
-96
lines changed

2 files changed

+40
-96
lines changed

src/compiler/commandLineParser.ts

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -421,91 +421,49 @@ namespace ts {
421421
* This method replace comment content by whitespace rather than completely remove them to keep positions in json parsing error reporting accurate.
422422
*/
423423
function removeComments(jsonText: string): string {
424-
let result = "";
425-
let processingString = false;
426-
let processingSingleLineComment = false;
427-
let processingMultiLineComment = false;
428-
for (let i = 0; i < jsonText.length; i++) {
429-
let currentCharCode = jsonText.charCodeAt(i);
430-
let currentChar = jsonText.charAt(i);
431-
let nextCharCode = (i + 1 < jsonText.length) ? jsonText.charCodeAt(i + 1) : undefined;
432-
if (processingString) {
433-
if (currentCharCode === CharacterCodes.backslash
434-
&& nextCharCode !== undefined) {
435-
// Found an escaped character
436-
// consume the \ and the escaped char
437-
result += currentChar;
438-
result += jsonText.charAt(i + 1);
439-
i += 1;
440-
}
441-
else if (currentCharCode === CharacterCodes.doubleQuote) {
442-
// End of string
443-
result += currentChar;
444-
processingString = false;
445-
}
446-
else {
447-
// String content
448-
result += currentChar;
449-
}
450-
}
451-
else if (processingSingleLineComment) {
452-
if (isLineBreak(currentCharCode)) {
453-
// End of single line comment
454-
processingSingleLineComment = false;
455-
// Keep the line breaks to keep line numbers aligned
456-
result += currentChar;
457-
}
458-
else {
459-
// replace comment content by whitespaces
460-
result += " ";
461-
}
424+
let output = "";
425+
let scanner = createScanner(ScriptTarget.ES5, /* skipTrivia */ false, LanguageVariant.Standard, jsonText);
426+
let token: SyntaxKind;
427+
while ((token = scanner.scan()) !== SyntaxKind.EndOfFileToken) {
428+
switch (token) {
429+
case SyntaxKind.SingleLineCommentTrivia:
430+
case SyntaxKind.MultiLineCommentTrivia:
431+
// replace comments with whitespaces to preserve original characters position
432+
output += replaceWithWhitespaces(scanner.getTokenText());
433+
break;
434+
default:
435+
output += scanner.getTokenText();
436+
break;
462437
}
463-
else if (processingMultiLineComment) {
464-
if (currentCharCode === CharacterCodes.asterisk && nextCharCode === CharacterCodes.slash) {
465-
// End of comment */
466-
result += " ";
467-
i += 1;
468-
processingMultiLineComment = false;
469-
}
470-
else if (isLineBreak(currentCharCode)) {
471-
// Keep the line breaks to Keep line aligned
472-
result += currentChar;
438+
}
439+
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;
473452
}
474453
else {
475-
// replace comment content by whitespaces
476-
result += " ";
454+
pos += 1;
477455
}
478456
}
479-
else if (currentCharCode === CharacterCodes.doubleQuote) {
480-
// String start
481-
result += currentChar;
482-
processingString = true;
483-
}
484-
else if (currentCharCode === CharacterCodes.hash) {
485-
// Start of # comment
486-
result += " ";
487-
processingSingleLineComment = true;
488-
}
489-
else if (currentCharCode === CharacterCodes.slash && nextCharCode === CharacterCodes.slash) {
490-
// Start of // comment
491-
result += " ";
492-
i += 1;
493-
processingSingleLineComment = true;
494-
}
495-
else if (currentCharCode === CharacterCodes.slash && nextCharCode === CharacterCodes.asterisk) {
496-
// Start of /**/ comment
497-
result += " ";
498-
i += 1;
499-
processingMultiLineComment = true;
500-
}
501-
else {
502-
// Keep other characters
503-
result += currentChar;
504-
}
457+
result += nSpaces(pos - start);
458+
return result;
459+
460+
function nSpaces(n: number): string {
461+
return new Array(n + 1).join(" ");
462+
};
505463
}
506-
return result;
507464
}
508465

466+
509467
/**
510468
* Parse the contents of a config file (tsconfig.json).
511469
* @param json The contents of the config file to parse

tests/cases/unittests/tsconfigParsing.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ module ts {
2121

2222
it("returns empty config for file with comments only", () => {
2323
assertParseResult("// Comment", { config: {} });
24-
assertParseResult("# Comment", { config: {} });
2524
assertParseResult("/* Comment*/", { config: {} });
2625
});
2726

@@ -37,19 +36,12 @@ module ts {
3736
"file.d.ts"
3837
]
3938
}`, { config: { exclude: ["file.d.ts"] } });
40-
assertParseResult(
41-
`{
42-
# Excluded files
43-
"exclude": [
44-
# Exclude d.ts
45-
"file.d.ts"
46-
]
47-
}`, { config: { exclude: ["file.d.ts"] } });
39+
4840
assertParseResult(
4941
`{
5042
/* Excluded
51-
Files
52-
*/
43+
Files
44+
*/
5345
"exclude": [
5446
/* multiline comments can be in the middle of a line */"file.d.ts"
5547
]
@@ -60,16 +52,10 @@ module ts {
6052
assertParseResult(
6153
`{
6254
"exclude": [
63-
"xx//file.d.ts"
55+
"xx//file.d.ts"
6456
]
6557
}`, { config: { exclude: ["xx//file.d.ts"] } });
66-
assertParseResult(
67-
`{
68-
"exclude": [
69-
"xx#file.d.ts"
70-
]
71-
}`, { config: { exclude: ["xx#file.d.ts"] } });
72-
assertParseResult(
58+
assertParseResult(
7359
`{
7460
"exclude": [
7561
"xx/*file.d.ts*/"

0 commit comments

Comments
 (0)