Merge pull request #9095 from RyanCavanaugh/implicitTypeReferences

Implicit type inclusion changes
This commit is contained in:
Ryan Cavanaugh
2016-06-13 16:33:22 -07:00
committed by GitHub
100 changed files with 603 additions and 523 deletions
+1
View File
@@ -1,6 +1,7 @@
// @outFile: concat.js
// @module: amd
// @moduleResolution: node
// @Filename: A:/bar.ts
import {z} from "./foo";
export var x = z + z;
@@ -1,7 +1,7 @@
// @noImplicitReferences: true
// @traceResolution: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @filename: /types/lib/index.d.ts
@@ -1,6 +1,6 @@
// @noImplicitReferences: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @traceResolution: true
// @filename: /ref.d.ts
@@ -1,6 +1,6 @@
// @noImplicitReferences: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @traceResolution: true
// @types: lib
// @out: output.js
@@ -1,6 +1,6 @@
// @noImplicitReferences: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @traceResolution: true
// @out: output.js
@@ -1,6 +1,6 @@
// @noImplicitReferences: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @traceResolution: true
// @filename: /ref.d.ts
@@ -1,7 +1,7 @@
// @noImplicitReferences: true
// @traceResolution: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @types: lib
// @filename: /types/lib/index.d.ts
@@ -1,6 +1,6 @@
// @noImplicitReferences: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @traceResolution: true
// $ comes from d.ts file - no need to add type reference directive
@@ -1,7 +1,7 @@
// @noImplicitReferences: true
// @traceResolution: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// $ comes from d.ts file - no need to add type reference directive
@@ -1,7 +1,7 @@
// @noImplicitReferences: true
// @traceResolution: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @filename: /ref.d.ts
export interface $ { x }
@@ -1,7 +1,7 @@
// @noImplicitReferences: true
// @traceResolution: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// $ comes from type declaration file - type reference directive should be added
@@ -1,7 +1,7 @@
// @noImplicitReferences: true
// @traceResolution: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// local value shadows global - no need to add type reference directive
@@ -1,6 +1,6 @@
// @noImplicitReferences: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @traceResolution: true
// @types: lib
@@ -1,6 +1,6 @@
// @noImplicitReferences: true
// @declaration: true
// @typesRoot: /
// @typeRoots: /types
// @traceResolution: true
// @filename: /types/lib/index.d.ts
@@ -1,13 +1,14 @@
// @noImplicitReferences: true
// @traceResolution: true
// @typesRoot: /
// @currentDirectory: /src
// @typeRoots: types
// We can find typings in the ./types folder
// @filename: /types/jquery/index.d.ts
// @filename: /src/types/jquery/index.d.ts
declare var $: { foo(): void };
// @filename: /consumer.ts
// @filename: /src/consumer.ts
/// <reference types="jquery" />
$.foo();
@@ -1,18 +1,19 @@
// @noImplicitReferences: true
// @traceResolution: true
// @typesRoot: /
// @currentDirectory: /foo
// @typeRoots: ./types
// package.json in a primary reference can refer to another file
// @filename: /types/jquery/package.json
// @filename: /foo/types/jquery/package.json
{
"typings": "jquery.d.ts"
}
// @filename: /types/jquery/jquery.d.ts
// @filename: /foo/types/jquery/jquery.d.ts
declare var $: { foo(): void };
// @filename: /consumer.ts
// @filename: /foo/consumer.ts
/// <reference types="jquery" />
$.foo();
@@ -6,7 +6,8 @@
// @filename: /a/tsconfig.json
{
"compilerOptions": {
"types": [ "jquery" ]
"types": [ "jquery" ],
"typeRoots": ["/a/types"]
}
}
@@ -1,7 +1,8 @@
// @noImplicitReferences: true
// @traceResolution: true
// @types: jquery
// @typesRoot: /a
// @typeRoots: /a/types
// @currentDirectory: /a
// @filename: /a/types/jquery/index.d.ts
declare var $: { foo(): void };
@@ -1,11 +1,15 @@
// @noImplicitReferences: true
// @traceResolution: true
// @types: jquery
// @currentDirectory: /
// @currentDirectory: /a
// @typeRoots: types
// @filename: /a/types/jquery/index.d.ts
declare var $: { foo(): void };
// @filename: /a/types/jquery2/index.d.ts
declare var $2: { foo(): void };
// @filename: /a/b/consumer.ts
$.foo();
$.foo(); // should OK
$2.foo(); // should error
@@ -1,6 +1,7 @@
// @noImplicitReferences: true
// @traceResolution: true
// @typesRoot: /
// @typeRoots: /types
// @currentDirectory: test
// package.json in a primary reference can refer to another file
@@ -1,6 +1,6 @@
// @noImplicitReferences: true
// @traceResolution: true
// @typesRoot: /src
// @currentDirectory: /src
// Secondary references are possible
@@ -1,6 +1,7 @@
// @noImplicitReferences: true
// @traceResolution: true
// @typesRoot: /src
// @typeRoots: /src
// @currentDirectory: test
// Secondary references may be duplicated if they agree in content
@@ -1,6 +1,7 @@
// @noImplicitReferences: true
// @traceResolution: true
// @currentDirectory: /
// @typeRoots: types
// Secondary references may not be duplicated if they disagree in content
@@ -1,9 +1,10 @@
// @noImplicitReferences: true
// @traceResolution: true
// @currentDirectory: /
// The primary lookup folder is relative to tsconfig.json, if present
// @filename: /types/alpha/index.d.ts
// @filename: /node_modules/@types/alpha/index.d.ts
declare var alpha: { a: string };
// @filename: /src/foo.ts
@@ -1,18 +1,19 @@
// @noImplicitReferences: true
// @traceResolution: true
// @typesRoot: /
// @typeRoots: /test/types
// @currentDirectory: /test
// Don't crash in circular library reference situations
// @filename: /types/alpha/index.d.ts
// @filename: /test/types/alpha/index.d.ts
/// <reference types="beta" />
declare var alpha: { a: string };
// @filename: /types/beta/index.d.ts
// @filename: /test/types/beta/index.d.ts
/// <reference types="alpha" />
declare var beta: { b: string };
// @filename: /foo.ts
// @filename: /test/foo.ts
/// <reference types="alpha" />
/// <reference types="beta" />
var x: string = alpha.a + beta.b;
@@ -1,20 +0,0 @@
// @noImplicitReferences: true
// @traceResolution: true
// Use types search path
// @filename: /share/typelib/alpha/index.d.ts
declare var alpha: { a: string };
// @filename: /base/src/foo.ts
/// <reference types="alpha" />
var x: string = alpha.a;
// @filename: /tsconfig.json
{
"compilerOptions": {
"typesSearchPaths": [
"./share/typelib"
]
}
}
@@ -1,10 +1,12 @@
// @traceResolution: true
// @noImplicitReferences: true
// @filename: tsconfig.json
// @filename: /tsconfig.json
{ "files": "a.ts" }
// @filename: node_modules/@types/jquery/index.d.ts
// @filename: /node_modules/@types/jquery/index.d.ts
declare var $: { x: any };
// @filename: a.ts
// @filename: /a.ts
/// <reference types="jquery" />
$.x;
@@ -1,6 +1,6 @@
/// <reference path="fourslash.ts"/>
// @typesRoot: src
// @typeRoots: src/types
// @Filename: src/types/lib/index.d.ts
/////*0*/declare let $: {x: number};
@@ -1,6 +1,6 @@
/// <reference path="fourslash.ts"/>
// @typesRoot: src
// @typeRoots: src/types
// @Filename: src/types/lib/index.d.ts
/////*0*/declare let $: {x: number};
@@ -1,6 +1,6 @@
/// <reference path="fourslash.ts"/>
/// <reference path="../fourslash.ts"/>
// @typesRoot: src
// @typeRoots: src/types
// @Filename: src/types/lib/index.d.ts
/////*0*/declare let $: {x: number};
+16 -13
View File
@@ -314,6 +314,7 @@ namespace ts {
getDefaultLibFileName: () => "lib.d.ts",
writeFile: (fileName, content): void => { throw new Error("NotImplemented"); },
getCurrentDirectory: () => currentDirectory,
getDirectories: () => [],
getCanonicalFileName: fileName => fileName.toLowerCase(),
getNewLine: () => "\r\n",
useCaseSensitiveFileNames: () => false,
@@ -397,6 +398,7 @@ export = C;
getDefaultLibFileName: () => "lib.d.ts",
writeFile: (fileName, content): void => { throw new Error("NotImplemented"); },
getCurrentDirectory: () => currentDirectory,
getDirectories: () => [],
getCanonicalFileName,
getNewLine: () => "\r\n",
useCaseSensitiveFileNames: () => useCaseSensitiveFileNames,
@@ -955,7 +957,7 @@ import b = require("./moduleB.ts");
describe("Type reference directive resolution: ", () => {
function test(typesRoot: string, typeDirective: string, primary: boolean, initialFile: File, targetFile: File, ...otherFiles: File[]) {
const host = createModuleResolutionHost(/*hasDirectoryExists*/ false, ...[initialFile, targetFile].concat(...otherFiles));
const result = resolveTypeReferenceDirective(typeDirective, initialFile.name, {typesRoot}, host);
const result = resolveTypeReferenceDirective(typeDirective, initialFile.name, {typeRoots: [typesRoot]}, host);
assert(result.resolvedTypeReferenceDirective.resolvedFileName !== undefined, "expected type directive to be resolved");
assert.equal(result.resolvedTypeReferenceDirective.resolvedFileName, targetFile.name, "unexpected result of type reference resolution");
assert.equal(result.resolvedTypeReferenceDirective.primary, primary, "unexpected 'primary' value");
@@ -965,64 +967,64 @@ import b = require("./moduleB.ts");
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/types/lib/index.d.ts" };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ true, f1, f2);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ true, f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/types/lib/typings/lib.d.ts" };
const package = { name: "/root/src/types/lib/package.json", content: JSON.stringify({types: "typings/lib.d.ts"}) };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ true, f1, f2, package);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ true, f1, f2, package);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/node_modules/lib/index.d.ts" };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ true, f1, f2);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/node_modules/lib/typings/lib.d.ts" };
const package = { name: "/root/src/node_modules/lib/package.json", content: JSON.stringify({types: "typings/lib.d.ts"}) };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ true, f1, f2, package);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/node_modules/@types/lib/index.d.ts" };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ true, f1, f2);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/src/node_modules/@types/lib/typings/lib.d.ts" };
const package = { name: "/root/src/node_modules/@types/lib/package.json", content: JSON.stringify({types: "typings/lib.d.ts"}) };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ true, f1, f2, package);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
}
});
it("Can be resolved from secondary location", () => {
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/node_modules/lib.d.ts" };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ false, f1, f2);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/node_modules/lib/index.d.ts" };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ false, f1, f2);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/node_modules/lib/typings/lib.d.ts" };
const package = { name: "/root/node_modules/lib/package.json", content: JSON.stringify({typings: "typings/lib.d.ts"}) };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/node_modules/@types/lib/index.d.ts" };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ false, f1, f2);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2);
}
{
const f1 = { name: "/root/src/app.ts" };
const f2 = { name: "/root/node_modules/@types/lib/typings/lib.d.ts" };
const package = { name: "/root/node_modules/@types/lib/package.json", content: JSON.stringify({typings: "typings/lib.d.ts"}) };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ false, f1, f2, package);
}
});
it("Primary resolution overrides secondary resolutions", () => {
@@ -1030,7 +1032,7 @@ import b = require("./moduleB.ts");
const f1 = { name: "/root/src/a/b/c/app.ts" };
const f2 = { name: "/root/src/types/lib/index.d.ts" };
const f3 = { name: "/root/src/a/b/node_modules/lib.d.ts" };
test(/*typesRoot*/"/root/src", /* typeDirective */"lib", /*primary*/ true, f1, f2, f3);
test(/*typesRoot*/"/root/src/types", /* typeDirective */"lib", /*primary*/ true, f1, f2, f3);
}
});
it("Reused program keeps errors", () => {
@@ -1050,6 +1052,7 @@ import b = require("./moduleB.ts");
throw new Error("NYI");
},
getCurrentDirectory: () => "/",
getDirectories: () => [],
getCanonicalFileName: f => f.toLowerCase(),
getNewLine: () => "\r\n",
useCaseSensitiveFileNames: () => false,
+20 -1
View File
@@ -117,6 +117,9 @@ namespace ts {
getCurrentDirectory(): string {
return "";
},
getDirectories(path: string): string[] {
return [];
},
getCanonicalFileName(fileName): string {
return sys && sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
},
@@ -256,6 +259,22 @@ namespace ts {
assert.isTrue(!program_1.structureIsReused);
});
it("fails if change affects type references", () => {
const program_1 = newProgram(files, ["a.ts"], { types: ["a"] });
updateProgram(program_1, ["a.ts"], { types: ["b"] }, files => {
});
assert.isTrue(!program_1.structureIsReused);
});
it("succeeds if change doesn't affect type references", () => {
const program_1 = newProgram(files, ["a.ts"], { types: ["a"] });
updateProgram(program_1, ["a.ts"], { types: ["a"] }, files => {
});
assert.isTrue(program_1.structureIsReused);
});
it("fails if change affects imports", () => {
const program_1 = newProgram(files, ["a.ts"], { target });
updateProgram(program_1, ["a.ts"], { target }, files => {
@@ -336,7 +355,7 @@ namespace ts {
{ name: "/a.ts", text: SourceText.New("/// <reference types='typedefs'/>", "", "var x = $") },
{ name: "/types/typedefs/index.d.ts", text: SourceText.New("", "", "declare var $: number") },
];
const options: CompilerOptions = { target, typesRoot: "/" };
const options: CompilerOptions = { target, typeRoots: ["/types"] };
const program_1 = newProgram(files, ["/a.ts"], options);
checkResolvedTypeDirectivesCache(program_1, "/a.ts", { "typedefs": { resolvedFileName: "/types/typedefs/index.d.ts", primary: true } });