Skip to content

Commit 0933ab5

Browse files
🤖 Pick PRs #62320 and #62338 into tsgo-port (#62637)
Co-authored-by: Andrew Branch <[email protected]> Co-authored-by: Andrew Branch <[email protected]>
1 parent 1ee9e0d commit 0933ab5

File tree

768 files changed

+13532
-6458
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

768 files changed

+13532
-6458
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ import {
339339
getMembersOfDeclaration,
340340
getModifiers,
341341
getModuleInstanceState,
342+
getModuleSpecifierOfBareOrAccessedRequire,
342343
getNameFromImportAttribute,
343344
getNameFromIndexInfo,
344345
getNameOfDeclaration,
@@ -4717,7 +4718,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
47174718
? location
47184719
: (isModuleDeclaration(location) ? location : location.parent && isModuleDeclaration(location.parent) && location.parent.name === location ? location.parent : undefined)?.name ||
47194720
(isLiteralImportTypeNode(location) ? location : undefined)?.argument.literal ||
4720-
(isVariableDeclaration(location) && location.initializer && isRequireCall(location.initializer, /*requireStringLiteralLikeArgument*/ true) ? location.initializer.arguments[0] : undefined) ||
4721+
isVariableDeclarationInitializedToBareOrAccessedRequire(location) && getModuleSpecifierOfBareOrAccessedRequire(location) ||
47214722
findAncestor(location, isImportCall)?.arguments[0] ||
47224723
findAncestor(location, or(isImportDeclaration, isJSDocImportTag, isExportDeclaration))?.moduleSpecifier ||
47234724
findAncestor(location, isExternalModuleImportEqualsDeclaration)?.moduleReference.expression;

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4661,7 +4661,7 @@
46614661
"category": "Error",
46624662
"code": 5094
46634663
},
4664-
"Option '{0}' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.": {
4664+
"Option '{0}' can only be used when 'module' is set to 'preserve', 'commonjs', or 'es2015' or later.": {
46654665
"category": "Error",
46664666
"code": 5095
46674667
},

src/compiler/program.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4370,8 +4370,8 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
43704370
createDiagnosticForOptionName(Diagnostics.Option_0_can_only_be_used_when_moduleResolution_is_set_to_node16_nodenext_or_bundler, "customConditions");
43714371
}
43724372

