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(napi): isolated-declaration #3718

Merged
merged 1 commit into from
Jun 17, 2024
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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{env, path::Path};
use oxc_allocator::Allocator;
use oxc_ast::Trivias;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_isolated_declarations::TransformerDts;
use oxc_isolated_declarations::IsolatedDeclarations;
use oxc_parser::Parser;
use oxc_span::SourceType;

Expand Down Expand Up @@ -33,7 +33,7 @@ fn main() {
println!("Original:\n");
println!("{source_text}\n");

let ret = TransformerDts::new(&allocator).build(&ret.program);
let ret = IsolatedDeclarations::new(&allocator).build(&ret.program);
let printed =
Codegen::<false>::new("", &source_text, Trivias::default(), CodegenOptions::default())
.build(&ret.program)
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use oxc_span::{GetSpan, SPAN};

use crate::{
diagnostics::{computed_property_name, extends_clause_expression},
TransformerDts,
IsolatedDeclarations,
};

impl<'a> TransformerDts<'a> {
impl<'a> IsolatedDeclarations<'a> {
pub fn is_literal_key(&self, key: &PropertyKey<'a>) -> bool {
match key {
PropertyKey::StringLiteral(_)
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_isolated_declarations/src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{GetSpan, SPAN};
use oxc_syntax::scope::ScopeFlags;

use crate::{diagnostics::signature_computed_property_name, TransformerDts};
use crate::{diagnostics::signature_computed_property_name, IsolatedDeclarations};

impl<'a> TransformerDts<'a> {
impl<'a> IsolatedDeclarations<'a> {
pub fn transform_variable_declaration(
&self,
decl: &VariableDeclaration<'a>,
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<'a> TransformerDts<'a> {
}
}

impl<'a> Visit<'a> for TransformerDts<'a> {
impl<'a> Visit<'a> for IsolatedDeclarations<'a> {
fn visit_ts_method_signature(&mut self, signature: &TSMethodSignature<'a>) {
self.report_signature_property_key(&signature.key, signature.computed);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use oxc_syntax::{
};
use rustc_hash::FxHashMap;

use crate::{diagnostics::enum_member_initializers, TransformerDts};
use crate::{diagnostics::enum_member_initializers, IsolatedDeclarations};

#[derive(Debug, Clone)]
enum ConstantValue {
Number(f64),
String(String),
}

impl<'a> TransformerDts<'a> {
impl<'a> IsolatedDeclarations<'a> {
pub fn transform_ts_enum_declaration(
&mut self,
decl: &TSEnumDeclaration<'a>,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use oxc_ast::ast::Function;
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::SPAN;

use crate::TransformerDts;
use crate::IsolatedDeclarations;

impl<'a> TransformerDts<'a> {
impl<'a> IsolatedDeclarations<'a> {
pub fn transform_function(&mut self, func: &Function<'a>) -> Option<Box<'a, Function<'a>>> {
if func.modifiers.is_contains_declare() {
None
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/inferrer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use oxc_span::{GetSpan, SPAN};

use crate::{
diagnostics::function_must_have_explicit_return_type, return_type::FunctionReturnType,
TransformerDts,
IsolatedDeclarations,
};

impl<'a> TransformerDts<'a> {
impl<'a> IsolatedDeclarations<'a> {
pub fn infer_type_from_expression(&self, expr: &Expression<'a>) -> Option<TSType<'a>> {
match expr {
Expression::BooleanLiteral(_) => Some(self.ctx.ast.ts_boolean_keyword(SPAN)),
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{SourceType, SPAN};
use scope::ScopeTree;

pub struct TransformerDtsReturn<'a> {
pub struct IsolatedDeclarationsReturn<'a> {
pub program: Program<'a>,
pub errors: Vec<OxcDiagnostic>,
}

pub struct TransformerDts<'a> {
pub struct IsolatedDeclarations<'a> {
ctx: Ctx<'a>,
scope: ScopeTree<'a>,
}

impl<'a> TransformerDts<'a> {
impl<'a> IsolatedDeclarations<'a> {
pub fn new(allocator: &'a Allocator) -> Self {
let ctx = Rc::new(TransformDtsCtx::new(allocator));
Self { ctx, scope: ScopeTree::new(allocator) }
Expand All @@ -46,16 +46,16 @@ impl<'a> TransformerDts<'a> {
/// # Errors
///
/// Returns `Vec<Error>` if any errors were collected during the transformation.
pub fn build(mut self, program: &Program<'a>) -> TransformerDtsReturn<'a> {
pub fn build(mut self, program: &Program<'a>) -> IsolatedDeclarationsReturn<'a> {
let source_type = SourceType::default().with_module(true).with_typescript_definition(true);
let directives = self.ctx.ast.new_vec();
let stmts = self.transform_program(program);
let program = self.ctx.ast.program(SPAN, source_type, directives, None, stmts);
TransformerDtsReturn { program, errors: self.ctx.take_errors() }
IsolatedDeclarationsReturn { program, errors: self.ctx.take_errors() }
}
}

impl<'a> TransformerDts<'a> {
impl<'a> IsolatedDeclarations<'a> {
pub fn transform_program(
&mut self,
program: &Program<'a>,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use oxc_allocator::Box;
use oxc_ast::Visit;
use oxc_span::{GetSpan, SPAN};

use crate::TransformerDts;
use crate::IsolatedDeclarations;

impl<'a> TransformerDts<'a> {
impl<'a> IsolatedDeclarations<'a> {
pub fn transform_export_named_declaration(
&mut self,
decl: &ExportNamedDeclaration<'a>,
Expand Down
7 changes: 5 additions & 2 deletions crates/oxc_isolated_declarations/src/return_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use oxc_ast::{
use oxc_span::{Atom, GetSpan};
use oxc_syntax::scope::ScopeFlags;

use crate::{context::Ctx, diagnostics::type_containing_private_name, TransformerDts};
use crate::{context::Ctx, diagnostics::type_containing_private_name, IsolatedDeclarations};

/// Infer return type from return statement. Does not support multiple return statements.
pub struct FunctionReturnType<'a> {
Expand All @@ -23,7 +23,10 @@ pub struct FunctionReturnType<'a> {
}

impl<'a> FunctionReturnType<'a> {
pub fn infer(transformer: &TransformerDts<'a>, body: &FunctionBody<'a>) -> Option<TSType<'a>> {
pub fn infer(
transformer: &IsolatedDeclarations<'a>,
body: &FunctionBody<'a>,
) -> Option<TSType<'a>> {
let mut visitor = FunctionReturnType {
ctx: Rc::clone(&transformer.ctx),
return_expression: None,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_isolated_declarations/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use oxc_ast::ast::{
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::SPAN;

use crate::TransformerDts;
use crate::IsolatedDeclarations;

impl<'a> TransformerDts<'a> {
impl<'a> IsolatedDeclarations<'a> {
pub fn transform_function_to_ts_type(&self, func: &Function<'a>) -> Option<TSType<'a>> {
let return_type = self.infer_function_return_type(func);
let params = self.transform_formal_parameters(&func.params);
Expand Down
2 changes: 2 additions & 0 deletions napi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
*.node
39 changes: 39 additions & 0 deletions napi/isolated-declarations/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "oxc_isolated_declarations_napi"
version = "0.0.0"
publish = false
authors.workspace = true
description.workspace = true
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
categories.workspace = true

[lints]
workspace = true

[lib]
crate-type = ["cdylib"]
test = false
doctest = false

[dependencies]
oxc_allocator = { workspace = true }
oxc_parser = { workspace = true }
oxc_ast = { workspace = true }
oxc_span = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_codegen = { workspace = true }
oxc_isolated_declarations = { workspace = true }

napi = { workspace = true }
napi-derive = { workspace = true }

[package.metadata.cargo-shear]
ignored = ["napi"]

[build-dependencies]
napi-build = { workspace = true }
3 changes: 3 additions & 0 deletions napi/isolated-declarations/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
napi_build::setup();
}
11 changes: 11 additions & 0 deletions napi/isolated-declarations/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* tslint:disable */
/* eslint-disable */

/* auto-generated by NAPI-RS */

export interface IsolatedDeclarationsResult {
sourceText: string
errors: Array<string>
}
/** TypeScript Isolated Declarations for Standalone DTS Emit */
export function isolatedDeclaration(filename: string, sourceText: string): IsolatedDeclarationsResult
Loading
Loading