Skip to content

Commit 31cae0d

Browse files
committed
Use path.join
1 parent 19ba0d9 commit 31cae0d

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

common/reviews/api/node-core-library.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ export interface IRealNodeModulePathResolverOptions {
611611
// (undocumented)
612612
fs: Pick<typeof nodeFs, 'lstatSync' | 'readlinkSync'>;
613613
// (undocumented)
614-
path: Pick<typeof nodePath, 'format' | 'isAbsolute' | 'parse' | 'resolve' | 'sep'>;
614+
path: Pick<typeof nodePath, 'format' | 'isAbsolute' | 'join' | 'parse' | 'resolve' | 'sep'>;
615615
}
616616

617617
// @public (undocumented)

libraries/node-core-library/src/RealNodeModulePath.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as nodePath from 'path';
1010
*/
1111
export interface IRealNodeModulePathResolverOptions {
1212
fs: Pick<typeof nodeFs, 'lstatSync' | 'readlinkSync'>;
13-
path: Pick<typeof nodePath, 'format' | 'isAbsolute' | 'parse' | 'resolve' | 'sep'>;
13+
path: Pick<typeof nodePath, 'format' | 'isAbsolute' | 'join' | 'parse' | 'resolve' | 'sep'>;
1414
}
1515

1616
/**
@@ -57,7 +57,6 @@ export class RealNodeModulePathResolver {
5757
const self: this = this;
5858

5959
function realNodeModulePathInternal(input: string): string {
60-
input = self._normalizeTrailingSlash(input);
6160
// Find the last node_modules path segment
6261
const nodeModulesIndex: number = input.lastIndexOf(nodeModulesToken);
6362
if (nodeModulesIndex < 0) {
@@ -75,7 +74,10 @@ export class RealNodeModulePathResolver {
7574
if (linkEnd < 0) {
7675
// Symlink missing, so see if anything before the last node_modules needs resolving,
7776
// and preserve the rest of the path
78-
return `${realNodeModulePathInternal(input.slice(0, nodeModulesIndex))}${input.slice(nodeModulesIndex)}`;
77+
return path.join(
78+
realNodeModulePathInternal(input.slice(0, nodeModulesIndex)),
79+
input.slice(nodeModulesIndex + 1)
80+
);
7981
}
8082

8183
linkStart = linkEnd;
@@ -95,7 +97,7 @@ export class RealNodeModulePathResolver {
9597
// Cache the resolution to avoid the readlink call in subsequent calls
9698
cache.set(linkCandidate, linkTarget);
9799
cache.set(linkTarget, linkTarget);
98-
return `${linkTarget}${input.slice(linkEnd)}`;
100+
return path.join(linkTarget, input.slice(linkEnd + 1));
99101
}
100102

101103
// Relative path or does not exist
@@ -105,22 +107,23 @@ export class RealNodeModulePathResolver {
105107
if (linkTarget) {
106108
// Relative path in symbolic link. Should be resolved relative to real path of base path.
107109
const resolvedTarget: string = path.resolve(
108-
`${realpathBeforeNodeModules}${input.slice(nodeModulesIndex, linkStart)}`,
110+
realpathBeforeNodeModules,
111+
input.slice(nodeModulesIndex + 1, linkStart),
109112
linkTarget
110113
);
111114
// Cache the result of the combined resolution to avoid the readlink call in subsequent calls
112115
cache.set(linkCandidate, resolvedTarget);
113116
cache.set(resolvedTarget, resolvedTarget);
114-
return `${resolvedTarget}${input.slice(linkEnd)}`;
117+
return path.join(resolvedTarget, input.slice(linkEnd + 1));
115118
}
116119

117120
// No symlink, so just return the real path before the last node_modules combined with the
118121
// subsequent path segments
119-
return `${realpathBeforeNodeModules}${input.slice(nodeModulesIndex)}`;
122+
return path.join(realpathBeforeNodeModules, input.slice(nodeModulesIndex + 1));
120123
}
121124

122125
this.realNodeModulePath = (input: string) => {
123-
return realNodeModulePathInternal(path.format(path.parse(path.resolve(input))));
126+
return realNodeModulePathInternal(this._normalizeTrailingSlash(path.resolve(input)));
124127
};
125128
}
126129

0 commit comments

Comments
 (0)