Skip to content

Commit

Permalink
fixup! feat(linter): automatic fixes for noRelativeOrAbsoluteImportsA…
Browse files Browse the repository at this point in the history
…crossLibraries and noSelfCircularDependencies
  • Loading branch information
juristr committed Feb 9, 2022
1 parent 043b333 commit 0690d25
Showing 1 changed file with 150 additions and 61 deletions.
211 changes: 150 additions & 61 deletions e2e/linter/src/linter.test.ts
Expand Up @@ -261,9 +261,9 @@ describe('Linter', () => {
}, 1000000);

describe('workspace boundary rules', () => {
const libA = uniq('lib-a');
const libB = uniq('lib-b');
const libC = uniq('lib-c');
const libA = uniq('tslib-a');
const libB = uniq('tslib-b');
const libC = uniq('tslib-c');
let projScope;

beforeAll(() => {
Expand All @@ -272,79 +272,168 @@ describe('Linter', () => {
runCLI(`generate @nrwl/workspace:lib ${libB}`);
runCLI(`generate @nrwl/workspace:lib ${libC}`);

// updateFile(`apps/${myapp}/src/main.ts`, `console.log("should fail");`);
});
/**
* create tslib-a structure
*/
createFile(
`libs/${libA}/src/lib/tslib-a.ts`,
`
export function libASayHello(): string {
return 'Hi from tslib-a';
}
`
);

xdescribe('aaashould autofix noSelfCircularDependencies', () => {
beforeAll(() => {
/*
import { func1, func2 } from '@scope/same-lib';
createFile(
`libs/${libA}/src/lib/some-non-exported-function.ts`,
`
export function someNonPublicLibFunction() {
return 'this function is exported, but not via the libs barrel file';
}
export function someSelectivelyExportedFn() {
return 'this fn is exported selectively in the barrel file';
}
`
);

createFile(
`libs/${libA}/src/index.ts`,
`
export * from './lib/tslib-a';
should be transformed into
export { someSelectivelyExportedFn } from './lib/some-non-exported-function';
`
);

import { func1 } from './func1';
import { func2 } from './func2';
*/
/**
* create tslib-b structure
*/
createFile(
`libs/${libB}/src/index.ts`,
`
export * from './lib/tslib-b';
`
);

createFile(
`libs/${libC}/src/lib/another-func.ts`,
`
export function anotherFunc() {
return 'hi';
}
`
);

updateFile(
`libs/${libC}/src/lib/index.ts`,
`
export * from './lib/${names(libC).fileName}';
export * from './lib/another-func';
createFile(
`libs/${libB}/src/lib/tslib-b.ts`,
`
);
import { libASayHello } from '@${projScope}/tslib-a';
// import { someNonPublicLibFunction } from '../../../tslib-a/src/lib/some-non-exported-function';
import { someSelectivelyExportedFn } from '@${projScope}/@rabobank/tslib-a';
export function tslibB(): string {
someNonPublicLibFunction();
someSelectivelyExportedFn();
libASayHello();
return 'hi there';
}
`
);

createFile(
`libs/${libC}/src/lib/lib-c-another.ts`,
`
import { ${
names(libC).propertyName
}, anotherFunc } from '@${projScope}/${libC}';
/**
* create tslib-c structure
*/

createFile(
`libs/${libC}/src/index.ts`,
`
export * from './lib/tslib-c';
export * from './lib/constant';
`
);

createFile(
`libs/${libC}/src/lib/constant.ts`,
`
export const SOME_CONSTANT = 'some constant value';
export const someFunc1 = () => 'hi';
export function someFunc2() {
return 'hi2';
}
`
);

createFile(
`libs/${libC}/src/lib/tslib-c-another.ts`,
`
import { tslibC } from './tslib-c';
import { SOME_CONSTANT, someFunc1, someFunc2 } from './constant';
export function someStuff() {
anotherFunc();
return ${names(libC).propertyName}();
someFunc1();
someFunc2();
tslibC();
console.log(SOME_CONSTANT);
return 'hi';
}
`
);

createFile(
`libs/${libC}/src/lib/tslib-c.ts`,
`
);
import { someFunc1, someFunc2, SOME_CONSTANT } from './constant';
// scenario 2
export function tslibC(): string {
someFunc1();
someFunc2();
console.log(SOME_CONSTANT);
return 'tslib-c';
}
`
);
});

it('should fix noSelfCircularDependencies', () => {
const stdout = runCLI(`lint ${libC}`, {
silenceError: true,
});
expect(stdout).toContain(
'Projects should use relative imports to import from other files within the same project'
);

it('should fix a circular self reference', () => {
const stdout = runCLI(`lint ${libC}`, {
silenceError: true,
});
expect(stdout).toContain(
'Projects should use relative imports to import from other files within the same project'
);
// fix them
const fixedStout = runCLI(`lint ${libC} --fix`, {
silenceError: true,
});
expect(fixedStout).toContain('Successfully ran target lint for project');

const fileContent = readFile(`libs/${libC}/src/lib/tslib-c-another.ts`);
expect(fileContent).toContain(`
import { tslibC } from './tslib-c';
import { SOME_CONSTANT, someFunc1, someFunc2 } from './constant';
`);

const fileContentTslibC = readFile(`libs/${libC}/src/lib/tslib-c.ts`);
expect(fileContentTslibC).toContain(`
import { someFunc1, someFunc2, SOME_CONSTANT } from './constant';
`);
});

// fix them
const fixedStout = runCLI(`lint ${libC} --fix`, {
silenceError: true,
});
expect(fixedStout).not.toContain(
'Projects should use relative imports to import from other files within the same project'
);
const fileContent = readFile(`libs/${libC}/src/lib/lib-c-another.ts`);
expect(fileContent).toContain(
`import { ${names(libC).propertyName} } from './${
names(libC).fileName
}';`
);
expect(fileContent).toContain(
`import { anotherFunc } from './another-func';`
);
it('should fix noRelativeOrAbsoluteImportsAcrossLibraries', () => {
const stdout = runCLI(`lint ${libB}`, {
silenceError: true,
});
expect(stdout).toContain(
'Projects cannot be imported by a relative or absolute path, and must begin with a npm scope'
);

// fix them
const fixedStout = runCLI(`lint ${libB} --fix`, {
silenceError: true,
});
expect(fixedStout).toContain('Successfully ran target lint for project');

const fileContent = readFile(`libs/${libB}/src/lib/tslib-b.ts`);
expect(fileContent).toContain(`
import { libASayHello } from '@${projScope}/tslib-a';
`);
expect(fileContent).toContain(`
import { someSelectivelyExportedFn } from '@${projScope}/tslib-a';
`);
});
});
});
Expand Down

0 comments on commit 0690d25

Please sign in to comment.