Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ast): enter AstKind::ExportDefaultDeclaration, AstKind::ExportNamedDeclaration and AstKind::ExportAllDeclaration #2317

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
9 changes: 9 additions & 0 deletions crates/oxc_ast/src/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ pub enum AstKind<'a> {
ImportSpecifier(&'a ImportSpecifier),
ImportDefaultSpecifier(&'a ImportDefaultSpecifier),
ImportNamespaceSpecifier(&'a ImportNamespaceSpecifier),
ExportDefaultDeclaration(&'a ExportDefaultDeclaration<'a>),
ExportNamedDeclaration(&'a ExportNamedDeclaration<'a>),
ExportAllDeclaration(&'a ExportAllDeclaration<'a>),

// JSX
// Please make sure to add these to `is_jsx` below.
Expand Down Expand Up @@ -430,6 +433,9 @@ impl<'a> GetSpan for AstKind<'a> {
Self::ImportSpecifier(x) => x.span,
Self::ImportDefaultSpecifier(x) => x.span,
Self::ImportNamespaceSpecifier(x) => x.span,
Self::ExportDefaultDeclaration(x) => x.span,
Self::ExportNamedDeclaration(x) => x.span,
Self::ExportAllDeclaration(x) => x.span,

Self::JSXOpeningElement(x) => x.span,
Self::JSXClosingElement(x) => x.span,
Expand Down Expand Up @@ -615,6 +621,9 @@ impl<'a> AstKind<'a> {
Self::ImportSpecifier(_) => "ImportSpecifier".into(),
Self::ImportDefaultSpecifier(_) => "ImportDefaultSpecifier".into(),
Self::ImportNamespaceSpecifier(_) => "ImportNamespaceSpecifier".into(),
Self::ExportDefaultDeclaration(_) => "ExportDefaultDeclaration".into(),
Self::ExportNamedDeclaration(_) => "ExportNamedDeclaration".into(),
Self::ExportAllDeclaration(_) => "ExportAllDeclaration".into(),
Self::JSXOpeningElement(_) => "JSXOpeningElement".into(),
Self::JSXClosingElement(_) => "JSXClosingElement".into(),
Self::JSXElementName(_) => "JSXElementName".into(),
Expand Down
13 changes: 12 additions & 1 deletion crates/oxc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1382,9 +1382,16 @@ pub trait Visit<'a>: Sized {
self.leave_node(kind);
}

fn visit_export_all_declaration(&mut self, _decl: &ExportAllDeclaration<'a>) {}
fn visit_export_all_declaration(&mut self, decl: &ExportAllDeclaration<'a>) {
let kind = AstKind::ExportAllDeclaration(self.alloc(decl));
self.enter_node(kind);
self.visit_string_literal(&decl.source);
self.leave_node(kind);
}

fn visit_export_default_declaration(&mut self, decl: &ExportDefaultDeclaration<'a>) {
let kind = AstKind::ExportDefaultDeclaration(self.alloc(decl));
self.enter_node(kind);
match &decl.declaration {
ExportDefaultDeclarationKind::Expression(expr) => self.visit_expression(expr),
ExportDefaultDeclarationKind::FunctionDeclaration(func) => {
Expand All @@ -1393,15 +1400,19 @@ pub trait Visit<'a>: Sized {
ExportDefaultDeclarationKind::ClassDeclaration(class) => self.visit_class(class),
_ => {}
}
self.leave_node(kind);
}

fn visit_export_named_declaration(&mut self, decl: &ExportNamedDeclaration<'a>) {
let kind = AstKind::ExportNamedDeclaration(self.alloc(decl));
self.enter_node(kind);
if let Some(decl) = &decl.declaration {
self.visit_declaration(decl);
}
if let Some(ref source) = decl.source {
self.visit_string_literal(source);
}
self.leave_node(kind);
}

fn visit_enum_member(&mut self, member: &TSEnumMember<'a>) {
Expand Down
16 changes: 15 additions & 1 deletion crates/oxc_ast/src/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,9 +1381,16 @@ pub trait VisitMut<'a>: Sized {
self.leave_node(kind);
}

fn visit_export_all_declaration(&mut self, _decl: &mut ExportAllDeclaration<'a>) {}
fn visit_export_all_declaration(&mut self, decl: &mut ExportAllDeclaration<'a>) {
let kind = AstKind::ExportAllDeclaration(self.alloc(decl));
self.enter_node(kind);
self.visit_string_literal(&mut decl.source);
self.leave_node(kind);
}

fn visit_export_default_declaration(&mut self, decl: &mut ExportDefaultDeclaration<'a>) {
let kind = AstKind::ExportDefaultDeclaration(self.alloc(decl));
self.enter_node(kind);
match &mut decl.declaration {
ExportDefaultDeclarationKind::Expression(expr) => self.visit_expression(expr),
ExportDefaultDeclarationKind::FunctionDeclaration(func) => {
Expand All @@ -1392,12 +1399,19 @@ pub trait VisitMut<'a>: Sized {
ExportDefaultDeclarationKind::ClassDeclaration(class) => self.visit_class(class),
_ => {}
}
self.leave_node(kind);
}

fn visit_export_named_declaration(&mut self, decl: &mut ExportNamedDeclaration<'a>) {
let kind = AstKind::ExportNamedDeclaration(self.alloc(decl));
self.enter_node(kind);
if let Some(decl) = &mut decl.declaration {
self.visit_declaration(decl);
}
if let Some(source) = &mut decl.source {
self.visit_string_literal(source);
}
self.leave_node(kind);
}

fn visit_enum_member(&mut self, member: &mut TSEnumMember<'a>) {
Expand Down
8 changes: 3 additions & 5 deletions crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_ast::{ast::ModuleDeclaration, AstKind};
use oxc_ast::AstKind;
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
Expand Down Expand Up @@ -91,10 +91,8 @@ impl Rule for NoInnerDeclarations {
parent_kind,
AstKind::Program(_)
| AstKind::StaticBlock(_)
| AstKind::ModuleDeclaration(
ModuleDeclaration::ExportNamedDeclaration(_)
| ModuleDeclaration::ExportDefaultDeclaration(_)
)
| AstKind::ExportNamedDeclaration(_)
| AstKind::ExportDefaultDeclaration(_)
| AstKind::ForStatementInit(_)
| AstKind::ForInStatement(_)
| AstKind::ForOfStatement(_)
Expand Down
12 changes: 4 additions & 8 deletions crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,10 +1641,8 @@ impl<'a> Visit<'a> for SemanticBuilder<'a> {
impl<'a> SemanticBuilder<'a> {
fn enter_kind(&mut self, kind: AstKind<'a>) {
match kind {
AstKind::ModuleDeclaration(decl) => {
if !matches!(decl, ModuleDeclaration::ImportDeclaration(_)) {
self.current_symbol_flags |= SymbolFlags::Export;
}
AstKind::ExportDefaultDeclaration(_) | AstKind::ExportNamedDeclaration(_) => {
self.current_symbol_flags |= SymbolFlags::Export;
}
AstKind::ImportSpecifier(specifier) => {
specifier.bind(self);
Expand Down Expand Up @@ -1759,10 +1757,8 @@ impl<'a> SemanticBuilder<'a> {
self.current_node_flags -= NodeFlags::Class;
self.class_table_builder.pop_class();
}
AstKind::ModuleDeclaration(decl) => {
if !matches!(decl, ModuleDeclaration::ImportDeclaration(_)) {
self.current_symbol_flags -= SymbolFlags::Export;
}
AstKind::ExportDefaultDeclaration(_) | AstKind::ExportNamedDeclaration(_) => {
self.current_symbol_flags -= SymbolFlags::Export;
}
AstKind::LabeledStatement(_) => self.label_builder.leave(),
AstKind::StaticBlock(_) | AstKind::Function(_) => {
Expand Down
5 changes: 1 addition & 4 deletions crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,7 @@ fn check_class(class: &Class, node: &AstNode<'_>, ctx: &SemanticBuilder<'_>) {

if class.is_declaration()
&& class.id.is_none()
&& !matches!(
ctx.nodes.parent_kind(node.id()),
Some(AstKind::ModuleDeclaration(ModuleDeclaration::ExportDefaultDeclaration(_)))
)
&& !matches!(ctx.nodes.parent_kind(node.id()), Some(AstKind::ExportDefaultDeclaration(_)))
{
let start = class.span.start;
ctx.error(RequireClassName(Span::new(start, start + 5)));
Expand Down