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

This commit is contained in:
Dunqing 2024-02-05 17:43:30 +08:00 committed by GitHub
parent a3570d41f0
commit d571839ab8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 44 additions and 19 deletions

View file

@ -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.
@ -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,
@ -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(),

View file

@ -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) => {
@ -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>) {

View file

@ -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) => {
@ -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>) {

View file

@ -1,4 +1,4 @@
use oxc_ast::{ast::ModuleDeclaration, AstKind};
use oxc_ast::AstKind;
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
@ -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(_)

View file

@ -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);
@ -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(_) => {

View file

@ -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)));