Skip to content

Commit

Permalink
Actually fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Mar 31, 2023
1 parent 1fddb2a commit 8c81daf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ import {
setValueDeclaration,
ShorthandPropertyAssignment,
shouldPreserveConstEnums,
shouldResolveJsRequire,
SignatureDeclaration,
skipParentheses,
sliceAfter,
Expand Down Expand Up @@ -3525,7 +3526,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
if (!isBindingPattern(node.name)) {
const possibleVariableDecl = node.kind === SyntaxKind.VariableDeclaration ? node : node.parent.parent;
if (isInJSFile(node) &&
getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Bundler &&
shouldResolveJsRequire(compilerOptions) &&
isVariableDeclarationInitializedToBareOrAccessedRequire(possibleVariableDecl) &&
!getJSDocTypeTag(node) &&
!(getCombinedModifierFlags(node) & ModifierFlags.Export)
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ import {
ShorthandPropertyAssignment,
shouldAllowImportingTsExtension,
shouldPreserveConstEnums,
shouldResolveJsRequire,
Signature,
SignatureDeclaration,
SignatureFlags,
Expand Down Expand Up @@ -34163,8 +34164,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

// In JavaScript files, calls to any identifier 'require' are treated as external module imports
if (isInJSFile(node) && (getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Bundler || compilerOptions.noDtsResolution) && isCommonJsRequire(node)) {
// `bundler` doesn't support resolving `require`, but needs to in `noDtsResolution` to support Find Source Definition
if (isInJSFile(node) && shouldResolveJsRequire(compilerOptions) && isCommonJsRequire(node)) {
return resolveExternalModuleTypeByLiteral(node.arguments![0] as StringLiteral);
}

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ import {
WriteFileCallbackData,
writeFileEnsuringDirectories,
zipToModeAwareCache,
shouldResolveJsRequire,
} from "./_namespaces/ts";
import * as performance from "./_namespaces/ts.performance";

Expand Down Expand Up @@ -3211,8 +3212,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
collectModuleReferences(node, /*inAmbientModule*/ false);
}

// `require` has no special meaning in `--moduleResolution bundler`
const shouldProcessRequires = isJavaScriptFile && getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Bundler;
const shouldProcessRequires = isJavaScriptFile && shouldResolveJsRequire(options);
if ((file.flags & NodeFlags.PossiblyContainsDynamicImport) || shouldProcessRequires) {
collectDynamicImportOrRequireCalls(file);
}
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8438,6 +8438,12 @@ export function moduleResolutionSupportsPackageJsonExportsAndImports(moduleResol
|| moduleResolution === ModuleResolutionKind.Bundler;
}

/** @internal */
export function shouldResolveJsRequire(compilerOptions: CompilerOptions): boolean {
// `bundler` doesn't support resolving `require`, but needs to in `noDtsResolution` to support Find Source Definition
return !!compilerOptions.noDtsResolution || getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Bundler;
}

/** @internal */
export function getResolvePackageJsonExports(compilerOptions: CompilerOptions) {
const moduleResolution = getEmitModuleResolutionKind(compilerOptions);
Expand Down
28 changes: 20 additions & 8 deletions tests/baselines/reference/goToSource15_bundler.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
// === goToSourceDefinition ===
// === /node_modules/react/index.js ===
// [|'use strict';
//
// if (process.env.NODE_ENV === 'production') {
// module.exports = require('./cjs/react.production.min.js');
// } else {
// module.exports = require('./cjs/react.development.js');
// }|]
// === /node_modules/react/cjs/react.production.min.js ===
// 'use strict';exports.[|{| defId: 0 |}useState|]=function(a){};exports.version='16.8.6';

// === /node_modules/react/cjs/react.development.js ===
// 'use strict';
// if (process.env.NODE_ENV !== 'production') {
// (function() {
// function useState(initialState) {}
// exports.[|{| defId: 1 |}useState|] = useState;
// exports.version = '16.8.6';
// }());
// }

// === /index.ts ===
// import { /*GOTO SOURCE DEF*/useState } from 'react';

// === Details ===
[
{
"defId": 0,
"containerKind": "",
"containerName": "",
"kind": "",
"name": ""
},
{
"defId": 1,
"containerKind": "",
"containerName": "",
"kind": "",
Expand Down

0 comments on commit 8c81daf

Please sign in to comment.