From 638e4b758a7f46b4bebae5da3bd89cd5ca3a67e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Arod?= Date: Sun, 1 Nov 2015 15:31:16 +0100 Subject: [PATCH] Use regex for repacing comments content. Use space for indents --- src/compiler/commandLineParser.ts | 28 +---- tests/cases/unittests/tsconfigParsing.ts | 154 +++++++++++------------ 2 files changed, 79 insertions(+), 103 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 840e2a7e584..aaf3bf624b6 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -428,8 +428,8 @@ namespace ts { switch (token) { case SyntaxKind.SingleLineCommentTrivia: case SyntaxKind.MultiLineCommentTrivia: - // replace comments with whitespaces to preserve original characters position - output += replaceWithWhitespaces(scanner.getTokenText()); + // replace comments with whitespace to preserve original character positions + output += scanner.getTokenText().replace(/\S/g, " "); break; default: output += scanner.getTokenText(); @@ -437,30 +437,6 @@ namespace ts { } } return output; - - function replaceWithWhitespaces(commentTokenText: string): string { - let result = ""; - let pos = 0; - let start = 0; - while (pos < commentTokenText.length) { - if (isLineBreak(commentTokenText.charCodeAt(pos))) { - let nbCharToReplace = pos - start; - result += nSpaces(nbCharToReplace); - result += commentTokenText.charAt(pos); - pos += 1; - start = pos; - } - else { - pos += 1; - } - } - result += nSpaces(pos - start); - return result; - - function nSpaces(n: number): string { - return new Array(n + 1).join(" "); - }; - } } diff --git a/tests/cases/unittests/tsconfigParsing.ts b/tests/cases/unittests/tsconfigParsing.ts index 7490b525000..3603d22f314 100644 --- a/tests/cases/unittests/tsconfigParsing.ts +++ b/tests/cases/unittests/tsconfigParsing.ts @@ -1,86 +1,86 @@ /// /// -module ts { - describe('parseConfigFileTextToJson', () => { - function assertParseResult(jsonText: string, expectedConfigObject: { config?: any; error?: Diagnostic }) { - let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); - assert.equal(JSON.stringify(parsed), JSON.stringify(expectedConfigObject)); - } - - function assertParseError(jsonText: string) { - let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); - assert.isTrue(undefined === parsed.config); - assert.isTrue(undefined !== parsed.error); - } +namespace ts { + describe('parseConfigFileTextToJson', () => { + function assertParseResult(jsonText: string, expectedConfigObject: { config?: any; error?: Diagnostic }) { + let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); + assert.equal(JSON.stringify(parsed), JSON.stringify(expectedConfigObject)); + } - it("returns empty config for file with only whitespaces", () => { - assertParseResult("", { config : {} }); - assertParseResult(" ", { config : {} }); - }); - - it("returns empty config for file with comments only", () => { - assertParseResult("// Comment", { config: {} }); - assertParseResult("/* Comment*/", { config: {} }); - }); + function assertParseError(jsonText: string) { + let parsed = ts.parseConfigFileTextToJson("/apath/tsconfig.json", jsonText); + assert.isTrue(undefined === parsed.config); + assert.isTrue(undefined !== parsed.error); + } - it("returns empty config when config is empty object", () => { - assertParseResult("{}", { config: {} }); - }); + it("returns empty config for file with only whitespaces", () => { + assertParseResult("", { config : {} }); + assertParseResult(" ", { config : {} }); + }); - it("returns config object without comments", () => { - assertParseResult( - `{ // Excluded files - "exclude": [ - // Exclude d.ts - "file.d.ts" - ] - }`, { config: { exclude: ["file.d.ts"] } }); - - assertParseResult( - `{ - /* Excluded - Files - */ - "exclude": [ - /* multiline comments can be in the middle of a line */"file.d.ts" - ] - }`, { config: { exclude: ["file.d.ts"] } }); - }); + it("returns empty config for file with comments only", () => { + assertParseResult("// Comment", { config: {} }); + assertParseResult("/* Comment*/", { config: {} }); + }); - it("keeps string content untouched", () => { - assertParseResult( - `{ - "exclude": [ - "xx//file.d.ts" - ] - }`, { config: { exclude: ["xx//file.d.ts"] } }); - assertParseResult( - `{ - "exclude": [ - "xx/*file.d.ts*/" - ] - }`, { config: { exclude: ["xx/*file.d.ts*/"] } }); + it("returns empty config when config is empty object", () => { + assertParseResult("{}", { config: {} }); + }); + + it("returns config object without comments", () => { + assertParseResult( + `{ // Excluded files + "exclude": [ + // Exclude d.ts + "file.d.ts" + ] + }`, { config: { exclude: ["file.d.ts"] } }); + + assertParseResult( + `{ + /* Excluded + Files + */ + "exclude": [ + /* multiline comments can be in the middle of a line */"file.d.ts" + ] + }`, { config: { exclude: ["file.d.ts"] } }); + }); + + it("keeps string content untouched", () => { + assertParseResult( + `{ + "exclude": [ + "xx//file.d.ts" + ] + }`, { config: { exclude: ["xx//file.d.ts"] } }); + assertParseResult( + `{ + "exclude": [ + "xx/*file.d.ts*/" + ] + }`, { config: { exclude: ["xx/*file.d.ts*/"] } }); + }); + + it("handles escaped characters in strings correctly", () => { + assertParseResult( + `{ + "exclude": [ + "xx\\"//files" + ] + }`, { config: { exclude: ["xx\"//files"] } }); + + assertParseResult( + `{ + "exclude": [ + "xx\\\\" // end of line comment + ] + }`, { config: { exclude: ["xx\\"] } }); + }); + + it("returns object with error when json is invalid", () => { + assertParseError("invalid"); + }); }); - - it("handles escaped characters in strings correctly", () => { - assertParseResult( - `{ - "exclude": [ - "xx\\"//files" - ] - }`, { config: { exclude: ["xx\"//files"] } }); - - assertParseResult( - `{ - "exclude": [ - "xx\\\\" // end of line comment - ] - }`, { config: { exclude: ["xx\\"] } }); - }); - - it("returns object with error when json is invalid", () => { - assertParseError("invalid"); - }); - }); }