( filePath: string, )
| 23 | * Returns `undefined` if the file is not inside node_modules. |
| 24 | */ |
| 25 | export function getNodeModulesPackageRoot( |
| 26 | filePath: string, |
| 27 | ): string | undefined { |
| 28 | const normalized = normalizePath(filePath) |
| 29 | const nodeModulesIndex = normalized.lastIndexOf(class="st">'/node_modules/') |
| 30 | if (nodeModulesIndex === -1) return undefined |
| 31 | |
| 32 | const packageStart = nodeModulesIndex + class="st">'/node_modules/'.length |
| 33 | const rest = normalized.slice(packageStart) |
| 34 | const firstSlash = rest.indexOf(class="st">'/') |
| 35 | |
| 36 | let packageName: string |
| 37 | if (rest.startsWith(class="st">'@')) { |
| 38 | class="cm">// scoped package: @scope/pkg |
| 39 | const secondSlash = rest.indexOf(class="st">'/', firstSlash + 1) |
| 40 | packageName = secondSlash === -1 ? rest : rest.slice(0, secondSlash) |
| 41 | } else { |
| 42 | packageName = firstSlash === -1 ? rest : rest.slice(0, firstSlash) |
| 43 | } |
| 44 | return normalized.slice(0, packageStart) + packageName |
| 45 | } |
| 46 | |
| 47 | class="cm">// Virtual modules should be prefixed with a null byte to avoid a |
| 48 | class="cm">// false positive class="st">"missing source" warning. We also check for certain |
no test coverage detected