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

feat(linter): implement useImportExtensions #2725

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

minht11
Copy link
Contributor

@minht11 minht11 commented May 5, 2024

Summary

Closes #2674
Implements import/extensions requirement for relative extensions.

Rule suggests unsafe fix based on current file and import path, given the limitations of not being able to access file system.

Test Plan

Added snapshots

@github-actions github-actions bot added A-Project Area: project A-Linter Area: linter L-JavaScript Language: JavaScript and super languages A-Diagnostic Area: diagnostocis A-Changelog Area: changelog labels May 5, 2024
Copy link

codspeed-hq bot commented May 5, 2024

CodSpeed Performance Report

Merging #2725 will not alter performance

Comparing minht11:feat/useImportExtensions (9a5a6d6) with main (9a05b77)

Summary

✅ 92 untouched benchmarks

@vohoanglong0107
Copy link
Contributor

Should we disable this rule for typescript files? For a typical typescript projects, adding files extensions is usually handled by the bundler, and typescript doesn't even allow imported files to end with .ts or .tsx

@minht11
Copy link
Contributor Author

minht11 commented May 8, 2024

@vohoanglong0107 As of typescript 5.0 you can use .ts extensions, this rule would not have much use otherwise. I don't see case where user would want to enable .js extensions, but not .ts

Copy link
Contributor

@arendjr arendjr left a comment

Choose a reason for hiding this comment

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

Nice rule!

Just a performance nit.

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Overall it looks good. There are few missing cases, and left few suggestions to avoid allocations

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

The rule doesn't cover import("./file.js") and require("./file.js"). @minht11, do you plan to add it to this PR or a following PR?

@minht11 minht11 force-pushed the feat/useImportExtensions branch 2 times, most recently from 47f9eba to 3bbc149 Compare May 11, 2024 11:25
@Conaclos
Copy link
Member

Once the conflicts resolved, I think we can merge this PR.

@minht11
Copy link
Contributor Author

minht11 commented May 17, 2024

@Conaclos what about windows tests? Right now they fail, because on windows os paths us different slash. Should I replace pathBuf with vector or something like that?

@ematipico
Copy link
Member

@Conaclos what about windows tests? Right now they fail, because on windows os paths us different slash. Should I replace pathBuf with vector or something like that?

We shouldn't rely on PathBuf to compose the new imports. Instead, we should do that manually.

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

I am going to block it until we fix Windows test cases.

We should compose the new import specifier manually instead of using PathBuf.

@arendjr
Copy link
Contributor

arendjr commented May 18, 2024

Ah, I've also run into that at some point. Maybe we should add it to the contributing guide somewhere. Path is usually better for inspecting path-like strings, but plain String is better for building paths (at least for our use cases) if you don't want to run into surprises on Windows.

@minht11
Copy link
Contributor Author

minht11 commented May 19, 2024

I replaced path_buf with string concatenation, windows test now pass.

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

The documentation doesn't mention whether it supports import() and require() or not, we should add that

use crate::JsRuleAction;

declare_rule! {
/// Require import extensions for relative imports.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Require import extensions for relative imports.
/// Enforce file extensions for relative imports.

/// Require import extensions for relative imports.
///
/// Browsers and Node.js do not natively support importing files without extensions. This rule
/// enforces the use of import extensions for relative imports to make the code more consistent.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// enforces the use of import extensions for relative imports to make the code more consistent.
/// enforces the use of file extensions for relative imports to make the code more consistent.

/// Browsers and Node.js do not natively support importing files without extensions. This rule
/// enforces the use of import extensions for relative imports to make the code more consistent.
///
/// Tooling also benefits from explicit import extensions, because they do not need to guess which
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// Tooling also benefits from explicit import extensions, because they do not need to guess which
/// Tooling also benefits from explicit file extensions, because they do not need to guess which

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Project Area: project L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📎 Implement lint rule to require file extension on relative imports - import/extensions
5 participants