Skip to content

Commit 04e2169

Browse files
committed
Merge pull request #1116 from Microsoft/normalizeSlashes
Consolidate normalizing slashes from harness to use it from typescript core.ts
2 parents 23b8613 + 45e4e16 commit 04e2169

File tree

9 files changed

+38
-89
lines changed

9 files changed

+38
-89
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ build.json
3232
tests/webhost/*.d.ts
3333
tests/webhost/webtsc.js
3434
tests/*.js
35+
tests/*.js.map
3536
tests/*.d.ts
3637
*.config
3738
scripts/debug.bat

src/harness/fourslashRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class FourslashRunner extends RunnerBase {
2020
});
2121

2222
this.tests.forEach((fn: string) => {
23-
fn = Harness.Path.switchToForwardSlashes(fn);
23+
fn = ts.normalizeSlashes(fn);
2424
var justName = fn.replace(/^.*[\\\/]/, '');
2525

2626
// Convert to relative path

src/harness/harness.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,11 @@ module Harness.Path {
117117
}
118118

119119
export function filePath(fullPath: string) {
120-
fullPath = switchToForwardSlashes(fullPath);
120+
fullPath = ts.normalizeSlashes(fullPath);
121121
var components = fullPath.split("/");
122122
var path: string[] = components.slice(0, components.length - 1);
123123
return path.join("/") + "/";
124124
}
125-
126-
export function switchToForwardSlashes(path: string) {
127-
return path.replace(/\\/g, "/").replace(/\/\//g, '/');
128-
}
129125
}
130126

131127
module Harness {
@@ -564,7 +560,7 @@ module Harness {
564560
// Register input files
565561
function register(file: { unitName: string; content: string; }) {
566562
if (file.content !== undefined) {
567-
var filename = Path.switchToForwardSlashes(file.unitName);
563+
var filename = ts.normalizeSlashes(file.unitName);
568564
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, scriptTarget, /*version:*/ "0");
569565
}
570566
};
@@ -782,7 +778,7 @@ module Harness {
782778
var filemap: { [name: string]: ts.SourceFile; } = {};
783779
var register = (file: { unitName: string; content: string; }) => {
784780
if (file.content !== undefined) {
785-
var filename = Path.switchToForwardSlashes(file.unitName);
781+
var filename = ts.normalizeSlashes(file.unitName);
786782
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, options.target, /*version:*/ "0");
787783
}
788784
};

src/harness/loggedIO.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ module Playback {
175175
}
176176

