Skip to content

Add undefined guard #24212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -1325,4 +1327,4 @@ namespace ts {
}
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions tests/cases/compiler/declarationFilesGeneratingTypeReferences.ts
Original file line number Diff line number Diff line change
@@ -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;
}