Skip to content

Commit

Permalink
[Fix] : fix accidental removal of comma in certain cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Apr 6, 2023
1 parent 5680a1f commit 3ef4222
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
6 changes: 3 additions & 3 deletions src/rules/consistent-type-specifier-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
27 changes: 27 additions & 0 deletions tests/src/rules/consistent-type-specifier-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ef4222

Please sign in to comment.