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 bug where package with sub-import would require extension #32

Closed

Conversation

mpetrunic
Copy link

Should fix #21

@@ -8,7 +8,7 @@ const path = require("path")
const fs = require("fs")
const mapTypescriptExtension = require("../util/map-typescript-extension")
const visitImport = require("../util/visit-import")
const packageNamePattern = /^(?:@[^/\\]+[/\\])?[^/\\]+$/u
const packageNamePattern = /^(?:@[^/\\]+[/\\])?(?:[^./\\]+[/\\])*[^/\\]+$/u

Choose a reason for hiding this comment

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

good to have a comment to explain the regex - regex is always easy to write but hard to read :)

Copy link
Author

Choose a reason for hiding this comment

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

Will do!
FYI old regex was matching:
optional @scope/ followed by non-slash characters
which excluded imports like @scope/pkg/dir
new one:
optional @scope/ followed by an optional number of groups containing 1 or more characters ending with a slash followed by by non-slash characters

Copy link

Choose a reason for hiding this comment

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

Here is a quick before and after using my favourite regex renderer 😄

Before: /^(?:@[^/\\]+[/\\])?[^/\\]+$/u
image

After: /^(?:@[^/\\]+[/\\])?(?:[^./\\]+[/\\])*[^/\\]+$/u
image

It does looks like the change should work too:
image

image

filename: fixture("test.ts"),
code: "import {test} from '@typescript-eslint/parser/index'",
options: ["always"],
},
Copy link

Choose a reason for hiding this comment

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

if the ./index was defined in its exports, the ext is disallowed, else it's always required in esm.

"exports": {"./index": "...."}

Copy link
Author

Choose a reason for hiding this comment

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

Ah, true, that said, I cannot figure out how to trigger a falling test, like if in code as reported in issue if you have import { keysPBM, supportedKeys } from "@libp2p/crypto/keys"; it will report an error and put extension even though it shouldn't. But cannot figure out how to make falling test case for that

@neviaumi
Copy link

Are you planning to merge this PR in v16 major ? @aladdin-add

@aladdin-add
Copy link

It can be, but it can also be released later in v16.x.x - given it is a non-breaking bugfix.

I kind of lost the context, will be back to review this later. :)

@calebeby
Copy link

calebeby commented Aug 4, 2023

Hey @aladdin-add I'd love for this PR to get merged! Is it ready to merge or are you still waiting for your feedback to be addressed? I'm happy to make a new PR with the added requested comments if it would be helpful!

@scagood
Copy link

scagood commented Oct 25, 2023

I think a better solution here might be to change lib/util/import-target.js and then use a flag from there to detect if this is a module 🤔

An example of what I mean can be found in #132

@scagood
Copy link

scagood commented Nov 7, 2023

This should have been resolved by #132

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

Successfully merging this pull request may close these issues.

n/file-extension-in-import triggered incorrectly on some modules with / in path.
5 participants