Skip to content
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ import {
getMembersOfDeclaration,
getModifiers,
getModuleInstanceState,
getModuleSpecifierOfBareOrAccessedRequire,
getNameFromImportAttribute,
getNameFromIndexInfo,
getNameOfDeclaration,
Expand Down Expand Up @@ -4713,7 +4714,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
? location
: (isModuleDeclaration(location) ? location : location.parent && isModuleDeclaration(location.parent) && location.parent.name === location ? location.parent : undefined)?.name ||
(isLiteralImportTypeNode(location) ? location : undefined)?.argument.literal ||
(isVariableDeclaration(location) && location.initializer && isRequireCall(location.initializer, /*requireStringLiteralLikeArgument*/ true) ? location.initializer.arguments[0] : undefined) ||
isVariableDeclarationInitializedToBareOrAccessedRequire(location) && getModuleSpecifierOfBareOrAccessedRequire(location) ||
findAncestor(location, isImportCall)?.arguments[0] ||
findAncestor(location, or(isImportDeclaration, isJSDocImportTag, isExportDeclaration))?.moduleSpecifier ||
findAncestor(location, isExternalModuleImportEqualsDeclaration)?.moduleReference.expression;
Expand Down
11 changes: 7 additions & 4 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4436,10 +4436,7 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
function getIgnoreDeprecationsVersion(): Version {
const ignoreDeprecations = options.ignoreDeprecations;
if (ignoreDeprecations) {
// While we could do Version.tryParse here to support any version,
// for now, only allow "5.0". We aren't planning on deprecating anything
// until 6.0.
if (ignoreDeprecations === "5.0") {
if (ignoreDeprecations === "5.0" || ignoreDeprecations === "6.0") {
return new Version(ignoreDeprecations);
}
reportInvalidIgnoreDeprecations();
Expand Down Expand Up @@ -4527,6 +4524,12 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
createDeprecatedDiagnostic("preserveValueImports", /*value*/ undefined, "verbatimModuleSyntax");
}
});

checkDeprecations("6.0", "7.0", createDiagnostic, createDeprecatedDiagnostic => {
if (options.moduleResolution === ModuleResolutionKind.Node10) {
createDeprecatedDiagnostic("moduleResolution", "node10");
}
});
}

