Skip to content

Commit 96efd13

Browse files
Merge pull request #1447 from micalevisk/fix/issue-1444
fix(utils): when updating a non-existing nestjs module
2 parents 2a3c6cd + f187e0f commit 96efd13

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/utils/metadata.manager.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ export class MetadataManager {
2424
metadata: string,
2525
symbol: string,
2626
staticOptions?: DeclarationOptions['staticOptions'],
27-
): string {
27+
): string | undefined {
2828
const source: SourceFile = createSourceFile(
2929
'filename.ts',
3030
this.content,
3131
ScriptTarget.ES2017,
3232
);
3333
const decoratorNodes: Node[] = this.getDecoratorMetadata(source, '@Module');
3434
const node: Node = decoratorNodes[0];
35+
// If there is no occurrence of `@Module` decorator, nothing will be inserted
36+
if (!node) {
37+
return;
38+
}
3539
const matchingProperties: ObjectLiteralElement[] = (
3640
node as ObjectLiteralExpression
3741
).properties

src/utils/module-metadata.declarator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export class ModuleMetadataDeclarator {
99
options.symbol,
1010
options.staticOptions,
1111
);
12-
return inserted;
12+
return inserted ?? content;
1313
}
1414
}

test/utils/metadata.manager.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,14 @@ describe('Metadata Manager', () => {
344344
'export class FooModule {}\n'
345345
);
346346
});
347+
it('should return undefined if there is no metadata to update', () => {
348+
const metadata = 'imports';
349+
const symbol = 'FooModule';
350+
const manager = new MetadataManager(
351+
'import { Module } from \'@nestjs/common\';\n' +
352+
'\n' +
353+
'export class FooModule {}\n'
354+
);
355+
expect(manager.insert(metadata, symbol)).toEqual(undefined);
356+
});
347357
});

0 commit comments

Comments
 (0)