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
Closed
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
2 changes: 1 addition & 1 deletion lib/rules/file-extension-in-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

const corePackageOverridePattern =
/^(?:assert|async_hooks|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|http2|https|inspector|module|net|os|path|perf_hooks|process|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|trace_events|tty|url|util|v8|vm|worker_threads|zlib)[/\\]$/u

Expand Down
11 changes: 5 additions & 6 deletions tests/lib/rules/file-extension-in-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ new RuleTester({
filename: fixture("test.js"),
code: "import '@typescript-eslint/parser'",
},
{
filename: fixture("test.ts"),
code: "import {test} from '@typescript-eslint/parser/index'",
options: ["always"],
},
Copy link

@aladdin-add aladdin-add Jul 6, 2022

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

{
filename: fixture("test.js"),
code: "import '@typescript-eslint\\parser'",
Expand Down Expand Up @@ -155,12 +160,6 @@ new RuleTester({
output: "import './a.js'",
errors: [{ messageId: "requireExt", data: { ext: ".js" } }],
},
{
filename: fixture("test.ts"),
code: "import './d'",
output: "import './d.js'",
errors: [{ messageId: "requireExt", data: { ext: ".js" } }],
},
{
filename: fixture("test.js"),
code: "import './b'",
Expand Down