Skip to content

Commit 141d4bf

Browse files
committed
TS: Handle multiple slashes in scope name
1 parent 7fa0fea commit 141d4bf

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

  • javascript/extractor/lib/typescript/src

javascript/extractor/lib/typescript/src/common.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ import { VirtualSourceRoot } from "./virtual_source_root";
66
/**
77
* Extracts the package name from the prefix of an import string.
88
*/
9-
const packageNameRex = /^(?:@[\w.-]+[/\\])?\w[\w.-]*(?=[/\\]|$)/;
9+
const packageNameRex = /^(?:@[\w.-]+[/\\]+)?\w[\w.-]*(?=[/\\]|$)/;
1010
const extensions = ['.ts', '.tsx', '.d.ts', '.js', '.jsx'];
1111

12+
function getPackageName(importString: string) {
13+
let packageNameMatch = packageNameRex.exec(importString);
14+
if (packageNameMatch == null) return null;
15+
let packageName = packageNameMatch[0];
16+
if (packageName.charAt(0) === '@') {
17+
packageName = packageName.replace(/[/\\]+/g, '/'); // Normalize slash after the scope.
18+
}
19+
return packageName;
20+
}
21+
1222
export class Project {
1323
public program: ts.Program = null;
1424
private host: ts.CompilerHost;
@@ -75,9 +85,8 @@ export class Project {
7585
*/
7686
private redirectModuleName(moduleName: string, containingFile: string, options: ts.CompilerOptions): ts.ResolvedModule {
7787
// Get a package name from the leading part of the module name, e.g. '@scope/foo' from '@scope/foo/bar'.
78-
let packageNameMatch = packageNameRex.exec(moduleName);
79-
if (packageNameMatch == null) return null;
80-
let packageName = packageNameMatch[0];
88+
let packageName = getPackageName(moduleName);
89+
if (packageName == null) return null;
8190

8291
// Get the overridden location of this package, if one exists.
8392
let packageEntryPoint = this.packageEntryPoints.get(packageName);

0 commit comments

Comments
 (0)