Skip to content

Commit c025921

Browse files
committed
ref(parser): use native path module to infer path
1 parent bd45dfc commit c025921

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

packages/node/src/module.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
1-
import { basename, dirname } from '@sentry/utils';
1+
import { posix, sep } from 'path';
2+
3+
const isWindowsPlatform = sep === '\\';
24

35
/** normalizes Windows paths */
4-
function normalizePath(path: string): string {
6+
function normalizeWindowsPath(path: string): string {
57
return path
68
.replace(/^[A-Z]:/, '') // remove Windows-style prefix
79
.replace(/\\/g, '/'); // replace all `\` instances with `/`
810
}
911

1012
/** Gets the module from a filename */
11-
export function getModule(filename: string | undefined): string | undefined {
13+
export function getModule(
14+
filename: string | undefined,
15+
normalizeWindowsPathSeparator: boolean = isWindowsPlatform,
16+
): string | undefined {
1217
if (!filename) {
1318
return;
1419
}
1520

16-
const normalizedFilename = normalizePath(filename);
21+
const normalizedFilename = normalizeWindowsPathSeparator ? normalizeWindowsPath(filename) : filename;
22+
23+
// eslint-disable-next-line prefer-const
24+
let { root, dir, base: basename, ext } = posix.parse(normalizedFilename);
1725

18-
// We could use optional chaining here but webpack does like that mixed with require
19-
const base = normalizePath(
20-
`${(require && require.main && require.main.filename && dirname(require.main.filename)) || global.process.cwd()}/`,
21-
);
26+
const base = (require && require.main && require.main.filename && dir) || global.process.cwd();
27+
28+
const normalizedBase = `${base}/`;
2229

2330
// It's specifically a module
24-
const file = basename(normalizedFilename, '.js');
31+
let file = basename;
32+
33+
if (ext === '.js') {
34+
file = file.slice(0, file.length - '.js'.length);
35+
}
36+
37+
if (!root && !dir) {
38+
// No dirname whatsoever
39+
dir = '.';
40+
}
2541

26-
const path = dirname(normalizedFilename);
27-
let n = path.lastIndexOf('/node_modules/');
42+
let n = dir.lastIndexOf('/node_modules/');
2843
if (n > -1) {
2944
// /node_modules/ is 14 chars
30-
return `${path.slice(n + 14).replace(/\//g, '.')}:${file}`;
45+
return `${dir.slice(n + 14).replace(/\//g, '.')}:${file}`;
3146
}
3247
// Let's see if it's a part of the main module
3348
// To be a part of main module, it has to share the same base
34-
n = `${path}/`.lastIndexOf(base, 0);
49+
n = `${dir}/`.lastIndexOf(normalizedBase, 0);
3550

3651
if (n === 0) {
37-
let moduleName = path.slice(base.length).replace(/\//g, '.');
52+
let moduleName = dir.slice(normalizedBase.length).replace(/\//g, '.');
3853
if (moduleName) {
3954
moduleName += ':';
4055
}

packages/node/test/module.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function withFilename(fn: () => void, filename: string) {
1818
describe('getModule', () => {
1919
test('Windows', () => {
2020
withFilename(() => {
21-
expect(getModule('C:\\Users\\users\\Tim\\Desktop\\node_modules\\module.js')).toEqual('module');
21+
expect(getModule('C:\\Users\\users\\Tim\\Desktop\\node_modules\\module.js', true)).toEqual('module');
2222
}, 'C:\\Users\\Tim\\app.js');
2323
});
2424

0 commit comments

Comments
 (0)