diff --git a/CHANGELOG.md b/CHANGELOG.md index c16d2c9ffa..5109ebb8d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange ### Fixed - [`no-duplicates`]: remove duplicate identifiers in duplicate imports ([#2577], thanks [@joe-matsec]) - TypeScript config: fix resolver extension settings (thanks [@gajus]) +- [`consistent-type-specifier-style`]: fix accidental removal of comma in certain cases (thanks [@bradzacher]) ### Changed - [Docs] [`no-duplicates`]: fix example schema ([#2684], thanks [@simmo]) diff --git a/src/rules/consistent-type-specifier-style.js b/src/rules/consistent-type-specifier-style.js index 869eea91ff..bb8fdf8498 100644 --- a/src/rules/consistent-type-specifier-style.js +++ b/src/rules/consistent-type-specifier-style.js @@ -7,9 +7,9 @@ function isComma(token) { function removeSpecifiers(fixes, fixer, sourceCode, specifiers) { for (const specifier of specifiers) { // remove the trailing comma - const comma = sourceCode.getTokenAfter(specifier, isComma); - if (comma) { - fixes.push(fixer.remove(comma)); + const token = sourceCode.getTokenAfter(specifier); + if (token && isComma(token)) { + fixes.push(fixer.remove(token)); } fixes.push(fixer.remove(specifier)); } diff --git a/tests/src/rules/consistent-type-specifier-style.js b/tests/src/rules/consistent-type-specifier-style.js index 440ef3aff5..7799238c32 100644 --- a/tests/src/rules/consistent-type-specifier-style.js +++ b/tests/src/rules/consistent-type-specifier-style.js @@ -168,6 +168,33 @@ const COMMON_TESTS = { type: 'ImportSpecifier', }], }, + // https://github.com/import-js/eslint-plugin-import/issues/2753 + { + code: `\ +import { Component, type ComponentProps } from "package-1"; +import { + Component1, + Component2, + Component3, + Component4, + Component5, +} from "package-2";`, + output: `\ +import { Component } from "package-1"; +import type {ComponentProps} from "package-1"; +import { + Component1, + Component2, + Component3, + Component4, + Component5, +} from "package-2";`, + options: ['prefer-top-level'], + errors: [{ + message: 'Prefer using a top-level type-only import instead of inline type specifiers.', + type: 'ImportSpecifier', + }], + }, // // prefer-inline