Skip to content

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.

use crate::{AstNode, context::LintContext, rule::Rule};

fn global_require_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected require().").with_label(span)
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

The error message should include a help message to guide users on how to fix the issue. Consider adding .with_help("Move require() to top-level module scope") to make the diagnostic more actionable, similar to how other node rules provide help messages.

Suggested change
OxcDiagnostic::warn("Unexpected require().").with_label(span)
OxcDiagnostic::warn("Unexpected require().")
.with_label(span)
.with_help("Move require() to top-level module scope")

Copilot uses AI. Check for mistakes.
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.

1 participant