Skip to content

feat(linter): add n/global-require rule#17283

Merged
camc314 merged 2 commits intooxc-project:mainfrom
baevm:n/global-require
Dec 23, 2025
Merged

feat(linter): add n/global-require rule#17283
camc314 merged 2 commits intooxc-project:mainfrom
baevm:n/global-require

Conversation

@baevm
Copy link
Contributor

@baevm baevm commented Dec 22, 2025

this PR adds n/global-require rule, issue #493

source doc
source code

Copilot AI review requested due to automatic review settings December 22, 2025 17:47
@baevm baevm requested a review from camc314 as a code owner December 22, 2025 17:47
@github-actions github-actions bot added A-linter Area - Linter C-enhancement Category - New feature or request labels Dec 22, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds the n/global-require linter rule from eslint-plugin-n, which enforces that require() calls are placed at top-level module scope. This helps improve code clarity by making dependencies easier to identify and prevents potential performance issues from synchronous loading within functions or conditional statements.

Key Changes:

  • Implements the GlobalRequire rule that detects require() calls not at module scope
  • Adds comprehensive test cases covering various invalid patterns (functions, conditionals, try-catch, arrow functions)
  • Properly handles shadowed require identifiers to avoid false positives

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
crates/oxc_linter/src/rules/node/global_require.rs Core implementation of the global-require rule with logic to detect non-top-level require() calls
crates/oxc_linter/src/snapshots/node_global_require.snap Snapshot test output showing expected diagnostic messages for various violation patterns
crates/oxc_linter/src/rules.rs Registers the new global_require module in the node plugin section
crates/oxc_linter/src/generated/rule_runner_impls.rs Generated implementation registering the rule to run on CallExpression AST nodes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@camc314 camc314 left a comment

Choose a reason for hiding this comment

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

Thank you!

@camc314 camc314 self-assigned this Dec 23, 2025
@camc314 camc314 merged commit 94a482b into oxc-project:main Dec 23, 2025
19 checks passed
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 23, 2025

CodSpeed Performance Report

Merging #17283 will not alter performance

Comparing baevm:n/global-require (2cb025a) with main (5cc7c90)1

Summary

✅ 4 untouched
⏩ 41 skipped2

Footnotes

  1. No successful run was found on main (1dc371e) during the generation of this report, so 5cc7c90 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 41 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

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

Labels

A-linter Area - Linter C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants