Skip to content

Commit

Permalink
fix(core): support path detection from decorators in bun
Browse files Browse the repository at this point in the history
Closes #5496
  • Loading branch information
B4nan committed May 5, 2024
1 parent fbeb97e commit 6683bcc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/core/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,13 @@ export class Utils {
// In some situations (e.g. swc 1.3.4+), the presence of a source map can obscure the call to
// __decorate(), replacing it with the constructor name. To support these cases we look for
// Reflect.decorate() as well.
let line = stack.findIndex(line => line.includes('__decorate') || line.includes('Reflect.decorate'))!;
let line = stack.findIndex(line => line.match(/__decorate|Reflect\.decorate/));

// bun does not have those lines at all, only the DecorateProperty/DecorateConstructor,
// but those are also present in node, so we need to check this only if they weren't found.
if (line === -1) {
line = stack.findIndex(line => line.match(/DecorateProperty|DecorateConstructor/));
}

if (line === -1) {
return name;
Expand All @@ -800,6 +806,10 @@ export class Utils {
line++;
}

if (stack[line].match(/DecorateProperty|DecorateConstructor/)) {
line += 2;
}

if (Utils.normalizePath(stack[line]).includes('node_modules/tslib/tslib')) {
line++;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,34 @@ describe('Utils', () => {
])).toBe('/opt/app/entity/requirement.ts');
});

test('lookup path from decorator with bun', () => {
expect(Utils.lookupPathFromDecorator('Requirement', [
'Error',
' at lookupPathFromDecorator (/opt/app/node_modules/@mikro-orm/core/utils/Utils.js:636:20)',
' at getMetadataFromDecorator (/opt/app/node_modules/@mikro-orm/core/metadata/MetadataStorage.js:28:51)',
' at <anonymous> (/opt/app/node_modules/@mikro-orm/core/decorators/PrimaryKey.js:9:67)',
' at DecorateProperty (/opt/app/node_modules/reflect-metadata/Reflect.js:561:67)',
' at A (bun:wrap:1:2617)',
' at module code (/opt/app/src/entities/Book.ts:11:42)',
' at moduleEvaluation (:1:11)',
' at <anonymous> (:2:1)',
' at processTicksAndRejections (:12:39)',
])).toBe('/opt/app/src/entities/Book.ts');

expect(Utils.lookupPathFromDecorator('Requirement', [
'Error',
' at lookupPathFromDecorator (/opt/app/node_modules/@mikro-orm/core/utils/Utils.js:636:20)',
' at getMetadataFromDecorator (/opt/app/node_modules/@mikro-orm/core/metadata/MetadataStorage.js:28:51)',
' at <anonymous> (/opt/app/node_modules/@mikro-orm/core/decorators/Entity.js:8:47)',
' at DecorateConstructor (/opt/app/node_modules/reflect-metadata/Reflect.js:549:67)',
' at A (bun:wrap:1:2617)',
' at module code (/opt/app/src/entities/Book.ts:9:8)',
' at moduleEvaluation (:1:11)',
' at <anonymous> (:2:1)',
' at processTicksAndRejections (:12:39)',
])).toBe('/opt/app/src/entities/Book.ts');
});

test('lookup path from decorator on windows', () => {
// with tslib, via ts-node
const stack1 = [
Expand Down

0 comments on commit 6683bcc

Please sign in to comment.