diff --git a/crates/oxc_ast/src/lib.rs b/crates/oxc_ast/src/lib.rs index 20db17d10..d1cc192bb 100644 --- a/crates/oxc_ast/src/lib.rs +++ b/crates/oxc_ast/src/lib.rs @@ -11,7 +11,6 @@ mod serialize; pub mod ast; mod ast_builder; mod ast_kind; -pub mod context; pub mod module_record; mod source_type; mod span; diff --git a/crates/oxc_ast/src/source_type.rs b/crates/oxc_ast/src/source_type.rs index 6cdc41160..7de5d9d80 100644 --- a/crates/oxc_ast/src/source_type.rs +++ b/crates/oxc_ast/src/source_type.rs @@ -2,8 +2,6 @@ use std::path::Path; use thiserror::Error; -use crate::context::Context; - /// Source Type for JavaScript vs TypeScript / Script vs Module / JSX #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct SourceType { @@ -62,16 +60,6 @@ impl Default for SourceType { pub const VALID_EXTENSIONS: [&str; 8] = ["js", "mjs", "cjs", "jsx", "ts", "mts", "cts", "tsx"]; impl SourceType { - #[must_use] - pub fn default_context(&self) -> Context { - let ctx = Context::default().and_ambient(self.is_typescript_definition()); - match self.module_kind { - ModuleKind::Script => ctx, - // for [top-level-await](https://tc39.es/proposal-top-level-await/) - ModuleKind::Module => ctx.and_await(true), - } - } - #[must_use] pub fn is_script(self) -> bool { self.module_kind == ModuleKind::Script diff --git a/crates/oxc_ast/src/context.rs b/crates/oxc_parser/src/context.rs similarity index 100% rename from crates/oxc_ast/src/context.rs rename to crates/oxc_parser/src/context.rs diff --git a/crates/oxc_parser/src/cursor.rs b/crates/oxc_parser/src/cursor.rs index 4d9c20973..50ea7c5f7 100644 --- a/crates/oxc_parser/src/cursor.rs +++ b/crates/oxc_parser/src/cursor.rs @@ -1,10 +1,10 @@ //! Code related to navigating `Token`s from the lexer -use oxc_ast::{context::Context, Span}; +use oxc_ast::Span; use oxc_diagnostics::Result; use crate::lexer::{Kind, LexerCheckpoint, LexerContext, Token}; -use crate::{diagnostics, Parser}; +use crate::{diagnostics, Context, Parser}; pub struct ParserCheckpoint<'a> { lexer: LexerCheckpoint<'a>, diff --git a/crates/oxc_parser/src/js/class.rs b/crates/oxc_parser/src/js/class.rs index 6f1741771..3e265f7db 100644 --- a/crates/oxc_parser/src/js/class.rs +++ b/crates/oxc_parser/src/js/class.rs @@ -1,9 +1,9 @@ use oxc_allocator::{Box, Vec}; -use oxc_ast::{ast::*, context::StatementContext, syntax_directed_operations::PropName, Span}; +use oxc_ast::{ast::*, syntax_directed_operations::PropName, Span}; use oxc_diagnostics::Result; use super::list::ClassElements; -use crate::{diagnostics, lexer::Kind, list::NormalList, Parser}; +use crate::{diagnostics, lexer::Kind, list::NormalList, Parser, StatementContext}; type Extends<'a> = Vec<'a, (Expression<'a>, Option>>, Span)>; diff --git a/crates/oxc_parser/src/js/declaration.rs b/crates/oxc_parser/src/js/declaration.rs index 82fb288c4..001e1b963 100644 --- a/crates/oxc_parser/src/js/declaration.rs +++ b/crates/oxc_parser/src/js/declaration.rs @@ -1,8 +1,8 @@ use oxc_allocator::Box; -use oxc_ast::{ast::*, context::StatementContext, GetSpan, Span}; +use oxc_ast::{ast::*, GetSpan, Span}; use oxc_diagnostics::Result; -use crate::{diagnostics, lexer::Kind, Parser}; +use crate::{diagnostics, lexer::Kind, Parser, StatementContext}; #[derive(Clone, Debug, Copy, Eq, PartialEq)] pub enum VariableDeclarationParent { diff --git a/crates/oxc_parser/src/js/function.rs b/crates/oxc_parser/src/js/function.rs index 389b37920..fb894adc4 100644 --- a/crates/oxc_parser/src/js/function.rs +++ b/crates/oxc_parser/src/js/function.rs @@ -1,13 +1,9 @@ use oxc_allocator::Box; -use oxc_ast::{ - ast::*, - context::{Context, StatementContext}, - AstBuilder, GetSpan, Span, -}; +use oxc_ast::{ast::*, AstBuilder, GetSpan, Span}; use oxc_diagnostics::Result; use super::list::FormalParameterList; -use crate::{diagnostics, lexer::Kind, list::SeparatedList, Parser}; +use crate::{diagnostics, lexer::Kind, list::SeparatedList, Context, Parser, StatementContext}; type ArrowFunctionHead<'a> = ( Option>>, diff --git a/crates/oxc_parser/src/js/module.rs b/crates/oxc_parser/src/js/module.rs index 8e21e2112..ba1c1420d 100644 --- a/crates/oxc_parser/src/js/module.rs +++ b/crates/oxc_parser/src/js/module.rs @@ -1,10 +1,10 @@ use oxc_allocator::{Box, Vec}; -use oxc_ast::{ast::*, context::Context, Span}; +use oxc_ast::{ast::*, Span}; use oxc_diagnostics::Result; use super::function::FunctionKind; use super::list::{AssertEntries, ExportNamedSpecifiers, ImportSpecifierList}; -use crate::{diagnostics, lexer::Kind, list::SeparatedList, Parser}; +use crate::{diagnostics, lexer::Kind, list::SeparatedList, Context, Parser}; impl<'a> Parser<'a> { /// [Import Call](https://tc39.es/ecma262/#sec-import-calls) diff --git a/crates/oxc_parser/src/js/statement.rs b/crates/oxc_parser/src/js/statement.rs index 64a6eb30f..f36168cb9 100644 --- a/crates/oxc_parser/src/js/statement.rs +++ b/crates/oxc_parser/src/js/statement.rs @@ -1,9 +1,5 @@ use oxc_allocator::{Box, Vec}; -use oxc_ast::{ - ast::*, - context::{Context, StatementContext}, - Span, -}; +use oxc_ast::{ast::*, Span}; use oxc_diagnostics::Result; use super::{ @@ -11,7 +7,12 @@ use super::{ grammar::CoverGrammar, list::SwitchCases, }; -use crate::{diagnostics, lexer::Kind, list::NormalList, Parser}; +use crate::{ + diagnostics, + lexer::Kind, + list::NormalList, + Parser, {Context, StatementContext}, +}; impl<'a> Parser<'a> { /// `https://tc39.es/ecma262/#prod-StatementList` diff --git a/crates/oxc_parser/src/lib.rs b/crates/oxc_parser/src/lib.rs index 4abd58bfd..085fcac03 100644 --- a/crates/oxc_parser/src/lib.rs +++ b/crates/oxc_parser/src/lib.rs @@ -4,6 +4,7 @@ #![feature(portable_simd)] #![feature(slice_as_chunks)] +mod context; mod cursor; mod list; mod state; @@ -17,8 +18,9 @@ mod lexer; use std::rc::Rc; +use context::{Context, StatementContext}; use oxc_allocator::Allocator; -use oxc_ast::{ast::Program, context::Context, AstBuilder, SourceType, Span, Trivias}; +use oxc_ast::{ast::Program, AstBuilder, ModuleKind, SourceType, Span, Trivias}; use oxc_diagnostics::{Error, Result}; use crate::{ @@ -77,7 +79,7 @@ impl<'a> Parser<'a> { token: Token::default(), prev_token_end: 0, state: ParserState::new(allocator), - ctx: source_type.default_context(), + ctx: Self::default_context(source_type), ast: AstBuilder::new(allocator), } } @@ -123,6 +125,16 @@ impl<'a> Parser<'a> { Ok(self.ast.program(span, directives, statements, self.source_type)) } + #[must_use] + pub fn default_context(source_type: SourceType) -> Context { + let ctx = Context::default().and_ambient(source_type.is_typescript_definition()); + match source_type.module_kind() { + ModuleKind::Script => ctx, + // for [top-level-await](https://tc39.es/proposal-top-level-await/) + ModuleKind::Module => ctx.and_await(true), + } + } + /// Check for Flow declaration if the file cannot be parsed. /// The declaration must be [on the first line before any code](https://flow.org/en/docs/usage/#toc-prepare-your-code-for-flow) fn flow_error(&self) -> Option { diff --git a/crates/oxc_parser/src/ts/statement.rs b/crates/oxc_parser/src/ts/statement.rs index f628ef38b..87a6c1702 100644 --- a/crates/oxc_parser/src/ts/statement.rs +++ b/crates/oxc_parser/src/ts/statement.rs @@ -1,16 +1,18 @@ use oxc_allocator::Box; -use oxc_ast::{ast::*, context::StatementContext, Span}; +use oxc_ast::{ast::*, Span}; use oxc_diagnostics::Result; use super::{ list::{TSEnumMemberList, TSInterfaceOrObjectBodyList}, types::ModifierFlags, }; -use crate::js::declaration::{VariableDeclarationContext, VariableDeclarationParent}; -use crate::js::function::FunctionKind; -use crate::lexer::Kind; -use crate::list::{NormalList, SeparatedList}; -use crate::Parser; +use crate::{ + js::declaration::{VariableDeclarationContext, VariableDeclarationParent}, + js::function::FunctionKind, + lexer::Kind, + list::{NormalList, SeparatedList}, + Parser, StatementContext, +}; impl<'a> Parser<'a> { /** ------------------- Enum ------------------ */ diff --git a/crates/oxc_parser/src/ts/types.rs b/crates/oxc_parser/src/ts/types.rs index 7a82d90b8..b97c6c2d7 100644 --- a/crates/oxc_parser/src/ts/types.rs +++ b/crates/oxc_parser/src/ts/types.rs @@ -1,6 +1,6 @@ use bitflags::bitflags; use oxc_allocator::{Box, Vec}; -use oxc_ast::{ast::*, context::Context}; +use oxc_ast::ast::*; use oxc_diagnostics::Result; use super::list::{ @@ -11,7 +11,7 @@ use crate::{ js::list::{ArrayPatternList, ObjectPatternProperties}, lexer::Kind, list::{NormalList, SeparatedList}, - Parser, + Context, Parser, }; bitflags! {