Skip to content

feat(span)!: use ModuleKind::CommonJS for .cjs and .cts file extensions#18117

Merged
graphite-app[bot] merged 1 commit intomainfrom
fix/using-in-script-top-level
Jan 17, 2026
Merged

feat(span)!: use ModuleKind::CommonJS for .cjs and .cts file extensions#18117
graphite-app[bot] merged 1 commit intomainfrom
fix/using-in-script-top-level

Conversation

@Boshen
Copy link
Member

@Boshen Boshen commented Jan 17, 2026

Summary

  • Updates SourceType::from_path() to return ModuleKind::CommonJS for .cjs/.cts files (previously returned ModuleKind::Script)
  • Fixes eslint/no-eval to check !is_module() instead of is_script() to handle both scripts and CommonJS (matching ESLint's sourceType: "commonjs" behavior)
  • Fixes eslint/no-redeclare to run for both scripts and CommonJS files
  • Updates tests to use proper CommonJS assertions

This follows up on #18089 which added ModuleKind::CommonJS support but didn't update the file extension mapping.

Test plan

  • cargo test -p oxc_span
  • cargo test -p oxc_linter

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings January 17, 2026 04:42
@github-actions github-actions bot added A-linter Area - Linter A-semantic Area - Semantic C-bug Category - Bug labels Jan 17, 2026
@Boshen Boshen changed the title fix(span): use ModuleKind::CommonJS for .cjs and .cts file extensions fix(span)!: use ModuleKind::CommonJS for .cjs and .cts file extensions Jan 17, 2026
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 pull request updates the handling of .cjs and .cts file extensions to properly treat them as CommonJS modules instead of plain scripts. The change enables CommonJS-specific semantics like top-level return and new.target, while also enforcing script-specific restrictions like preventing top-level using declarations.

Changes:

  • Updates SourceType::from_path() to map .cjs/.cts files to ModuleKind::CommonJS instead of ModuleKind::Script
  • Fixes linter rules (no_eval, no_redeclare) to properly handle both scripts and CommonJS by checking !is_module() instead of is_script()
  • Adds semantic checker for using declarations to prevent them at the top level of scripts (while allowing them in CommonJS and modules)

Reviewed changes

Copilot reviewed 7 out of 17 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
crates/oxc_span/src/source_type.rs Updates file extension mapping to return ModuleKind::CommonJS for .cjs/.cts files and adds comprehensive test assertions
crates/oxc_semantic/src/diagnostics.rs Adds new diagnostic for invalid top-level using declarations in scripts
crates/oxc_semantic/src/checker/mod.rs Registers the new variable declaration checker
crates/oxc_semantic/src/checker/javascript.rs Implements check to prevent top-level using in scripts while allowing in modules and CommonJS
crates/oxc_linter/src/rules/eslint/no_redeclare.rs Updates to run for both scripts and CommonJS (not just scripts)
crates/oxc_linter/src/rules/eslint/no_eval.rs Updates to check !is_module() instead of is_script() with clarifying comment
crates/oxc_linter/src/loader/partial_loader/vue.rs Updates test to use from_extension() to get correct CommonJS type
tasks/coverage/misc/pass/script-using-in-block.js New test showing using is allowed in blocks within scripts
tasks/coverage/misc/fail/script-top-level-using.js New test showing using is not allowed at top level of scripts
Multiple snapshot files Updated test results showing CommonJS files now parse correctly and script-level using is properly rejected

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

@Boshen Boshen force-pushed the fix/using-in-script-top-level branch from 0ed20a4 to 9becfb9 Compare January 17, 2026 04:47
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Jan 17, 2026
Copy link
Member Author

Boshen commented Jan 17, 2026

Merge activity

@Boshen Boshen changed the title fix(span)!: use ModuleKind::CommonJS for .cjs and .cts file extensions feat(span)!: use ModuleKind::CommonJS for .cjs and .cts file extensions Jan 17, 2026
@github-actions github-actions bot added the C-enhancement Category - New feature or request label Jan 17, 2026
@Boshen Boshen force-pushed the fix/using-in-script-top-level branch from 562d2dd to 895a020 Compare January 17, 2026 09:02
@github-actions github-actions bot added A-cli Area - CLI A-linter-plugins Area - Linter JS plugins labels Jan 17, 2026
…tensions (#18117)

## Summary

- Updates `SourceType::from_path()` to return `ModuleKind::CommonJS` for `.cjs`/`.cts` files (previously returned `ModuleKind::Script`)
- Fixes `eslint/no-eval` to check `!is_module()` instead of `is_script()` to handle both scripts and CommonJS (matching ESLint's `sourceType: "commonjs"` behavior)
- Fixes `eslint/no-redeclare` to run for both scripts and CommonJS files
- Updates tests to use proper CommonJS assertions

This follows up on #18089 which added `ModuleKind::CommonJS` support but didn't update the file extension mapping.

## Test plan

- [x] `cargo test -p oxc_span`
- [x] `cargo test -p oxc_linter`

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@graphite-app graphite-app bot force-pushed the fix/using-in-script-top-level branch from 895a020 to ba9c750 Compare January 17, 2026 09:10
@graphite-app graphite-app bot merged commit ba9c750 into main Jan 17, 2026
21 checks passed
@graphite-app graphite-app bot deleted the fix/using-in-script-top-level branch January 17, 2026 09:17
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Jan 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area - CLI A-linter Area - Linter A-linter-plugins Area - Linter JS plugins A-semantic Area - Semantic C-bug Category - Bug C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments