From 211f2952b651daf72af5a0377a84855c44c319c6 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sat, 2 Jan 2021 01:32:40 +0700 Subject: [PATCH] Improve ESM compatibility Fixes #386 --- config/plugins.js | 31 ++++++++++-------------- package.json | 5 +++- test/fixtures/overrides/test/bar.js | 2 +- test/fixtures/overrides/test/foo.js | 2 +- test/fixtures/webpack/no-config/file3.js | 2 +- test/lint-files.js | 2 +- 6 files changed, 21 insertions(+), 23 deletions(-) diff --git a/config/plugins.js b/config/plugins.js index 008b1b73..9d2a56cd 100644 --- a/config/plugins.js +++ b/config/plugins.js @@ -152,6 +152,9 @@ module.exports = { } ], + // TODO: Remove this when https://github.com/sindresorhus/eslint-plugin-unicorn/pull/977 is out. + 'unicorn/import-index': 'off', + // TODO: Disabled for now as I don't have time to deal with the backslash that might come from this. Try to enable this rule in 2021. 'unicorn/no-null': 'off', @@ -179,20 +182,17 @@ module.exports = { 'import/export': 'error', 'import/extensions': [ 'error', + 'always', { - js: 'never', - jsx: 'never', - json: 'always', - svg: 'always', - css: 'always' + ignorePackages: true } ], - // Disabled as it causes problems with TypeScript when you use mixed ESM and CommonJS - // TODO: Enable again when I target only ESM + // Disabled as it causes problems with TypeScript when you use mixed ESM and CommonJS. + // TODO: Enable again when I target only ESM. // 'import/first': 'error', - // Disabled as it doesn't work with TypeScript + // Disabled as it doesn't work with TypeScript. // This issue and some others: https://github.com/benmosher/eslint-plugin-import/issues/1341 // 'import/named': 'error', @@ -211,12 +211,7 @@ module.exports = { // Enable this sometime in the future when Node.js has ES2015 module support // 'import/no-cycle': 'error' - 'import/no-useless-path-segments': [ - 'error', - { - noUselessIndex: true - } - ], + 'import/no-useless-path-segments': 'error', // Disabled as it doesn't work with TypeScript // 'import/newline-after-import': 'error', @@ -242,7 +237,7 @@ module.exports = { // 'import/no-unresolved': [ // 'error', // { - // commonjs: true + // commonjs: false // } // ], @@ -264,11 +259,11 @@ module.exports = { } ], - // Redundant with import/no-extraneous-dependencies + // Redundant with `import/no-extraneous-dependencies`. // 'node/no-extraneous-import': 'error', // 'node/no-extraneous-require': 'error', - // Redundant with import/no-unresolved + // Redundant with `import/no-unresolved`. // 'node/no-missing-import': 'error', // 'node/no-missing-require': 'error', @@ -357,7 +352,7 @@ module.exports = { 'eslint-comments/no-aggregating-enable': 'error', 'eslint-comments/no-duplicate-disable': 'error', - // Disabled as it's already covered by the `unicorn/no-abusive-eslint-disable` rule + // Disabled as it's already covered by the `unicorn/no-abusive-eslint-disable` rule. // 'eslint-comments/no-unlimited-disable': 'error', 'eslint-comments/no-unused-disable': 'error', diff --git a/package.json b/package.json index ee826f19..27de5d5e 100644 --- a/package.json +++ b/package.json @@ -109,7 +109,10 @@ "webpack": "^5.10.0" }, "eslintConfig": { - "extends": "eslint-config-xo" + "extends": "eslint-config-xo", + "rules": { + "unicorn/import-index": "off" + } }, "eslintIgnore": [ "test/fixtures" diff --git a/test/fixtures/overrides/test/bar.js b/test/fixtures/overrides/test/bar.js index 85f458aa..aa059fbf 100644 --- a/test/fixtures/overrides/test/bar.js +++ b/test/fixtures/overrides/test/bar.js @@ -1,5 +1,5 @@ import test from 'ava' -import fn from '..' +import fn from '../index.js' test('main', t => { t.is(fn('foo'), fn('foobar')) diff --git a/test/fixtures/overrides/test/foo.js b/test/fixtures/overrides/test/foo.js index c3a4f108..19efccc0 100644 --- a/test/fixtures/overrides/test/foo.js +++ b/test/fixtures/overrides/test/foo.js @@ -1,5 +1,5 @@ import test from 'ava' -import fn from '..' +import fn from '../index.js' test('main', t => { t.is(fn('foo'), fn('foobar')) diff --git a/test/fixtures/webpack/no-config/file3.js b/test/fixtures/webpack/no-config/file3.js index 49b9264f..3e126e4f 100644 --- a/test/fixtures/webpack/no-config/file3.js +++ b/test/fixtures/webpack/no-config/file3.js @@ -1 +1 @@ -import _ from '!./file2'; // eslint-disable-line no-unused-vars +import _ from '!./file2.js'; // eslint-disable-line no-unused-vars diff --git a/test/lint-files.js b/test/lint-files.js index 3459c2b0..fb70184c 100644 --- a/test/lint-files.js +++ b/test/lint-files.js @@ -260,7 +260,7 @@ test('webpack import resolver is used if {webpack: true}', async t => { } }); - t.is(results[0].errorCount, 0); + t.is(results[0].errorCount, 0, JSON.stringify(results[0])); }); async function configType(t, {dir}) {