From 724e656acbd9ec6b658e8498a1ebd86e9fc2d02c Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 3 May 2018 08:02:56 -0700 Subject: [PATCH] Don't show a definition at a 'require' call (#23822) --- src/services/goToDefinition.ts | 4 +++- .../goToDefinitionSignatureAlias_require.ts | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/goToDefinitionSignatureAlias_require.ts diff --git a/src/services/goToDefinition.ts b/src/services/goToDefinition.ts index 977f9617353..cda61134b87 100644 --- a/src/services/goToDefinition.ts +++ b/src/services/goToDefinition.ts @@ -31,7 +31,9 @@ namespace ts.GoToDefinition { const sigInfo = createDefinitionFromSignatureDeclaration(typeChecker, calledDeclaration); // For a function, if this is the original function definition, return just sigInfo. // If this is the original constructor definition, parent is the class. - return typeChecker.getRootSymbols(symbol).some(s => calledDeclaration.symbol === s || calledDeclaration.symbol.parent === s) + return typeChecker.getRootSymbols(symbol).some(s => calledDeclaration.symbol === s || calledDeclaration.symbol.parent === s) || + // TODO: GH#23742 Following check shouldn't be necessary if 'require' is an alias + symbol.declarations.some(d => isVariableDeclaration(d) && d.initializer && isRequireCall(d.initializer, /*checkArgumentIsStringLiteralLike*/ false)) ? [sigInfo] : [sigInfo, ...getDefinitionFromSymbol(typeChecker, symbol, node)]; } diff --git a/tests/cases/fourslash/goToDefinitionSignatureAlias_require.ts b/tests/cases/fourslash/goToDefinitionSignatureAlias_require.ts new file mode 100644 index 00000000000..8fc2390b3ba --- /dev/null +++ b/tests/cases/fourslash/goToDefinitionSignatureAlias_require.ts @@ -0,0 +1,12 @@ +/// + +// @allowJs: true + +// @Filename: /a.js +////module.exports = function /*f*/f() {} + +// @Filename: /b.js +////const f = require("./a"); +////[|/*use*/f|](); + +verify.goToDefinition("use", "f");