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: separate constructors from methods for type literals and interfaces #568

Merged
merged 3 commits into from
Apr 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,18 @@ impl<'a, 'b> DiagnosticDocNodeVisitor<'a, 'b> {
}

fn visit_interface_def(&mut self, def: &crate::interface::InterfaceDef) {
// constructors
for constructor in &def.constructors {
self
.diagnostics
.check_missing_js_doc(&constructor.js_doc, &constructor.location);
self.diagnostics.check_missing_return_type(
constructor.return_type.as_ref(),
&constructor.js_doc,
&constructor.location,
);
}

// properties
for prop in &def.properties {
self
Expand Down
17 changes: 9 additions & 8 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use serde::Serialize;

use crate::params::ts_fn_param_to_param_def;
use crate::ts_type::CallSignatureDef;
use crate::ts_type::ConstructorDef;
use crate::ts_type::IndexSignatureDef;
use crate::ts_type::MethodDef;
use crate::ts_type::PropertyDef;
Expand All @@ -23,6 +24,8 @@ pub struct InterfaceDef {
/// set when the interface is a default export
pub def_name: Option<String>,
pub extends: Vec<TsTypeDef>,
#[serde(default)]
pub constructors: Vec<ConstructorDef>,
pub methods: Vec<MethodDef>,
pub properties: Vec<PropertyDef>,
pub call_signatures: Vec<CallSignatureDef>,
Expand Down Expand Up @@ -78,6 +81,7 @@ pub fn get_doc_for_ts_interface_decl(
) -> (String, InterfaceDef) {
let interface_name = interface_decl.id.sym.to_string();

let mut constructors = vec![];
let mut methods = vec![];
let mut properties = vec![];
let mut call_signatures = vec![];
Expand Down Expand Up @@ -266,7 +270,7 @@ pub fn get_doc_for_ts_interface_decl(
}
}
TsConstructSignatureDecl(ts_construct_sig) => {
if let Some(construct_js_doc) =
if let Some(js_doc) =
js_doc_for_range(parsed_source, &ts_construct_sig.range())
{
let mut params = vec![];
Expand All @@ -286,19 +290,15 @@ pub fn get_doc_for_ts_interface_decl(
.as_ref()
.map(|rt| TsTypeDef::new(parsed_source, &rt.type_ann));

let construct_sig_def = MethodDef {
name: "new".to_string(),
kind: deno_ast::swc::ast::MethodKind::Method,
js_doc: construct_js_doc,
let construct_sig_def = ConstructorDef {
js_doc,
location: get_location(parsed_source, ts_construct_sig.start()),
computed: false,
optional: false,
params,
return_type: maybe_return_type,
type_params,
};

methods.push(construct_sig_def);
constructors.push(construct_sig_def);
}
}
}
Expand All @@ -318,6 +318,7 @@ pub fn get_doc_for_ts_interface_decl(
let interface_def = InterfaceDef {
def_name,
extends,
constructors,
methods,
properties,
call_signatures,
Expand Down
4 changes: 4 additions & 0 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ impl<'a> DocPrinter<'a> {
) -> FmtResult {
let interface_def = node.interface_def.as_ref().unwrap();

for constructor in &interface_def.constructors {
writeln!(w, "{}{}", Indent(1), constructor)?;
self.format_jsdoc(w, &constructor.js_doc, 2)?;
}
for property_def in &interface_def.properties {
writeln!(w, "{}{}", Indent(1), property_def)?;
self.format_jsdoc(w, &property_def.js_doc, 2)?;
Expand Down
3 changes: 3 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ export { Hello } from "./reexport.ts";
"declarationKind": "export",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [],
"callSignatures": [],
Expand Down Expand Up @@ -937,6 +938,7 @@ async fn json_module() {
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"constructors": [],
"methods": [],
"properties": [{
"name": "a",
Expand Down Expand Up @@ -1031,6 +1033,7 @@ async fn json_module() {
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"constructors": [],
"methods": [],
"properties": [{
"name": "a",
Expand Down
34 changes: 28 additions & 6 deletions src/ts_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ impl TsTypeDef {
}

fn ts_type_lit(parsed_source: &ParsedSource, other: &TsTypeLit) -> Self {
let mut constructors = vec![];
let mut methods = vec![];
let mut properties = vec![];
let mut call_signatures = vec![];
Expand Down Expand Up @@ -538,25 +539,22 @@ impl TsTypeDef {
.as_ref()
.map(|rt| TsTypeDef::new(parsed_source, &rt.type_ann));

let construct_sig_def = MethodDef {
name: "new".to_string(),
let construct_sig_def = ConstructorDef {
js_doc: prop_js_doc,
kind: MethodKind::Method,
location: get_location(parsed_source, ts_construct_sig.start()),
computed: false,
optional: false,
params,
return_type: maybe_return_type,
type_params,
};

methods.push(construct_sig_def);
constructors.push(construct_sig_def);
}
}
}
}

let type_literal = TsTypeLiteralDef {
constructors,
methods,
properties,
call_signatures,
Expand Down Expand Up @@ -927,6 +925,28 @@ pub struct TsMappedTypeDef {
pub ts_type: Option<Box<TsTypeDef>>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ConstructorDef {
#[serde(skip_serializing_if = "JsDoc::is_empty", default)]
pub js_doc: JsDoc,
pub params: Vec<ParamDef>,
pub return_type: Option<TsTypeDef>,
pub type_params: Vec<TsTypeParamDef>,
pub location: Location,
}

impl Display for ConstructorDef {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(
f,
"{}({})",
colors::magenta("constructor"),
SliceDisplayer::new(&self.params, ", ", false),
)
}
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct MethodDef {
Expand Down Expand Up @@ -1081,6 +1101,8 @@ impl Display for IndexSignatureDef {
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TsTypeLiteralDef {
#[serde(default)]
pub constructors: Vec<ConstructorDef>,
pub methods: Vec<MethodDef>,
pub properties: Vec<PropertyDef>,
pub call_signatures: Vec<CallSignatureDef>,
Expand Down
1 change: 1 addition & 0 deletions tests/specs/Jsdoc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ interface B
},
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [],
"callSignatures": [],
Expand Down
1 change: 1 addition & 0 deletions tests/specs/destructuring_assignment_object.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private const rest
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"constructors": [],
"methods": [],
"properties": [
{
Expand Down
1 change: 1 addition & 0 deletions tests/specs/destructuring_assignment_object_assignment.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private const obj: { a: string; b: string; }
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"constructors": [],
"methods": [],
"properties": [
{
Expand Down
1 change: 1 addition & 0 deletions tests/specs/export_class_object_extends.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Bar extends obj.Foo
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"constructors": [],
"methods": [],
"properties": [
{
Expand Down
1 change: 1 addition & 0 deletions tests/specs/export_const_basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ const tpl2: string
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"constructors": [],
"methods": [
{
"name": "get",
Expand Down
1 change: 1 addition & 0 deletions tests/specs/export_default_interface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface default
"interfaceDef": {
"defName": "Reader",
"extends": [],
"constructors": [],
"methods": [
{
"name": "read",
Expand Down
1 change: 1 addition & 0 deletions tests/specs/export_fn2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ private interface AssignOpts
"declarationKind": "private",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [
{
Expand Down
3 changes: 3 additions & 0 deletions tests/specs/export_interface.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ interface Reader extends Foo, Bar
}
}
],
"constructors": [],
"methods": [
{
"name": "read",
Expand Down Expand Up @@ -199,6 +200,7 @@ interface Reader extends Foo, Bar
"declarationKind": "private",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [],
"callSignatures": [],
Expand All @@ -218,6 +220,7 @@ interface Reader extends Foo, Bar
"declarationKind": "private",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [],
"callSignatures": [],
Expand Down
1 change: 1 addition & 0 deletions tests/specs/export_interface2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ interface TypedIface<T>
"declarationKind": "export",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [
{
"name": "something",
Expand Down
1 change: 1 addition & 0 deletions tests/specs/export_interface_accessors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ interface Thing
"declarationKind": "export",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [
{
"name": "size",
Expand Down
25 changes: 12 additions & 13 deletions tests/specs/export_type_alias_literal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ error[missing-jsdoc]: exported symbol is missing JSDoc documentation
# output.txt
Defined in file:///mod.ts:1:1

type A = { new(d: string): A; a(): void; b?(): void; c(): string; c(v: number); }
type A = { a(): void; b?(): void; c(): string; c(v: number); }


# output.json
Expand All @@ -38,16 +38,8 @@ type A = { new(d: string): A; a(): void; b?(): void; c(): string; c(v: number);
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"methods": [
"constructors": [
{
"name": "new",
"kind": "method",
"location": {
"filename": "file:///mod.ts",
"line": 2,
"col": 2,
"byteIndex": 20
},
"params": [
{
"kind": "identifier",
Expand All @@ -60,7 +52,6 @@ type A = { new(d: string): A; a(): void; b?(): void; c(): string; c(v: number);
}
}
],
"optional": false,
"returnType": {
"repr": "A",
"kind": "typeRef",
Expand All @@ -69,8 +60,16 @@ type A = { new(d: string): A; a(): void; b?(): void; c(): string; c(v: number);
"typeName": "A"
}
},
"typeParams": []
},
"typeParams": [],
"location": {
"filename": "file:///mod.ts",
"line": 2,
"col": 2,
"byteIndex": 20
}
}
],
"methods": [
{
"name": "a",
"kind": "method",
Expand Down
2 changes: 2 additions & 0 deletions tests/specs/exports_all_with_private.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private namespace H
"declarationKind": "export",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [],
"callSignatures": [],
Expand Down Expand Up @@ -215,6 +216,7 @@ private namespace H
"declarationKind": "private",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [],
"callSignatures": [],
Expand Down
1 change: 1 addition & 0 deletions tests/specs/generic_instantiated_with_tuple_type.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface Generic<T>
"declarationKind": "export",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [],
"callSignatures": [],
Expand Down
1 change: 1 addition & 0 deletions tests/specs/import_equals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface Options
"declarationKind": "export",
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [],
"callSignatures": [],
Expand Down
1 change: 1 addition & 0 deletions tests/specs/indented_with_tabs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace Tabs
},
"interfaceDef": {
"extends": [],
"constructors": [],
"methods": [],
"properties": [
{
Expand Down
2 changes: 2 additions & 0 deletions tests/specs/infer_object_literal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const a: { d(e: string): void; h(): string; h(value: string); [[t]](u: string):
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"constructors": [],
"methods": [
{
"name": "d",
Expand Down Expand Up @@ -219,6 +220,7 @@ const a: { d(e: string): void; h(): string; h(value: string); [[t]](u: string):
"repr": "",
"kind": "typeLiteral",
"typeLiteral": {
"constructors": [],
"methods": [],
"properties": [
{
Expand Down