Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reflection): ensure complete stripping of relative paths with multiple leading slashes #4844

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,4 +1161,8 @@ export class Utils {

}

static stripRelativePath(str: string): string {
return str.replace(/^(?:\.\.\/|\.\/)+/, '/');
}

}
2 changes: 1 addition & 1 deletion packages/reflection/src/TsMorphMetadataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class TsMorphMetadataProvider extends MetadataProvider {
await this.initSourceFiles();
}

const source = this.sources.find(s => s.getFilePath().endsWith(tsPath.replace(/^\.+/, '')));
const source = this.sources.find(s => s.getFilePath().endsWith(Utils.stripRelativePath(tsPath)));
AbdlrahmanSaberAbdo marked this conversation as resolved.
Show resolved Hide resolved

if (!source && validate) {
throw new MetadataError(`Source file '${tsPath}' not found. Check your 'entitiesTs' option and verify you have 'compilerOptions.declaration' enabled in your 'tsconfig.json'. If you are using webpack, see https://bit.ly/35pPDNn`);
Expand Down
16 changes: 16 additions & 0 deletions tests/Utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ describe('Utils', () => {
expect(c.inner.p).toBeInstanceOf(Promise);
});

describe('stripRelativePath', () => {
test('Remove single leading dot (./)', () => {
const path = './my/path';
expect(Utils.stripRelativePath(path)).toEqual('/my/path');
});
test('Remove multiple leading dots (../)', () => {
const path = '../my/path';
expect(Utils.stripRelativePath(path)).toEqual('/my/path');
});

test('Remove multiple leading dots and slashes (../../)', () => {
const path = '../../my/path';
expect(Utils.stripRelativePath(path)).toEqual('/my/path');
});

});
/**
* regression test for running code coverage with nyc, mocha and ts-node and entity has default constructor value as enum parameter
*/
Expand Down