4373-
if (moduleResolution === ModuleResolutionKind.Bundler && !emitModuleKindIsNonNodeESM(moduleKind) && moduleKind !== ModuleKind.Preserve) {
4374-
createOptionValueDiagnostic("moduleResolution", Diagnostics.Option_0_can_only_be_used_when_module_is_set_to_preserve_or_to_es2015_or_later, "bundler");
4373+
if (moduleResolution === ModuleResolutionKind.Bundler && !emitModuleKindIsNonNodeESM(moduleKind) && moduleKind !== ModuleKind.Preserve && moduleKind !== ModuleKind.CommonJS) {
4374+
createOptionValueDiagnostic("moduleResolution", Diagnostics.Option_0_can_only_be_used_when_module_is_set_to_preserve_commonjs_or_es2015_or_later, "bundler");
43754375
}
43764376

43774377
if (
@@ -4435,10 +4435,7 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
44354435
function getIgnoreDeprecationsVersion(): Version {
44364436
const ignoreDeprecations = options.ignoreDeprecations;
44374437
if (ignoreDeprecations) {
4438-
// While we could do Version.tryParse here to support any version,
4439-
// for now, only allow "5.0". We aren't planning on deprecating anything
4440-
// until 6.0.
4441-
if (ignoreDeprecations === "5.0") {
4438+
if (ignoreDeprecations === "5.0" || ignoreDeprecations === "6.0") {
44424439
return new Version(ignoreDeprecations);
44434440
}
44444441
reportInvalidIgnoreDeprecations();
@@ -4526,6 +4523,12 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
45264523
createDeprecatedDiagnostic("preserveValueImports", /*value*/ undefined, "verbatimModuleSyntax");
45274524
}
45284525
});
4526+
4527+
checkDeprecations("6.0", "7.0", createDiagnostic, createDeprecatedDiagnostic => {
4528+
if (options.moduleResolution === ModuleResolutionKind.Node10) {
4529+
createDeprecatedDiagnostic("moduleResolution", "node10");
4530+
}
4531+
});
45294532
}
45304533

45314534
function verifyDeprecatedProjectReference(ref: ProjectReference, parentFile: JsonSourceFile | undefined, index: number) {

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7338,6 +7338,9 @@ export enum ModuleResolutionKind {
73387338
* Use the new name or consider switching to a modern module resolution target.
73397339
*/
73407340
NodeJs = 2,
7341+
/**
7342+
* @deprecated
7343+
*/
73417344
Node10 = 2,
73427345
// Starting with node12, node's module resolver has significant departures from traditional cjs resolution
73437346
// to better support ECMAScript modules and their use within node - however more features are still being added.

src/compiler/utilities.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,6 +3823,20 @@ export function isBindingElementOfBareOrAccessedRequire(node: Node): node is Bin
38233823
return isBindingElement(node) && isVariableDeclarationInitializedToBareOrAccessedRequire(node.parent.parent);
38243824
}
38253825

3826+
/** @internal */
3827+
export function getModuleSpecifierOfBareOrAccessedRequire(node: VariableDeclarationInitializedTo<RequireOrImportCall | AccessExpression>): StringLiteralLike | undefined {
3828+
if (isVariableDeclarationInitializedToRequire(node)) {
3829+
return node.initializer.arguments[0];
3830+
}
3831+
if (isVariableDeclarationInitializedToBareOrAccessedRequire(node)) {
3832+
const leftmost = getLeftmostAccessExpression(node.initializer);
3833+
if (isRequireCall(leftmost, /*requireStringLiteralLikeArgument*/ true)) {
3834+
return leftmost.arguments[0];
3835+
}
3836+
}
3837+
return undefined;
3838+
}
3839+
38263840
function isVariableDeclarationInitializedWithRequireHelper(node: Node, allowAccessedRequire: boolean) {
38273841
return isVariableDeclaration(node) &&
38283842
!!node.initializer &&
@@ -8974,9 +8988,6 @@ const _computedOptions = createComputedCompilerOptions({
89748988
let moduleResolution = compilerOptions.moduleResolution;
89758989
if (moduleResolution === undefined) {
89768990
switch (_computedOptions.module.computeValue(compilerOptions)) {
8977-
case ModuleKind.CommonJS:
8978-
moduleResolution = ModuleResolutionKind.Node10;
8979-
break;
89808991
case ModuleKind.Node16:
89818992
case ModuleKind.Node18:
89828993
case ModuleKind.Node20:
@@ -8985,6 +8996,7 @@ const _computedOptions = createComputedCompilerOptions({
89858996
case ModuleKind.NodeNext:
89868997
moduleResolution = ModuleResolutionKind.NodeNext;
89878998
break;
8999+
case ModuleKind.CommonJS:
89889000
case ModuleKind.Preserve:
89899001
moduleResolution = ModuleResolutionKind.Bundler;
89909002
break;

src/server/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3269,6 +3269,7 @@ export const enum ModuleResolutionKind {
32693269
Node = "node",
32703270
/** @deprecated Renamed to `Node10` */
32713271
NodeJs = "node",
3272+
/** @deprecated */
32723273
Node10 = "node10",
32733274
Node16 = "node16",
32743275
NodeNext = "nodenext",

src/services/completions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,10 +4184,6 @@ function getCompletionData(
41844184
return charactersFuzzyMatchInString(symbolName, lowerCaseTokenText);
41854185
},
41864186
(info, symbolName, isFromAmbientModule, exportMapKey) => {
4187-
if (detailsEntryId && !some(info, i => detailsEntryId.source === stripQuotes(i.moduleSymbol.name))) {
4188-
return;
4189-
}
4190-
41914187
// Do a relatively cheap check to bail early if all re-exports are non-importable
41924188
// due to file location or package.json dependency filtering. For non-node16+
41934189
// module resolution modes, getting past this point guarantees that we'll be
@@ -4216,6 +4212,10 @@ function getCompletionData(
42164212
({ exportInfo = info[0], moduleSpecifier } = result);
42174213
}
42184214

4215+
if (detailsEntryId && (detailsEntryId.source !== moduleSpecifier && !some(info, i => detailsEntryId.source === stripQuotes(i.moduleSymbol.name)))) {
4216+
return;
4217+
}
4218+
42194219
const isDefaultExport = exportInfo.exportKind === ExportKind.Default;
42204220
const symbol = isDefaultExport && getLocalSymbolForExportDefault(Debug.checkDefined(exportInfo.symbol)) || Debug.checkDefined(exportInfo.symbol);
42214221

src/services/stringCompletions.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,8 +1067,9 @@ function getCompletionEntriesForNonRelativeModules(
10671067
if (tryFileExists(host, packageFile)) {
10681068
const packageJson = readJson(packageFile, host);
10691069
const fragmentSubpath = components.join("/") + (components.length && hasTrailingDirectorySeparator(fragment) ? "/" : "");
1070-
exportsOrImportsLookup((packageJson as MapLike<unknown>).exports, fragmentSubpath, packageDirectory, /*isExports*/ true, /*isImports*/ false);
1071-
return;
1070+
if (exportsOrImportsLookup((packageJson as MapLike<unknown>).exports, fragmentSubpath, packageDirectory, /*isExports*/ true, /*isImports*/ false)) {
1071+
return;
1072+
}
10721073
}
10731074
return nodeModulesDirectoryOrImportsLookup(ancestor);
10741075
};
@@ -1079,9 +1080,10 @@ function getCompletionEntriesForNonRelativeModules(
10791080

10801081
return arrayFrom(result.values());
10811082

1082-
function exportsOrImportsLookup(lookupTable: unknown, fragment: string, baseDirectory: string, isExports: boolean, isImports: boolean) {
1083+
/** Returns true if the search should stop */
1084+
function exportsOrImportsLookup(lookupTable: unknown, fragment: string, baseDirectory: string, isExports: boolean, isImports: boolean): boolean {
10831085
if (typeof lookupTable !== "object" || lookupTable === null) { // eslint-disable-line no-restricted-syntax
1084-
return; // null lookupTable or entrypoint only
1086+
return lookupTable !== undefined; // null lookupTable or entrypoint only
10851087
}
10861088
const keys = getOwnKeys(lookupTable as MapLike<unknown>);
10871089
const conditions = getConditions(compilerOptions, mode);
@@ -1105,6 +1107,7 @@ function getCompletionEntriesForNonRelativeModules(
11051107
},
11061108
comparePatternKeys,
11071109
);
1110+
return true;
11081111
}
11091112
}
11101113

src/testRunner/unittests/tscWatch/watchEnvironment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ describe("unittests:: tscWatch:: watchEnvironment:: tsc-watch with different pol
288288
sys: () => {
289289
const configFile: File = {
290290
path: `/user/username/projects/myproject/tsconfig.json`,
291-
content: "{}",
291+
content: `{ "compilerOptions": { "moduleResolution": "node10" } }`,
292292
};
293293
const file1: File = {
294294
path: `/user/username/projects/myproject/src/file1.ts`,

src/testRunner/unittests/tsserver/autoImportProvider.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ describe("unittests:: tsserver:: autoImportProvider::", () => {
188188

189189
// Create auto import provider project, ensure still !session.getProjectService().pendingEnsureProjectForOpenFiles
190190
host.writeFile(packageJson.path, packageJson.content);
191+
// package.json was watched in --moduleResolution bundler
192+
assert.isTrue(session.getProjectService().pendingEnsureProjectForOpenFiles);
193+
host.runQueuedTimeoutCallbacks();
194+
assert.isFalse(session.getProjectService().pendingEnsureProjectForOpenFiles);
195+
191196
session.host.baselineHost("Before getPackageJsonAutoImportProvider");
192197
hostProject.getPackageJsonAutoImportProvider();
193198
assert.isFalse(session.getProjectService().pendingEnsureProjectForOpenFiles);

0 commit comments

Comments
 (0)