Skip to content

Commit

Permalink
Merge pull request #1036 from micalevisk/fix-manager-indentation-issue
Browse files Browse the repository at this point in the history
fix(utils): when imports list has no indentation
  • Loading branch information
kamilmysliwiec committed May 16, 2022
2 parents 5130410 + 2a301f2 commit 9914ef3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Stack Overflow is a much better place to ask questions since:

To save your and our time, we will systematically close all issues that are requests for general support and redirect people to Stack Overflow.

If you would like to chat about the question in real-time, you can reach out via [our gitter channel][gitter].
If you would like to chat about the question in real-time, you can reach out via [our discord channel][discord].

## <a name="issue"></a> Found a Bug?
If you find a bug in the source code, you can help us by
Expand Down Expand Up @@ -243,7 +243,7 @@ changes to be accepted, the CLA must be signed. It's a quick process, we promise
[corporate-cla]: http://code.google.com/legal/corporate-cla-v1.0.html
[dev-doc]: https://github.com/nestjs/nest/blob/master/docs/DEVELOPER.md
[github]: https://github.com/nestjs/nest
[gitter]: https://gitter.im/nestjs/nest
[discord]: https://discord.gg/nestjs
[individual-cla]: http://code.google.com/legal/individual-cla-v1.0.html
[js-style-guide]: https://google.github.io/styleguide/jsguide.html
[jsfiddle]: http://jsfiddle.net
Expand Down
11 changes: 6 additions & 5 deletions src/utils/metadata.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,12 @@ export class MetadataManager {
toInsert = staticOptions ? this.addBlankLines(symbol) : `${symbol}`;
} else {
const text = (node as Node).getFullText(source);
if (text.match(/^\r?\n/)) {
toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${symbol}`;
} else {
toInsert = `, ${symbol}`;
}
const itemSeparator = (
text.match(/^\r?\n(\r?)\s+/) ||
text.match(/^\r?\n/) ||
' '
)[0];
toInsert = `,${itemSeparator}${symbol}`;
}
return this.content.split('').reduce((content, char, index) => {
if (index === position) {
Expand Down
45 changes: 45 additions & 0 deletions test/utils/metadata.manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,51 @@ describe('Metadata Manager', () => {
'export class FooModule {}\n'
);
});
it('should insert the symbol to the metadata reusing the line separator', () => {
const lineSeparator = '\r\n'

const manager = new MetadataManager(
'import { Module } from \'@nestjs/common\';\n' +
'\n' +
'@Module({\n' +
` imports: [${lineSeparator}` +
'BarModule]\n' +
'})\n' +
'export class FooModule {}\n'
);
const metadata = 'imports';
const symbol = 'FooModule';
expect(manager.insert(metadata, symbol)).toEqual(
'import { Module } from \'@nestjs/common\';\n' +
'\n' +
'@Module({\n' +
` imports: [${lineSeparator}` +
`BarModule,${lineSeparator}`+
'FooModule]\n' +
'})\n' +
'export class FooModule {}\n'
);
});
it("should insert the symbol to the metadata separated by a blank space when there's no line separator", () => {
const manager = new MetadataManager(
'import { Module } from \'@nestjs/common\';\n' +
'\n' +
'@Module({\n' +
' imports: [BarModule]\n' +
'})\n' +
'export class FooModule {}\n'
);
const metadata = 'imports';
const symbol = 'FooModule';
expect(manager.insert(metadata, symbol)).toEqual(
'import { Module } from \'@nestjs/common\';\n' +
'\n' +
'@Module({\n' +
' imports: [BarModule, FooModule]\n' +
'})\n' +
'export class FooModule {}\n'
);
});
it('should insert the symbol to right metadata', () => {
const metadata = 'imports';
const symbol = 'FooModule';
Expand Down

0 comments on commit 9914ef3

Please sign in to comment.