Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
todor-a wants to merge7 commits intoimport-js:main
base:main
Choose a base branch
Loading
fromtodor-a:3130-no-duplicate-side-effect

Conversation

todor-a
Copy link

@todor-atodor-a commentedJun 25, 2025
edited
Loading

Fixes#3130,#3195.

oleasteo reacted with thumbs up emoji
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?

todor-a reacted with eyes emoji
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?

constkinds=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 usehttps://npmjs.com/object.groupby instead, but that's still slightly more efficient)

and an even simpler approach:

varhasType=false;varhasSideEffect=false;varhasOther=false;for(vari=0;!hasOther&&i<nodes.length;i+=1){varnode=nodes[i];if(node.importKind==='type'){hasType=true;}elseif(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.

Copy link
Author

Choose a reason for hiding this comment

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

nice, done

Copy link
Author

Choose a reason for hiding this comment

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

bump

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
Author

@todor-atodor-aJul 1, 2025
edited
Loading

Choose a reason for hiding this comment

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

(sorry this is moving so slow)

i pushed a fix that allows for this condition, let me know what you think. also pushed a fix for point 2 from the linked issue.

taking a look at the failing test now

Copy link
Author

Choose a reason for hiding this comment

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

pushed a fix for both issues in#3195

had to disable one of the tests if the parser is babel because apparently it does not supportimport type * as

https://astexplorer.net/#/gist/a676569326155f55fa15da7fd791ffda/latest

@codecovCodecov
Copy link

codecovbot commentedJun 25, 2025
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.79%. Comparing base(01c9eb0) to head(6ab02d0).

Additional details and impacted files
@@             Coverage Diff             @@##             main    #3194       +/-   ##===========================================+ Coverage   82.25%   95.79%   +13.54%===========================================  Files          94       83       -11       Lines        4283     3712      -571       Branches     1478     1345      -133     ===========================================+ Hits         3523     3556       +33+ Misses        760      156      -604

☔ 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.

@todor-atodor-a requested a review fromljharbJuly 2, 2025 06:16
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ljharbljharbAwaiting requested review from ljharb

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

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

[8]ページ先頭

©2009-2025 Movatter.jp