Skip to content
Closed
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
9 changes: 9 additions & 0 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ impl<'a> SemanticBuilder<'a> {
let scope_id = self.scope.add_scope(None, AstNodeId::DUMMY, ScopeFlags::Top);
program.scope_id.set(Some(scope_id));
} else {
// Upgrade "script" to "module`.
if self.source_type.is_unambiguous() {
self.source_type = if program.body.iter().any(Statement::is_module_declaration) {
self.source_type.with_module(true)
} else {
self.source_type.with_script(true)
}
}

// Count the number of nodes, scopes, symbols, and references.
// Use these counts to reserve sufficient capacity in `AstNodes`, `ScopeTree`
// and `SymbolTable` to store them.
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ pub fn check_module_declaration<'a>(
let start = decl.span().start;
let span = Span::new(start, start + 6);
match ctx.source_type.module_kind() {
ModuleKind::Unambiguous => {}
ModuleKind::Script => {
ctx.error(module_code(text, span));
}
Expand Down
12 changes: 12 additions & 0 deletions crates/oxc_span/src/source_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ impl SourceType {
self.module_kind == ModuleKind::Module
}

pub fn is_unambiguous(self) -> bool {
self.module_kind == ModuleKind::Unambiguous
}

pub fn module_kind(self) -> ModuleKind {
self.module_kind
}
Expand Down Expand Up @@ -211,6 +215,14 @@ impl SourceType {
self
}

#[must_use]
pub const fn with_unambiguous(mut self, yes: bool) -> Self {
if yes {
self.module_kind = ModuleKind::Unambiguous;
}
self
}

#[must_use]
pub const fn with_typescript(mut self, yes: bool) -> Self {
if yes {
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_span/src/source_type/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use oxc_ast_macros::ast;
#[cfg(feature = "serialize")]
use ::{serde::Serialize, tsify::Tsify};
use {serde::Serialize, tsify::Tsify};

/// Source Type for JavaScript vs TypeScript / Script vs Module / JSX
#[ast]
Expand Down Expand Up @@ -48,6 +48,8 @@ pub enum ModuleKind {
Script = 0,
/// ES6 Module
Module = 1,
/// Unambiguous
Unambiguous = 2,
}

/// JSX for JavaScript and TypeScript
Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_transformer/src/options/babel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ impl BabelOptions {
}

pub fn is_module(&self) -> bool {
self.source_type.as_ref().map_or(false, |s| matches!(s.as_str(), "module" | "unambiguous"))
self.source_type.as_ref().map_or(false, |s| s.as_str() == "module")
}

pub fn is_unambiguous(&self) -> bool {
self.source_type.as_ref().map_or(false, |s| s.as_str() == "unambiguous")
}

/// Returns
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/codegen_misc.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
codegen_misc Summary:
AST Parsed : 27/27 (100.00%)
Positive Passed: 27/27 (100.00%)
AST Parsed : 29/29 (100.00%)
Positive Passed: 29/29 (100.00%)
5 changes: 5 additions & 0 deletions tasks/coverage/misc/pass/babel-16776-m.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
await /x.y/g;

export { }

var x, g;
3 changes: 3 additions & 0 deletions tasks/coverage/misc/pass/babel-16776-s.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
await /x.y/g;

var x, g;
10 changes: 9 additions & 1 deletion tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ commit: 3bcfee23

parser_babel Summary:
AST Parsed : 2093/2101 (99.62%)
Positive Passed: 2083/2101 (99.14%)
Positive Passed: 2082/2101 (99.10%)
Negative Passed: 1382/1493 (92.57%)
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.1-sloppy-labeled-functions-if-body/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/invalid-fn-decl-labeled-inside-if/input.js
Expand Down Expand Up @@ -132,6 +132,14 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/o
· ──────────
╰────
help: new.target is only allowed in constructors and functions invoked using thew `new` operator
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/import-meta/input.js

× Unexpected import.meta expression
╭─[babel/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/import-meta/input.js:1:13]
Copy link
Member Author

Choose a reason for hiding this comment

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

Need sawUnambiguousESM from babel parrser

1 │ console.log(import.meta);
· ───────────
╰────
help: import.meta is only allowed in module code
Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/arrow-function/generic-tsx-babel-7/input.ts

× Expected `<` but found `EOF`
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/parser_misc.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
parser_misc Summary:
AST Parsed : 27/27 (100.00%)
Positive Passed: 27/27 (100.00%)
AST Parsed : 29/29 (100.00%)
Positive Passed: 29/29 (100.00%)
Negative Passed: 17/17 (100.00%)

× Unexpected token
Expand Down
684 changes: 373 additions & 311 deletions tasks/coverage/parser_typescript.snap

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions tasks/coverage/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ commit: 3bcfee23

semantic_babel Summary:
AST Parsed : 2101/2101 (100.00%)
Positive Passed: 1736/2101 (82.63%)
Positive Passed: 1735/2101 (82.58%)
tasks/coverage/babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.3-function-in-if-body/input.js
semantic error: Scope children mismatch:
after transform: ScopeId(0): [ScopeId(1), ScopeId(2)]
Expand All @@ -27,6 +27,9 @@ semantic error: Scope children mismatch:
after transform: ScopeId(3): [ScopeId(4)]
rebuilt : ScopeId(3): [ScopeId(4)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/core/sourcetype-unambiguous/import-meta/input.js
semantic error: Unexpected import.meta expression

tasks/coverage/babel/packages/babel-parser/test/fixtures/core/uncategorised/230/input.js
semantic error: Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
Expand Down Expand Up @@ -769,8 +772,9 @@ semantic error: `export = <value>;` is only supported when compiling modules to
Please consider using `export default <value>;`, or add @babel/plugin-transform-modules-commonjs to your Babel config.

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous/input.ts
semantic error: `export = <value>;` is only supported when compiling modules to CommonJS.
Please consider using `export default <value>;`, or add @babel/plugin-transform-modules-commonjs to your Babel config.
semantic error: Unresolved references mismatch:
after transform: ["f"]
rebuilt : []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/export-type/input.ts
semantic error: Bindings mismatch:
Expand Down Expand Up @@ -894,8 +898,14 @@ semantic error: `import lib = require(...);` is only supported when compiling mo
Please consider using `import lib from '...';` alongside Typescript's --allowSyntheticDefaultImports option, or add @babel/plugin-transform-modules-commonjs to your Babel config.

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require-in-unambiguous/input.ts
semantic error: `import lib = require(...);` is only supported when compiling modules to CommonJS.
Please consider using `import lib from '...';` alongside Typescript's --allowSyntheticDefaultImports option, or add @babel/plugin-transform-modules-commonjs to your Babel config.
semantic error: Missing SymbolId: a
Missing ReferenceId: require
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import/input.ts
semantic error: Missing SymbolId: A
Expand Down
9 changes: 7 additions & 2 deletions tasks/coverage/semantic_misc.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
semantic_misc Summary:
AST Parsed : 27/27 (100.00%)
Positive Passed: 15/27 (55.56%)
AST Parsed : 29/29 (100.00%)
Positive Passed: 16/29 (55.17%)
tasks/coverage/misc/pass/babel-16776-m.js
semantic error: Symbol flags mismatch:
after transform: SymbolId(0): SymbolFlags(FunctionScopedVariable | Export)
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)

tasks/coverage/misc/pass/oxc-1288.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["from"]
Expand Down
Loading