Skip to content

Commit

Permalink
feat(semantic): distinguish type imports in ModuleRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Mar 22, 2024
1 parent 1c07a99 commit 6113ba4
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast/js.rs
Expand Up @@ -6,6 +6,7 @@ use std::{cell::Cell, fmt, hash::Hash};
use oxc_allocator::{Box, Vec};
use oxc_span::{Atom, CompactStr, SourceType, Span};
use oxc_syntax::{
module_record::ImportOrExportKind,
operator::{
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
},
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast/mod.rs
Expand Up @@ -6,3 +6,4 @@ mod literal;
mod ts;

pub use self::{js::*, jsx::*, literal::*, ts::*};
pub use oxc_syntax::module_record::ImportOrExportKind;
19 changes: 1 addition & 18 deletions crates/oxc_ast/src/ast/ts.rs
Expand Up @@ -8,6 +8,7 @@

use oxc_allocator::{Box, Vec};
use oxc_span::{Atom, GetSpan, Span};
use oxc_syntax::module_record::ImportOrExportKind;
#[cfg(feature = "serialize")]
use serde::Serialize;
#[cfg(feature = "serialize")]
Expand Down Expand Up @@ -1079,24 +1080,6 @@ pub struct TSInstantiationExpression<'a> {
pub type_parameters: Box<'a, TSTypeParameterInstantiation<'a>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
pub enum ImportOrExportKind {
Value,
Type,
}

impl ImportOrExportKind {
pub fn is_value(&self) -> bool {
matches!(self, Self::Value)
}

pub fn is_type(&self) -> bool {
matches!(self, Self::Type)
}
}

// [`JSDoc`](https://github.com/microsoft/TypeScript/blob/54a554d8af2657630307cbfa8a3e4f3946e36507/src/compiler/types.ts#L393)

#[derive(Debug, Hash)]
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast_builder.rs
Expand Up @@ -10,6 +10,7 @@ use std::mem;
use oxc_allocator::{Allocator, Box, String, Vec};
use oxc_span::{Atom, GetSpan, SourceType, Span, SPAN};
use oxc_syntax::{
module_record::ImportOrExportKind,
operator::{
AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator,
},
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_parser/src/js/list.rs
Expand Up @@ -6,6 +6,7 @@ use oxc_diagnostics::{
Result,
};
use oxc_span::{Atom, CompactStr, GetSpan, Span};
use oxc_syntax::module_record::ImportOrExportKind;
use rustc_hash::FxHashMap;

use crate::{
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_parser/src/js/module.rs
Expand Up @@ -2,6 +2,7 @@ use oxc_allocator::{Box, Vec};
use oxc_ast::ast::*;
use oxc_diagnostics::Result;
use oxc_span::Span;
use oxc_syntax::module_record::ImportOrExportKind;

use super::{
function::FunctionKind,
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_parser/src/ts/statement.rs
Expand Up @@ -2,6 +2,7 @@ use oxc_allocator::Box;
use oxc_ast::ast::*;
use oxc_diagnostics::Result;
use oxc_span::Span;
use oxc_syntax::module_record::ImportOrExportKind;

use super::{
list::{TSEnumMemberList, TSInterfaceOrObjectBodyList},
Expand Down
14 changes: 10 additions & 4 deletions crates/oxc_semantic/src/module_record/builder.rs
Expand Up @@ -170,33 +170,39 @@ impl ModuleRecordBuilder {
}

fn visit_import_declaration(&mut self, decl: &ImportDeclaration) {
if decl.import_kind.is_type() {
return;
}
let module_request = NameSpan::new(decl.source.value.to_compact_str(), decl.source.span);

if let Some(specifiers) = &decl.specifiers {
for specifier in specifiers {
let (import_name, local_name) = match specifier {
let (import_name, local_name, import_kind) = match specifier {
ImportDeclarationSpecifier::ImportSpecifier(specifier) => (
ImportImportName::Name(NameSpan::new(
specifier.imported.name().to_compact_str(),
specifier.imported.span(),
)),
NameSpan::new(specifier.local.name.to_compact_str(), specifier.local.span),
if decl.import_kind.is_type() {
decl.import_kind
} else {
specifier.import_kind
},
),
ImportDeclarationSpecifier::ImportNamespaceSpecifier(specifier) => (
ImportImportName::NamespaceObject,
NameSpan::new(specifier.local.name.to_compact_str(), specifier.local.span),
decl.import_kind,
),
ImportDeclarationSpecifier::ImportDefaultSpecifier(specifier) => (
ImportImportName::Default(specifier.span),
NameSpan::new(specifier.local.name.to_compact_str(), specifier.local.span),
decl.import_kind,
),
};
self.add_import_entry(ImportEntry {
module_request: module_request.clone(),
import_name,
local_name,
import_kind,
});
}
}
Expand Down
24 changes: 24 additions & 0 deletions crates/oxc_syntax/src/module_record.rs
Expand Up @@ -5,6 +5,10 @@ use std::{fmt, hash::BuildHasherDefault, path::PathBuf, sync::Arc};
use dashmap::DashMap;
use indexmap::IndexMap;
use rustc_hash::{FxHashMap, FxHasher};
#[cfg(feature = "serialize")]
use serde::Serialize;
#[cfg(feature = "serialize")]
use tsify::Tsify;

use oxc_span::{CompactStr, Span};

Expand Down Expand Up @@ -148,6 +152,8 @@ pub struct ImportEntry {

/// The name that is used to locally access the imported value from within the importing module.
pub local_name: NameSpan,

pub import_kind: ImportOrExportKind,
}

/// `ImportName` For `ImportEntry`
Expand Down Expand Up @@ -265,6 +271,24 @@ pub struct FunctionMeta {
pub deprecated: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(rename_all = "camelCase"))]
pub enum ImportOrExportKind {
Value,
Type,
}

impl ImportOrExportKind {
pub fn is_value(&self) -> bool {
matches!(self, Self::Value)
}

pub fn is_type(&self) -> bool {
matches!(self, Self::Type)
}
}

#[cfg(test)]
mod test {
use super::{ExportExportName, ExportLocalName, ImportImportName, NameSpan};
Expand Down

0 comments on commit 6113ba4

Please sign in to comment.