@@ -9,7 +9,6 @@ namespace ts.JsTyping {
99 }
1010
1111 interface PackageJson {
12- _requiredBy ?: string [ ] ;
1312 dependencies ?: MapLike < string > ;
1413 devDependencies ?: MapLike < string > ;
1514 name ?: string ;
@@ -264,6 +263,29 @@ namespace ts.JsTyping {
264263 return ;
265264 }
266265
266+ // Collect all toplevel package names if we're looking in node_modules
267+ const parentPkgJsonPath = resolvePath ( packagesFolderPath , ".." , "package.json" ) ;
268+ const parentPkgJson : PackageJson = host . fileExists ( parentPkgJsonPath ) ? readConfigFile ( parentPkgJsonPath , path => host . readFile ( path ) ) . config : undefined ;
269+ const topLevelDeps = parentPkgJson && new Set ( ) ;
270+ if ( topLevelDeps ) {
271+ const topLevelDependencies = getOwnKeys ( parentPkgJson . dependencies || { } ) ;
272+ for ( const dep of topLevelDependencies ) {
273+ topLevelDeps . add ( dep ) ;
274+ }
275+ const topLevelDevDependencies = getOwnKeys ( parentPkgJson . devDependencies || { } ) ;
276+ for ( const dep of topLevelDevDependencies ) {
277+ topLevelDeps . add ( dep ) ;
278+ }
279+ const topLevelOptionalDependencies = getOwnKeys ( parentPkgJson . optionalDependencies || { } ) ;
280+ for ( const dep of topLevelOptionalDependencies ) {
281+ topLevelDeps . add ( dep ) ;
282+ }
283+ const topLevelPeerDependencies = getOwnKeys ( parentPkgJson . peerDependencies || { } ) ;
284+ for ( const dep of topLevelPeerDependencies ) {
285+ topLevelDeps . add ( dep ) ;
286+ }
287+ }
288+
267289 // depth of 2, so we access `node_modules/foo` but not `node_modules/foo/bar`
268290 const fileNames = host . readDirectory ( packagesFolderPath , [ Extension . Json ] , /*excludes*/ undefined , /*includes*/ undefined , /*depth*/ 2 ) ;
269291 if ( log ) log ( `Searching for typing names in ${ packagesFolderPath } ; all files: ${ JSON . stringify ( fileNames ) } ` ) ;
@@ -277,19 +299,17 @@ namespace ts.JsTyping {
277299 const result = readConfigFile ( normalizedFileName , ( path : string ) => host . readFile ( path ) ) ;
278300 const packageJson : PackageJson = result . config ;
279301
280- // npm 3's package.json contains a "_requiredBy" field
281- // we should include all the top level module names for npm 2, and only module names whose
282- // "_requiredBy" field starts with "#" or equals "/" for npm 3.
283- if ( baseFileName === "package.json" && packageJson . _requiredBy &&
284- filter ( packageJson . _requiredBy , ( r : string ) => r [ 0 ] === "#" || r === "/" ) . length === 0 ) {
285- continue ;
286- }
287-
288302 // If the package has its own d.ts typings, those will take precedence. Otherwise the package name will be used
289303 // to download d.ts files from DefinitelyTyped
290304 if ( ! packageJson . name ) {
291305 continue ;
292306 }
307+
308+ // This is the most portable way to check if a particular NPM package is, in fact, a toplevel dependency.
309+ if ( baseFileName === "package.json" && topLevelDeps && ! topLevelDeps . has ( packageJson . name ) ) {
310+ continue ;
311+ }
312+
293313 const ownTypes = packageJson . types || packageJson . typings ;
294314 if ( ownTypes ) {
295315 const absolutePath = getNormalizedAbsolutePath ( ownTypes , getDirectoryPath ( normalizedFileName ) ) ;
0 commit comments