feat(span): add sourceType: 'commonjs' support#18089
Conversation
Merge activity
|
There was a problem hiding this comment.
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::CommonJSvariant to the type system - Updated parser to allow top-level
returnin CommonJS mode - Updated semantic checker to allow top-level
new.targetin 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.
Merging this PR will not alter performance
Comparing Footnotes
|
## 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)
f572742 to
ea3ead0
Compare
## 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)
ea3ead0 to
08dad63
Compare
…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]>
…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]>
…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)
Remove 2 outdated comments. #18089 introduced `commonjs` source type.
…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.
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.
Follow-up to #18089. Maybe we should add `SourceType::script()` so that it's easier to create a SourceType with `ModuleType::Script`.
Follow-up to #18089. Maybe we should add `SourceType::script()` so that it's easier to create a SourceType with `ModuleType::Script`.
Summary
ModuleKind::CommonJSvariant to support CommonJS module type in the parserreturnstatements (CommonJS files are wrapped in a function)new.target(allowed in CommonJS function wrapper)import/exportstatements in CommonJSimport.metain CommonJSThe CommonJS source type can be set via:
SourceType::with_commonjs(true)methodsourceType: "commonjs"optionCloses #16200
Test plan
tasks/coverage/misc/pass/for valid CommonJS featurestasks/coverage/misc/fail/for invalid CommonJS usagesourceType: "commonjs"🤖 Generated with Claude Code