Skip to content

Commit

Permalink
fix: correct interface properties and methods (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Nov 29, 2021
1 parent e4ceb94 commit cc14e65
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 13 deletions.
Binary file modified lib/deno_doc_bg.wasm
Binary file not shown.
5 changes: 5 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export interface InterfaceMethodDef {
kind: MethodKind;
location: Location;
jsDoc?: JsDoc;
computed?: boolean;
optional: boolean;
params: ParamDef[];
returnType?: TsTypeDef;
Expand All @@ -203,6 +204,7 @@ export interface InterfacePropertyDef {
location: Location;
jsDoc?: JsDoc;
params: ParamDef[];
readonly?: boolean;
computed: boolean;
optional: boolean;
tsType?: TsTypeDef;
Expand Down Expand Up @@ -357,7 +359,9 @@ export interface LiteralIndexSignatureDef {

export interface LiteralMethodDef {
name: string;
kind: MethodKind;
params: ParamDef[];
computed?: boolean;
optional: boolean;
returnType?: TsTypeDef;
typeParams: TsTypeParamDef[];
Expand All @@ -366,6 +370,7 @@ export interface LiteralMethodDef {
export interface LiteralPropertyDef {
name: string;
params: ParamDef[];
readonly?: boolean;
computed: boolean;
optional: boolean;
tsType?: TsTypeDef;
Expand Down
1 change: 0 additions & 1 deletion src/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub fn magenta<S: AsRef<str>>(s: S) -> impl fmt::Display {
style(s, style_spec)
}

#[cfg(feature = "rust")]
pub fn bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_bold(true);
Expand Down
8 changes: 8 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ impl<T: Display> Display for SliceDisplayer<'_, T> {
}
}

pub(crate) fn display_computed(is_computed: bool, name: &str) -> impl Display {
colors::bold(if is_computed {
format!("[{}]", name)
} else {
name.to_string()
})
}

pub(crate) fn display_optional(is_optional: bool) -> impl Display {
colors::magenta(if is_optional { "?" } else { "" })
}
Expand Down
26 changes: 21 additions & 5 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::js_doc::JsDoc;
use crate::node::DeclarationKind;
use crate::params::ts_fn_param_to_param_def;
use crate::swc_util::get_location;
use crate::swc_util::is_false;
use crate::swc_util::js_doc_for_span;
use crate::ts_type::ts_type_ann_to_def;
use crate::ts_type::TsTypeDef;
Expand All @@ -21,7 +22,7 @@ use crate::ParamDef;

cfg_if! {
if #[cfg(feature = "rust")] {
use crate::colors;
use crate::display::display_computed;
use crate::display::display_optional;
use crate::display::display_readonly;
use crate::display::SliceDisplayer;
Expand All @@ -40,6 +41,8 @@ pub struct InterfaceMethodDef {
pub location: Location,
#[serde(skip_serializing_if = "JsDoc::is_empty")]
pub js_doc: JsDoc,
#[serde(skip_serializing_if = "is_false")]
pub computed: bool,
pub optional: bool,
pub params: Vec<ParamDef>,
pub return_type: Option<TsTypeDef>,
Expand Down Expand Up @@ -71,7 +74,7 @@ impl Display for InterfaceMethodDef {
write!(
f,
"{}{}({})",
colors::bold(&self.name),
display_computed(self.computed, &self.name),
display_optional(self.optional),
SliceDisplayer::new(&self.params, ", ", false),
)?;
Expand All @@ -90,6 +93,8 @@ pub struct InterfacePropertyDef {
#[serde(skip_serializing_if = "JsDoc::is_empty")]
pub js_doc: JsDoc,
pub params: Vec<ParamDef>,
#[serde(skip_serializing_if = "is_false")]
pub readonly: bool,
pub computed: bool,
pub optional: bool,
pub ts_type: Option<TsTypeDef>,
Expand All @@ -116,8 +121,9 @@ impl Display for InterfacePropertyDef {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(
f,
"{}{}",
colors::bold(&self.name),
"{}{}{}",
display_readonly(self.readonly),
display_computed(self.computed, &self.name),
display_optional(self.optional),
)?;
if let Some(ts_type) = &self.ts_type {
Expand Down Expand Up @@ -253,6 +259,7 @@ pub fn get_doc_for_ts_interface_decl(
kind: deno_ast::swc::ast::MethodKind::Method,
js_doc: method_js_doc,
location: get_location(parsed_source, ts_method_sig.span.lo()),
computed: ts_method_sig.computed,
optional: ts_method_sig.optional,
params,
return_type: maybe_return_type,
Expand All @@ -274,6 +281,7 @@ pub fn get_doc_for_ts_interface_decl(
kind: deno_ast::swc::ast::MethodKind::Getter,
js_doc: method_js_doc,
location: get_location(parsed_source, ts_getter_sig.span.lo()),
computed: ts_getter_sig.computed,
optional: ts_getter_sig.optional,
params: vec![],
return_type: maybe_return_type,
Expand All @@ -295,6 +303,7 @@ pub fn get_doc_for_ts_interface_decl(
kind: deno_ast::swc::ast::MethodKind::Setter,
js_doc: method_js_doc,
location: get_location(parsed_source, ts_setter_sig.span.lo()),
computed: ts_setter_sig.computed,
optional: ts_setter_sig.optional,
params,
return_type: None,
Expand Down Expand Up @@ -328,6 +337,7 @@ pub fn get_doc_for_ts_interface_decl(
location: get_location(parsed_source, ts_prop_sig.span.lo()),
params,
ts_type,
readonly: ts_prop_sig.readonly,
computed: ts_prop_sig.computed,
optional: ts_prop_sig.optional,
type_params,
Expand Down Expand Up @@ -396,14 +406,20 @@ pub fn get_doc_for_ts_interface_decl(
ts_construct_sig.type_params.as_ref(),
);

let maybe_return_type = ts_construct_sig
.type_ann
.as_ref()
.map(|rt| (&*rt.type_ann).into());

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

Expand Down
4 changes: 4 additions & 0 deletions src/swc_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ lazy_static! {
static ref JS_DOC_RE: Regex = Regex::new(r#"\s*\* ?"#).unwrap();
}

pub(crate) fn is_false(b: &bool) -> bool {
!b
}

fn parse_js_doc(js_doc_comment: &Comment) -> JsDoc {
let txt = js_doc_comment
.text
Expand Down
74 changes: 72 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,7 @@ export const tpl2 = `Value: ${num}`;
"typeLiteral":{
"methods":[{
"name":"get",
"kind": "method",
"params":[
{
"name":"key",
Expand Down Expand Up @@ -1592,6 +1593,7 @@ export const tpl2 = `Value: ${num}`;
"typeParams":[]
}, {
"name":"set",
"kind": "method",
"params":[
{
"name":"key",
Expand Down Expand Up @@ -2760,8 +2762,11 @@ export type NumberArray = Array<number>;
json_test!(export_type_alias_literal,
r#"
export type A = {
new (d: string): A;
a(): void;
b?(): void;
get c(): string;
set c(v: number);
};
"#;
[{
Expand All @@ -2780,8 +2785,35 @@ export type A = {
"kind": "typeLiteral",
"typeLiteral": {
"methods": [
{
"name": "new",
"kind": "method",
"params": [
{
"kind": "identifier",
"name": "d",
"optional": false,
"tsType": {
"repr": "string",
"kind": "keyword",
"keyword": "string",
}
}
],
"optional": false,
"returnType": {
"repr": "A",
"kind": "typeRef",
"typeRef": {
"typeParams": null,
"typeName": "A",
}
},
"typeParams": [],
},
{
"name": "a",
"kind": "method",
"params": [],
"optional": false,
"returnType": {
Expand All @@ -2793,6 +2825,7 @@ export type A = {
},
{
"name": "b",
"kind": "method",
"params": [],
"optional": true,
"returnType": {
Expand All @@ -2801,6 +2834,37 @@ export type A = {
"keyword": "void",
},
"typeParams": [],
},
{
"name": "c",
"kind": "getter",
"params": [],
"optional": false,
"returnType": {
"repr": "string",
"kind": "keyword",
"keyword": "string",
},
"typeParams": [],
},
{
"name": "c",
"kind": "setter",
"params": [
{
"kind": "identifier",
"name": "v",
"optional": false,
"tsType": {
"repr": "number",
"kind": "keyword",
"keyword": "number",
}
}
],
"optional": false,
"returnType": null,
"typeParams": [],
}
],
"properties": [],
Expand Down Expand Up @@ -4321,21 +4385,27 @@ export interface Interface {
export interface I {
m(a, b);
mo?(c);
[mc](d);
}
"#;
"m(a, b)",
"mo?(c)"
"mo?(c)",
"[mc](d)"
);

contains_test!(interface_property,
r#"
export interface I {
p: string;
po?: number;
readonly pro: string;
[pc]: string;
}
"#;
"p: string",
"po?: number"
"po?: number",
"readonly pro: string",
"[pc]: string"
);

contains_test!(interface_string_literal_property,
Expand Down
Loading

0 comments on commit cc14e65

Please sign in to comment.