177177
function findResultByPath<T>(wrapper: { resolvePath(s: string): string }, logArray: { path: string; result?: T }[], expectedPath: string, defaultValue?: T): T {
178-
var normalizedName = Harness.Path.switchToForwardSlashes(expectedPath).toLowerCase();
178+
var normalizedName = ts.normalizeSlashes(expectedPath).toLowerCase();
179179
// Try to find the result through normal filename
180180
for (var i = 0; i < logArray.length; i++) {
181-
if (Harness.Path.switchToForwardSlashes(logArray[i].path).toLowerCase() === normalizedName) {
181+
if (ts.normalizeSlashes(logArray[i].path).toLowerCase() === normalizedName) {
182182
return logArray[i].result;
183183
}
184184
}
@@ -203,7 +203,7 @@ module Playback {
203203
function pathsAreEquivalent(left: string, right: string, wrapper: { resolvePath(s: string): string }) {
204204
var key = left + '-~~-' + right;
205205
function areSame(a: string, b: string) {
206-
return Harness.Path.switchToForwardSlashes(a).toLowerCase() === Harness.Path.switchToForwardSlashes(b).toLowerCase();
206+
return ts.normalizeSlashes(a).toLowerCase() === ts.normalizeSlashes(b).toLowerCase();
207207
}
208208
function check() {
209209
if (Harness.Path.getFileName(left).toLowerCase() === Harness.Path.getFileName(right).toLowerCase()) {

src/harness/rwcRunner.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module RWC {
2323
function collateOutputs(outputFiles: Harness.Compiler.GeneratedFile[], clean?: (s: string) => string) {
2424
// Collect, test, and sort the filenames
2525
function cleanName(fn: string) {
26-
var lastSlash = Harness.Path.switchToForwardSlashes(fn).lastIndexOf('/');
26+
var lastSlash = ts.normalizeSlashes(fn).lastIndexOf('/');
2727
return fn.substr(lastSlash + 1).toLowerCase();
2828
}
2929
outputFiles.sort((a, b) => cleanName(a.fileName).localeCompare(cleanName(b.fileName)));
@@ -52,7 +52,7 @@ module RWC {
5252
var compilerResult: Harness.Compiler.CompilerResult;
5353
var compilerOptions: ts.CompilerOptions;
5454
var baselineOpts: Harness.Baseline.BaselineOptions = { Subfolder: 'rwc' };
55-
var baseName = /(.*)\/(.*).json/.exec(Harness.Path.switchToForwardSlashes(jsonPath))[2];
55+
var baseName = /(.*)\/(.*).json/.exec(ts.normalizeSlashes(jsonPath))[2];
5656
// Compile .d.ts files
5757
var declFileCompilationResult: {
5858
declInputFiles: { unitName: string; content: string }[];
@@ -99,7 +99,7 @@ module RWC {
9999
}
100100

101101
ts.forEach(ioLog.filesRead, fileRead => {
102-
var resolvedPath = Harness.Path.switchToForwardSlashes(sys.resolvePath(fileRead.path));
102+
var resolvedPath = ts.normalizeSlashes(sys.resolvePath(fileRead.path));
103103
var inInputList = ts.forEach(inputFiles, inputFile=> inputFile.unitName === resolvedPath);
104104
if (!inInputList) {
105105
// Add the file to other files
@@ -117,7 +117,7 @@ module RWC {
117117
});
118118

119119
function getHarnessCompilerInputUnit(fileName: string) {
120-
var resolvedPath = Harness.Path.switchToForwardSlashes(sys.resolvePath(fileName));
120+
var resolvedPath = ts.normalizeSlashes(sys.resolvePath(fileName));
121121
try {
122122
var content = sys.readFile(resolvedPath);
123123
}

src/services/compiler/pathUtils.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616
///<reference path='references.ts' />
1717

1818
module TypeScript {
19-
export function stripStartAndEndQuotes(str: string) {
20-
var firstCharCode = str && str.charCodeAt(0);
21-
if (str && str.length >= 2 && firstCharCode === str.charCodeAt(str.length - 1) && (firstCharCode === CharacterCodes.singleQuote || firstCharCode === CharacterCodes.doubleQuote)) {
22-
return str.substring(1, str.length - 1);
23-
}
24-
25-
return str;
26-
}
27-
28-
var switchToForwardSlashesRegEx = /\\/g;
29-
export function switchToForwardSlashes(path: string) {
30-
return path.replace(switchToForwardSlashesRegEx, "/");
31-
}
32-
3319
function isFileOfExtension(fname: string, ext: string) {
3420
var invariantFname = fname.toLocaleUpperCase();
3521
var invariantExt = ext.toLocaleUpperCase();
@@ -40,34 +26,4 @@ module TypeScript {
4026
export function isDTSFile(fname: string) {
4127
return isFileOfExtension(fname, ".d.ts");
4228
}
43-
44-
export function getPathComponents(path: string) {
45-
return path.split("/");
46-
}
47-
48-
var normalizePathRegEx = /^\\\\[^\\]/;
49-
export function normalizePath(path: string): string {
50-
// If it's a UNC style path (i.e. \\server\share), convert to a URI style (i.e. file://server/share)
51-
if (normalizePathRegEx.test(path)) {
52-
path = "file:" + path;
53-
}
54-
var parts = getPathComponents(switchToForwardSlashes(path));
55-
var normalizedParts: string[] = [];
56-
57-
for (var i = 0; i < parts.length; i++) {
58-
var part = parts[i];
59-
if (part === ".") {
60-
continue;
61-
}
62-
63-
if (normalizedParts.length > 0 && ArrayUtilities.last(normalizedParts) !== ".." && part === "..") {
64-
normalizedParts.pop();
65-
continue;
66-
}
67-
68-
normalizedParts.push(part);
69-
}
70-
71-
return normalizedParts.join("/");
72-
}
7329
}

src/services/services.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ module ts {
15291529
var filenames = host.getScriptFileNames();
15301530
for (var i = 0, n = filenames.length; i < n; i++) {
15311531
var filename = filenames[i];
1532-
this.filenameToEntry[switchToForwardSlashes(filename)] = {
1532+
this.filenameToEntry[normalizeSlashes(filename)] = {
15331533
filename: filename,
15341534
version: host.getScriptVersion(filename),
15351535
isOpen: host.getScriptIsOpen(filename)
@@ -1544,7 +1544,7 @@ module ts {
15441544
}
15451545

15461546
public getEntry(filename: string): HostFileInformation {
1547-
filename = switchToForwardSlashes(filename);
1547+
filename = normalizeSlashes(filename);
15481548
return lookUp(this.filenameToEntry, filename);
15491549
}
15501550

@@ -2322,7 +2322,7 @@ module ts {
23222322
function getSyntacticDiagnostics(filename: string) {
23232323
synchronizeHostData();
23242324

2325-
filename = switchToForwardSlashes(filename);
2325+
filename = normalizeSlashes(filename);
23262326

23272327
return program.getDiagnostics(getSourceFile(filename).getSourceFile());
23282328
}
@@ -2334,7 +2334,7 @@ module ts {
23342334
function getSemanticDiagnostics(filename: string) {
23352335
synchronizeHostData();
23362336

2337-
filename = switchToForwardSlashes(filename)
2337+
filename = normalizeSlashes(filename)
23382338
var compilerOptions = program.getCompilerOptions();
23392339
var checker = getFullTypeCheckChecker();
23402340
var targetSourceFile = getSourceFile(filename);
@@ -2415,7 +2415,7 @@ module ts {
24152415
function getCompletionsAtPosition(filename: string, position: number, isMemberCompletion: boolean) {
24162416
synchronizeHostData();
24172417

2418-
filename = switchToForwardSlashes(filename);
2418+
filename = normalizeSlashes(filename);
24192419

24202420
var syntacticStart = new Date().getTime();
24212421
var sourceFile = getSourceFile(filename);
@@ -2760,7 +2760,7 @@ module ts {
27602760
function getCompletionEntryDetails(filename: string, position: number, entryName: string): CompletionEntryDetails {
27612761
// Note: No need to call synchronizeHostData, as we have captured all the data we need
27622762
// in the getCompletionsAtPosition earlier
2763-
filename = switchToForwardSlashes(filename);
2763+
filename = normalizeSlashes(filename);
27642764

27652765
var sourceFile = getSourceFile(filename);
27662766

@@ -3260,7 +3260,7 @@ module ts {
32603260
function getQuickInfoAtPosition(fileName: string, position: number): QuickInfo {
32613261
synchronizeHostData();
32623262

3263-
fileName = switchToForwardSlashes(fileName);
3263+
fileName = normalizeSlashes(fileName);
32643264
var sourceFile = getSourceFile(fileName);
32653265
var node = getTouchingPropertyName(sourceFile, position);
32663266
if (!node) {
@@ -3362,7 +3362,7 @@ module ts {
33623362

33633363
synchronizeHostData();
33643364

3365-
filename = switchToForwardSlashes(filename);
3365+
filename = normalizeSlashes(filename);
33663366
var sourceFile = getSourceFile(filename);
33673367

33683368
var node = getTouchingPropertyName(sourceFile, position);
@@ -3426,7 +3426,7 @@ module ts {
34263426
function getOccurrencesAtPosition(filename: string, position: number): ReferenceEntry[] {
34273427
synchronizeHostData();
34283428

3429-
filename = switchToForwardSlashes(filename);
3429+
filename = normalizeSlashes(filename);
34303430
var sourceFile = getSourceFile(filename);
34313431

34323432
var node = getTouchingWord(sourceFile, position);
@@ -3876,7 +3876,7 @@ module ts {
38763876
function findReferences(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): ReferenceEntry[] {
38773877
synchronizeHostData();
38783878

3879-
fileName = switchToForwardSlashes(fileName);
3879+
fileName = normalizeSlashes(fileName);
38803880
var sourceFile = getSourceFile(fileName);
38813881

38823882
var node = getTouchingPropertyName(sourceFile, position);
@@ -4597,7 +4597,7 @@ module ts {
45974597

45984598
function getEmitOutput(filename: string): EmitOutput {
45994599
synchronizeHostData();
4600-
filename = switchToForwardSlashes(filename);
4600+
filename = normalizeSlashes(filename);
46014601
var compilerOptions = program.getCompilerOptions();
46024602
var targetSourceFile = program.getSourceFile(filename); // Current selected file to be output
46034603
// If --out flag is not specified, shouldEmitToOwnFile is true. Otherwise shouldEmitToOwnFile is false.
@@ -4771,7 +4771,7 @@ module ts {
47714771
function getSignatureHelpItems(fileName: string, position: number): SignatureHelpItems {
47724772
synchronizeHostData();
47734773

4774-
fileName = switchToForwardSlashes(fileName);
4774+
fileName = normalizeSlashes(fileName);
47754775
var sourceFile = getSourceFile(fileName);
47764776

47774777
return SignatureHelp.getSignatureHelpItems(sourceFile, position, typeInfoResolver, cancellationToken);
@@ -4841,12 +4841,12 @@ module ts {
48414841

48424842
/// Syntactic features
48434843
function getSyntaxTree(filename: string): TypeScript.SyntaxTree {
4844-
filename = switchToForwardSlashes(filename);
4844+
filename = normalizeSlashes(filename);
48454845
return syntaxTreeCache.getCurrentFileSyntaxTree(filename);
48464846
}
48474847

48484848
function getCurrentSourceFile(filename: string): SourceFile {
4849-
filename = switchToForwardSlashes(filename);
4849+
filename = normalizeSlashes(filename);
48504850
var currentSourceFile = syntaxTreeCache.getCurrentSourceFile(filename);
48514851
return currentSourceFile;
48524852
}
@@ -4913,14 +4913,14 @@ module ts {
49134913
}
49144914

49154915
function getNavigationBarItems(filename: string): NavigationBarItem[] {
4916-
filename = switchToForwardSlashes(filename);
4916+
filename = normalizeSlashes(filename);
49174917

49184918
return NavigationBar.getNavigationBarItems(getCurrentSourceFile(filename));
49194919
}
49204920

49214921
function getSemanticClassifications(fileName: string, span: TypeScript.TextSpan): ClassifiedSpan[] {
49224922
synchronizeHostData();
4923-
fileName = switchToForwardSlashes(fileName);
4923+
fileName = normalizeSlashes(fileName);
49244924

49254925
var sourceFile = getSourceFile(fileName);
49264926

@@ -4991,7 +4991,7 @@ module ts {
49914991

49924992
function getSyntacticClassifications(fileName: string, span: TypeScript.TextSpan): ClassifiedSpan[] {
49934993
// doesn't use compiler - no need to synchronize with host
4994-
fileName = switchToForwardSlashes(fileName);
4994+
fileName = normalizeSlashes(fileName);
49954995
var sourceFile = getCurrentSourceFile(fileName);
49964996

49974997
var result: ClassifiedSpan[] = [];
@@ -5121,7 +5121,7 @@ module ts {
51215121

51225122
function getOutliningSpans(filename: string): OutliningSpan[] {
51235123
// doesn't use compiler - no need to synchronize with host
5124-
filename = switchToForwardSlashes(filename);
5124+
filename = normalizeSlashes(filename);
51255125
var sourceFile = getCurrentSourceFile(filename);
51265126
return OutliningElementsCollector.collectElements(sourceFile);
51275127
}
@@ -5180,7 +5180,7 @@ module ts {
51805180
}
51815181

51825182
function getIndentationAtPosition(filename: string, position: number, editorOptions: EditorOptions) {
5183-
filename = switchToForwardSlashes(filename);
5183+
filename = normalizeSlashes(filename);
51845184

51855185
var start = new Date().getTime();
51865186
var sourceFile = getCurrentSourceFile(filename);
@@ -5217,21 +5217,21 @@ module ts {
52175217
}
52185218

52195219
function getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions): TextChange[] {
5220-
fileName = switchToForwardSlashes(fileName);
5220+
fileName = normalizeSlashes(fileName);
52215221

52225222
var manager = getFormattingManager(fileName, options);
52235223
return manager.formatSelection(start, end);
52245224
}
52255225

52265226
function getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions): TextChange[] {
5227-
fileName = switchToForwardSlashes(fileName);
5227+
fileName = normalizeSlashes(fileName);
52285228

52295229
var manager = getFormattingManager(fileName, options);
52305230
return manager.formatDocument();
52315231
}
52325232

52335233
function getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions): TextChange[] {
5234-
fileName = switchToForwardSlashes(fileName);
5234+
fileName = normalizeSlashes(fileName);
52355235

52365236
var manager = getFormattingManager(fileName, options);
52375237

@@ -5257,7 +5257,7 @@ module ts {
52575257
// anything away.
52585258
synchronizeHostData();
52595259

5260-
filename = TypeScript.switchToForwardSlashes(filename);
5260+
filename = normalizeSlashes(filename);
52615261

52625262
var sourceFile = getSourceFile(filename);
52635263

@@ -5417,7 +5417,7 @@ module ts {
54175417
function getRenameInfo(fileName: string, position: number): RenameInfo {
54185418
synchronizeHostData();
54195419

5420-
fileName = switchToForwardSlashes(fileName);
5420+
fileName = normalizeSlashes(fileName);
54215421
var sourceFile = getSourceFile(fileName);
54225422

54235423
var node = getTouchingWord(sourceFile, position);

src/services/shims.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,15 +858,15 @@ module ts {
858858

859859
forEach(result.referencedFiles, refFile => {
860860
convertResult.referencedFiles.push({
861-
path: switchToForwardSlashes(normalizePath(refFile.filename)),
861+
path: normalizePath(refFile.filename),
862862
position: refFile.pos,
863863
length: refFile.end - refFile.pos
864864
});
865865
});
866866

867867
forEach(result.importedFiles, importedFile => {
868868
convertResult.importedFiles.push({
869-
path: switchToForwardSlashes(importedFile.filename),
869+
path: normalizeSlashes(importedFile.filename),
870870
position: importedFile.pos,
871871
length: importedFile.end - importedFile.pos
872872
});

0 commit comments

Comments
 (0)