Skip to content
Open
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
24 changes: 15 additions & 9 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import {
emptyArray,
endsWith,
ensureTrailingDirectorySeparator,
equateValues,
every,
Extension,
extensionIsTS,
fileExtensionIs,
fileExtensionIsOneOf,
filter,
firstDefined,
flatten,
forEach,
forEachAncestorDirectory,
formatMessage,
Expand Down Expand Up @@ -808,13 +810,17 @@ export function resolvePackageNameToPackageJson(
* this list is only the set of defaults that are implicitly included.
*/
export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] {
// Use explicit type list from tsconfig.json
if (options.types) {
// Default to [] if nothing specified
if (options.types === undefined) {
return emptyArray;
}

if (!options.types.includes("*")) {
// No wildcard, no need to iterate anything
return options.types;
}

// Walk the primary type lookup locations
const result: string[] = [];
const wildcardMatches: string[] = [];
if (host.directoryExists && host.getDirectories) {
const typeRoots = getEffectiveTypeRoots(options, host);
if (typeRoots) {
Expand All @@ -829,19 +835,19 @@ export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: M
const isNotNeededPackage = host.fileExists(packageJsonPath) && (readJson(packageJsonPath, host) as PackageJson).typings === null;
if (!isNotNeededPackage) {
const baseFileName = getBaseFileName(normalized);

// At this stage, skip results with leading dot.
if (baseFileName.charCodeAt(0) !== CharacterCodes.dot) {
// Return just the type directive names
result.push(baseFileName);
wildcardMatches.push(baseFileName);
}
}
}
}
}
}
}
return result;

// Order potentially matters in program construction, so substitute
// in the wildcard in the position it was specified in the types array
return deduplicate(flatten(options.types.map(t => t === "*" ? wildcardMatches : t)), equateValues);
}

