Skip to content

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

Merged
5 commits merged into from
Jan 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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.lastIndexOf(nodeModulesPathPart);
if (idx === -1) {
return undefined;
}

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

indexAfterPackageName = moveToNextDirectorySeparatorIfAvailable(path, indexAfterPackageName);
}
const packageDirectory = path.slice(0, indexAfterPackageName);
const subModuleName = removeExtensionAndIndex(path.slice(indexAfterPackageName + 1));
return { packageDirectory, subModuleName };
}

function moveToNextDirectorySeparatorIfAvailable(path: string, prevSeparatorIndex: number): number {
const nextSeparatorIndex = path.indexOf(directorySeparator, prevSeparatorIndex + 1);
return nextSeparatorIndex === -1 ? prevSeparatorIndex : nextSeparatorIndex;
}

function removeExtensionAndIndex(path: string): string {
const noExtension = removeFileExtension(path);
return noExtension === "index" ? "" : removeSuffix(noExtension, "/index");
}

/* @internal */
export function directoryProbablyExists(directoryName: string, host: { directoryExists?: (directoryName: string) => boolean }): boolean {
// if host does not support 'directoryExists' assume that directory will exist
Expand Down
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'. ========",
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.

"======== 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,45 @@
//// [tests/cases/compiler/duplicatePackage_relativeImportWithinPackage_scoped.ts] ////

//// [package.json]
{
"name": "@foo/bar",
"version": "1.2.3"
}

//// [index.d.ts]
export class C {
private x: number;
}

//// [index.d.ts]
import { C } from "@foo/bar";
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/bar",
"version": "1.2.3"
}

//// [index.ts]
import { use } from "@foo/bar/use";
import { o } from "a";

use(o);


//// [index.js]
"use strict";
exports.__esModule = true;
var use_1 = require("@foo/bar/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/bar/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/bar/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/bar";
>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/bar/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/bar/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/bar/use' from '/index.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module '@foo/bar/use' from 'node_modules' folder, target file type 'TypeScript'.",
"Found 'package.json' at '/node_modules/@foo/bar/package.json'.",
"File '/node_modules/@foo/bar/use.ts' does not exist.",
"File '/node_modules/@foo/bar/use.tsx' does not exist.",
"File '/node_modules/@foo/bar/use.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/node_modules/@foo/bar/use.d.ts', result '/node_modules/@foo/bar/use.d.ts'.",
"======== Module name '@foo/bar/use' was successfully resolved to '/node_modules/@foo/bar/use.d.ts'. ========",
"======== 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/bar/use.d.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module as file / folder, candidate module location '/node_modules/@foo/bar/index', target file type 'TypeScript'.",
"File '/node_modules/@foo/bar/index.ts' does not exist.",
"File '/node_modules/@foo/bar/index.tsx' does not exist.",
"File '/node_modules/@foo/bar/index.d.ts' exist - use it as a name resolution result.",
"Found 'package.json' at '/node_modules/@foo/bar/package.json'.",
"======== Module name './index' was successfully resolved to '/node_modules/@foo/bar/index.d.ts'. ========",
"======== Resolving module '@foo/bar' from '/node_modules/a/index.d.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module '@foo/bar' from 'node_modules' folder, target file type 'TypeScript'.",
"Found 'package.json' at '/node_modules/a/node_modules/@foo/bar/package.json'.",
"File '/node_modules/a/node_modules/@foo/bar.ts' does not exist.",
"File '/node_modules/a/node_modules/@foo/bar.tsx' does not exist.",
"File '/node_modules/a/node_modules/@foo/bar.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/bar/index.ts' does not exist.",
"File '/node_modules/a/node_modules/@foo/bar/index.tsx' does not exist.",
"File '/node_modules/a/node_modules/@foo/bar/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/node_modules/a/node_modules/@foo/bar/index.d.ts', result '/node_modules/a/node_modules/@foo/bar/index.d.ts'.",
"======== Module name '@foo/bar' was successfully resolved to '/node_modules/a/node_modules/@foo/bar/index.d.ts'. ========"
]
Loading