From afab74b1ed5f3705d75a82135a1b9c95a8a56da6 Mon Sep 17 00:00:00 2001 From: Daniil Dubrava Date: Tue, 25 Oct 2022 15:31:52 +0300 Subject: [PATCH] feat(eslint-config): set import/no-cycle to error (#206) * feat(eslint-config): set import/no-cycle to error * test(eslint-config): add import-happy & import-unhappy tests --- packages/eslint-config/internal/import.js | 2 +- .../test/import/__fixtures__/dep-a.js | 1 + .../test/import/__fixtures__/dep-b.js | 3 +++ .../__fixtures__/import-happy.fixture.js | 3 +++ .../__fixtures__/import-unhappy.fixture.js | 3 +++ .../__snapshots__/import-happy.test.js.snap | 3 +++ .../__snapshots__/import-unhappy.test.js.snap | 13 +++++++++ .../test/import/import-happy.test.js | 27 +++++++++++++++++++ .../test/import/import-unhappy.test.js | 27 +++++++++++++++++++ 9 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 packages/eslint-config/test/import/__fixtures__/dep-a.js create mode 100644 packages/eslint-config/test/import/__fixtures__/dep-b.js create mode 100644 packages/eslint-config/test/import/__fixtures__/import-happy.fixture.js create mode 100644 packages/eslint-config/test/import/__fixtures__/import-unhappy.fixture.js create mode 100644 packages/eslint-config/test/import/__snapshots__/import-happy.test.js.snap create mode 100644 packages/eslint-config/test/import/__snapshots__/import-unhappy.test.js.snap create mode 100644 packages/eslint-config/test/import/import-happy.test.js create mode 100644 packages/eslint-config/test/import/import-unhappy.test.js diff --git a/packages/eslint-config/internal/import.js b/packages/eslint-config/internal/import.js index 6da5c68..82c49b9 100644 --- a/packages/eslint-config/internal/import.js +++ b/packages/eslint-config/internal/import.js @@ -29,6 +29,6 @@ module.exports = { ], 'import/order': ['warn', { groups: [['builtin', 'external', 'internal']] }], 'import/no-extraneous-dependencies': 'off', // need fine tuning - 'import/no-cycle': 'off', + 'import/no-cycle': 'error', }, }; diff --git a/packages/eslint-config/test/import/__fixtures__/dep-a.js b/packages/eslint-config/test/import/__fixtures__/dep-a.js new file mode 100644 index 0000000..fa1736f --- /dev/null +++ b/packages/eslint-config/test/import/__fixtures__/dep-a.js @@ -0,0 +1 @@ +export function a() {} diff --git a/packages/eslint-config/test/import/__fixtures__/dep-b.js b/packages/eslint-config/test/import/__fixtures__/dep-b.js new file mode 100644 index 0000000..cc3f660 --- /dev/null +++ b/packages/eslint-config/test/import/__fixtures__/dep-b.js @@ -0,0 +1,3 @@ +import './import-unhappy.fixture'; + +export function b() {} diff --git a/packages/eslint-config/test/import/__fixtures__/import-happy.fixture.js b/packages/eslint-config/test/import/__fixtures__/import-happy.fixture.js new file mode 100644 index 0000000..ef3d489 --- /dev/null +++ b/packages/eslint-config/test/import/__fixtures__/import-happy.fixture.js @@ -0,0 +1,3 @@ +import { a } from './dep-a'; + +a(); diff --git a/packages/eslint-config/test/import/__fixtures__/import-unhappy.fixture.js b/packages/eslint-config/test/import/__fixtures__/import-unhappy.fixture.js new file mode 100644 index 0000000..1d229f2 --- /dev/null +++ b/packages/eslint-config/test/import/__fixtures__/import-unhappy.fixture.js @@ -0,0 +1,3 @@ +import { b } from './dep-b'; + +b(); diff --git a/packages/eslint-config/test/import/__snapshots__/import-happy.test.js.snap b/packages/eslint-config/test/import/__snapshots__/import-happy.test.js.snap new file mode 100644 index 0000000..546a2a8 --- /dev/null +++ b/packages/eslint-config/test/import/__snapshots__/import-happy.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`import / happy path happy 1`] = `""`; diff --git a/packages/eslint-config/test/import/__snapshots__/import-unhappy.test.js.snap b/packages/eslint-config/test/import/__snapshots__/import-unhappy.test.js.snap new file mode 100644 index 0000000..3a8c707 --- /dev/null +++ b/packages/eslint-config/test/import/__snapshots__/import-unhappy.test.js.snap @@ -0,0 +1,13 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`import / unhappy path unhappy 1`] = ` +"error: Dependency cycle detected (import/no-cycle) at packages/eslint-config/test/import/__fixtures__/import-unhappy.fixture.js:1:1: +> 1 | import { b } from './dep-b'; + | ^ + 2 | + 3 | b(); + 4 | + + +1 error found." +`; diff --git a/packages/eslint-config/test/import/import-happy.test.js b/packages/eslint-config/test/import/import-happy.test.js new file mode 100644 index 0000000..45dd928 --- /dev/null +++ b/packages/eslint-config/test/import/import-happy.test.js @@ -0,0 +1,27 @@ +import ESlint from 'eslint'; +import path from 'path'; + +describe('import / happy path', () => { + const cli = new ESlint.CLIEngine({ + cwd: path.join(__dirname, '..'), + useEslintrc: false, + baseConfig: { + extends: ['../internal/import'], + parserOptions: { + sourceType: 'module', + }, + env: { + es6: true, + }, + }, + }); + + it('happy', () => { + const codeframe = cli.getFormatter('codeframe'); + const report = cli.executeOnFiles([ + path.join(__dirname, './__fixtures__/import-happy.fixture.js'), + ]); + + expect(codeframe(report.results)).toMatchSnapshot(); + }); +}); diff --git a/packages/eslint-config/test/import/import-unhappy.test.js b/packages/eslint-config/test/import/import-unhappy.test.js new file mode 100644 index 0000000..a3c2c1f --- /dev/null +++ b/packages/eslint-config/test/import/import-unhappy.test.js @@ -0,0 +1,27 @@ +import ESlint from 'eslint'; +import path from 'path'; + +describe('import / unhappy path', () => { + const cli = new ESlint.CLIEngine({ + cwd: path.join(__dirname, '..'), + useEslintrc: false, + baseConfig: { + extends: ['../internal/import'], + parserOptions: { + sourceType: 'module', + }, + env: { + es6: true, + }, + }, + }); + + it('unhappy', () => { + const codeframe = cli.getFormatter('codeframe'); + const report = cli.executeOnFiles([ + path.join(__dirname, './__fixtures__/import-unhappy.fixture.js'), + ]); + + expect(codeframe(report.results)).toMatchSnapshot(); + }); +});