From 0034e6938a95600288c44a10963506c7bd0cb464 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 2 Apr 2022 17:37:51 +0800 Subject: [PATCH] Deprecate `import-index` rule (#1787) Co-authored-by: Sindre Sorhus --- configs/recommended.js | 1 - docs/deprecated-rules.md | 4 + docs/rules/import-index.md | 85 ------------- index.js | 1 + readme.md | 1 - rules/import-index.js | 63 ---------- test/import-index.mjs | 187 ---------------------------- test/run-rules-on-codebase/lint.mjs | 2 - 8 files changed, 5 insertions(+), 339 deletions(-) delete mode 100644 docs/rules/import-index.md delete mode 100644 rules/import-index.js delete mode 100644 test/import-index.mjs diff --git a/configs/recommended.js b/configs/recommended.js index 901dee7985..27dc9d1f22 100644 --- a/configs/recommended.js +++ b/configs/recommended.js @@ -22,7 +22,6 @@ module.exports = { 'unicorn/expiring-todo-comments': 'error', 'unicorn/explicit-length-check': 'error', 'unicorn/filename-case': 'error', - 'unicorn/import-index': 'off', 'unicorn/import-style': 'error', 'unicorn/new-for-builtins': 'error', 'unicorn/no-abusive-eslint-disable': 'error', diff --git a/docs/deprecated-rules.md b/docs/deprecated-rules.md index 48156aefcd..04bea95386 100644 --- a/docs/deprecated-rules.md +++ b/docs/deprecated-rules.md @@ -1,5 +1,9 @@ # Deprecated Rules +## import-index + +This rule is outdated. JavaScript modules (ESM) do not support importing a directory. + ## no-array-instanceof This rule was renamed to [`no-instanceof-array`](rules/no-instanceof-array.md) to be more correct. diff --git a/docs/rules/import-index.md b/docs/rules/import-index.md deleted file mode 100644 index b3b2b712da..0000000000 --- a/docs/rules/import-index.md +++ /dev/null @@ -1,85 +0,0 @@ -# Enforce importing index files with `.` - - - -🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).* - - -Enforces importing index file with `.` instead of `./`, `./index` or `./index.js`. - -## Fail - -```js -const m = require('./'); -``` - -```js -const m = require('../'); -``` - -```js -const m = require('./index'); -``` - -```js -const m = require('./index.js'); -``` - -```js -const m = require('./foo/index.js'); -``` - -```js -const m = require('@foo/bar/index'); -``` - -```js -import m from './'; -``` - -```js -import m from './index'; -``` - -## Pass - -```js -const m = require('.'); -``` - -```js -const m = require('..'); -``` - -```js -const m = require('./foo'); -``` - -```js -const m = require('@foo/bar'); -``` - -```js -import m from '.'; -``` - -## Options - -### ignoreImports - -Type: `boolean`\ -Default: `false` - -Don't check `import` statements. - -Can be useful if you're using native `import` in Node.js where the filename and extension is required. - -```js -// eslint unicorn/import-index: ["error", {"ignoreImports": true}] -import m from './index'; // Passes -``` - -```js -// eslint unicorn/import-index: ["error", {"ignoreImports": false}] -import m from './index'; // Fails -``` diff --git a/index.js b/index.js index 8000a30f8d..1ab1204490 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const allRulesEnabledConfig = require('./configs/all.js'); const deprecatedRules = createDeprecatedRules({ // {ruleId: ReplacementRuleId | ReplacementRuleId[]}, if no replacement, use `{ruleId: []}` + 'import-index': [], 'no-array-instanceof': 'unicorn/no-instanceof-array', 'no-fn-reference-in-iterator': 'unicorn/no-array-callback-reference', 'no-reduce': 'unicorn/no-array-reduce', diff --git a/readme.md b/readme.md index 16689c14fa..214cc60ed8 100644 --- a/readme.md +++ b/readme.md @@ -64,7 +64,6 @@ Each rule has emojis denoting: | [expiring-todo-comments](docs/rules/expiring-todo-comments.md) | Add expiration conditions to TODO comments. | ✅ | | | | [explicit-length-check](docs/rules/explicit-length-check.md) | Enforce explicitly comparing the `length` or `size` property of a value. | ✅ | 🔧 | 💡 | | [filename-case](docs/rules/filename-case.md) | Enforce a case style for filenames. | ✅ | | | -| [import-index](docs/rules/import-index.md) | Enforce importing index files with `.`. | | 🔧 | | | [import-style](docs/rules/import-style.md) | Enforce specific import styles per module. | ✅ | | | | [new-for-builtins](docs/rules/new-for-builtins.md) | Enforce the use of `new` for all builtins, except `String`, `Number`, `Boolean`, `Symbol` and `BigInt`. | ✅ | 🔧 | | | [no-abusive-eslint-disable](docs/rules/no-abusive-eslint-disable.md) | Enforce specifying rules to disable in `eslint-disable` comments. | ✅ | | | diff --git a/rules/import-index.js b/rules/import-index.js deleted file mode 100644 index 90d23d1ee5..0000000000 --- a/rules/import-index.js +++ /dev/null @@ -1,63 +0,0 @@ -'use strict'; -const {STATIC_REQUIRE_SELECTOR} = require('./selectors/index.js'); - -const MESSAGE_ID = 'import-index'; -const messages = { - [MESSAGE_ID]: 'Do not reference the index file directly..', -}; - -const regexp = /^(?@.*?\/.*?|[./]+?.*?)\/(?:\.|(?:index(?:\.js)?))?$/; -const isImportingIndex = value => regexp.test(value); -const normalize = value => value.replace(regexp, '$'); - -const importIndex = (context, node, argument) => { - if (argument && isImportingIndex(argument.value)) { - return { - node, - messageId: MESSAGE_ID, - fix: fixer => fixer.replaceText(argument, `'${normalize(argument.value)}'`), - }; - } -}; - -/** @param {import('eslint').Rule.RuleContext} context */ -const create = context => { - const options = context.options[0] || {}; - - const rules = { - [STATIC_REQUIRE_SELECTOR]: node => importIndex(context, node, node.arguments[0]), - }; - - if (!options.ignoreImports) { - rules.ImportDeclaration = node => importIndex(context, node, node.source); - } - - return rules; -}; - -const schema = [ - { - type: 'object', - additionalProperties: false, - properties: { - ignoreImports: { - type: 'boolean', - default: false, - }, - }, - }, -]; - -/** @type {import('eslint').Rule.RuleModule} */ -module.exports = { - create, - meta: { - type: 'suggestion', - docs: { - description: 'Enforce importing index files with `.`.', - }, - fixable: 'code', - schema, - messages, - }, -}; diff --git a/test/import-index.mjs b/test/import-index.mjs deleted file mode 100644 index 8c90485ae1..0000000000 --- a/test/import-index.mjs +++ /dev/null @@ -1,187 +0,0 @@ -import {getTester} from './utils/test.mjs'; - -const {test} = getTester(import.meta); - -const error = { - messageId: 'import-index', -}; - -const ignoreImportsOptions = [ - { - ignoreImports: true, - }, -]; - -test({ - valid: [ - 'const m = require()', - 'const m = require(\'.\')', - { - code: 'const m = require(\'.\')', - options: ignoreImportsOptions, - }, - 'const m = require(\'..\')', - 'const m = require(\'../..\')', - 'const m = require(\'./foobar\')', - 'const m = require(\'foobar\')', - 'const m = require(\'index\')', - 'const m = require(\'index.js\')', - 'const m = require(\'indexbar\')', - 'const m = require(\'fooindex\')', - 'const m = require(\'@index/foo\')', - 'const m = require(\'@foo/index\')', - 'const m = require(\'@foo/index.js\')', - 'import m from \'.\'', - 'import m from \'..\'', - 'import m from \'../..\'', - 'import m from \'./foobar\'', - 'import m from \'foobar\'', - 'import m from \'index\'', - 'import m from \'index.js\'', - { - code: 'import m from \'index.js\'', - options: ignoreImportsOptions, - }, - 'import m from \'indexbar\'', - 'import m from \'fooindex\'', - 'import m from \'@index/foo\'', - 'import m from \'@foo/index\'', - 'import m from \'@foo/index.js\'', - { - code: 'import m from \'./\'', - options: ignoreImportsOptions, - }, - { - code: 'import m from \'./index\'', - options: ignoreImportsOptions, - }, - { - code: 'import m from \'./index.js\'', - options: ignoreImportsOptions, - }, - { - code: 'import m from \'../../index\'', - options: ignoreImportsOptions, - }, - { - code: 'import m from \'./foo/index.js\'', - options: ignoreImportsOptions, - }, - { - code: 'import m from \'./foobar/\'', - options: ignoreImportsOptions, - }, - { - code: 'import m from \'@foo/index/index\'', - options: ignoreImportsOptions, - }, - { - code: 'import m from \'@foo/index/index.js\'', - options: ignoreImportsOptions, - }, - ], - invalid: [ - { - code: 'const m = require(\'./\')', - errors: [error], - output: 'const m = require(\'.\')', - }, - { - code: 'const m = require(\'./.\')', - errors: [error], - output: 'const m = require(\'.\')', - }, - { - code: 'const m = require(\'./index\')', - errors: [error], - output: 'const m = require(\'.\')', - }, - { - code: 'const m = require(\'./index\')', - errors: [error], - output: 'const m = require(\'.\')', - options: ignoreImportsOptions, - }, - { - code: 'const m = require(\'./index.js\')', - errors: [error], - output: 'const m = require(\'.\')', - }, - { - code: 'const m = require(\'./index.js\')', - errors: [error], - output: 'const m = require(\'.\')', - options: ignoreImportsOptions, - }, - { - code: 'const m = require(\'../../index.js\')', - errors: [error], - output: 'const m = require(\'../..\')', - }, - { - code: 'const m = require(\'../.\')', - errors: [error], - output: 'const m = require(\'..\')', - }, - { - code: 'const m = require(\'./foo/index.js\')', - errors: [error], - output: 'const m = require(\'./foo\')', - }, - { - code: 'const m = require(\'./foobar/\')', - errors: [error], - output: 'const m = require(\'./foobar\')', - }, - { - code: 'const m = require(\'@foo/index/index\')', - errors: [error], - output: 'const m = require(\'@foo/index\')', - }, - { - code: 'const m = require(\'@foo/index/index.js\')', - errors: [error], - output: 'const m = require(\'@foo/index\')', - }, - { - code: 'import m from \'./\'', - errors: [error], - output: 'import m from \'.\'', - }, - { - code: 'import m from \'./index\'', - errors: [error], - output: 'import m from \'.\'', - }, - { - code: 'import m from \'./index.js\'', - errors: [error], - output: 'import m from \'.\'', - }, - { - code: 'import m from \'../../index\'', - errors: [error], - output: 'import m from \'../..\'', - }, - { - code: 'import m from \'./foo/index.js\'', - errors: [error], - output: 'import m from \'./foo\'', - }, - { - code: 'import m from \'./foobar/\'', - errors: [error], - output: 'import m from \'./foobar\'', - }, - { - code: 'import m from \'@foo/index/index\'', - errors: [error], - output: 'import m from \'@foo/index\'', - }, - { - code: 'import m from \'@foo/index/index.js\'', - errors: [error], - output: 'import m from \'@foo/index\'', - }, - ], -}); diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index c3b84d70fa..81aa37a28c 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -29,8 +29,6 @@ const eslint = new ESLint({ // Annoying 'unicorn/no-keyword-prefix': 'off', 'unicorn/no-unsafe-regex': 'off', - // Outdated - 'unicorn/import-index': 'off', // Not ready yet 'unicorn/prefer-string-replace-all': 'off', 'unicorn/prefer-top-level-await': 'off',