diff --git a/src/compiler/program.ts b/src/compiler/program.ts index afd2ddad9e157..f099d8581191a 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -720,8 +720,9 @@ namespace ts { const typesFile = tryReadTypesSection(packageJsonPath, candidate, state); if (typesFile) { const onlyRecordFailures = !directoryProbablyExists(getDirectoryPath(typesFile), state.host); - // The package.json "typings" property must specify the file with extension, so just try that exact filename. - const result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures, state); + // A package.json "typings" may specify an exact filename, or may choose to omit an extension. + const result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures, state) || + tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures, state); if (result) { return result; } diff --git a/tests/baselines/reference/typingsLookup4.js b/tests/baselines/reference/typingsLookup4.js new file mode 100644 index 0000000000000..c11bc13c6138b --- /dev/null +++ b/tests/baselines/reference/typingsLookup4.js @@ -0,0 +1,36 @@ +//// [tests/cases/conformance/typings/typingsLookup4.ts] //// + +//// [package.json] +{ "typings": "jquery.d.ts" } + +//// [jquery.d.ts] +export const j: number; + +//// [package.json] +{ "typings": "kquery" } + +//// [kquery.d.ts] +export const k: number; + +//// [package.json] +{ "typings": "lquery" } + +//// [lquery.ts] +export const l = 2; + +//// [a.ts] +import { j } from "jquery"; +import { k } from "kquery"; +import { l } from "lquery"; +j + k + l; + + +//// [lquery.js] +"use strict"; +exports.l = 2; +//// [a.js] +"use strict"; +var jquery_1 = require("jquery"); +var kquery_1 = require("kquery"); +var lquery_1 = require("lquery"); +jquery_1.j + kquery_1.k + lquery_1.l; diff --git a/tests/baselines/reference/typingsLookup4.symbols b/tests/baselines/reference/typingsLookup4.symbols new file mode 100644 index 0000000000000..144548c645265 --- /dev/null +++ b/tests/baselines/reference/typingsLookup4.symbols @@ -0,0 +1,27 @@ +=== /a.ts === +import { j } from "jquery"; +>j : Symbol(j, Decl(a.ts, 0, 8)) + +import { k } from "kquery"; +>k : Symbol(k, Decl(a.ts, 1, 8)) + +import { l } from "lquery"; +>l : Symbol(l, Decl(a.ts, 2, 8)) + +j + k + l; +>j : Symbol(j, Decl(a.ts, 0, 8)) +>k : Symbol(k, Decl(a.ts, 1, 8)) +>l : Symbol(l, Decl(a.ts, 2, 8)) + +=== /node_modules/@types/jquery/jquery.d.ts === +export const j: number; +>j : Symbol(j, Decl(jquery.d.ts, 0, 12)) + +=== /node_modules/@types/kquery/kquery.d.ts === +export const k: number; +>k : Symbol(k, Decl(kquery.d.ts, 0, 12)) + +=== /node_modules/@types/lquery/lquery.ts === +export const l = 2; +>l : Symbol(l, Decl(lquery.ts, 0, 12)) + diff --git a/tests/baselines/reference/typingsLookup4.trace.json b/tests/baselines/reference/typingsLookup4.trace.json new file mode 100644 index 0000000000000..4bca8456f5846 --- /dev/null +++ b/tests/baselines/reference/typingsLookup4.trace.json @@ -0,0 +1,93 @@ +[ + "======== Resolving module 'jquery' from '/a.ts'. ========", + "Module resolution kind is not specified, using 'NodeJs'.", + "Loading module 'jquery' from 'node_modules' folder.", + "File '/node_modules/jquery.ts' does not exist.", + "File '/node_modules/jquery.tsx' does not exist.", + "File '/node_modules/jquery.d.ts' does not exist.", + "File '/node_modules/jquery/package.json' does not exist.", + "File '/node_modules/jquery/index.ts' does not exist.", + "File '/node_modules/jquery/index.tsx' does not exist.", + "File '/node_modules/jquery/index.d.ts' does not exist.", + "File '/node_modules/@types/jquery.ts' does not exist.", + "File '/node_modules/@types/jquery.tsx' does not exist.", + "File '/node_modules/@types/jquery.d.ts' does not exist.", + "Found 'package.json' at '/node_modules/@types/jquery/package.json'.", + "'package.json' has 'typings' field 'jquery.d.ts' that references '/node_modules/@types/jquery/jquery.d.ts'.", + "File '/node_modules/@types/jquery/jquery.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/jquery/jquery.d.ts', result '/node_modules/@types/jquery/jquery.d.ts'", + "======== Module name 'jquery' was successfully resolved to '/node_modules/@types/jquery/jquery.d.ts'. ========", + "======== Resolving module 'kquery' from '/a.ts'. ========", + "Module resolution kind is not specified, using 'NodeJs'.", + "Loading module 'kquery' from 'node_modules' folder.", + "File '/node_modules/kquery.ts' does not exist.", + "File '/node_modules/kquery.tsx' does not exist.", + "File '/node_modules/kquery.d.ts' does not exist.", + "File '/node_modules/kquery/package.json' does not exist.", + "File '/node_modules/kquery/index.ts' does not exist.", + "File '/node_modules/kquery/index.tsx' does not exist.", + "File '/node_modules/kquery/index.d.ts' does not exist.", + "File '/node_modules/@types/kquery.ts' does not exist.", + "File '/node_modules/@types/kquery.tsx' does not exist.", + "File '/node_modules/@types/kquery.d.ts' does not exist.", + "Found 'package.json' at '/node_modules/@types/kquery/package.json'.", + "'package.json' has 'typings' field 'kquery' that references '/node_modules/@types/kquery/kquery'.", + "File '/node_modules/@types/kquery/kquery' does not exist.", + "File '/node_modules/@types/kquery/kquery.ts' does not exist.", + "File '/node_modules/@types/kquery/kquery.tsx' does not exist.", + "File '/node_modules/@types/kquery/kquery.d.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/kquery/kquery.d.ts', result '/node_modules/@types/kquery/kquery.d.ts'", + "======== Module name 'kquery' was successfully resolved to '/node_modules/@types/kquery/kquery.d.ts'. ========", + "======== Resolving module 'lquery' from '/a.ts'. ========", + "Module resolution kind is not specified, using 'NodeJs'.", + "Loading module 'lquery' from 'node_modules' folder.", + "File '/node_modules/lquery.ts' does not exist.", + "File '/node_modules/lquery.tsx' does not exist.", + "File '/node_modules/lquery.d.ts' does not exist.", + "File '/node_modules/lquery/package.json' does not exist.", + "File '/node_modules/lquery/index.ts' does not exist.", + "File '/node_modules/lquery/index.tsx' does not exist.", + "File '/node_modules/lquery/index.d.ts' does not exist.", + "File '/node_modules/@types/lquery.ts' does not exist.", + "File '/node_modules/@types/lquery.tsx' does not exist.", + "File '/node_modules/@types/lquery.d.ts' does not exist.", + "Found 'package.json' at '/node_modules/@types/lquery/package.json'.", + "'package.json' has 'typings' field 'lquery' that references '/node_modules/@types/lquery/lquery'.", + "File '/node_modules/@types/lquery/lquery' does not exist.", + "File '/node_modules/@types/lquery/lquery.ts' exist - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/lquery/lquery.ts', result '/node_modules/@types/lquery/lquery.ts'", + "======== Module name 'lquery' was successfully resolved to '/node_modules/@types/lquery/lquery.ts'. ========", + "======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/node_modules/@types'. ========", + "Resolving with primary search path '/node_modules/@types'", + "Found 'package.json' at '/node_modules/@types/jquery/package.json'.", + "'package.json' has 'typings' field 'jquery.d.ts' that references '/node_modules/@types/jquery/jquery.d.ts'.", + "File '/node_modules/@types/jquery/jquery.d.ts' exist - use it as a name resolution result.", + "======== Type reference directive 'jquery' was successfully resolved to '/node_modules/@types/jquery/jquery.d.ts', primary: true. ========", + "======== Resolving type reference directive 'kquery', containing file '/__inferred type names__.ts', root directory '/node_modules/@types'. ========", + "Resolving with primary search path '/node_modules/@types'", + "Found 'package.json' at '/node_modules/@types/kquery/package.json'.", + "'package.json' has 'typings' field 'kquery' that references '/node_modules/@types/kquery/kquery'.", + "File '/node_modules/@types/kquery/kquery' does not exist.", + "File '/node_modules/@types/kquery/kquery.d.ts' exist - use it as a name resolution result.", + "======== Type reference directive 'kquery' was successfully resolved to '/node_modules/@types/kquery/kquery.d.ts', primary: true. ========", + "======== Resolving type reference directive 'lquery', containing file '/__inferred type names__.ts', root directory '/node_modules/@types'. ========", + "Resolving with primary search path '/node_modules/@types'", + "Found 'package.json' at '/node_modules/@types/lquery/package.json'.", + "'package.json' has 'typings' field 'lquery' that references '/node_modules/@types/lquery/lquery'.", + "File '/node_modules/@types/lquery/lquery' does not exist.", + "File '/node_modules/@types/lquery/lquery.d.ts' does not exist.", + "File '/node_modules/@types/lquery/index.d.ts' does not exist.", + "Looking up in 'node_modules' folder, initial location '/'", + "File '/node_modules/lquery.ts' does not exist.", + "File '/node_modules/lquery.d.ts' does not exist.", + "File '/node_modules/lquery/package.json' does not exist.", + "File '/node_modules/lquery/index.ts' does not exist.", + "File '/node_modules/lquery/index.d.ts' does not exist.", + "File '/node_modules/@types/lquery.ts' does not exist.", + "File '/node_modules/@types/lquery.d.ts' does not exist.", + "Found 'package.json' at '/node_modules/@types/lquery/package.json'.", + "'package.json' has 'typings' field 'lquery' that references '/node_modules/@types/lquery/lquery'.", + "File '/node_modules/@types/lquery/lquery' does not exist.", + "File '/node_modules/@types/lquery/lquery.ts' exist - use it as a name resolution result.", + "======== Type reference directive 'lquery' was successfully resolved to '/node_modules/@types/lquery/lquery.ts', primary: false. ========" +] \ No newline at end of file diff --git a/tests/baselines/reference/typingsLookup4.types b/tests/baselines/reference/typingsLookup4.types new file mode 100644 index 0000000000000..d922c7b1dfa53 --- /dev/null +++ b/tests/baselines/reference/typingsLookup4.types @@ -0,0 +1,30 @@ +=== /a.ts === +import { j } from "jquery"; +>j : number + +import { k } from "kquery"; +>k : number + +import { l } from "lquery"; +>l : number + +j + k + l; +>j + k + l : number +>j + k : number +>j : number +>k : number +>l : number + +=== /node_modules/@types/jquery/jquery.d.ts === +export const j: number; +>j : number + +=== /node_modules/@types/kquery/kquery.d.ts === +export const k: number; +>k : number + +=== /node_modules/@types/lquery/lquery.ts === +export const l = 2; +>l : number +>2 : number + diff --git a/tests/cases/conformance/typings/typingsLookup4.ts b/tests/cases/conformance/typings/typingsLookup4.ts new file mode 100644 index 0000000000000..234090aebad52 --- /dev/null +++ b/tests/cases/conformance/typings/typingsLookup4.ts @@ -0,0 +1,31 @@ +// @traceResolution: true +// @noImplicitReferences: true +// @currentDirectory: / +// A file extension is optional in typings entries. + +// @filename: /tsconfig.json +{} + +// @filename: /node_modules/@types/jquery/package.json +{ "typings": "jquery.d.ts" } + +// @filename: /node_modules/@types/jquery/jquery.d.ts +export const j: number; + +// @filename: /node_modules/@types/kquery/package.json +{ "typings": "kquery" } + +// @filename: /node_modules/@types/kquery/kquery.d.ts +export const k: number; + +// @filename: /node_modules/@types/lquery/package.json +{ "typings": "lquery" } + +// @filename: /node_modules/@types/lquery/lquery.ts +export const l = 2; + +// @filename: /a.ts +import { j } from "jquery"; +import { k } from "kquery"; +import { l } from "lquery"; +j + k + l;