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: support TypeScript constructor params #267

Merged
merged 1 commit into from
Jul 26, 2022
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
4 changes: 2 additions & 2 deletions lib/deno_doc.generated.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @generated file from wasmbuild -- do not edit
// deno-lint-ignore-file
// deno-fmt-ignore-file
// source-hash: 911a6e458fd740647b72cf4831d372e83c774f78
// source-hash: d4e51733e1388110eb7303b860a8d43a5bafe503
let wasm;

const cachedTextDecoder = new TextDecoder("utf-8", {
Expand Down Expand Up @@ -392,7 +392,7 @@ const imports = {
__wbindgen_throw: function (arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
},
__wbindgen_closure_wrapper1046: function (arg0, arg1, arg2) {
__wbindgen_closure_wrapper1058: function (arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 196, __wbg_adapter_16);
return addHeapObject(ret);
},
Expand Down
Binary file modified lib/deno_doc_bg.wasm
Binary file not shown.
8 changes: 7 additions & 1 deletion lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,19 @@ export interface ClassDef {
decorators?: DecoratorDef[];
}

export type ClassConstructorParamDef = ParamDef & {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: seems more consistent to use an interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't here, because ParamDef is a tagged union type alias and interfaces can't extend type aliases.

accessibility?: Accessibility;
isOverride?: boolean;
readonly?: boolean;
};

export interface ClassConstructorDef {
jsDoc?: JsDoc;
accessibility?: Accessibility;
isOptional?: boolean;
hasBody?: boolean;
name: string;
params: ParamDef[];
params: ClassConstructorParamDef[];
location: Location;
}

Expand Down
51 changes: 45 additions & 6 deletions src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ cfg_if! {
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ClassConstructorParamDef {
#[serde(skip_serializing_if = "Option::is_none")]
pub accessibility: Option<deno_ast::swc::ast::Accessibility>,
#[serde(skip_serializing_if = "is_false")]
pub is_override: bool,
#[serde(flatten)]
pub param: ParamDef,
#[serde(skip_serializing_if = "is_false")]
pub readonly: bool,
}

#[cfg(feature = "rust")]
impl Display for ClassConstructorParamDef {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(
f,
"{}{}{}{}",
display_override(self.is_override),
display_accessibility(self.accessibility, true),
display_readonly(self.readonly),
self.param
)
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ClassConstructorDef {
Expand All @@ -61,7 +88,7 @@ pub struct ClassConstructorDef {
#[serde(skip_serializing_if = "is_false")]
pub has_body: bool,
pub name: String,
pub params: Vec<ParamDef>,
pub params: Vec<ClassConstructorParamDef>,
pub location: Location,
}

Expand All @@ -71,7 +98,7 @@ impl Display for ClassConstructorDef {
write!(
f,
"{}{}({})",
display_accessibility(self.accessibility),
display_accessibility(self.accessibility, false),
colors::magenta("constructor"),
SliceDisplayer::new(&self.params, ", ", false),
)
Expand Down Expand Up @@ -120,7 +147,7 @@ impl Display for ClassPropertyDef {
"{}{}{}{}{}{}{}",
display_abstract(self.is_abstract),
display_override(self.is_override),
display_accessibility(self.accessibility),
display_accessibility(self.accessibility, false),
display_static(self.is_static),
display_readonly(self.readonly),
colors::bold(&self.name),
Expand Down Expand Up @@ -194,7 +221,7 @@ impl Display for ClassMethodDef {
"{}{}{}{}{}{}{}{}{}({})",
display_abstract(self.is_abstract),
display_override(self.is_override),
display_accessibility(self.accessibility),
display_accessibility(self.accessibility, false),
display_static(self.is_static),
display_async(self.function_def.is_async),
display_method(self.kind),
Expand Down Expand Up @@ -268,17 +295,29 @@ pub fn class_to_class_def(
use deno_ast::swc::ast::ParamOrTsParamProp::*;

let param_def = match param {
Param(param) => param_to_param_def(parsed_source, param),
Param(param) => ClassConstructorParamDef {
accessibility: None,
is_override: false,
param: param_to_param_def(parsed_source, param),
readonly: false,
},
TsParamProp(ts_param_prop) => {
use deno_ast::swc::ast::TsParamPropParam;

match &ts_param_prop.param {
let param = match &ts_param_prop.param {
TsParamPropParam::Ident(ident) => {
ident_to_param_def(Some(parsed_source), ident)
}
TsParamPropParam::Assign(assign_pat) => {
assign_pat_to_param_def(Some(parsed_source), assign_pat)
}
};

ClassConstructorParamDef {
accessibility: ts_param_prop.accessibility,
is_override: ts_param_prop.is_override,
param,
readonly: ts_param_prop.readonly,
}
}
};
Expand Down
11 changes: 6 additions & 5 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ cfg_if! {
}

pub(crate) fn display_accessibility(
accessibility: Option<deno_ast::swc::ast::Accessibility>,
accessibility: Option<deno_ast::swc::ast::Accessibility>, show_public: bool
) -> impl Display {
colors::magenta(
match accessibility.unwrap_or(deno_ast::swc::ast::Accessibility::Public) {
deno_ast::swc::ast::Accessibility::Public => "",
deno_ast::swc::ast::Accessibility::Protected => "protected ",
deno_ast::swc::ast::Accessibility::Private => "private ",
match accessibility {
None => "",
Some(deno_ast::swc::ast::Accessibility::Public) => if show_public { "public " } else { "" },
Some(deno_ast::swc::ast::Accessibility::Protected) => "protected ",
Some(deno_ast::swc::ast::Accessibility::Private) => "private ",
},
)
}
Expand Down
81 changes: 79 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,7 @@ export class Foobar extends Fizz implements Buzz, Aldrin {
}
},
{
"accessibility": "private",
"name": "private2",
"kind": "identifier",
"optional": false,
Expand All @@ -1289,6 +1290,7 @@ export class Foobar extends Fizz implements Buzz, Aldrin {
}
},
{
"accessibility": "protected",
"name": "protected2",
"kind": "identifier",
"optional": false,
Expand Down Expand Up @@ -2306,6 +2308,79 @@ export let tpl = `foobarbaz`;
]
);

json_test!(export_class_ctor_properties,
r#"
export class A {
constructor(public readonly name: string, private private: number, public override public: boolean) {}
}"#;
[{
"kind": "class",
"name": "A",
"location": {
"filename": "file:///test.ts",
"line": 2,
"col": 0,
},
"declarationKind": "export",
"classDef": {
"isAbstract": false,
"constructors": [{
"accessibility": null,
"hasBody": true,
"name": "constructor",
"params": [
{
"accessibility": "public",
"kind": "identifier",
"name": "name",
"optional": false,
"tsType": {
"repr": "string",
"kind": "keyword",
"keyword": "string",
},
"readonly": true,
},
{
"accessibility": "private",
"kind": "identifier",
"name": "private",
"optional": false,
"tsType": {
"repr": "number",
"kind": "keyword",
"keyword": "number",
},
},
{
"accessibility": "public",
"isOverride": true,
"kind": "identifier",
"name": "public",
"optional": false,
"tsType": {
"repr": "boolean",
"kind": "keyword",
"keyword": "boolean",
}
}
],
"location": {
"filename": "file:///test.ts",
"line": 3,
"col": 2,
},
}],
"properties": [],
"indexSignatures": [],
"methods": [],
"extends": null,
"implements": [],
"typeParams": [],
"superTypeParams": [],
}
}]);

json_test!(export_default_class,
r#"
/** Class doc */
Expand Down Expand Up @@ -2352,6 +2427,7 @@ export default class Foobar {
}
},
{
"accessibility": "private",
"name": "private2",
"kind": "identifier",
"optional": false,
Expand All @@ -2362,6 +2438,7 @@ export default class Foobar {
}
},
{
"accessibility": "protected",
"name": "protected2",
"kind": "identifier",
"optional": false,
Expand Down Expand Up @@ -4655,10 +4732,10 @@ export class Class {
contains_test!(class_constructor,
r#"
export class Class {
constructor(a, b) {}
constructor(public a, readonly b) {}
}
"#;
"constructor(a, b)"
"constructor(public a, readonly b)"
);

contains_test!(class_details,
Expand Down