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: 🐛 Fixed case with empty separator added #52

Merged
merged 1 commit into from
Feb 11, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion __tests__/optimize-imports-mocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type TestCase = { input: string; expected: string };
export type TestCase = { input: string; expected: string; noOfRuns?: number };

export const readmeExample: TestCase = {
input: `import fs from 'fs';
Expand Down Expand Up @@ -97,3 +97,17 @@ if (environment.production) {
enableProdMode();
}`,
};

export const emptyNewLineSeparator: TestCase = {
noOfRuns: 2,
input: `import { Component, HostListener } from '@angular/core';
import { Observable } from 'rxjs';
import { MatDialogRef } from '@angular/material/dialog';


import { AboutDialogBloc, AboutState } from './about-dialog.bloc';`,
expected: `import { Component, HostListener } from '@angular/core';
import { Observable } from 'rxjs';
import { MatDialogRef } from '@angular/material/dialog';
import { AboutDialogBloc, AboutState } from './about-dialog.bloc';`,
};
20 changes: 16 additions & 4 deletions __tests__/optimize-imports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { actions, optimizeImports } from '@ic/conductor/optimize-imports';
import * as config from '@ic/config';
import fs from 'fs';
import { Config } from '@ic/types';
import { readmeExample, comments, TestCase, codeBetweenImports } from './optimize-imports-mocks';
import { readmeExample, comments, TestCase, codeBetweenImports, emptyNewLineSeparator } from './optimize-imports-mocks';
import { defaultConfig } from '@ic/defaultConfig';

jest.mock('fs');
Expand All @@ -15,16 +15,23 @@ describe('optimizeImports', () => {
thirdPartyDependencies: new Set<string>(['@angular/core', 'rxjs']),
};

let spy: jasmine.Spy;
beforeEach(() => {
spyOn(config, 'getConfig').and.returnValue(basicConfig);
spy = spyOn(config, 'getConfig');
spy.and.returnValue(basicConfig);
(fs.existsSync as any).mockReturnValue(true);
(fs.writeFileSync as any).mockClear();
});

async function assertConductor({ expected, input }: TestCase) {
async function assertConductor({ expected, input, noOfRuns }: TestCase) {
(fs.readFileSync as any).mockReturnValue(Buffer.from(input));
let noOfRun = noOfRuns ?? 1;
const file = 'test.ts';
const result = await optimizeImports(file);
let result: string;
do {
result = await optimizeImports(file);
} while (--noOfRun > 0);

expect(fs.writeFileSync).toHaveBeenCalledWith(file, expected);

return result;
Expand All @@ -42,6 +49,11 @@ describe('optimizeImports', () => {
await assertConductor(codeBetweenImports);
});

it('should work empty new line separator', async () => {
spy.and.returnValue({ ...basicConfig, separator: '' });
await assertConductor(emptyNewLineSeparator);
});

it('should not change conducted file', async () => {
(fs.readFileSync as any).mockReturnValue(Buffer.from(readmeExample.expected));
const file = 'test.ts';
Expand Down
12 changes: 11 additions & 1 deletion src/conductor/format-import-statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ function hasImports([, imports]: CategoryEntry) {
}

function toImportBlock([, imports]: CategoryEntry) {
return [...imports.values()].join('\n');
return [...imports.values()].map((l) => trim(l, ' \n')).join('\n');
}

function escapeRegex(string: string) {
return string.replace(/[\[\](){}?*+\^$\\.|\-]/g, '\\$&');
}

function trim(input: string, characters: string) {
characters = escapeRegex(characters);

return input.replace(new RegExp('^[' + characters + ']+|[' + characters + ']+$', 'g'), '');
}