Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/oxlint/test/fixtures/languageOptions/output.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# stdout
```
x language-options-plugin(lang): languageOptions:
| sourceType: script
| sourceType: commonjs
| ecmaVersion: 2026
| parserOptions: {"sourceType":"script"}
| parserOptions: {"sourceType":"commonjs"}
| globals: {}
| env: {"builtin":true}
,-[files/index.cjs:1:1]
Expand Down
5 changes: 4 additions & 1 deletion crates/oxc_linter/src/loader/partial_loader/vue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ mod test {
let cases = [
("<script>debugger</script>", Some(SourceType::mjs())),
("<script lang = 'tsx' >debugger</script>", Some(SourceType::tsx())),
(r#"<script lang = "cjs" >debugger</script>"#, Some(SourceType::cjs())),
(
r#"<script lang = "cjs" >debugger</script>"#,
Some(SourceType::from_extension("cjs").unwrap()),
),
("<script lang=tsx>debugger</script>", Some(SourceType::tsx())),
("<script lang = 'xxx'>debugger</script>", None),
(r#"<script lang = "xxx">debugger</script>"#, None),
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ impl Rule for NoEval {
}

let is_valid = if scope_flags.is_top() {
ctx.semantic().source_type().is_script()
// In scripts and CommonJS, `this` at top level refers to the global object
// In ES modules, `this` at top level is undefined
!ctx.semantic().source_type().is_module()
} else {
let node = ctx.nodes().get_node(ctx.scoping().get_node_id(scope_id));
ast_util::is_default_this_binding(ctx, node, true)
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_redeclare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use javascript_globals::GLOBALS;

use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{ModuleKind, Span};
use oxc_span::Span;
use schemars::JsonSchema;
use serde::Deserialize;

Expand Down Expand Up @@ -122,8 +122,8 @@ impl Rule for NoRedeclare {
}

fn should_run(&self, ctx: &ContextHost) -> bool {
// Modules run in their own scope, and don't conflict with existing globals
ctx.source_type().module_kind() == ModuleKind::Script
// ES modules run in their own scope, and don't conflict with existing globals
!ctx.source_type().is_module()
}
}

Expand Down
15 changes: 12 additions & 3 deletions crates/oxc_span/src/source_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl From<FileExtension> for SourceType {

let module_kind = match file_ext {
Js | Tsx | Ts | Jsx | Mts | Mjs => ModuleKind::Module,
Cjs | Cts => ModuleKind::Script,
Cjs | Cts => ModuleKind::CommonJS,
};

let variant = match file_ext {
Expand Down Expand Up @@ -665,9 +665,14 @@ mod tests {

assert!(!ts.is_script());
assert!(!mts.is_script());
assert!(cts.is_script());
assert!(!cts.is_script());
assert!(!tsx.is_script());

assert!(!ts.is_commonjs());
assert!(!mts.is_commonjs());
assert!(cts.is_commonjs());
assert!(!tsx.is_commonjs());

assert!(ts.is_strict());
assert!(mts.is_strict());
assert!(!cts.is_strict());
Expand Down Expand Up @@ -700,7 +705,11 @@ mod tests {

assert!(!dts.is_script());
assert!(!dmts.is_script());
assert!(dcts.is_script());
assert!(!dcts.is_script());

assert!(!dts.is_commonjs());
assert!(!dmts.is_commonjs());
assert!(dcts.is_commonjs());

assert!(dts.is_strict());
assert!(dmts.is_strict());
Expand Down
40 changes: 1 addition & 39 deletions tasks/coverage/snapshots/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -1,47 +1,9 @@
parser_misc Summary:
AST Parsed : 59/59 (100.00%)
Positive Passed: 55/59 (93.22%)
Positive Passed: 59/59 (100.00%)
Negative Passed: 128/129 (99.22%)
Expect Syntax Error: tasks/coverage/misc/fail/script-top-level-using.js

Expect to Parse: tasks/coverage/misc/pass/commonjs-top-level-new-target.cjs

× Unexpected new.target expression
╭─[misc/pass/commonjs-top-level-new-target.cjs:2:1]
1 │ // CommonJS allows top-level new.target because the file is wrapped in a function
2 │ new.target;
· ──────────
╰────
help: new.target is only allowed in constructors and functions invoked using the `new` operator

Expect to Parse: tasks/coverage/misc/pass/commonjs-top-level-new-target.cts

× Unexpected new.target expression
╭─[misc/pass/commonjs-top-level-new-target.cts:2:1]
1 │ // TypeScript CommonJS allows top-level new.target
2 │ new.target;
· ──────────
╰────
help: new.target is only allowed in constructors and functions invoked using the `new` operator

Expect to Parse: tasks/coverage/misc/pass/commonjs-top-level-return.cjs

× TS(1108): A 'return' statement can only be used within a function body.
╭─[misc/pass/commonjs-top-level-return.cjs:2:1]
1 │ // CommonJS allows top-level return because the file is wrapped in a function
2 │ return 42;
· ──────
╰────

Expect to Parse: tasks/coverage/misc/pass/commonjs-top-level-return.cts

× TS(1108): A 'return' statement can only be used within a function body.
╭─[misc/pass/commonjs-top-level-return.cts:2:1]
1 │ // TypeScript CommonJS allows top-level return
2 │ return 42;
· ──────
╰────


× Cannot assign to 'arguments' in strict mode
╭─[misc/fail/arguments-eval.ts:1:10]
Expand Down
14 changes: 1 addition & 13 deletions tasks/coverage/snapshots/semantic_misc.snap
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
semantic_misc Summary:
AST Parsed : 59/59 (100.00%)
Positive Passed: 36/59 (61.02%)
semantic Error: tasks/coverage/misc/pass/commonjs-top-level-new-target.cjs
Unexpected new.target expression

semantic Error: tasks/coverage/misc/pass/commonjs-top-level-new-target.cts
Unexpected new.target expression

semantic Error: tasks/coverage/misc/pass/commonjs-top-level-return.cjs
A 'return' statement can only be used within a function body.

semantic Error: tasks/coverage/misc/pass/commonjs-top-level-return.cts
A 'return' statement can only be used within a function body.

Positive Passed: 40/59 (67.80%)
semantic Error: tasks/coverage/misc/pass/declare-let-private.ts
Bindings mismatch:
after transform: ScopeId(0): ["private"]
Expand Down
Loading