Skip to content

Commit 7f535b2

Browse files
committed
one bugfix with trailing commas
1 parent 768fb1c commit 7f535b2

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

src/rules/no-duplicates.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,24 @@ function getInlineTypeFix(nodes, sourceCode) {
9494
return fixer => {
9595
const fixes = [];
9696

97-
// if (!semver.satisfies(typescriptPkg.version, '>= 4.5')) {
98-
// throw new Error('Your version of TypeScript does not support inline type imports.');
99-
// }
100-
10197
// push to first import
10298
let [firstImport, ...rest] = nodes;
103-
const valueImport = nodes.find((n) => n.specifiers.every((spec) => spec.importKind === 'value')) || nodes.find((n) => n.specifiers.some((spec) => spec.type === 'ImportDefaultSpecifier'));
99+
// const valueImport = nodes.find((n) => n.specifiers.every((spec) => spec.importKind === 'value')) || nodes.find((n) => n.specifiers.some((spec) => spec.type === 'ImportDefaultSpecifier'));
100+
const valueImport = nodes.find((n) => n.specifiers.some((spec) => spec.type === 'ImportDefaultSpecifier'));
104101
if (valueImport) {
105102
firstImport = valueImport;
106103
rest = nodes.filter((n) => n !== firstImport);
107104
}
108105

109106
const nodeTokens = sourceCode.getTokens(firstImport);
110107
// we are moving the rest of the Type or Inline Type imports here.
111-
const nodeClosingBrace = nodeTokens.find(token => isPunctuator(token, '}'));
112-
// const preferInline = context.options[0] && context.options[0]['prefer-inline'];
108+
const nodeClosingBraceIndex = nodeTokens.findIndex(token => isPunctuator(token, '}'));
109+
const nodeClosingBrace = nodeTokens[nodeClosingBraceIndex];
110+
const tokenBeforeClosingBrace = nodeTokens[nodeClosingBraceIndex - 1];
113111
if (nodeClosingBrace) {
112+
if (rest.length && isComma(tokenBeforeClosingBrace)) {
113+
fixes.push(fixer.remove(tokenBeforeClosingBrace));
114+
}
114115
rest.forEach((node) => {
115116
// these will be all Type imports, no Value specifiers
116117
// then add inline type specifiers to importKind === 'type' import

tests/src/rules/no-duplicates.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,23 @@ context('TypeScript', function () {
614614
},
615615
],
616616
}),
617+
test({
618+
code: "import {type x} from './foo'; import {y} from './foo'",
619+
...parserConfig,
620+
output: `import {type x, y} from './foo'; `,
621+
errors: [
622+
{
623+
line: 1,
624+
column: 22,
625+
message: "'./foo' imported multiple times.",
626+
},
627+
{
628+
line: 1,
629+
column: 47,
630+
message: "'./foo' imported multiple times.",
631+
},
632+
],
633+
}),
617634
].concat(!tsVersionSatisfies('>= 4.5') || !typescriptEslintParserSatisfies('>= 5.7.0') ? [] : [
618635
// without prefer-inline, will dedupe with type import kind
619636
test({
@@ -1018,20 +1035,34 @@ context('TypeScript', function () {
10181035
},
10191036
],
10201037
}),
1021-
// #2834 Detect duplicates across type and regular imports
10221038
test({
1023-
code: "import {AValue} from './foo'; import type {AType} from './foo'",
1039+
code: "import { type C, } from './foo';import {AValue, BValue, } from './foo';",
10241040
...parserConfig,
10251041
options: [{ 'prefer-inline': true }],
1026-
output: `import {AValue,type AType} from './foo'; `,
1042+
output: "import { type C , AValue, BValue} from './foo';",
10271043
errors: [
10281044
{
10291045
line: 1,
1030-
column: 22,
1046+
column: 25,
10311047
message: "'./foo' imported multiple times.",
10321048
},
10331049
{
10341050
line: 1,
1051+
column: 64,
1052+
message: "'./foo' imported multiple times.",
1053+
}
1054+
],
1055+
}),
1056+
// #2834 Detect duplicates across type and regular imports
1057+
test({
1058+
code: "import {AValue} from './foo'; import type {AType} from './foo'",
1059+
...parserConfig,
1060+
options: [{ 'prefer-inline': true }],
1061+
output: `import {AValue,type AType} from './foo'; `,
1062+
errors: [
1063+
{
1064+
line: 1,
1065+
column: 22,
10351066
column: 56,
10361067
message: "'./foo' imported multiple times.",
10371068
},

0 commit comments

Comments
 (0)