feat(isolated_declarations): print jsdoc comments (#5858)

This commit is contained in:
Boshen 2024-09-18 14:09:00 +00:00
parent c96b712f6b
commit dfbde2c602
6 changed files with 61 additions and 23 deletions

View file

@ -3550,6 +3550,7 @@ impl<'a> Gen for TSInterfaceDeclaration<'a> {
p.print_curly_braces(self.body.span, self.body.body.is_empty(), |p| {
for item in &self.body.body {
p.print_indent();
p.print_leading_comments(item.span().start);
item.print(p, ctx);
p.print_semicolon();
p.print_soft_newline();

View file

@ -2,7 +2,7 @@
use std::{env, path::Path};
use oxc_allocator::Allocator;
use oxc_codegen::CodeGenerator;
use oxc_codegen::{CodeGenerator, CommentOptions};
use oxc_isolated_declarations::IsolatedDeclarations;
use oxc_parser::Parser;
use oxc_span::SourceType;
@ -32,8 +32,15 @@ fn main() {
println!("Original:\n");
println!("{source_text}\n");
let ret = IsolatedDeclarations::new(&allocator).build(&ret.program);
let printed = CodeGenerator::new().build(&ret.program).source_text;
let id_ret = IsolatedDeclarations::new(&allocator).build(&ret.program);
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!("{printed}\n");

View file

@ -8,12 +8,12 @@ use crate::{diagnostics::default_export_inferred, IsolatedDeclarations};
impl<'a> IsolatedDeclarations<'a> {
pub fn transform_export_named_declaration(
&mut self,
decl: &ExportNamedDeclaration<'a>,
prev_decl: &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(
decl.span(),
prev_decl.span,
Some(decl),
self.ast.vec(),
None,

View file

@ -3,7 +3,7 @@ mod deno;
use std::{fs, path::Path, sync::Arc};
use oxc_allocator::Allocator;
use oxc_codegen::CodeGenerator;
use oxc_codegen::{CodeGenerator, CommentOptions};
use oxc_isolated_declarations::IsolatedDeclarations;
use oxc_parser::Parser;
use oxc_span::SourceType;
@ -11,15 +11,22 @@ use oxc_span::SourceType;
fn transform(path: &Path, source_text: &str) -> String {
let allocator = Allocator::default();
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 code = CodeGenerator::new().build(&ret.program).source_text;
let id_ret = IsolatedDeclarations::new(&allocator).build(&parser_ret.program);
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");
if !ret.errors.is_empty() {
if !id_ret.errors.is_empty() {
let source = Arc::new(source_text.to_string());
let error_messages = ret
let error_messages = id_ret
.errors
.iter()
.map(|d| d.clone().with_source_code(Arc::clone(&source)))

View file

@ -1,6 +1,6 @@
use napi_derive::napi;
use oxc_allocator::Allocator;
use oxc_codegen::CodegenReturn;
use oxc_codegen::{CodegenReturn, CommentOptions};
use oxc_isolated_declarations::IsolatedDeclarations;
use oxc_span::SourceType;
@ -47,5 +47,11 @@ pub fn isolated_declaration(
pub(crate) fn build_declarations(ctx: &TransformContext<'_>) -> CodegenReturn {
let transformed_ret = IsolatedDeclarations::new(ctx.allocator).build(&ctx.program());
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)
}

View file

@ -4,23 +4,38 @@ import oxc from './index.js';
console.log(`Testing on ${process.platform}-${process.arch}`);
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.deepEqual(ret.map, expected.map);
assert(ret.errors.length == 0);
}
test(oxc.isolatedDeclaration('test.ts', 'class A {}', { sourcemap: true }), {
code: 'declare class A {}\n',
const id = `
/**
* 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: {
mappings: 'AAAA,cAAM,EAAE,CAAE',
mappings: ';;;AAIA,OAAO,cAAM,EAAE;;;;CAIb;AACD',
names: [],
sources: ['test.ts'],
sourcesContent: ['class A {}'],
sourcesContent: [id],
version: 3,
},
});
@ -67,3 +82,5 @@ test(
'$RefreshReg$(_c, "App");\n',
},
);
console.log('Success.');