Skip to content

fix(no-duplicate): fix no-duplicate false positive on side-effect + type imports #3194

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

todor-a
Copy link

@todor-a todor-a commented Jun 25, 2025

Fixes #3130.

Comment on lines +269 to +276
const typeImports = nodes.filter((node) => node.importKind === 'type');
const sideEffectImports = nodes.filter((node) => node.specifiers.length === 0);
const valueImports = nodes.filter((node) => !typeImports.includes(node) && !sideEffectImports.includes(node));

if (typeImports.length > 0 && sideEffectImports.length > 0 && valueImports.length === 0) {
continue;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

the way this is written, it is literally always true, because nodes.length is > 1, and everything in it is in one of the three groups (since the "value" condition is an "else"). is that intentional?

Copy link
Author

Choose a reason for hiding this comment

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

(since the "value" condition is an "else")

not sure i got this. my thinking was - if there are duplicates and one of them is a type import, while the other is a side effect, do not err

Copy link
Member

Choose a reason for hiding this comment

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

ahhhhh you're right, this is === 0. ok, so it seems like maybe this would work as well?

const kinds = Object.groupBy(nodes, (node) => node.importKind === 'type' ? 'type' : node.specifiers.length === 0 ? 'sideEffect' : 'other');
if (kinds.type.length > 0 && kinds.sideEffect.length > 0 && kinds.other.length === 0) {
  continue;
}

(ofc Object.groupBy would need to use https://npmjs.com/object.groupby instead, but that's still slightly more efficient)

and an even simpler approach:

var hasType = false;
var hasSideEffect = false;
var hasOther = false;
for (var i = 0; !hasOther && i < nodes.length; i += 1) {
  var node = nodes[i];
  if (node.importKind === 'type') {
    hasType = true;
  } else if (node.specifiers.length === 0) {
    hasSideEffect = true;
  } else {
    hasOther = true;
  }
}
if (!hasOther && hasType && hasSideEffect) {
  continue;
}

that way uses no deps, breaks as soon as it finds an invalidating condition, and avoids creating three arrays.

Comment on lines +555 to +562
test({
code: `
import type { A } from 'a';
import 'a';
`,
options: [{ 'prefer-inline': true }],
...parserConfig,
}),
Copy link
Member

Choose a reason for hiding this comment

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

let's also add a similar invalid test, with output?

Copy link
Author

Choose a reason for hiding this comment

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

do you mean this:

import { A } from 'a';
import 'a';

?

Copy link
Member

Choose a reason for hiding this comment

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

i think more like

import type { A } from 'a';
import B from 'a';

with prefer-inline set to true?

Copy link
Author

Choose a reason for hiding this comment

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

ah, got it, will add it

Copy link
Author

Choose a reason for hiding this comment

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

This fails but I think for the wrong reasons and is a different issue. It currently suggests:

import B, type { A } from 'a';

which is syntactically wrong

Copy link
Member

Choose a reason for hiding this comment

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

I agree, I think that's part of #3195 perhaps? It'd be cool to fix that too here :-)

Copy link

codecov bot commented Jun 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.61%. Comparing base (01c9eb0) to head (4045eed).

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #3194       +/-   ##
===========================================
+ Coverage   82.25%   95.61%   +13.36%     
===========================================
  Files          94       83       -11     
  Lines        4283     3696      -587     
  Branches     1478     1336      -142     
===========================================
+ Hits         3523     3534       +11     
+ Misses        760      162      -598     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[no-duplicates] prefer-inline breaks unassigned and top-level type imports
2 participants