-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Get packageId for relative import within a package #21130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -801,7 +801,9 @@ namespace ts { | |
} | ||
const resolvedFromFile = loadModuleFromFile(extensions, candidate, failedLookupLocations, onlyRecordFailures, state); | ||
if (resolvedFromFile) { | ||
return noPackageId(resolvedFromFile); | ||
const nm = considerPackageJson ? parseNodeModuleFromPath(resolvedFromFile.path) : undefined; | ||
const packageId = nm && getPackageJsonInfo(nm.packageDirectory, nm.subModuleName, failedLookupLocations, /*onlyRecordFailures*/ false, state).packageId; | ||
return withPackageId(packageId, resolvedFromFile); | ||
} | ||
} | ||
if (!onlyRecordFailures) { | ||
|
@@ -816,6 +818,45 @@ namespace ts { | |
return loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, considerPackageJson); | ||
} | ||
|
||
const nodeModulesPathPart = "/node_modules/"; | ||
|
||
/** | ||
* This will be called on the successfully resolved path from `loadModuleFromFile`. | ||
* (Not neeeded for `loadModuleFromNodeModules` as that looks up the `package.json` as part of resolution.) | ||
* | ||
* packageDirectory is the directory of the package itself. | ||
* subModuleName is the path within the package. | ||
* For `blah/node_modules/foo/index.d.ts` this is { packageDirectory: "foo", subModuleName: "" }. (Part before "/node_modules/" is ignored.) | ||
* For `/node_modules/foo/bar.d.ts` this is { packageDirectory: "foo", subModuleName": "bar" }. | ||
* For `/node_modules/@types/foo/bar/index.d.ts` this is { packageDirectory: "@types/foo", subModuleName: "bar" }. | ||
*/ | ||
function parseNodeModuleFromPath(path: string): { packageDirectory: string, subModuleName: string } | undefined { | ||
path = normalizePath(path); | ||
const idx = path.indexOf(nodeModulesPathPart); | ||
if (idx === -1) { | ||
return undefined; | ||
} | ||
|
||
const indexAfterNodeModules = idx + nodeModulesPathPart.length; | ||
let indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterNodeModules); | ||
if (path.charCodeAt(indexAfterNodeModules) === CharacterCodes.at) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a test for scoped packages as well |
||
indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterPackageName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to get a path like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A path should be a complete file path like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we concerned with malformed paths and packages? As a scenario, do we care if someone is mucking about and makes a malformed entry manually? I personally think tsserver shouldn't crash, and ideally we would surface a message to a user. Do we have some way of handling situations like this gracefully? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's just, like, your opinion, man. 😜 But we shouldn't get a malformed path here because this is only called after a successful module resolution. And even if we did, I don't see a place where this function would crash. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does the method work with submodules under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case |
||
} | ||
const packageDirectory = path.slice(0, indexAfterPackageName); | ||
const subModuleName = removeExtensionAndIndex(path.slice(indexAfterPackageName)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the caller Should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. subModuleName is the name of a path within a package, so for |
||
return { packageDirectory, subModuleName }; | ||
} | ||
|
||
function moveToNextDirectorySeparatorIfAvailable(path: string, prevSeparatorIndex: number): number { | ||
const nextSeparatorIndex = path.indexOf(directorySeparator, prevSeparatorIndex); | ||
return nextSeparatorIndex === -1 ? prevSeparatorIndex : nextSeparatorIndex; | ||
} | ||
|
||
function removeExtensionAndIndex(path: string): string { | ||
const noExtension = removeFileExtension(path); | ||
return noExtension === "index" ? "" : removeSuffix(noExtension, "/index"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Presumably, we won't reach here for (e.g.) "index.vb"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
/* @internal */ | ||
export function directoryProbablyExists(directoryName: string, host: { directoryExists?: (directoryName: string) => boolean }): boolean { | ||
// if host does not support 'directoryExists' assume that directory will exist | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//// [tests/cases/compiler/duplicatePackage_relativeImportWithinPackage.ts] //// | ||
|
||
//// [package.json] | ||
{ | ||
"name": "foo", | ||
"version": "1.2.3" | ||
} | ||
|
||
//// [index.d.ts] | ||
export class C { | ||
private x: number; | ||
} | ||
|
||
//// [index.d.ts] | ||
import { C } from "foo"; | ||
export const o: C; | ||
|
||
//// [use.d.ts] | ||
import { C } from "./index"; | ||
export function use(o: C): void; | ||
|
||
//// [index.d.ts] | ||
export class C { | ||
private x: number; | ||
} | ||
|
||
//// [package.json] | ||
{ | ||
"name": "foo", | ||
"version": "1.2.3" | ||
} | ||
|
||
//// [index.ts] | ||
import { use } from "foo/use"; | ||
import { o } from "a"; | ||
|
||
use(o); | ||
|
||
|
||
//// [index.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
var use_1 = require("foo/use"); | ||
var a_1 = require("a"); | ||
use_1.use(a_1.o); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
=== /index.ts === | ||
import { use } from "foo/use"; | ||
>use : Symbol(use, Decl(index.ts, 0, 8)) | ||
|
||
import { o } from "a"; | ||
>o : Symbol(o, Decl(index.ts, 1, 8)) | ||
|
||
use(o); | ||
>use : Symbol(use, Decl(index.ts, 0, 8)) | ||
>o : Symbol(o, Decl(index.ts, 1, 8)) | ||
|
||
=== /node_modules/a/node_modules/foo/index.d.ts === | ||
export class C { | ||
>C : Symbol(C, Decl(index.d.ts, 0, 0)) | ||
|
||
private x: number; | ||
>x : Symbol(C.x, Decl(index.d.ts, 0, 16)) | ||
} | ||
|
||
=== /node_modules/a/index.d.ts === | ||
import { C } from "foo"; | ||
>C : Symbol(C, Decl(index.d.ts, 0, 8)) | ||
|
||
export const o: C; | ||
>o : Symbol(o, Decl(index.d.ts, 1, 12)) | ||
>C : Symbol(C, Decl(index.d.ts, 0, 8)) | ||
|
||
=== /node_modules/foo/use.d.ts === | ||
import { C } from "./index"; | ||
>C : Symbol(C, Decl(use.d.ts, 0, 8)) | ||
|
||
export function use(o: C): void; | ||
>use : Symbol(use, Decl(use.d.ts, 0, 28)) | ||
>o : Symbol(o, Decl(use.d.ts, 1, 20)) | ||
>C : Symbol(C, Decl(use.d.ts, 0, 8)) | ||
|
||
=== /node_modules/foo/index.d.ts === | ||
export class C { | ||
>C : Symbol(C, Decl(index.d.ts, 0, 0)) | ||
|
||
private x: number; | ||
>x : Symbol(C.x, Decl(index.d.ts, 0, 16)) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
[ | ||
"======== Resolving module 'foo/use' from '/index.ts'. ========", | ||
"Module resolution kind is not specified, using 'NodeJs'.", | ||
"Loading module 'foo/use' from 'node_modules' folder, target file type 'TypeScript'.", | ||
"Found 'package.json' at '/node_modules/foo/package.json'.", | ||
"File '/node_modules/foo/use.ts' does not exist.", | ||
"File '/node_modules/foo/use.tsx' does not exist.", | ||
"File '/node_modules/foo/use.d.ts' exist - use it as a name resolution result.", | ||
"Resolving real path for '/node_modules/foo/use.d.ts', result '/node_modules/foo/use.d.ts'.", | ||
"======== Module name 'foo/use' was successfully resolved to '/node_modules/foo/use.d.ts'. ========", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not related to this PR, but just a thought that came to me, we are not logging the packageid anywhere. we should probably to make these tests more statically verifiable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
"======== Resolving module 'a' from '/index.ts'. ========", | ||
"Module resolution kind is not specified, using 'NodeJs'.", | ||
"Loading module 'a' from 'node_modules' folder, target file type 'TypeScript'.", | ||
"File '/node_modules/a/package.json' does not exist.", | ||
"File '/node_modules/a.ts' does not exist.", | ||
"File '/node_modules/a.tsx' does not exist.", | ||
"File '/node_modules/a.d.ts' does not exist.", | ||
"File '/node_modules/a/index.ts' does not exist.", | ||
"File '/node_modules/a/index.tsx' does not exist.", | ||
"File '/node_modules/a/index.d.ts' exist - use it as a name resolution result.", | ||
"Resolving real path for '/node_modules/a/index.d.ts', result '/node_modules/a/index.d.ts'.", | ||
"======== Module name 'a' was successfully resolved to '/node_modules/a/index.d.ts'. ========", | ||
"======== Resolving module './index' from '/node_modules/foo/use.d.ts'. ========", | ||
"Module resolution kind is not specified, using 'NodeJs'.", | ||
"Loading module as file / folder, candidate module location '/node_modules/foo/index', target file type 'TypeScript'.", | ||
"File '/node_modules/foo/index.ts' does not exist.", | ||
"File '/node_modules/foo/index.tsx' does not exist.", | ||
"File '/node_modules/foo/index.d.ts' exist - use it as a name resolution result.", | ||
"Found 'package.json' at '/node_modules/foo/package.json'.", | ||
"======== Module name './index' was successfully resolved to '/node_modules/foo/index.d.ts'. ========", | ||
"======== Resolving module 'foo' from '/node_modules/a/index.d.ts'. ========", | ||
"Module resolution kind is not specified, using 'NodeJs'.", | ||
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.", | ||
"Found 'package.json' at '/node_modules/a/node_modules/foo/package.json'.", | ||
"File '/node_modules/a/node_modules/foo.ts' does not exist.", | ||
"File '/node_modules/a/node_modules/foo.tsx' does not exist.", | ||
"File '/node_modules/a/node_modules/foo.d.ts' does not exist.", | ||
"'package.json' does not have a 'typings' field.", | ||
"'package.json' does not have a 'types' field.", | ||
"File '/node_modules/a/node_modules/foo/index.ts' does not exist.", | ||
"File '/node_modules/a/node_modules/foo/index.tsx' does not exist.", | ||
"File '/node_modules/a/node_modules/foo/index.d.ts' exist - use it as a name resolution result.", | ||
"Resolving real path for '/node_modules/a/node_modules/foo/index.d.ts', result '/node_modules/a/node_modules/foo/index.d.ts'.", | ||
"======== Module name 'foo' was successfully resolved to '/node_modules/a/node_modules/foo/index.d.ts'. ========" | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
=== /index.ts === | ||
import { use } from "foo/use"; | ||
>use : (o: C) => void | ||
|
||
import { o } from "a"; | ||
>o : C | ||
|
||
use(o); | ||
>use(o) : void | ||
>use : (o: C) => void | ||
>o : C | ||
|
||
=== /node_modules/a/node_modules/foo/index.d.ts === | ||
export class C { | ||
>C : C | ||
|
||
private x: number; | ||
>x : number | ||
} | ||
|
||
=== /node_modules/a/index.d.ts === | ||
import { C } from "foo"; | ||
>C : typeof C | ||
|
||
export const o: C; | ||
>o : C | ||
>C : C | ||
|
||
=== /node_modules/foo/use.d.ts === | ||
import { C } from "./index"; | ||
>C : typeof C | ||
|
||
export function use(o: C): void; | ||
>use : (o: C) => void | ||
>o : C | ||
>C : C | ||
|
||
=== /node_modules/foo/index.d.ts === | ||
export class C { | ||
>C : C | ||
|
||
private x: number; | ||
>x : number | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// @noImplicitReferences: true | ||
// @traceResolution: true | ||
|
||
// @Filename: /node_modules/a/node_modules/foo/package.json | ||
{ | ||
"name": "foo", | ||
"version": "1.2.3" | ||
} | ||
|
||
// @Filename: /node_modules/a/node_modules/foo/index.d.ts | ||
export class C { | ||
private x: number; | ||
} | ||
|
||
// @Filename: /node_modules/a/index.d.ts | ||
import { C } from "foo"; | ||
export const o: C; | ||
|
||
// @Filename: /node_modules/foo/use.d.ts | ||
import { C } from "./index"; | ||
export function use(o: C): void; | ||
|
||
// @Filename: /node_modules/foo/index.d.ts | ||
export class C { | ||
private x: number; | ||
} | ||
|
||
// @Filename: /node_modules/foo/package.json | ||
{ | ||
"name": "foo", | ||
"version": "1.2.3" | ||
} | ||
|
||
// @Filename: /index.ts | ||
import { use } from "foo/use"; | ||
import { o } from "a"; | ||
|
||
use(o); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should not this be
lastIndexOf
instead? you want the last node_modules in the list right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