Skip to content

feat(span): add sourceType: 'commonjs' support#18089

Merged
graphite-app[bot] merged 1 commit intomainfrom
feat/commonjs-source-type
Jan 16, 2026
Merged

feat(span): add sourceType: 'commonjs' support#18089
graphite-app[bot] merged 1 commit intomainfrom
feat/commonjs-source-type

Conversation

@Boshen
Copy link
Member

@Boshen Boshen commented Jan 16, 2026

Summary

  • Add ModuleKind::CommonJS variant to support CommonJS module type in the parser
  • Enable top-level return statements (CommonJS files are wrapped in a function)
  • Enable top-level new.target (allowed in CommonJS function wrapper)
  • Add proper errors for import/export statements in CommonJS
  • Add proper errors for import.meta in CommonJS

The CommonJS source type can be set via:

  • SourceType::with_commonjs(true) method
  • Babel's sourceType: "commonjs" option

Closes #16200

Test plan

  • Added test cases in tasks/coverage/misc/pass/ for valid CommonJS features
  • Added test cases in tasks/coverage/misc/fail/ for invalid CommonJS usage
  • Enabled Babel conformance tests for sourceType: "commonjs"
  • All existing tests pass

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings January 16, 2026 15:34
@github-actions github-actions bot added A-linter Area - Linter A-parser Area - Parser A-semantic Area - Semantic A-cli Area - CLI A-transformer Area - Transformer / Transpiler A-linter-plugins Area - Linter JS plugins C-enhancement Category - New feature or request labels Jan 16, 2026
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Jan 16, 2026
Copy link
Member Author

Boshen commented Jan 16, 2026

Merge activity

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 adds support for CommonJS module type (sourceType: 'commonjs') in the parser to match Babel's behavior. CommonJS files are wrapped in a function wrapper, which enables top-level return and new.target statements while disallowing ES module features like import/export statements and import.meta.

Changes:

  • Added ModuleKind::CommonJS variant to the type system
  • Updated parser to allow top-level return in CommonJS mode
  • Updated semantic checker to allow top-level new.target in CommonJS mode and properly error on ES module features
  • Added test cases demonstrating valid and invalid CommonJS usage

Reviewed changes

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

