mirror of
https://github.com/danbulant/oxc
synced 2026-05-22 13:48:55 +00:00
feat(isolated_declarations): print jsdoc comments (#5858)
This commit is contained in:
parent
c96b712f6b
commit
dfbde2c602
6 changed files with 61 additions and 23 deletions
|
|
@ -3550,6 +3550,7 @@ impl<'a> Gen for TSInterfaceDeclaration<'a> {
|
||||||
p.print_curly_braces(self.body.span, self.body.body.is_empty(), |p| {
|
p.print_curly_braces(self.body.span, self.body.body.is_empty(), |p| {
|
||||||
for item in &self.body.body {
|
for item in &self.body.body {
|
||||||
p.print_indent();
|
p.print_indent();
|
||||||
|
p.print_leading_comments(item.span().start);
|
||||||
item.print(p, ctx);
|
item.print(p, ctx);
|
||||||
p.print_semicolon();
|
p.print_semicolon();
|
||||||
p.print_soft_newline();
|
p.print_soft_newline();
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
use std::{env, path::Path};
|
use std::{env, path::Path};
|
||||||
|
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::Allocator;
|
||||||
use oxc_codegen::CodeGenerator;
|
use oxc_codegen::{CodeGenerator, CommentOptions};
|
||||||
use oxc_isolated_declarations::IsolatedDeclarations;
|
use oxc_isolated_declarations::IsolatedDeclarations;
|
||||||
use oxc_parser::Parser;
|
use oxc_parser::Parser;
|
||||||
use oxc_span::SourceType;
|
use oxc_span::SourceType;
|
||||||
|
|
@ -32,8 +32,15 @@ fn main() {
|
||||||
println!("Original:\n");
|
println!("Original:\n");
|
||||||
println!("{source_text}\n");
|
println!("{source_text}\n");
|
||||||
|
|
||||||
let ret = IsolatedDeclarations::new(&allocator).build(&ret.program);
|
let id_ret = IsolatedDeclarations::new(&allocator).build(&ret.program);
|
||||||
let printed = CodeGenerator::new().build(&ret.program).source_text;
|
let printed = CodeGenerator::new()
|
||||||
|
.enable_comment(
|
||||||
|
&source_text,
|
||||||
|
ret.trivias,
|
||||||
|
CommentOptions { preserve_annotate_comments: false },
|
||||||
|
)
|
||||||
|
.build(&id_ret.program)
|
||||||
|
.source_text;
|
||||||
|
|
||||||
println!("Dts Emit:\n");
|
println!("Dts Emit:\n");
|
||||||
println!("{printed}\n");
|
println!("{printed}\n");
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,12 @@ use crate::{diagnostics::default_export_inferred, IsolatedDeclarations};
|
||||||
impl<'a> IsolatedDeclarations<'a> {
|
impl<'a> IsolatedDeclarations<'a> {
|
||||||
pub fn transform_export_named_declaration(
|
pub fn transform_export_named_declaration(
|
||||||
&mut self,
|
&mut self,
|
||||||
decl: &ExportNamedDeclaration<'a>,
|
prev_decl: &ExportNamedDeclaration<'a>,
|
||||||
) -> Option<ExportNamedDeclaration<'a>> {
|
) -> Option<ExportNamedDeclaration<'a>> {
|
||||||
let decl = self.transform_declaration(decl.declaration.as_ref()?, false)?;
|
let decl = self.transform_declaration(prev_decl.declaration.as_ref()?, false)?;
|
||||||
|
|
||||||
Some(self.ast.export_named_declaration(
|
Some(self.ast.export_named_declaration(
|
||||||
decl.span(),
|
prev_decl.span,
|
||||||
Some(decl),
|
Some(decl),
|
||||||
self.ast.vec(),
|
self.ast.vec(),
|
||||||
None,
|
None,
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ mod deno;
|
||||||
use std::{fs, path::Path, sync::Arc};
|
use std::{fs, path::Path, sync::Arc};
|
||||||
|
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::Allocator;
|
||||||
use oxc_codegen::CodeGenerator;
|
use oxc_codegen::{CodeGenerator, CommentOptions};
|
||||||
use oxc_isolated_declarations::IsolatedDeclarations;
|
use oxc_isolated_declarations::IsolatedDeclarations;
|
||||||
use oxc_parser::Parser;
|
use oxc_parser::Parser;
|
||||||
use oxc_span::SourceType;
|
use oxc_span::SourceType;
|
||||||
|
|
@ -11,15 +11,22 @@ use oxc_span::SourceType;
|
||||||
fn transform(path: &Path, source_text: &str) -> String {
|
fn transform(path: &Path, source_text: &str) -> String {
|
||||||
let allocator = Allocator::default();
|
let allocator = Allocator::default();
|
||||||
let source_type = SourceType::from_path(path).unwrap();
|
let source_type = SourceType::from_path(path).unwrap();
|
||||||
let program = Parser::new(&allocator, source_text, source_type).parse().program;
|
let parser_ret = Parser::new(&allocator, source_text, source_type).parse();
|
||||||
|
|
||||||
let ret = IsolatedDeclarations::new(&allocator).build(&program);
|
let id_ret = IsolatedDeclarations::new(&allocator).build(&parser_ret.program);
|
||||||
let code = CodeGenerator::new().build(&ret.program).source_text;
|
let code = CodeGenerator::new()
|
||||||
|
.enable_comment(
|
||||||
|
source_text,
|
||||||
|
parser_ret.trivias,
|
||||||
|
CommentOptions { preserve_annotate_comments: false },
|
||||||
|
)
|
||||||
|
.build(&id_ret.program)
|
||||||
|
.source_text;
|
||||||
|
|
||||||
let mut snapshot = format!("==================== .D.TS ====================\n\n{code}\n\n");
|
let mut snapshot = format!("==================== .D.TS ====================\n\n{code}\n\n");
|
||||||
if !ret.errors.is_empty() {
|
if !id_ret.errors.is_empty() {
|
||||||
let source = Arc::new(source_text.to_string());
|
let source = Arc::new(source_text.to_string());
|
||||||
let error_messages = ret
|
let error_messages = id_ret
|
||||||
.errors
|
.errors
|
||||||
.iter()
|
.iter()
|
||||||
.map(|d| d.clone().with_source_code(Arc::clone(&source)))
|
.map(|d| d.clone().with_source_code(Arc::clone(&source)))
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use napi_derive::napi;
|
use napi_derive::napi;
|
||||||
use oxc_allocator::Allocator;
|
use oxc_allocator::Allocator;
|
||||||
use oxc_codegen::CodegenReturn;
|
use oxc_codegen::{CodegenReturn, CommentOptions};
|
||||||
use oxc_isolated_declarations::IsolatedDeclarations;
|
use oxc_isolated_declarations::IsolatedDeclarations;
|
||||||
use oxc_span::SourceType;
|
use oxc_span::SourceType;
|
||||||
|
|
||||||
|
|
@ -47,5 +47,11 @@ pub fn isolated_declaration(
|
||||||
pub(crate) fn build_declarations(ctx: &TransformContext<'_>) -> CodegenReturn {
|
pub(crate) fn build_declarations(ctx: &TransformContext<'_>) -> CodegenReturn {
|
||||||
let transformed_ret = IsolatedDeclarations::new(ctx.allocator).build(&ctx.program());
|
let transformed_ret = IsolatedDeclarations::new(ctx.allocator).build(&ctx.program());
|
||||||
ctx.add_diagnostics(transformed_ret.errors);
|
ctx.add_diagnostics(transformed_ret.errors);
|
||||||
ctx.codegen().build(&transformed_ret.program)
|
ctx.codegen()
|
||||||
|
.enable_comment(
|
||||||
|
ctx.source_text(),
|
||||||
|
ctx.trivias.clone(),
|
||||||
|
CommentOptions { preserve_annotate_comments: false },
|
||||||
|
)
|
||||||
|
.build(&transformed_ret.program)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,23 +4,38 @@ import oxc from './index.js';
|
||||||
console.log(`Testing on ${process.platform}-${process.arch}`);
|
console.log(`Testing on ${process.platform}-${process.arch}`);
|
||||||
|
|
||||||
function test(ret, expected) {
|
function test(ret, expected) {
|
||||||
console.log(ret.code);
|
|
||||||
console.log(ret.map);
|
|
||||||
for (const error of ret.errors) {
|
|
||||||
console.log(error);
|
|
||||||
}
|
|
||||||
assert.equal(ret.code, expected.code);
|
assert.equal(ret.code, expected.code);
|
||||||
assert.deepEqual(ret.map, expected.map);
|
assert.deepEqual(ret.map, expected.map);
|
||||||
assert(ret.errors.length == 0);
|
assert(ret.errors.length == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
test(oxc.isolatedDeclaration('test.ts', 'class A {}', { sourcemap: true }), {
|
const id = `
|
||||||
code: 'declare class A {}\n',
|
/**
|
||||||
|
* jsdoc 1
|
||||||
|
*/
|
||||||
|
export class A {
|
||||||
|
/**
|
||||||
|
* jsdoc 2
|
||||||
|
*/
|
||||||
|
foo = "bar";
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
test(oxc.isolatedDeclaration('test.ts', id, { sourcemap: true }), {
|
||||||
|
code: '/**\n' +
|
||||||
|
'* jsdoc 1\n' +
|
||||||
|
'*/\n' +
|
||||||
|
'export declare class A {\n' +
|
||||||
|
'\t/**\n' +
|
||||||
|
'\t* jsdoc 2\n' +
|
||||||
|
'\t*/\n' +
|
||||||
|
'\tfoo: string;\n' +
|
||||||
|
'}\n',
|
||||||
map: {
|
map: {
|
||||||
mappings: 'AAAA,cAAM,EAAE,CAAE',
|
mappings: ';;;AAIA,OAAO,cAAM,EAAE;;;;CAIb;AACD',
|
||||||
names: [],
|
names: [],
|
||||||
sources: ['test.ts'],
|
sources: ['test.ts'],
|
||||||
sourcesContent: ['class A {}'],
|
sourcesContent: [id],
|
||||||
version: 3,
|
version: 3,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
@ -67,3 +82,5 @@ test(
|
||||||
'$RefreshReg$(_c, "App");\n',
|
'$RefreshReg$(_c, "App");\n',
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log('Success.');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue