Skip to content
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

Get packageId for relative import within a package #21130

Merged
5 commits merged into from
Jan 17, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jan 10, 2018

Fixes #20945 (tested with the sample repository provided)

If we're loading relatively from a file, we will parse the result path to see if we're currently in a node_modules; if so, we will look for a packageId for the current package.

@ghost ghost requested review from aozgaa, amcasey and sheetalkamat January 10, 2018 21:55
Copy link
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable.

@@ -816,6 +818,33 @@ namespace ts {
return loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, considerPackageJson);
}

function parseNodeModuleFromPath(path: string): { packageDirectory: string, subModuleName: string } | undefined {
const nodeModules = "/node_modules/";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this case-sensitive even on Windows?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed if we normalize the path first.


function indexOfNextSlash(s: string, pos: number): number {
const x = s.indexOf(directorySeparator, pos);
return x === -1 ? pos : x;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This special case makes sense for this application, but probably not in general. Would it make sense to move this helper into parseNodeModuleFromPath?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of moving things into closures if they don't need to close over any variables, but this could be named better.


function removeExtensionAndIndex(fileName: string): string {
const noExtension = removeFileExtension(fileName);
return noExtension === "index" ? "" : removeSuffix(noExtension, "/index");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably, we won't reach here for (e.g.) "index.vb"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removeFileExtension only removes the supported file extensions (and also correctly removes all of .d.ts and not just .ts. Although it's also true that fileName shouldn't have a .vb extension since we'd never resolve a module to a .vb file.

@ghost
Copy link
Author

ghost commented Jan 11, 2018

@amcasey Good to merge?

@amcasey
Copy link
Member

amcasey commented Jan 11, 2018

No objections here, but it might be nice to have a second set of eyes.

@ghost
Copy link
Author

ghost commented Jan 11, 2018

@aozgaa @sheetalkamat

Copy link
Contributor

@aozgaa aozgaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some variable naming suggestions and questions about valid inputs.

@@ -816,6 +818,34 @@ namespace ts {
return loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocations, onlyRecordFailures, state, considerPackageJson);
}

function parseNodeModuleFromPath(path: string): { packageDirectory: string, subModuleName: string } | undefined {
path = normalizePath(path);
const nodeModules = "/node_modules/";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe rename to nodeModulesPathPart and declare outside the function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const indexAfterNodeModules = idx + nodeModules.length;
let indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterNodeModules);
if (path.charCodeAt(indexAfterNodeModules) === CharacterCodes.at) {
indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterPackageName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to get a path like /node_modules/@asdf? How should we handle such an input if so?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A path should be a complete file path like /node_modules/@asdf/pkg/index.d.ts, not just /node_modules/@asdf.

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think tsserver shouldn't crash

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.

indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterPackageName);
}
const packageDirectory = path.slice(0, indexAfterPackageName);
const subModuleName = removeExtensionAndIndex(path.slice(indexAfterPackageName));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the caller loadModuleFromNodeModulesFolder and the use of getPackageJsonInfo, it looks like subModuleName should refer to submodule within a nested module (eg: "core" in "@angular/core"). Is this correct?

Should subModuleName be "" when we are handling a module without submodules?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subModuleName is the name of a path within a package, so for @angular/core/foo.d.ts it would be "foo" and for @angular/core/index.d.ts it would be the empty string "".

}

function moveToNextDirectorySeparatorIfAvailable(path: string, pos: number): number {
const indexOfSlash = path.indexOf(directorySeparator, pos);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe indexOfNextSlash?

return { packageDirectory, subModuleName };
}

function moveToNextDirectorySeparatorIfAvailable(path: string, pos: number): number {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should pos necessarily be the index of a slash? Could we rename the argument to slashIndex or add a comment if so?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return indexOfSlash === -1 ? pos : indexOfSlash;
}

function removeExtensionAndIndex(fileName: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a convention where fileName is a path? If it someone were to input just the last part of a path here (eg: "index.d.ts") then the result we would get would be "index". Is that okay?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename the parameter to path.

const indexAfterNodeModules = idx + nodeModules.length;
let indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterNodeModules);
if (path.charCodeAt(indexAfterNodeModules) === CharacterCodes.at) {
indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterPackageName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the method work with submodules under @types (eg: "@types/someModule/Submodule")? Or is that not a valid input here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case subModuleName would be "Submodule".

/**
* packageDirectory is the directory of the package itself.
* subModuleName is the path within the package.
* For `foo/index.d.ts` this is { packageDirectory: "foo", subModuleName: "" }.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these examples start with "/node_modules/"?

@ghost ghost force-pushed the duplicatePackage_relativeImportWithinPackage branch from 095694a to c7bde6f Compare January 17, 2018 16:05

const indexAfterNodeModules = idx + nodeModulesPathPart.length;
let indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterNodeModules);
if (path.charCodeAt(indexAfterNodeModules) === CharacterCodes.at) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test for scoped packages as well

*/
function parseNodeModuleFromPath(path: string): { packageDirectory: string, subModuleName: string } | undefined {
path = normalizePath(path);
const idx = path.indexOf(nodeModulesPathPart);
Copy link
Contributor

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

"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'. ========",
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@mhegazy mhegazy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments

@ghost ghost mentioned this pull request Jan 17, 2018
@ghost ghost force-pushed the duplicatePackage_relativeImportWithinPackage branch from d5c4f2d to 3420bd6 Compare January 17, 2018 18:48
@ghost ghost merged commit 61fb845 into master Jan 17, 2018
@ghost ghost deleted the duplicatePackage_relativeImportWithinPackage branch January 17, 2018 19:14
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants