Skip to content

Commit 3025495

Browse files
committed
one bugfix with trailing commas
1 parent 806a0f8 commit 3025495

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/rules/no-duplicates.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,24 @@ function getInlineTypeFix(nodes, sourceCode) {
9292
return fixer => {
9393
const fixes = [];
9494

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

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

tests/src/rules/no-duplicates.js

+35
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,6 +1035,24 @@ context('TypeScript', function () {
10181035
},
10191036
],
10201037
}),
1038+
test({
1039+
code: "import { type C, } from './foo';import {AValue, BValue, } from './foo';",
1040+
...parserConfig,
1041+
options: [{ 'prefer-inline': true }],
1042+
output: "import { type C , AValue, BValue} from './foo';",
1043+
errors: [
1044+
{
1045+
line: 1,
1046+
column: 25,
1047+
message: "'./foo' imported multiple times.",
1048+
},
1049+
{
1050+
line: 1,
1051+
column: 64,
1052+
message: "'./foo' imported multiple times.",
1053+
},
1054+
],
1055+
}),
10211056
]);
10221057

10231058
ruleTester.run('no-duplicates', rule, {

0 commit comments

Comments
 (0)