Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ function getFix(first, rest, sourceCode, context) {
// Add *only* the new identifiers that don't already exist, and track any new identifiers so we don't add them again in the next loop
const [specifierText, updatedExistingIdentifiers] = 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;
const hasLineComment = (/\/\/[^\n]*$/).test(trimmed);
let curWithType;
if (trimmed.length > 0 && preferInline && isTypeSpecifier) {
curWithType = `type ${trimmed}`;
} else if (hasLineComment && trimmed.length > 0) {
curWithType = `${trimmed}\n`;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the newlines? that's not behavior the rule has had before.

} else {
curWithType = trimmed;
}
if (existingIdentifiers.has(trimmed)) {
return [text, set];
}
Expand Down
45 changes: 11 additions & 34 deletions tests/src/rules/no-duplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ ruleTester.run('no-duplicates', rule, {
invalid: [
test({
code: "import { x } from './foo'; import { y } from './foo'",
output: "import { x , y } from './foo'; ",
output: "import { x ,y} from './foo'; ",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems wrong to me - either both x and y should have spaces between them and the closest curly brace, or neither should.

errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),

test({
code: "import {x} from './foo'; import {y} from './foo'; import { z } from './foo'",
output: "import {x,y, z } from './foo'; ",
output: "import {x,y,z} from './foo'; ",
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),

// ensure resolved path results in warnings
test({
code: "import { x } from './bar'; import { y } from 'bar';",
output: "import { x , y } from './bar'; ",
output: "import { x ,y} from './bar'; ",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here

settings: {
'import/resolve': {
paths: [path.join(process.cwd(), 'tests', 'files')],
Expand Down Expand Up @@ -107,7 +107,7 @@ ruleTester.run('no-duplicates', rule, {

test({
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'; ",
parser: parsers.BABEL_OLD,
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),
Expand Down Expand Up @@ -145,15 +145,15 @@ ruleTester.run('no-duplicates', rule, {
// #2347: duplicate identifiers should be removed
test({
code: "import {a,b} from './foo'; import { b, c } from './foo'; import {b,c,d} from './foo'",
output: "import {a,b, c ,d} from './foo'; ",
output: "import {a,b,c,d} from './foo'; ",
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
parser,
}),

// #2347: duplicate identifiers should be removed, but not if they are adjacent to comments
test({
code: "import {a} from './foo'; import { a/*,b*/ } from './foo'",
output: "import {a, a/*,b*/ } from './foo'; ",
output: "import {a,a/*,b*/} from './foo'; ",
errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
parser,
}),
Expand Down Expand Up @@ -186,7 +186,8 @@ ruleTester.run('no-duplicates', rule, {

test({
code: "import './foo'; import { /*x*/} from './foo'; import {//y\n} from './foo'; import {z} from './foo'",
output: "import { /*x*///y\nz} from './foo'; ",
output: "import {/*x*///y\nz} from './foo'; ",

errors: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),

Expand Down Expand Up @@ -373,7 +374,7 @@ import {x,y} from './foo'
// #2027 long import list generate empty lines
test({
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: ['\'./foo\' imported multiple times.', '\'./foo\' imported multiple times.'],
}),

Expand Down Expand Up @@ -402,22 +403,7 @@ import {x,y} from './foo'
${''}
export default TestComponent;
`,
output: `
import {
DEFAULT_FILTER_KEYS,
BULK_DISABLED,
${''}
BULK_ACTIONS_ENABLED
} from '../constants';
import React from 'react';
${''}
const TestComponent = () => {
return <div>
</div>;
}
${''}
export default TestComponent;
`,
output: `\n import {\n DEFAULT_FILTER_KEYS,\n BULK_DISABLED,\n BULK_ACTIONS_ENABLED} from '../constants';\n import React from 'react';\n ${''}\n const TestComponent = () => {\n return <div>\n </div>;\n }\n ${''}\n export default TestComponent;\n `,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm confused, this now has a newline after the { but not one before the }? this seems unrelated to the purpose of the PR.

errors: ["'../constants' imported multiple times.", "'../constants' imported multiple times."],
...jsxConfig,
}),
Expand All @@ -439,16 +425,7 @@ import {x,y} from './foo'
} from 'bar';

`,
output: `
import {A1,B1,C1} from 'foo';
${''}
import {
A2,
${''}
B2,
C2} from 'bar';
${''}
`,
output: `\n import {A1,B1,C1} from 'foo';\n ${''}\n import {\n A2,\n B2,C2} from 'bar';\n ${''}\n `,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question here

errors: [
{
message: "'foo' imported multiple times.",
Expand Down
Loading