From 349367d991d6cc3ec691dbb842f11275de3a4190 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 11 Aug 2014 15:35:15 -0700 Subject: [PATCH] Consolidate logic to use the lib in one location in the tests --- src/harness/fourslash.ts | 9 +- src/harness/harness.ts | 18 +- src/harness/harnessLanguageService.ts | 2 +- src/harness/projectsRunner.ts | 6 +- .../duplicateStringIndexers.errors.txt | 155 ++++-------------- .../reference/duplicateStringIndexers.js | 64 ++++---- .../types/members/duplicateStringIndexers.ts | 49 +++--- 7 files changed, 108 insertions(+), 195 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 6692059c806..cb85169321b 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -254,7 +254,7 @@ module FourSlash { } }); - this.languageServiceShimHost.addScript('lib.d.ts', Harness.Compiler.libTextMinimal); + this.languageServiceShimHost.addDefaultLibrary(); // Sneak into the language service and get its compiler so we can examine the syntax trees @@ -1467,7 +1467,7 @@ module FourSlash { var referenceLanguageService = referenceLanguageServiceShim.languageService; // Add lib.d.ts to the reference language service - referenceLanguageServiceShimHost.addScript('lib.d.ts', Harness.Compiler.libTextMinimal); + referenceLanguageServiceShimHost.addDefaultLibrary(); for (var i = 0; i < this.testData.files.length; i++) { var file = this.testData.files[i]; @@ -1885,7 +1885,7 @@ module FourSlash { // Cache these between executions so we don't have to re-parse them for every test var fourslashSourceFile: ts.SourceFile = undefined; - var libdtsSourceFile: ts.SourceFile = undefined; + export function runFourSlashTestContent(content: string, fileName: string): TestXmlData { // Parse out the files and their metadata var testData = parseTestData(content, fileName); @@ -1896,12 +1896,11 @@ module FourSlash { var fourslashFilename = 'fourslash.ts'; var tsFn = 'tests/cases/fourslash/' + fourslashFilename; fourslashSourceFile = fourslashSourceFile || ts.createSourceFile(tsFn, Harness.IO.readFile(tsFn), ts.ScriptTarget.ES5, /*version*/ 0, /*isOpen*/ false); - libdtsSourceFile = libdtsSourceFile || ts.createSourceFile('lib.d.ts', Harness.Compiler.libTextMinimal, ts.ScriptTarget.ES3, /*version*/ 0, /*isOpen*/ false); var files: { [filename: string]: ts.SourceFile; } = {}; files[fourslashFilename] = fourslashSourceFile; files[fileName] = ts.createSourceFile(fileName, content, ts.ScriptTarget.ES5, /*version*/ 0, /*isOpen*/ false); - files['lib.d.ts'] = libdtsSourceFile; + files[Harness.Compiler.defaultLibFileName] = Harness.Compiler.defaultLibSourceFile; var host = Harness.Compiler.createCompilerHost(files, (fn, contents) => result = contents); var program = ts.createProgram([fileName, fourslashFilename], {}, host); diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 841c3b47aa4..dce80544222 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -434,15 +434,15 @@ module Harness { export var libFolder: string; switch (Utils.getExecutionEnvironment()) { case Utils.ExecutionEnvironment.CScript: - libFolder = Path.filePath(global['WScript'].ScriptFullName); + libFolder = "built/local/"; tcServicesFilename = "built/local/typescriptServices.js"; break; case Utils.ExecutionEnvironment.Node: - libFolder = (__dirname + '/'); + libFolder = "built/local/"; tcServicesFilename = "built/local/typescriptServices.js"; break; case Utils.ExecutionEnvironment.Browser: - libFolder = "bin/"; + libFolder = "built/local/"; tcServicesFilename = "built/local/typescriptServices.js"; break; default: @@ -531,8 +531,8 @@ module Harness { } } - export var libText = IO.readFile(libFolder + "lib.d.ts"); - export var libTextMinimal = IO.readFile('bin/lib.core.d.ts'); + export var defaultLibFileName = 'lib.d.ts'; + export var defaultLibSourceFile = ts.createSourceFile(defaultLibFileName, IO.readFile(libFolder + 'lib.core.d.ts'), /*languageVersion*/ ts.ScriptTarget.ES5); export function createCompilerHost(filemap: { [filename: string]: ts.SourceFile; }, writeFile: (fn: string, contents: string, writeByteOrderMark:boolean) => void): ts.CompilerHost { return { @@ -542,15 +542,15 @@ module Harness { if (fn in filemap) { return filemap[fn]; } else { - var lib = 'lib.d.ts'; - if (fn.substr(fn.length - lib.length) === lib) { - return filemap[fn] = ts.createSourceFile('lib.d.ts', libTextMinimal, languageVersion); + var lib = defaultLibFileName; + if (fn === defaultLibFileName) { + return defaultLibSourceFile; } // Don't throw here -- the compiler might be looking for a test that actually doesn't exist as part of the TC return null; } }, - getDefaultLibFilename: () => 'lib.d.ts', + getDefaultLibFilename: () => defaultLibFileName, writeFile: writeFile, getCanonicalFileName: ts.getCanonicalFileName, useCaseSensitiveFileNames: () => sys.useCaseSensitiveFileNames, diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index 65f02582d63..cb8ef61f413 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -175,7 +175,7 @@ module Harness.LanguageService { } public addDefaultLibrary() { - this.addScript("lib.d.ts", Harness.Compiler.libText); + this.addScript(Harness.Compiler.defaultLibFileName, Harness.Compiler.defaultLibSourceFile.text); } public getHostIdentifier(): string { diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 5ddcf57804c..5b4b1712e8e 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -136,8 +136,8 @@ class ProjectRunner extends RunnerBase { function getSourceFile(filename: string, languageVersion: ts.ScriptTarget): ts.SourceFile { var sourceFile: ts.SourceFile = undefined; - if (filename === 'lib.d.ts') { - sourceFile = ts.createSourceFile('lib.d.ts', Harness.Compiler.libTextMinimal, languageVersion); + if (filename === Harness.Compiler.defaultLibFileName) { + sourceFile = Harness.Compiler.defaultLibSourceFile; } else { assert.isTrue(!ts.filter(readInputFiles, sourceFile => sourceFile.filename == filename).length, "Compiler trying to read same file again: " + filename); @@ -217,7 +217,7 @@ class ProjectRunner extends RunnerBase { function createCompilerHost(): ts.CompilerHost { return { getSourceFile: getSourceFile, - getDefaultLibFilename: () => "lib.d.ts", + getDefaultLibFilename: () => Harness.Compiler.defaultLibFileName, writeFile: writeFile, getCurrentDirectory: getCurrentDirectory, getCanonicalFileName: ts.getCanonicalFileName, diff --git a/tests/baselines/reference/duplicateStringIndexers.errors.txt b/tests/baselines/reference/duplicateStringIndexers.errors.txt index d5a31bd238e..f4cfe243bd3 100644 --- a/tests/baselines/reference/duplicateStringIndexers.errors.txt +++ b/tests/baselines/reference/duplicateStringIndexers.errors.txt @@ -1,138 +1,47 @@ -==== tests/cases/conformance/types/members/duplicateStringIndexers.ts (52 errors) ==== +==== tests/cases/conformance/types/members/duplicateStringIndexers.ts (6 errors) ==== // it is an error to have duplicate index signatures of the same kind in a type - interface Number { - [x: string]: string; - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'toExponential' of type '(fractionDigits?: number) => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'toFixed' of type '(fractionDigits?: number) => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'toPrecision' of type '(precision?: number) => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'toString' of type '(radix?: number) => string' is not assignable to string index type 'string'. - [x: string]: string; - ~~~~~~~~~~~~~~~~~~~~ + module test { + interface Number { + [x: string]: string; + [x: string]: string; + ~~~~~~~~~~~~~~~~~~~~ !!! Duplicate string index signature. - } + } - interface String { - [x: string]: string; - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'charAt' of type '(pos: number) => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'charCodeAt' of type '(index: number) => number' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'concat' of type '(...strings: string[]) => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'indexOf' of type '(searchString: string, position?: number) => number' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'lastIndexOf' of type '(searchString: string, position?: number) => number' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'length' of type 'number' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'localeCompare' of type '(that: string) => number' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'match' of type '{ (regexp: string): string[]; (regexp: RegExp): string[]; }' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'replace' of type '{ (searchValue: string, replaceValue: string): string; (searchValue: string, replaceValue: (substring: string, ...args: any[]) => string): string; (searchValue: RegExp, replaceValue: string): string; (searchValue: RegExp, replaceValue: (substring: string, ...args: any[]) => string): string; }' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'search' of type '{ (regexp: string): number; (regexp: RegExp): number; }' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'slice' of type '(start?: number, end?: number) => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'split' of type '{ (separator: string, limit?: number): string[]; (separator: RegExp, limit?: number): string[]; }' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'substr' of type '(from: number, length?: number) => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'substring' of type '(start: number, end?: number) => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'toLocaleLowerCase' of type '() => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'toLocaleUpperCase' of type '() => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'toLowerCase' of type '() => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'toString' of type '() => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'toUpperCase' of type '() => string' is not assignable to string index type 'string'. - ~~~~~~~~~~~~~~~~~~~~ -!!! Property 'trim' of type '() => string' is not assignable to string index type 'string'. - [x: string]: string; - ~~~~~~~~~~~~~~~~~~~~ + interface String { + [x: string]: string; + [x: string]: string; + ~~~~~~~~~~~~~~~~~~~~ !!! Duplicate string index signature. - } + } - interface Array { - [x: string]: T; - ~~~~~~~~~~~~~~~ -!!! Property 'concat' of type '{ (...items: U[]): T[]; (...items: T[]): T[]; }' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'every' of type '(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) => boolean' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'filter' of type '(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) => T[]' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'forEach' of type '(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any) => void' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'indexOf' of type '(searchElement: T, fromIndex?: number) => number' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'join' of type '(separator?: string) => string' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'lastIndexOf' of type '(searchElement: T, fromIndex?: number) => number' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'length' of type 'number' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'map' of type '(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any) => U[]' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'pop' of type '() => T' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'push' of type '(...items: T[]) => number' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'reduce' of type '{ (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; }' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'reduceRight' of type '{ (callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue?: T): T; (callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; }' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'reverse' of type '() => T[]' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'shift' of type '() => T' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'slice' of type '(start?: number, end?: number) => T[]' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'some' of type '(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any) => boolean' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'sort' of type '(compareFn?: (a: T, b: T) => number) => T[]' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'splice' of type '{ (start: number): T[]; (start: number, deleteCount: number, ...items: T[]): T[]; }' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'toLocaleString' of type '() => string' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'toString' of type '() => string' is not assignable to string index type 'T'. - ~~~~~~~~~~~~~~~ -!!! Property 'unshift' of type '(...items: T[]) => number' is not assignable to string index type 'T'. - [x: string]: T; - ~~~~~~~~~~~~~~~ + interface Array { + [x: string]: T; + [x: string]: T; + ~~~~~~~~~~~~~~~ !!! Duplicate string index signature. - } + } - class C { - [x: string]: string; - [x: string]: string; - ~~~~~~~~~~~~~~~~~~~~ + class C { + [x: string]: string; + [x: string]: string; + ~~~~~~~~~~~~~~~~~~~~ !!! Duplicate string index signature. - } + } - interface I { - [x: string]: string; - [x: string]: string; - ~~~~~~~~~~~~~~~~~~~~ + interface I { + [x: string]: string; + [x: string]: string; + ~~~~~~~~~~~~~~~~~~~~ !!! Duplicate string index signature. - } + } - var a: { - [x: string]: string; - [x: string]: string; - ~~~~~~~~~~~~~~~~~~~~ + var a: { + [x: string]: string; + [x: string]: string; + ~~~~~~~~~~~~~~~~~~~~ !!! Duplicate string index signature. + } } - \ No newline at end of file diff --git a/tests/baselines/reference/duplicateStringIndexers.js b/tests/baselines/reference/duplicateStringIndexers.js index bace9c15228..6937530c576 100644 --- a/tests/baselines/reference/duplicateStringIndexers.js +++ b/tests/baselines/reference/duplicateStringIndexers.js @@ -1,42 +1,46 @@ //// [duplicateStringIndexers.ts] // it is an error to have duplicate index signatures of the same kind in a type -interface Number { - [x: string]: string; - [x: string]: string; -} +module test { + interface Number { + [x: string]: string; + [x: string]: string; + } -interface String { - [x: string]: string; - [x: string]: string; -} + interface String { + [x: string]: string; + [x: string]: string; + } -interface Array { - [x: string]: T; - [x: string]: T; -} + interface Array { + [x: string]: T; + [x: string]: T; + } -class C { - [x: string]: string; - [x: string]: string; -} + class C { + [x: string]: string; + [x: string]: string; + } -interface I { - [x: string]: string; - [x: string]: string; -} + interface I { + [x: string]: string; + [x: string]: string; + } -var a: { - [x: string]: string; - [x: string]: string; + var a: { + [x: string]: string; + [x: string]: string; + } } - //// [duplicateStringIndexers.js] -var C = (function () { - function C() { - } - return C; -})(); -var a; +var test; +(function (test) { + var C = (function () { + function C() { + } + return C; + })(); + var a; +})(test || (test = {})); diff --git a/tests/cases/conformance/types/members/duplicateStringIndexers.ts b/tests/cases/conformance/types/members/duplicateStringIndexers.ts index e2d8def6a0c..67e12d88017 100644 --- a/tests/cases/conformance/types/members/duplicateStringIndexers.ts +++ b/tests/cases/conformance/types/members/duplicateStringIndexers.ts @@ -1,32 +1,33 @@ // it is an error to have duplicate index signatures of the same kind in a type -interface Number { - [x: string]: string; - [x: string]: string; -} +module test { + interface Number { + [x: string]: string; + [x: string]: string; + } -interface String { - [x: string]: string; - [x: string]: string; -} + interface String { + [x: string]: string; + [x: string]: string; + } -interface Array { - [x: string]: T; - [x: string]: T; -} + interface Array { + [x: string]: T; + [x: string]: T; + } -class C { - [x: string]: string; - [x: string]: string; -} + class C { + [x: string]: string; + [x: string]: string; + } -interface I { - [x: string]: string; - [x: string]: string; -} + interface I { + [x: string]: string; + [x: string]: string; + } -var a: { - [x: string]: string; - [x: string]: string; + var a: { + [x: string]: string; + [x: string]: string; + } } -