Add undefined guard

This commit is contained in:
Mohamed Hegazy 2018-05-17 11:45:34 -07:00
parent 6af5d8b940
commit 8dc9b76b6d
5 changed files with 99 additions and 8 deletions

View File

@ -217,16 +217,18 @@ namespace ts {
function getFileReferenceForTypeName(typeName: string): FileReference | undefined {
// Elide type references for which we have imports
for (const importStatement of emittedImports) {
if (isImportEqualsDeclaration(importStatement) && isExternalModuleReference(importStatement.moduleReference)) {
const expr = importStatement.moduleReference.expression;
if (isStringLiteralLike(expr) && expr.text === typeName) {
if (emittedImports) {
for (const importStatement of emittedImports) {
if (isImportEqualsDeclaration(importStatement) && isExternalModuleReference(importStatement.moduleReference)) {
const expr = importStatement.moduleReference.expression;
if (isStringLiteralLike(expr) && expr.text === typeName) {
return undefined;
}
}
else if (isImportDeclaration(importStatement) && isStringLiteral(importStatement.moduleSpecifier) && importStatement.moduleSpecifier.text === typeName) {
return undefined;
}
}
else if (isImportDeclaration(importStatement) && isStringLiteral(importStatement.moduleSpecifier) && importStatement.moduleSpecifier.text === typeName) {
return undefined;
}
}
return { fileName: typeName, pos: -1, end: -1 };
}
@ -1325,4 +1327,4 @@ namespace ts {
}
return false;
}
}
}

View File

@ -0,0 +1,46 @@
//// [tests/cases/compiler/declarationFilesGeneratingTypeReferences.ts] ////
//// [index.d.ts]
interface JQuery {
}
//// [app.ts]
/// <reference types="jquery"/>
namespace Test {
export var x: JQuery;
}
//// [out.js]
/// <reference types="jquery"/>
var Test;
(function (Test) {
})(Test || (Test = {}));
//// [out.d.ts]
/// <reference types="jquery" />
declare namespace Test {
var x: JQuery;
}
//// [DtsFileErrors]
out.d.ts(1,23): error TS2688: Cannot find type definition file for 'jquery'.
==== /a/node_modules/@types/jquery/index.d.ts (0 errors) ====
interface JQuery {
}
==== out.d.ts (1 errors) ====
/// <reference types="jquery" />
~~~~~~
!!! error TS2688: Cannot find type definition file for 'jquery'.
declare namespace Test {
var x: JQuery;
}

View File

@ -0,0 +1,15 @@
=== /a/node_modules/@types/jquery/index.d.ts ===
interface JQuery {
>JQuery : Symbol(JQuery, Decl(index.d.ts, 0, 0))
}
=== /a/app.ts ===
/// <reference types="jquery"/>
namespace Test {
>Test : Symbol(Test, Decl(app.ts, 0, 0))
export var x: JQuery;
>x : Symbol(x, Decl(app.ts, 2, 14))
>JQuery : Symbol(JQuery, Decl(index.d.ts, 0, 0))
}

View File

@ -0,0 +1,15 @@
=== /a/node_modules/@types/jquery/index.d.ts ===
interface JQuery {
>JQuery : JQuery
}
=== /a/app.ts ===
/// <reference types="jquery"/>
namespace Test {
>Test : typeof Test
export var x: JQuery;
>x : JQuery
>JQuery : JQuery
}

View File

@ -0,0 +1,13 @@
// @declaration: true
// @outFile: out.js
// @filename: /a/node_modules/@types/jquery/index.d.ts
interface JQuery {
}
// @filename: /a/app.ts
/// <reference types="jquery"/>
namespace Test {
export var x: JQuery;
}