Merge pull request #1116 from Microsoft/normalizeSlashes

Consolidate normalizing slashes from harness to use it from typescript core.ts
This commit is contained in:
Sheetal Nandi
2014-11-10 16:28:11 -08:00
9 changed files with 38 additions and 89 deletions

1
.gitignore vendored
View File

@@ -32,6 +32,7 @@ build.json
tests/webhost/*.d.ts
tests/webhost/webtsc.js
tests/*.js
tests/*.js.map
tests/*.d.ts
*.config
scripts/debug.bat

View File

@@ -20,7 +20,7 @@ class FourslashRunner extends RunnerBase {
});
this.tests.forEach((fn: string) => {
fn = Harness.Path.switchToForwardSlashes(fn);
fn = ts.normalizeSlashes(fn);
var justName = fn.replace(/^.*[\\\/]/, '');
// Convert to relative path

View File

@@ -117,15 +117,11 @@ module Harness.Path {
}
export function filePath(fullPath: string) {
fullPath = switchToForwardSlashes(fullPath);
fullPath = ts.normalizeSlashes(fullPath);
var components = fullPath.split("/");
var path: string[] = components.slice(0, components.length - 1);
return path.join("/") + "/";
}
export function switchToForwardSlashes(path: string) {
return path.replace(/\\/g, "/").replace(/\/\//g, '/');
}
}
module Harness {
@@ -564,7 +560,7 @@ module Harness {
// Register input files
function register(file: { unitName: string; content: string; }) {
if (file.content !== undefined) {
var filename = Path.switchToForwardSlashes(file.unitName);
var filename = ts.normalizeSlashes(file.unitName);
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, scriptTarget, /*version:*/ "0");
}
};
@@ -782,7 +778,7 @@ module Harness {
var filemap: { [name: string]: ts.SourceFile; } = {};
var register = (file: { unitName: string; content: string; }) => {
if (file.content !== undefined) {
var filename = Path.switchToForwardSlashes(file.unitName);
var filename = ts.normalizeSlashes(file.unitName);
filemap[getCanonicalFileName(filename)] = ts.createSourceFile(filename, file.content, options.target, /*version:*/ "0");
}
};

View File

@@ -175,10 +175,10 @@ module Playback {
}
function findResultByPath<T>(wrapper: { resolvePath(s: string): string }, logArray: { path: string; result?: T }[], expectedPath: string, defaultValue?: T): T {
var normalizedName = Harness.Path.switchToForwardSlashes(expectedPath).toLowerCase();
var normalizedName = ts.normalizeSlashes(expectedPath).toLowerCase();
// Try to find the result through normal filename
for (var i = 0; i < logArray.length; i++) {
if (Harness.Path.switchToForwardSlashes(logArray[i].path).toLowerCase() === normalizedName) {
if (ts.normalizeSlashes(logArray[i].path).toLowerCase() === normalizedName) {
return logArray[i].result;
}
}
@@ -203,7 +203,7 @@ module Playback {
function pathsAreEquivalent(left: string, right: string, wrapper: { resolvePath(s: string): string }) {
var key = left + '-~~-' + right;
function areSame(a: string, b: string) {
return Harness.Path.switchToForwardSlashes(a).toLowerCase() === Harness.Path.switchToForwardSlashes(b).toLowerCase();
return ts.normalizeSlashes(a).toLowerCase() === ts.normalizeSlashes(b).toLowerCase();
}
function check() {
if (Harness.Path.getFileName(left).toLowerCase() === Harness.Path.getFileName(right).toLowerCase()) {

View File

@@ -23,7 +23,7 @@ module RWC {
function collateOutputs(outputFiles: Harness.Compiler.GeneratedFile[], clean?: (s: string) => string) {
// Collect, test, and sort the filenames
function cleanName(fn: string) {
var lastSlash = Harness.Path.switchToForwardSlashes(fn).lastIndexOf('/');
var lastSlash = ts.normalizeSlashes(fn).lastIndexOf('/');
return fn.substr(lastSlash + 1).toLowerCase();
}
outputFiles.sort((a, b) => cleanName(a.fileName).localeCompare(cleanName(b.fileName)));
@@ -52,7 +52,7 @@ module RWC {
var compilerResult: Harness.Compiler.CompilerResult;
var compilerOptions: ts.CompilerOptions;
var baselineOpts: Harness.Baseline.BaselineOptions = { Subfolder: 'rwc' };
var baseName = /(.*)\/(.*).json/.exec(Harness.Path.switchToForwardSlashes(jsonPath))[2];
var baseName = /(.*)\/(.*).json/.exec(ts.normalizeSlashes(jsonPath))[2];
// Compile .d.ts files
var declFileCompilationResult: {
declInputFiles: { unitName: string; content: string }[];
@@ -99,7 +99,7 @@ module RWC {
}
ts.forEach(ioLog.filesRead, fileRead => {
var resolvedPath = Harness.Path.switchToForwardSlashes(sys.resolvePath(fileRead.path));
var resolvedPath = ts.normalizeSlashes(sys.resolvePath(fileRead.path));
var inInputList = ts.forEach(inputFiles, inputFile=> inputFile.unitName === resolvedPath);
if (!inInputList) {
// Add the file to other files
@@ -117,7 +117,7 @@ module RWC {
});
function getHarnessCompilerInputUnit(fileName: string) {
var resolvedPath = Harness.Path.switchToForwardSlashes(sys.resolvePath(fileName));
var resolvedPath = ts.normalizeSlashes(sys.resolvePath(fileName));
try {
var content = sys.readFile(resolvedPath);
}

View File

@@ -16,20 +16,6 @@
///<reference path='references.ts' />
module TypeScript {
export function stripStartAndEndQuotes(str: string) {
var firstCharCode = str && str.charCodeAt(0);
if (str && str.length >= 2 && firstCharCode === str.charCodeAt(str.length - 1) && (firstCharCode === CharacterCodes.singleQuote || firstCharCode === CharacterCodes.doubleQuote)) {
return str.substring(1, str.length - 1);
}
return str;
}
var switchToForwardSlashesRegEx = /\\/g;
export function switchToForwardSlashes(path: string) {
return path.replace(switchToForwardSlashesRegEx, "/");
}
function isFileOfExtension(fname: string, ext: string) {
var invariantFname = fname.toLocaleUpperCase();
var invariantExt = ext.toLocaleUpperCase();
@@ -40,34 +26,4 @@ module TypeScript {
export function isDTSFile(fname: string) {
return isFileOfExtension(fname, ".d.ts");
}
export function getPathComponents(path: string) {
return path.split("/");
}
var normalizePathRegEx = /^\\\\[^\\]/;
export function normalizePath(path: string): string {
// If it's a UNC style path (i.e. \\server\share), convert to a URI style (i.e. file://server/share)
if (normalizePathRegEx.test(path)) {
path = "file:" + path;
}
var parts = getPathComponents(switchToForwardSlashes(path));
var normalizedParts: string[] = [];
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (part === ".") {
continue;
}
if (normalizedParts.length > 0 && ArrayUtilities.last(normalizedParts) !== ".." && part === "..") {
normalizedParts.pop();
continue;
}
normalizedParts.push(part);
}
return normalizedParts.join("/");
}
}

View File

@@ -1529,7 +1529,7 @@ module ts {
var filenames = host.getScriptFileNames();
for (var i = 0, n = filenames.length; i < n; i++) {
var filename = filenames[i];
this.filenameToEntry[switchToForwardSlashes(filename)] = {
this.filenameToEntry[normalizeSlashes(filename)] = {
filename: filename,
version: host.getScriptVersion(filename),
isOpen: host.getScriptIsOpen(filename)
@@ -1544,7 +1544,7 @@ module ts {
}
public getEntry(filename: string): HostFileInformation {
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
return lookUp(this.filenameToEntry, filename);
}
@@ -2322,7 +2322,7 @@ module ts {
function getSyntacticDiagnostics(filename: string) {
synchronizeHostData();
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
return program.getDiagnostics(getSourceFile(filename).getSourceFile());
}
@@ -2334,7 +2334,7 @@ module ts {
function getSemanticDiagnostics(filename: string) {
synchronizeHostData();
filename = switchToForwardSlashes(filename)
filename = normalizeSlashes(filename)
var compilerOptions = program.getCompilerOptions();
var checker = getFullTypeCheckChecker();
var targetSourceFile = getSourceFile(filename);
@@ -2415,7 +2415,7 @@ module ts {
function getCompletionsAtPosition(filename: string, position: number, isMemberCompletion: boolean) {
synchronizeHostData();
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
var syntacticStart = new Date().getTime();
var sourceFile = getSourceFile(filename);
@@ -2760,7 +2760,7 @@ module ts {
function getCompletionEntryDetails(filename: string, position: number, entryName: string): CompletionEntryDetails {
// Note: No need to call synchronizeHostData, as we have captured all the data we need
// in the getCompletionsAtPosition earlier
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
var sourceFile = getSourceFile(filename);
@@ -3260,7 +3260,7 @@ module ts {
function getQuickInfoAtPosition(fileName: string, position: number): QuickInfo {
synchronizeHostData();
fileName = switchToForwardSlashes(fileName);
fileName = normalizeSlashes(fileName);
var sourceFile = getSourceFile(fileName);
var node = getTouchingPropertyName(sourceFile, position);
if (!node) {
@@ -3362,7 +3362,7 @@ module ts {
synchronizeHostData();
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
var sourceFile = getSourceFile(filename);
var node = getTouchingPropertyName(sourceFile, position);
@@ -3426,7 +3426,7 @@ module ts {
function getOccurrencesAtPosition(filename: string, position: number): ReferenceEntry[] {
synchronizeHostData();
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
var sourceFile = getSourceFile(filename);
var node = getTouchingWord(sourceFile, position);
@@ -3876,7 +3876,7 @@ module ts {
function findReferences(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): ReferenceEntry[] {
synchronizeHostData();
fileName = switchToForwardSlashes(fileName);
fileName = normalizeSlashes(fileName);
var sourceFile = getSourceFile(fileName);
var node = getTouchingPropertyName(sourceFile, position);
@@ -4597,7 +4597,7 @@ module ts {
function getEmitOutput(filename: string): EmitOutput {
synchronizeHostData();
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
var compilerOptions = program.getCompilerOptions();
var targetSourceFile = program.getSourceFile(filename); // Current selected file to be output
// If --out flag is not specified, shouldEmitToOwnFile is true. Otherwise shouldEmitToOwnFile is false.
@@ -4771,7 +4771,7 @@ module ts {
function getSignatureHelpItems(fileName: string, position: number): SignatureHelpItems {
synchronizeHostData();
fileName = switchToForwardSlashes(fileName);
fileName = normalizeSlashes(fileName);
var sourceFile = getSourceFile(fileName);
return SignatureHelp.getSignatureHelpItems(sourceFile, position, typeInfoResolver, cancellationToken);
@@ -4841,12 +4841,12 @@ module ts {
/// Syntactic features
function getSyntaxTree(filename: string): TypeScript.SyntaxTree {
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
return syntaxTreeCache.getCurrentFileSyntaxTree(filename);
}
function getCurrentSourceFile(filename: string): SourceFile {
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
var currentSourceFile = syntaxTreeCache.getCurrentSourceFile(filename);
return currentSourceFile;
}
@@ -4913,14 +4913,14 @@ module ts {
}
function getNavigationBarItems(filename: string): NavigationBarItem[] {
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
return NavigationBar.getNavigationBarItems(getCurrentSourceFile(filename));
}
function getSemanticClassifications(fileName: string, span: TypeScript.TextSpan): ClassifiedSpan[] {
synchronizeHostData();
fileName = switchToForwardSlashes(fileName);
fileName = normalizeSlashes(fileName);
var sourceFile = getSourceFile(fileName);
@@ -4991,7 +4991,7 @@ module ts {
function getSyntacticClassifications(fileName: string, span: TypeScript.TextSpan): ClassifiedSpan[] {
// doesn't use compiler - no need to synchronize with host
fileName = switchToForwardSlashes(fileName);
fileName = normalizeSlashes(fileName);
var sourceFile = getCurrentSourceFile(fileName);
var result: ClassifiedSpan[] = [];
@@ -5121,7 +5121,7 @@ module ts {
function getOutliningSpans(filename: string): OutliningSpan[] {
// doesn't use compiler - no need to synchronize with host
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
var sourceFile = getCurrentSourceFile(filename);
return OutliningElementsCollector.collectElements(sourceFile);
}
@@ -5180,7 +5180,7 @@ module ts {
}
function getIndentationAtPosition(filename: string, position: number, editorOptions: EditorOptions) {
filename = switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
var start = new Date().getTime();
var sourceFile = getCurrentSourceFile(filename);
@@ -5217,21 +5217,21 @@ module ts {
}
function getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions): TextChange[] {
fileName = switchToForwardSlashes(fileName);
fileName = normalizeSlashes(fileName);
var manager = getFormattingManager(fileName, options);
return manager.formatSelection(start, end);
}
function getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions): TextChange[] {
fileName = switchToForwardSlashes(fileName);
fileName = normalizeSlashes(fileName);
var manager = getFormattingManager(fileName, options);
return manager.formatDocument();
}
function getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions): TextChange[] {
fileName = switchToForwardSlashes(fileName);
fileName = normalizeSlashes(fileName);
var manager = getFormattingManager(fileName, options);
@@ -5257,7 +5257,7 @@ module ts {
// anything away.
synchronizeHostData();
filename = TypeScript.switchToForwardSlashes(filename);
filename = normalizeSlashes(filename);
var sourceFile = getSourceFile(filename);
@@ -5417,7 +5417,7 @@ module ts {
function getRenameInfo(fileName: string, position: number): RenameInfo {
synchronizeHostData();
fileName = switchToForwardSlashes(fileName);
fileName = normalizeSlashes(fileName);
var sourceFile = getSourceFile(fileName);
var node = getTouchingWord(sourceFile, position);

View File

@@ -858,7 +858,7 @@ module ts {
forEach(result.referencedFiles, refFile => {
convertResult.referencedFiles.push({
path: switchToForwardSlashes(normalizePath(refFile.filename)),
path: normalizePath(refFile.filename),
position: refFile.pos,
length: refFile.end - refFile.pos
});
@@ -866,7 +866,7 @@ module ts {
forEach(result.importedFiles, importedFile => {
convertResult.importedFiles.push({
path: switchToForwardSlashes(importedFile.filename),
path: normalizeSlashes(importedFile.filename),
position: importedFile.pos,
length: importedFile.end - importedFile.pos
});

View File

@@ -259,10 +259,6 @@ module ts {
return n.kind === SyntaxKind.StringLiteral || n.kind === SyntaxKind.NumericLiteral || isWord(n);
}
export var switchToForwardSlashesRegEx = /\\/g;
export function switchToForwardSlashes(path: string) {
return path.replace(switchToForwardSlashesRegEx, "/");
}
export function isComment(n: Node): boolean {
return n.kind === SyntaxKind.SingleLineCommentTrivia || n.kind === SyntaxKind.MultiLineCommentTrivia;
}