Show a summary per file
File Description
crates/oxc_span/src/source_type.rs Added CommonJS variant to ModuleKind enum with documentation and helper methods
crates/oxc_span/src/generated/derive_estree.rs Added ESTree serialization for CommonJS module kind
crates/oxc_parser/src/lib.rs Enabled top-level return for CommonJS source type
crates/oxc_semantic/src/checker/javascript.rs Added CommonJS-specific validation for module declarations and meta properties
crates/oxc_transformer/src/options/babel/mod.rs Added is_commonjs() helper method for Babel options
tasks/coverage/src/babel/mod.rs Added CommonJS source type detection from Babel options
tasks/coverage/misc/pass/*.cjs, *.cts Test cases for valid CommonJS features (top-level return, new.target)
tasks/coverage/misc/fail/*.cjs Test cases for invalid CommonJS features (import/export, import.meta, top-level await)
npm/oxc-types/types.d.ts Updated TypeScript type definitions to include "commonjs" module kind
apps/oxlint/src-js/generated/types.d.ts Updated TypeScript type definitions for oxlint
napi/parser/src-js/generated/**/*.js Updated JavaScript deserializers to handle CommonJS discriminant
tasks/coverage/snapshots/*.snap Updated test snapshots showing improved test coverage

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

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 16, 2026

Merging this PR will not alter performance

✅ 42 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing feat/commonjs-source-type (f572742) with main (6956543)

Open in CodSpeed

Footnotes

  1. 3 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.

graphite-app bot pushed a commit that referenced this pull request Jan 16, 2026
## Summary

- Add `ModuleKind::CommonJS` variant to support CommonJS module type in the parser
- Enable top-level `return` statements (CommonJS files are wrapped in a function)
- Enable top-level `new.target` (allowed in CommonJS function wrapper)
- Add proper errors for `import`/`export` statements in CommonJS
- Add proper errors for `import.meta` in CommonJS

The CommonJS source type can be set via:
- `SourceType::with_commonjs(true)` method
- Babel's `sourceType: "commonjs"` option

Closes #16200

## Test plan

- [x] Added test cases in `tasks/coverage/misc/pass/` for valid CommonJS features
- [x] Added test cases in `tasks/coverage/misc/fail/` for invalid CommonJS usage
- [x] Enabled Babel conformance tests for `sourceType: "commonjs"`
- [x] All existing tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@graphite-app graphite-app bot force-pushed the feat/commonjs-source-type branch from f572742 to ea3ead0 Compare January 16, 2026 16:03
## Summary

- Add `ModuleKind::CommonJS` variant to support CommonJS module type in the parser
- Enable top-level `return` statements (CommonJS files are wrapped in a function)
- Enable top-level `new.target` (allowed in CommonJS function wrapper)
- Add proper errors for `import`/`export` statements in CommonJS
- Add proper errors for `import.meta` in CommonJS

The CommonJS source type can be set via:
- `SourceType::with_commonjs(true)` method
- Babel's `sourceType: "commonjs"` option

Closes #16200

## Test plan

- [x] Added test cases in `tasks/coverage/misc/pass/` for valid CommonJS features
- [x] Added test cases in `tasks/coverage/misc/fail/` for invalid CommonJS usage
- [x] Enabled Babel conformance tests for `sourceType: "commonjs"`
- [x] All existing tests pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@graphite-app graphite-app bot force-pushed the feat/commonjs-source-type branch from ea3ead0 to 08dad63 Compare January 16, 2026 16:08
@graphite-app graphite-app bot merged commit 08dad63 into main Jan 16, 2026
22 checks passed
@graphite-app graphite-app bot deleted the feat/commonjs-source-type branch January 16, 2026 16:14
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Jan 16, 2026
Boshen added a commit that referenced this pull request Jan 17, 2026
…nsions

Previously, `.cjs` and `.cts` files were using `ModuleKind::Script` which
was inconsistent with the recent addition of `ModuleKind::CommonJS` in #18089.

This change:
- Updates `SourceType::from_path()` to return `ModuleKind::CommonJS` for `.cjs`/`.cts`
- 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

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Boshen added a commit that referenced this pull request Jan 17, 2026
…nsions

Previously, `.cjs` and `.cts` files were using `ModuleKind::Script` which
was inconsistent with the recent addition of `ModuleKind::CommonJS` in #18089.

This change:
- Updates `SourceType::from_path()` to return `ModuleKind::CommonJS` for `.cjs`/`.cts`
- 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

Co-Authored-By: Claude Opus 4.5 <[email protected]>
graphite-app bot pushed a commit that referenced this pull request 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 bot pushed a commit that referenced this pull request Jan 18, 2026
Remove 2 outdated comments. #18089 introduced `commonjs` source type.
graphite-app bot pushed a commit that referenced this pull request Jan 19, 2026
…18197)

#18089 added support in parser for `commonjs` source type.

Extend this support to the NAPI packages - accept `sourceType: 'commonjs'` in options for `oxc-parser` and `oxc-transform`.
graphite-app bot pushed a commit that referenced this pull request Jan 19, 2026
…18198)

`commonjs` source type was previously supported in `RuleTester` only with `eslintCompat` option enabled (and it was then treated as `script`).

#18089 added support in Oxc parser for `commonjs`. Accept it in `RuleTester` (either with or without ESLint compatibility mode) and parse properly as CommonJS.
graphite-app bot pushed a commit that referenced this pull request Jan 19, 2026
Presumably it's safe to mangle top-level symbols in CommonJS files. Alter the mangler example to do that.

Previously, we had no way to distinguish between script and CommonJS file types. After #18089 introduced a `commonjs` source type, we now do.
graphite-app bot pushed a commit that referenced this pull request Jan 19, 2026
…18250)

Another follow-up after #18089.

Skip checking export declarations in CommonJS files. CommonJS files can't contain `export` declarations, so there's no point searching for them.
graphite-app bot pushed a commit that referenced this pull request Jan 20, 2026
Follow-up to #18089.

Maybe we should add `SourceType::script()` so that it's easier to create a SourceType with `ModuleType::Script`.
graphite-app bot pushed a commit that referenced this pull request Jan 20, 2026
Follow-up to #18089.

Maybe we should add `SourceType::script()` so that it's easier to create a SourceType with `ModuleType::Script`.
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-parser Area - Parser A-semantic Area - Semantic A-transformer Area - Transformer / Transpiler C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support sourceType: 'commonjs'

1 participant

Comments