Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] consistent-type-specifier-style: fix accidental removal of comma in certain cases #2754

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 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 ([#2754], thanks [@bradzacher])

### Changed
- [Docs] [`no-duplicates`]: fix example schema ([#2684], thanks [@simmo])
Expand Down Expand Up @@ -1065,6 +1066,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#2754]: https://github.com/import-js/eslint-plugin-import/pull/2754
[#2699]: https://github.com/import-js/eslint-plugin-import/pull/2699
[#2664]: https://github.com/import-js/eslint-plugin-import/pull/2664
[#2613]: https://github.com/import-js/eslint-plugin-import/pull/2613
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