Skip to content

Commit 365cb58

Browse files
TypeScript Botandrewbranch
TypeScript Bot
andauthored
🤖 Pick PR #53613 (Fix Go To Source Definition in `--m...) into release-5.0 (#53617)
Co-authored-by: Andrew Branch <[email protected]>
1 parent 97dac8a commit 365cb58

File tree

6 files changed

+83
-9
lines changed

6 files changed

+83
-9
lines changed

‎src/compiler/binder.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ import {
8484
getContainingClass,
8585
getEffectiveContainerForJSDocTemplateTag,
8686
getElementOrPropertyAccessName,
87-
getEmitModuleResolutionKind,
8887
getEmitScriptTarget,
8988
getEnclosingBlockScopeContainer,
9089
getErrorSpanForNode,
@@ -241,7 +240,6 @@ import {
241240
ModifierFlags,
242241
ModuleBlock,
243242
ModuleDeclaration,
244-
ModuleResolutionKind,
245243
Mutable,
246244
NamespaceExportDeclaration,
247245
Node,
@@ -279,6 +277,7 @@ import {
279277
setValueDeclaration,
280278
ShorthandPropertyAssignment,
281279
shouldPreserveConstEnums,
280+
shouldResolveJsRequire,
282281
SignatureDeclaration,
283282
skipParentheses,
284283
sliceAfter,
@@ -3532,7 +3531,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
35323531
if (!isBindingPattern(node.name)) {
35333532
const possibleVariableDecl = node.kind === SyntaxKind.VariableDeclaration ? node : node.parent.parent;
35343533
if (isInJSFile(node) &&
3535-
getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Bundler &&
3534+
shouldResolveJsRequire(options) &&
35363535
isVariableDeclarationInitializedToBareOrAccessedRequire(possibleVariableDecl) &&
35373536
!getJSDocTypeTag(node) &&
35383537
!(getCombinedModifierFlags(node) & ModifierFlags.Export)

‎src/compiler/checker.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,7 @@ import {
934934
ShorthandPropertyAssignment,
935935
shouldAllowImportingTsExtension,
936936
shouldPreserveConstEnums,
937+
shouldResolveJsRequire,
937938
Signature,
938939
SignatureDeclaration,
939940
SignatureFlags,
@@ -3995,7 +3996,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
39953996
const hasDefaultOnly = isOnlyImportedAsDefault(specifier);
39963997
const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, specifier);
39973998
if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) {
3998-
if (hasExportAssignmentSymbol(moduleSymbol) && !(getAllowSyntheticDefaultImports(compilerOptions) || getESModuleInterop(compilerOptions))) {
3999+
if (hasExportAssignmentSymbol(moduleSymbol) && !allowSyntheticDefaultImports) {
39994000
const compilerOptionName = moduleKind >= ModuleKind.ES2015 ? "allowSyntheticDefaultImports" : "esModuleInterop";
40004001
const exportEqualsSymbol = moduleSymbol.exports!.get(InternalSymbolName.ExportEquals);
40014002
const exportAssignment = exportEqualsSymbol!.valueDeclaration;
@@ -4138,7 +4139,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
41384139
if (!isIdentifier(name)) {
41394140
return undefined;
41404141
}
4141-
const suppressInteropError = name.escapedText === InternalSymbolName.Default && !!(compilerOptions.allowSyntheticDefaultImports || getESModuleInterop(compilerOptions));
4142+
const suppressInteropError = name.escapedText === InternalSymbolName.Default && allowSyntheticDefaultImports;
41424143
const targetSymbol = resolveESModuleSymbol(moduleSymbol, moduleSpecifier, /*dontResolveAlias*/ false, suppressInteropError);
41434144
if (targetSymbol) {
41444145
if (name.escapedText) {
@@ -9177,7 +9178,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
91779178
// If `target` refers to a shorthand module symbol, the name we're trying to pull out isn;t recoverable from the target symbol
91789179
// In such a scenario, we must fall back to looking for an alias declaration on `symbol` and pulling the target name from that
91799180
let verbatimTargetName = isShorthandAmbientModuleSymbol(target) && getSomeTargetNameFromDeclarations(symbol.declarations) || unescapeLeadingUnderscores(target.escapedName);
9180-
if (verbatimTargetName === InternalSymbolName.ExportEquals && (getESModuleInterop(compilerOptions) || compilerOptions.allowSyntheticDefaultImports)) {
9181+
if (verbatimTargetName === InternalSymbolName.ExportEquals && allowSyntheticDefaultImports) {
91819182
// target refers to an `export=` symbol that was hoisted into a synthetic default - rename here to match
91829183
verbatimTargetName = InternalSymbolName.Default;
91839184
}
@@ -33993,7 +33994,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3399333994
}
3399433995

3399533996
// In JavaScript files, calls to any identifier 'require' are treated as external module imports
33996-
if (isInJSFile(node) && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Bundler && isCommonJsRequire(node)) {
33997+
if (isInJSFile(node) && shouldResolveJsRequire(compilerOptions) && isCommonJsRequire(node)) {
3399733998
return resolveExternalModuleTypeByLiteral(node.arguments![0] as StringLiteral);
3399833999
}
3399934000

‎src/compiler/program.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ import {
284284
setParentRecursive,
285285
setResolvedModule,
286286
setResolvedTypeReferenceDirective,
287+
shouldResolveJsRequire,
287288
skipTrivia,
288289
skipTypeChecking,
289290
some,
@@ -3207,8 +3208,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
32073208
collectModuleReferences(node, /*inAmbientModule*/ false);
32083209
}
32093210

3210-
// `require` has no special meaning in `--moduleResolution bundler`
3211-
const shouldProcessRequires = isJavaScriptFile && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Bundler;
3211+
const shouldProcessRequires = isJavaScriptFile && shouldResolveJsRequire(options);
32123212
if ((file.flags & NodeFlags.PossiblyContainsDynamicImport) || shouldProcessRequires) {
32133213
collectDynamicImportOrRequireCalls(file);
32143214
}

‎src/compiler/utilities.ts

+6
Original file line numberDiff line numberDiff line change
@@ -8421,6 +8421,12 @@ export function moduleResolutionSupportsPackageJsonExportsAndImports(moduleResol
84218421
|| moduleResolution === ModuleResolutionKind.Bundler;
84228422
}
84238423

8424+
/** @internal */
8425+
export function shouldResolveJsRequire(compilerOptions: CompilerOptions): boolean {
8426+
// `bundler` doesn't support resolving `require`, but needs to in `noDtsResolution` to support Find Source Definition
8427+
return !!compilerOptions.noDtsResolution || getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Bundler;
8428+
}
8429+
84248430
/** @internal */
84258431
export function getResolvePackageJsonExports(compilerOptions: CompilerOptions) {
84268432
const moduleResolution = getEmitModuleResolutionKind(compilerOptions);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// === goToSourceDefinition ===
2+
// === /node_modules/react/cjs/react.production.min.js ===
3+
// 'use strict';exports.[|{| defId: 0 |}useState|]=function(a){};exports.version='16.8.6';
4+
5+
// === /node_modules/react/cjs/react.development.js ===
6+
// 'use strict';
7+
// if (process.env.NODE_ENV !== 'production') {
8+
// (function() {
9+
// function useState(initialState) {}
10+
// exports.[|{| defId: 1 |}useState|] = useState;
11+
// exports.version = '16.8.6';
12+
// }());
13+
// }
14+
15+
// === /index.ts ===
16+
// import { /*GOTO SOURCE DEF*/useState } from 'react';
17+
18+
// === Details ===
19+
[
20+
{
21+
"defId": 0,
22+
"containerKind": "",
23+
"containerName": "",
24+
"kind": "",
25+
"name": ""
26+
},
27+
{
28+
"defId": 1,
29+
"containerKind": "",
30+
"containerName": "",
31+
"kind": "",
32+
"name": ""
33+
}
34+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @Filename: /tsconfig.json
4+
//// { "compilerOptions": { "module": "esnext", "moduleResolution": "bundler" } }
5+
6+
// @Filename: /node_modules/react/package.json
7+
//// { "name": "react", "version": "16.8.6", "main": "index.js" }
8+
9+
// @Filename: /node_modules/react/index.js
10+
//// 'use strict';
11+
////
12+
//// if (process.env.NODE_ENV === 'production') {
13+
//// module.exports = require('./cjs/react.production.min.js');
14+
//// } else {
15+
//// module.exports = require('./cjs/react.development.js');
16+
//// }
17+
18+
// @Filename: /node_modules/react/cjs/react.production.min.js
19+
//// 'use strict';exports./*production*/useState=function(a){};exports.version='16.8.6';
20+
21+
// @Filename: /node_modules/react/cjs/react.development.js
22+
//// 'use strict';
23+
//// if (process.env.NODE_ENV !== 'production') {
24+
//// (function() {
25+
//// function useState(initialState) {}
26+
//// exports./*development*/useState = useState;
27+
//// exports.version = '16.8.6';
28+
//// }());
29+
//// }
30+
31+
// @Filename: /index.ts
32+
//// import { [|/*start*/useState|] } from 'react';
33+
34+
verify.goToSourceDefinition("start", ["production", "development"]);

0 commit comments

Comments
 (0)