function verifyDeprecatedProjectReference(ref: ProjectReference, parentFile: JsonSourceFile | undefined, index: number) {
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7330,6 +7330,9 @@ export enum ModuleResolutionKind {
* Use the new name or consider switching to a modern module resolution target.
*/
NodeJs = 2,
/**
* @deprecated
*/
Node10 = 2,
// Starting with node12, node's module resolver has significant departures from traditional cjs resolution
// to better support ECMAScript modules and their use within node - however more features are still being added.
Expand Down
18 changes: 15 additions & 3 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3824,6 +3824,20 @@ export function isBindingElementOfBareOrAccessedRequire(node: Node): node is Bin
return isBindingElement(node) && isVariableDeclarationInitializedToBareOrAccessedRequire(node.parent.parent);
}

/** @internal */
export function getModuleSpecifierOfBareOrAccessedRequire(node: VariableDeclarationInitializedTo<RequireOrImportCall | AccessExpression>): StringLiteralLike | undefined {
if (isVariableDeclarationInitializedToRequire(node)) {
return node.initializer.arguments[0];
}
if (isVariableDeclarationInitializedToBareOrAccessedRequire(node)) {
const leftmost = getLeftmostAccessExpression(node.initializer);
if (isRequireCall(leftmost, /*requireStringLiteralLikeArgument*/ true)) {
return leftmost.arguments[0];
}
}
return undefined;
}

function isVariableDeclarationInitializedWithRequireHelper(node: Node, allowAccessedRequire: boolean) {
return isVariableDeclaration(node) &&
!!node.initializer &&
Expand Down Expand Up @@ -8992,9 +9006,6 @@ const _computedOptions = createComputedCompilerOptions({
let moduleResolution = compilerOptions.moduleResolution;
if (moduleResolution === undefined) {
switch (_computedOptions.module.computeValue(compilerOptions)) {
case ModuleKind.CommonJS:
moduleResolution = ModuleResolutionKind.Node10;
break;
case ModuleKind.Node16:
case ModuleKind.Node18:
case ModuleKind.Node20:
Expand All @@ -9003,6 +9014,7 @@ const _computedOptions = createComputedCompilerOptions({
case ModuleKind.NodeNext:
moduleResolution = ModuleResolutionKind.NodeNext;
break;
case ModuleKind.CommonJS:
case ModuleKind.Preserve:
moduleResolution = ModuleResolutionKind.Bundler;
break;
Expand Down
1 change: 1 addition & 0 deletions src/server/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3269,6 +3269,7 @@ export const enum ModuleResolutionKind {
Node = "node",
/** @deprecated Renamed to `Node10` */
NodeJs = "node",
/** @deprecated */
Node10 = "node10",
Node16 = "node16",
NodeNext = "nodenext",
Expand Down
8 changes: 4 additions & 4 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4185,10 +4185,6 @@ function getCompletionData(
return charactersFuzzyMatchInString(symbolName, lowerCaseTokenText);
},
(info, symbolName, isFromAmbientModule, exportMapKey) => {
if (detailsEntryId && !some(info, i => detailsEntryId.source === stripQuotes(i.moduleSymbol.name))) {
return;
}

// Do a relatively cheap check to bail early if all re-exports are non-importable
// due to file location or package.json dependency filtering. For non-node16+
// module resolution modes, getting past this point guarantees that we'll be
Expand Down Expand Up @@ -4217,6 +4213,10 @@ function getCompletionData(
({ exportInfo = info[0], moduleSpecifier } = result);
}

if (detailsEntryId && (detailsEntryId.source !== moduleSpecifier && !some(info, i => detailsEntryId.source === stripQuotes(i.moduleSymbol.name)))) {
return;
}

const isDefaultExport = exportInfo.exportKind === ExportKind.Default;
const symbol = isDefaultExport && getLocalSymbolForExportDefault(Debug.checkDefined(exportInfo.symbol)) || Debug.checkDefined(exportInfo.symbol);

Expand Down
11 changes: 7 additions & 4 deletions src/services/stringCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1067,8 +1067,9 @@ function getCompletionEntriesForNonRelativeModules(
if (tryFileExists(host, packageFile)) {
const packageJson = readJson(packageFile, host);
const fragmentSubpath = components.join("/") + (components.length && hasTrailingDirectorySeparator(fragment) ? "/" : "");
exportsOrImportsLookup((packageJson as MapLike<unknown>).exports, fragmentSubpath, packageDirectory, /*isExports*/ true, /*isImports*/ false);
return;
if (exportsOrImportsLookup((packageJson as MapLike<unknown>).exports, fragmentSubpath, packageDirectory, /*isExports*/ true, /*isImports*/ false)) {
return;
}
}
return nodeModulesDirectoryOrImportsLookup(ancestor);
};
Expand All @@ -1079,9 +1080,10 @@ function getCompletionEntriesForNonRelativeModules(

return arrayFrom(result.values());

function exportsOrImportsLookup(lookupTable: unknown, fragment: string, baseDirectory: string, isExports: boolean, isImports: boolean) {
/** Returns true if the search should stop */
function exportsOrImportsLookup(lookupTable: unknown, fragment: string, baseDirectory: string, isExports: boolean, isImports: boolean): boolean {
if (typeof lookupTable !== "object" || lookupTable === null) { // eslint-disable-line no-restricted-syntax
return; // null lookupTable or entrypoint only
return lookupTable !== undefined; // null lookupTable or entrypoint only
}
const keys = getOwnKeys(lookupTable as MapLike<unknown>);
const conditions = getConditions(compilerOptions, mode);
Expand All @@ -1105,6 +1107,7 @@ function getCompletionEntriesForNonRelativeModules(
},
comparePatternKeys,
);
return true;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tscWatch/watchEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ describe("unittests:: tscWatch:: watchEnvironment:: tsc-watch with different pol
sys: () => {
const configFile: File = {
path: `/user/username/projects/myproject/tsconfig.json`,
content: "{}",
content: `{ "compilerOptions": { "moduleResolution": "node10" } }`,
};
const file1: File = {
path: `/user/username/projects/myproject/src/file1.ts`,
Expand Down
5 changes: 5 additions & 0 deletions src/testRunner/unittests/tsserver/autoImportProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ describe("unittests:: tsserver:: autoImportProvider::", () => {

// Create auto import provider project, ensure still !session.getProjectService().pendingEnsureProjectForOpenFiles
host.writeFile(packageJson.path, packageJson.content);
// package.json was watched in --moduleResolution bundler
assert.isTrue(session.getProjectService().pendingEnsureProjectForOpenFiles);
host.runQueuedTimeoutCallbacks();
assert.isFalse(session.getProjectService().pendingEnsureProjectForOpenFiles);

session.host.baselineHost("Before getPackageJsonAutoImportProvider");
hostProject.getPackageJsonAutoImportProvider();
assert.isFalse(session.getProjectService().pendingEnsureProjectForOpenFiles);
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/completionsIncomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const indexFile: File = {

const tsconfigFile: File = {
path: "/home/src/project/project/tsconfig.json",
content: `{ "compilerOptions": { "module": "commonjs" } }`,
content: `{ "compilerOptions": { "module": "commonjs", "moduleResolution": "node10" } }`,
};

const packageJsonFile: File = {
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/getEditsForFileRename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("unittests:: tsserver:: getEditsForFileRename::", () => {
getDefaultLibFileName: options => ts.getDefaultLibFileName(options),
readFile: path => host.readFile(path),
fileExists: path => host.fileExists(path),
resolveModuleNames: (moduleNames, containingFile) => moduleNames.map(name => ts.resolveModuleName(name, containingFile, options, lsHost, moduleResolutionCache).resolvedModule),
resolveModuleNames: (moduleNames, containingFile) => moduleNames.map(name => ts.resolveModuleName(name, containingFile, options, lsHost, moduleResolutionCache, /*redirectedReference*/ undefined, ts.ModuleKind.CommonJS).resolvedModule),
getResolvedModuleWithFailedLookupLocationsFromCache: (moduleName, containingFile, mode) => moduleResolutionCache.getFromDirectoryCache(moduleName, mode, ts.getDirectoryPath(containingFile), /*redirectedReference*/ undefined),
};
const service = ts.createLanguageService(lsHost);
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 @@ -2472,7 +2472,7 @@ describe("unittests:: tsserver:: typingsInstaller:: recomputing resolutions of u
ts.server.updateProjectIfDirty(project);
const program = project.getLanguageService().getProgram()!;
const sourceFile = program.getSourceFileByPath(appPath)!;
const foooResolution = program.getResolvedModule(sourceFile, "fooo", /*mode*/ undefined)!.resolvedModule!;
const foooResolution = program.getResolvedModule(sourceFile, "fooo", ts.ModuleKind.CommonJS)!.resolvedModule!;
project.writeLog(`Resolution from : ${sourceFile.fileName} for "fooo" goes to: ${jsonToReadableText(foooResolution)}`);
return foooResolution;
}
Expand Down

This file was deleted.

4 changes: 4 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2530,6 +2530,7 @@ declare namespace ts {
Node = "node",
/** @deprecated Renamed to `Node10` */
NodeJs = "node",
/** @deprecated */
Node10 = "node10",
Node16 = "node16",
NodeNext = "nodenext",
Expand Down Expand Up @@ -6958,6 +6959,9 @@ declare namespace ts {
* Use the new name or consider switching to a modern module resolution target.
*/
NodeJs = 2,
/**
* @deprecated
*/
Node10 = 2,
Node16 = 3,
NodeNext = 99,
Expand Down
20 changes: 16 additions & 4 deletions tests/baselines/reference/cachedModuleResolution1.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
"File '/a/package.json' does not exist.",
"File '/package.json' does not exist.",
"======== Resolving module 'foo' from '/a/b/c/d/e/app.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Explicitly specified module resolution kind: 'Bundler'.",
"Resolving in CJS mode with conditions 'require', 'types'.",
"File '/a/b/c/d/e/package.json' does not exist.",
"File '/a/b/c/d/package.json' does not exist.",
"File '/a/b/c/package.json' does not exist.",
"File '/a/b/package.json' does not exist according to earlier cached lookups.",
"File '/a/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/a/b/c/d/e/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/d/node_modules' does not exist, skipping all lookups in it.",
Expand All @@ -16,8 +23,13 @@
"Resolving real path for '/a/b/node_modules/foo.d.ts', result '/a/b/node_modules/foo.d.ts'.",
"======== Module name 'foo' was successfully resolved to '/a/b/node_modules/foo.d.ts'. ========",
"======== Resolving module 'foo' from '/a/b/c/lib.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Explicitly specified module resolution kind: 'Bundler'.",
"Resolving in CJS mode with conditions 'require', 'types'.",
"File '/a/b/c/package.json' does not exist according to earlier cached lookups.",
"File '/a/b/package.json' does not exist according to earlier cached lookups.",
"File '/a/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Resolution for module 'foo' was found in cache from location '/a/b/c'.",
"======== Module name 'foo' was successfully resolved to '/a/b/node_modules/foo.d.ts'. ========",
Expand Down
20 changes: 16 additions & 4 deletions tests/baselines/reference/cachedModuleResolution2.trace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
"File '/a/package.json' does not exist.",
"File '/package.json' does not exist.",
"======== Resolving module 'foo' from '/a/b/c/lib.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Explicitly specified module resolution kind: 'Bundler'.",
"Resolving in CJS mode with conditions 'require', 'types'.",
"File '/a/b/c/package.json' does not exist.",
"File '/a/b/package.json' does not exist according to earlier cached lookups.",
"File '/a/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/a/b/c/node_modules' does not exist, skipping all lookups in it.",
"File '/a/b/node_modules/foo.ts' does not exist.",
Expand All @@ -14,8 +19,15 @@
"Resolving real path for '/a/b/node_modules/foo.d.ts', result '/a/b/node_modules/foo.d.ts'.",
"======== Module name 'foo' was successfully resolved to '/a/b/node_modules/foo.d.ts'. ========",
"======== Resolving module 'foo' from '/a/b/c/d/e/app.ts'. ========",
"Explicitly specified module resolution kind: 'Node10'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Explicitly specified module resolution kind: 'Bundler'.",
"Resolving in CJS mode with conditions 'require', 'types'.",
"File '/a/b/c/d/e/package.json' does not exist.",
"File '/a/b/c/d/package.json' does not exist.",
"File '/a/b/c/package.json' does not exist according to earlier cached lookups.",
"File '/a/b/package.json' does not exist according to earlier cached lookups.",
"File '/a/package.json' does not exist according to earlier cached lookups.",
"File '/package.json' does not exist according to earlier cached lookups.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"Directory '/a/b/c/d/e/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/d/node_modules' does not exist, skipping all lookups in it.",
Expand Down
Loading
Loading