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(no-duplicates): type prefix without new line #206

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 9 additions & 6 deletions src/rules/no-duplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ function getFix(
specifier.identifiers.reduce(
([text, set], cur) => {
const trimmed = cur.trim() // Trim whitespace before/after to compare to our set of existing identifiers
const curWithType =
trimmed.length > 0 && preferInline && isTypeSpecifier
? `type ${cur}`
: cur
if (existingIdentifiers.has(trimmed)) {
if (trimmed.length === 0 || existingIdentifiers.has(trimmed)) {
return [text, set]
}

const curWithType =
preferInline && isTypeSpecifier
? cur.replace(/^(\s*)/, '$1type ')
: cur

return [
text.length > 0 ? `${text},${curWithType}` : curWithType,
set.add(trimmed),
Expand Down Expand Up @@ -267,8 +269,9 @@ function getFix(
)
}
} else if (openBrace != null && closeBrace != null && !shouldAddDefault()) {
const tokenBefore = sourceCode.getTokenBefore(closeBrace)!
// `import {...} './foo'` → `import {..., ...} from './foo'`
fixes.push(fixer.insertTextBefore(closeBrace, specifiersText))
fixes.push(fixer.insertTextAfter(tokenBefore, specifiersText))
}

// Remove imports whose specifiers have been moved into the first import.
Expand Down
51 changes: 41 additions & 10 deletions test/rules/no-duplicates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ruleTester.run('no-duplicates', rule, {
invalid: [
tInvalid({
code: "import { x } from './foo'; import { y } from './foo'",
output: "import { x , y } from './foo'; ",
output: "import { x, y } from './foo'; ",
errors: [createDuplicatedError('./foo'), createDuplicatedError('./foo')],
}),

Expand All @@ -87,7 +87,7 @@ ruleTester.run('no-duplicates', rule, {
// ensure resolved path results in warnings
tInvalid({
code: "import { x } from './bar'; import { y } from 'bar';",
output: "import { x , y } from './bar'; ",
output: "import { x, y } from './bar'; ",
settings: {
'import-x/resolve': {
paths: [path.resolve('test/fixtures')],
Expand Down Expand Up @@ -133,7 +133,7 @@ ruleTester.run('no-duplicates', rule, {

tInvalid({
code: "import type { x } from './foo'; import type { y } from './foo'",
output: "import type { x , y } from './foo'; ",
output: "import type { x, y } from './foo'; ",
languageOptions: { parser: require(parsers.BABEL) },
errors: [createDuplicatedError('./foo'), createDuplicatedError('./foo')],
}),
Expand All @@ -146,7 +146,7 @@ ruleTester.run('no-duplicates', rule, {

tInvalid({
code: "import { x, /* x */ } from './foo'; import {//y\ny//y2\n} from './foo'",
output: "import { x, /* x */ //y\ny//y2\n} from './foo'; ",
output: "import { x,//y\ny//y2\n /* x */ } from './foo'; ",
languageOptions: { parser: require(parsers.ESPREE) },
errors: [createDuplicatedError('./foo'), createDuplicatedError('./foo')],
}),
Expand Down Expand Up @@ -195,7 +195,7 @@ ruleTester.run('no-duplicates', rule, {

tInvalid({
code: "import { } from './foo'; import {x} from './foo'",
output: "import { x} from './foo'; ",
output: "import {x } from './foo'; ",
errors: [createDuplicatedError('./foo'), createDuplicatedError('./foo')],
}),

Expand Down Expand Up @@ -419,7 +419,7 @@ import {x,y} from './foo'
// #2027 long import list generate empty lines
tInvalid({
code: "import { Foo } from './foo';\nimport { Bar } from './foo';\nexport const value = {}",
output: "import { Foo , Bar } from './foo';\nexport const value = {}",
output: "import { Foo, Bar } from './foo';\nexport const value = {}",
errors: [createDuplicatedError('./foo'), createDuplicatedError('./foo')],
}),

Expand Down Expand Up @@ -452,8 +452,8 @@ export default TestComponent;
import {
DEFAULT_FILTER_KEYS,
BULK_DISABLED,

BULK_ACTIONS_ENABLED

} from '../constants';
import React from 'react';

Expand Down Expand Up @@ -493,9 +493,9 @@ export default TestComponent;
${''}
import {
A2,
${''}
B2,
C2} from 'bar';
C2
} from 'bar';
${''}
`,
errors: [
Expand Down Expand Up @@ -822,7 +822,7 @@ describe('TypeScript', () => {
code: "import type { AType as BType } from './foo'; import { CValue } from './foo'",
...parserConfig,
options: [{ 'prefer-inline': true }],
output: `import { type AType as BType , CValue } from './foo'; `,
output: `import { type AType as BType, CValue } from './foo'; `,
errors: [
{
...createDuplicatedError('./foo'),
Expand All @@ -836,6 +836,37 @@ describe('TypeScript', () => {
},
],
}),
tInvalid({
code: `
import {
a
} from './foo';
import type {
b,
c,
} from './foo';`,
...parserConfig,
options: [{ 'prefer-inline': true }],
output: `
import {
a,
type b,
type c
} from './foo';
`,
SukkaW marked this conversation as resolved.
Show resolved Hide resolved
errors: [
{
...createDuplicatedError('./foo'),
line: 4,
column: 20,
},
{
...createDuplicatedError('./foo'),
line: 8,
column: 20,
},
],
}),
]),
]

Expand Down
Loading