export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, NonRelativeNameResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, PackageJsonInfoCache {
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/tsbuild/moduleResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ describe("unittests:: tsbuild:: moduleResolution:: handles the modules and optio
TestServerHost.createWatchedSystem({
"/home/src/workspaces/project/packages/pkg1_index.ts": `export const theNum: TheNum = "type1";`,
"/home/src/workspaces/project/packages/pkg1.tsconfig.json": jsonToReadableText({
compilerOptions: { composite: true, typeRoots: ["./typeroot1"] },
compilerOptions: { composite: true, typeRoots: ["./typeroot1"], types: ["sometype"] },
files: ["./pkg1_index.ts"],
}),
"/home/src/workspaces/project/packages/typeroot1/sometype/index.d.ts": dedent`declare type TheNum = "type1";`,
"/home/src/workspaces/project/packages/pkg2_index.ts": `export const theNum: TheNum2 = "type2";`,
"/home/src/workspaces/project/packages/pkg2.tsconfig.json": jsonToReadableText({
compilerOptions: { composite: true, typeRoots: ["./typeroot2"] },
compilerOptions: { composite: true, typeRoots: ["./typeroot2"], types: ["sometype"] },
files: ["./pkg2_index.ts"],
}),
"/home/src/workspaces/project/packages/typeroot2/sometype/index.d.ts": dedent`declare type TheNum2 = "type2";`,
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/tsc/incremental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ declare global {
"/home/src/workspaces/project/node_modules/react/jsx-runtime.js": "export {}", // js needs to be present so there's a resolution result
"/home/src/workspaces/project/node_modules/@types/react/index.d.ts": getJsxLibraryContent(), // doesn't contain a jsx-runtime definition
"/home/src/workspaces/project/src/index.tsx": `export const App = () => <div propA={true}></div>;`,
"/home/src/workspaces/project/tsconfig.json": jsonToReadableText({ compilerOptions: { module: "commonjs", jsx: "react-jsx", incremental: true, jsxImportSource: "react" } }),
"/home/src/workspaces/project/tsconfig.json": jsonToReadableText({ compilerOptions: { module: "commonjs", jsx: "react-jsx", incremental: true, jsxImportSource: "react", types: ["react"] } }),
}),
commandLineArgs: ts.emptyArray,
});
Expand All @@ -201,7 +201,7 @@ declare global {
"/home/src/workspaces/project/node_modules/react/jsx-runtime.js": "export {}", // js needs to be present so there's a resolution result
"/home/src/workspaces/project/node_modules/@types/react/index.d.ts": getJsxLibraryContent(), // doesn't contain a jsx-runtime definition
"/home/src/workspaces/project/src/index.tsx": `export const App = () => <div propA={true}></div>;`,
"/home/src/workspaces/project/tsconfig.json": jsonToReadableText({ compilerOptions: { module: "commonjs", jsx: "react-jsx", incremental: true, jsxImportSource: "react" } }),
"/home/src/workspaces/project/tsconfig.json": jsonToReadableText({ compilerOptions: { module: "commonjs", jsx: "react-jsx", incremental: true, jsxImportSource: "react", types: ["react"] } }),
}),
commandLineArgs: ["--strict"],
});
Expand Down
5 changes: 4 additions & 1 deletion src/testRunner/unittests/tscWatch/resolutionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ describe("unittests:: tscWatch:: resolutionCache:: tsc-watch module resolution c
TestServerHost.createWatchedSystem([{
path: "/users/username/projects/project/foo.ts",
content: `import * as fs from "fs";`,
}, {
path: "/users/username/projects/project/tsconfig.json",
content: jsonToReadableText({ compilerOptions: { types: ["node"] } }),
}], { currentDirectory: "/users/username/projects/project" }),
edits: [
{
Expand Down Expand Up @@ -565,7 +568,7 @@ declare namespace NodeJS {
};
const tsconfig: File = {
path: `/user/username/projects/myproject/tsconfig.json`,
content: "{}",
content: jsonToReadableText({ compilerOptions: { types: ["node"] } }),
};
const { nodeAtTypesIndex, nodeAtTypesBase, nodeAtTypes36Base, nodeAtTypesGlobals } = getNodeAtTypes();
return TestServerHost.createWatchedSystem(
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/configuredProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ describe("unittests:: tsserver:: configuredProjects:: non-existing directories l
const config = {
path: "/user/username/projects/project/a/tsconfig.json",
content: jsonToReadableText({
compiler: {},
compilerOptions: { types: ["typings"] },
files: [],
}),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { value1 } from "../node_modules/.cache/someFile.d.ts";`,
};
const tsconfig: File = {
path: "/home/src/projects/project/tsconfig.json",
content: "{}",
content: jsonToReadableText({ compilerOptions: { types: ["node"] } }),
};
const host = TestServerHost.createServerHost([file1, file2, file3, file3, file4, nodeModulesFile1, nodeModulesFile2, tsconfig]);
const session = new TestSession(host);
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/resolutionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("unittests:: tsserver:: resolutionCache:: tsserverProjectSystem watchin
const tsconfig = {
path: "/users/username/projects/project/tsconfig.json",
content: jsonToReadableText({
compilerOptions: {},
compilerOptions: { types: ["lib1", "lib2"] },
exclude: ["node_modules"],
}),
};
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/typingsInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe("unittests:: tsserver:: typingsInstaller:: General functionality", () =
});
openExternalProjectForSession({
projectFileName,
options: {},
options: { types: ["*"] },
rootFiles: [toExternalFile(appJs.path)],
typeAcquisition: { enable: true, include: ["node"] },
}, session);
Expand Down
19 changes: 19 additions & 0 deletions tests/baselines/reference/jsDeclarationsTypeReferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ module.exports = {
export const thing: Something;
import Something_1 = require("fs");
import Something = Something_1.Something;


//// [DtsFileErrors]


tests/cases/conformance/jsdoc/declarations/out/index.d.ts(2,30): error TS2307: Cannot find module 'fs' or its corresponding type declarations.


==== tests/cases/conformance/jsdoc/declarations/out/index.d.ts (1 errors) ====
export const thing: Something;
import Something_1 = require("fs");
~~~~
!!! error TS2307: Cannot find module 'fs' or its corresponding type declarations.
import Something = Something_1.Something;
Comment on lines +34 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something isn't right here; probably need to update the test as it explicitly makes a @types/node dir that should be loaded.


==== node_modules/@types/node/index.d.ts (0 errors) ====
declare module "fs" {
export class Something {}
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/jsDeclarationsTypeReferences3.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,26 @@ export namespace A {
}
import Something_1 = require("fs");
import Something = Something_1.Something;


//// [DtsFileErrors]


tests/cases/conformance/jsdoc/declarations/out/index.d.ts(6,30): error TS2307: Cannot find module 'fs' or its corresponding type declarations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here.



==== tests/cases/conformance/jsdoc/declarations/out/index.d.ts (1 errors) ====
export namespace A {
namespace B {
let thing: Something;
}
}
import Something_1 = require("fs");
~~~~
!!! error TS2307: Cannot find module 'fs' or its corresponding type declarations.
import Something = Something_1.Something;

==== node_modules/@types/node/index.d.ts (0 errors) ====
declare module "fs" {
export class Something {}
}
7 changes: 0 additions & 7 deletions tests/baselines/reference/library-reference-1.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,5 @@
"File '/src/types/jquery/package.json' does not exist.",
"File '/src/types/jquery/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/src/types/jquery/index.d.ts', result '/src/types/jquery/index.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/src/types/jquery/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'jquery', containing file '/.src/__inferred type names__.ts', root directory '/src/types'. ========",
"Resolving with primary search path '/src/types'.",
"File '/src/types/jquery.d.ts' does not exist.",
"File '/src/types/jquery/package.json' does not exist according to earlier cached lookups.",
"File '/src/types/jquery/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/src/types/jquery/index.d.ts', result '/src/types/jquery/index.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/src/types/jquery/index.d.ts', primary: true. ========"
]
8 changes: 0 additions & 8 deletions tests/baselines/reference/library-reference-10.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,5 @@
"'package.json' has 'typings' field 'jquery.d.ts' that references '/foo/types/jquery/jquery.d.ts'.",
"File '/foo/types/jquery/jquery.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/foo/types/jquery/jquery.d.ts', result '/foo/types/jquery/jquery.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/foo/types/jquery/jquery.d.ts', primary: true. ========",
"======== Resolving type reference directive 'jquery', containing file '/.src/__inferred type names__.ts', root directory '/foo/types'. ========",
"Resolving with primary search path '/foo/types'.",
"File '/foo/types/jquery.d.ts' does not exist.",
"File '/foo/types/jquery/package.json' exists according to earlier cached lookups.",
"'package.json' has 'typings' field 'jquery.d.ts' that references '/foo/types/jquery/jquery.d.ts'.",
"File '/foo/types/jquery/jquery.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/foo/types/jquery/jquery.d.ts', result '/foo/types/jquery/jquery.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/foo/types/jquery/jquery.d.ts', primary: true. ========"
]
9 changes: 0 additions & 9 deletions tests/baselines/reference/library-reference-2.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,5 @@
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
"File '/types/jquery/jquery.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/types/jquery/jquery.d.ts', result '/types/jquery/jquery.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========",
"======== Resolving type reference directive 'jquery', containing file '/test/__inferred type names__.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/jquery.d.ts' does not exist.",
"File '/types/jquery/package.json' exists according to earlier cached lookups.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
"File '/types/jquery/jquery.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/types/jquery/jquery.d.ts', result '/types/jquery/jquery.d.ts'.",
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========"
]
5 changes: 1 addition & 4 deletions tests/baselines/reference/library-reference-6.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
"File '/node_modules/@types/alpha/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/package.json' does not exist.",
"File '/node_modules/package.json' does not exist.",
"File '/package.json' does not exist.",
"======== Resolving type reference directive 'alpha', containing file '/__inferred type names__.ts'. ========",
"Resolution for type reference directive 'alpha' was found in cache from location '/'.",
"======== Type reference directive 'alpha' was successfully resolved to '/node_modules/@types/alpha/index.d.ts', primary: true. ========"
"File '/package.json' does not exist."
]
8 changes: 1 addition & 7 deletions tests/baselines/reference/library-reference-8.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,5 @@
"File '/test/types/alpha/package.json' does not exist according to earlier cached lookups.",
"File '/test/types/alpha/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/test/types/alpha/index.d.ts', result '/test/types/alpha/index.d.ts'.",
"======== Type reference directive 'alpha' was successfully resolved to '/test/types/alpha/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'alpha', containing file '/test/__inferred type names__.ts'. ========",
"Resolution for type reference directive 'alpha' was found in cache from location '/test'.",
"======== Type reference directive 'alpha' was successfully resolved to '/test/types/alpha/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'beta', containing file '/test/__inferred type names__.ts'. ========",
"Resolution for type reference directive 'beta' was found in cache from location '/test'.",
"======== Type reference directive 'beta' was successfully resolved to '/test/types/beta/index.d.ts', primary: true. ========"
"======== Type reference directive 'alpha' was successfully resolved to '/test/types/alpha/index.d.ts', primary: true. ========"
]
15 changes: 0 additions & 15 deletions tests/baselines/reference/modulePreserve3.errors.txt

This file was deleted.

15 changes: 1 addition & 14 deletions tests/baselines/reference/modulePreserve3.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,5 @@
"File '/node_modules/@types/react/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/react/jsx-runtime.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/node_modules/@types/react/jsx-runtime.d.ts', result '/node_modules/@types/react/jsx-runtime.d.ts'.",
"======== Module name 'react/jsx-runtime' was successfully resolved to '/node_modules/@types/react/jsx-runtime.d.ts'. ========",
"======== Resolving type reference directive 'react', containing file '/.src/__inferred type names__.ts', root directory '/.src/node_modules/@types,/node_modules/@types'. ========",
"Resolving with primary search path '/.src/node_modules/@types, /node_modules/@types'.",
"Directory '/.src/node_modules/@types' does not exist, skipping all lookups in it.",
"File '/node_modules/@types/react/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/react/index.d.ts' does not exist.",
"Looking up in 'node_modules' folder, initial location '/.src'.",
"Searching all ancestor node_modules directories for preferred extensions: Declaration.",
"Directory '/.src/node_modules' does not exist, skipping all lookups in it.",
"File '/node_modules/react.d.ts' does not exist.",
"File '/node_modules/@types/react/package.json' does not exist according to earlier cached lookups.",
"File '/node_modules/@types/react.d.ts' does not exist.",
"File '/node_modules/@types/react/index.d.ts' does not exist.",
"======== Type reference directive 'react' was not resolved. ========"
"======== Module name 'react/jsx-runtime' was successfully resolved to '/node_modules/@types/react/jsx-runtime.d.ts'. ========"
]
Loading