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

fix(compiler): inherit parent docstring in case no docstring was provided in child member #7172

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion packages/@winglang/wingc/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ fn render_docs(markdown: &mut CodeMaker, docs: &Docs) {
// Psuedo-abstract marker, mostly useful internally
| "abstract"
// Marker type use, not for users
| "skipDocs" | "wingType"
| "skipDocs" | "wingType" | "noinflight"
) {
return;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/@winglang/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ const WINGSDK_SIM_IRESOURCE_FQN: &'static str = formatcp!(
assembly = WINGSDK_ASSEMBLY_NAME,
iface = WINGSDK_SIM_IRESOURCE
);
const WINGSDK_RESOURCE_FQN: &'static str = formatcp!(
"{assembly}.{class}",
assembly = WINGSDK_ASSEMBLY_NAME,
class = WINGSDK_RESOURCE
);

const CONSTRUCT_BASE_CLASS: &'static str = "constructs.Construct";
const CONSTRUCT_BASE_INTERFACE: &'static str = "constructs.IConstruct";
Expand Down
91 changes: 91 additions & 0 deletions packages/@winglang/wingc/src/lsp/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,97 @@ Json.stringify({});
"#,
);

test_hover_list!(
parent_class_docs_are_inherited_on_declaration,
r#"
/// Some parent docs
class Parent {
/// Some method docs
pub method() {}
}

class Child extends Parent {
//^
pub method() {}
}

new Child().method();
"#,
);

test_hover_list!(
parent_class_docs_are_inherited_on_method_declaration,
r#"
/// Some parent docs
class Parent {
/// Some method docs
pub method() {}
}

class Child extends Parent {
pub method() {}
//^
}

new Child().method();
"#,
);

test_hover_list!(
parent_class_docs_are_inherited_on_call,
r#"
/// Some parent docs
class Parent {
/// Some method docs
pub method() {}
}

class Child extends Parent {
pub method() {}
}

new Child().method();
//^
"#,
);

test_hover_list!(
parent_class_docs_are_inherited_on_method_call,
r#"
/// Some parent docs
class Parent {
/// Some method docs
pub method() {}
}

class Child extends Parent {
pub method() {}
//^
}

new Child().method();
//^
"#,
);

test_hover_list!(
parent_class_docs_are_overridden,
r#"
/// Some parent docs
class Parent {
/// Some method docs
pub method() {}
}

/// Some child docs
class Child extends Parent {
}

new Child().method();
//^
"#,
);

test_hover_list!(
ignoe_empty_lines_in_doc,
r#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ source: packages/@winglang/wingc/src/lsp/completions.rs
- label: this
kind: 6
detail: "inflight (): void"
documentation:
kind: markdown
value: "Shared behavior between all Wing SDK resources.\n\n*@noinflight*"
sortText: bb|this
- label: x
kind: 6
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nnew(): Child\n```\n---\nSome parent docs"
range:
start:
line: 11
character: 4
end:
line: 11
character: 9

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nclass Child extends Parent {\n method(): void;\n}\n```\n---\nSome parent docs"
range:
start:
line: 7
character: 6
end:
line: 7
character: 11

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\npreflight method(): void\n```\n---\nSome method docs"
range:
start:
line: 8
character: 6
end:
line: 8
character: 12

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\npreflight method(): void\n```\n---\nSome method docs"
range:
start:
line: 8
character: 6
end:
line: 8
character: 12

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nnew(): Child\n```\n---\nSome child docs"
range:
start:
line: 11
character: 4
end:
line: 11
character: 9

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/hover.rs
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
source: libs/wingc/src/lsp/hover.rs
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nclass Foo {\n // No public members\n}\n```"
value: "```wing\nclass Foo {\n // No public members\n}\n```\n---\nShared behavior between all Wing SDK resources.\n\n*@noinflight*"
range:
start:
line: 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/hover.rs
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
source: libs/wingc/src/lsp/hover.rs
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nclass Foo {\n // No public members\n}\n```"
value: "```wing\nclass Foo {\n // No public members\n}\n```\n---\nShared behavior between all Wing SDK resources.\n\n*@noinflight*"
range:
start:
line: 4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
source: libs/wingc/src/lsp/hover.rs
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
value: "```wing\nclass Foo {\n // No public members\n}\n```"
value: "```wing\nclass Foo {\n // No public members\n}\n```\n---\nShared behavior between all Wing SDK resources.\n\n*@noinflight*"
range:
start:
line: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: libs/wingc/src/lsp/hover.rs
source: packages/@winglang/wingc/src/lsp/hover.rs
---
contents:
kind: markdown
Expand Down
45 changes: 39 additions & 6 deletions packages/@winglang/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::ast::{
};
use crate::comp_ctx::{CompilationContext, CompilationPhase};
use crate::diagnostic::{report_diagnostic, Diagnostic, DiagnosticAnnotation, DiagnosticSeverity, TypeError, WingSpan};
use crate::docs::Docs;
use crate::docs::{Docs, Documented};
use crate::file_graph::{File, FileGraph};
use crate::parser::normalize_path;
use crate::type_check::has_type_stmt::HasStatementVisitor;
Expand All @@ -31,8 +31,8 @@ use crate::{
debug, CONSTRUCT_BASE_CLASS, CONSTRUCT_BASE_INTERFACE, CONSTRUCT_NODE_PROPERTY, DEFAULT_PACKAGE_NAME,
UTIL_CLASS_NAME, WINGSDK_APP, WINGSDK_ARRAY, WINGSDK_ASSEMBLY_NAME, WINGSDK_BRINGABLE_MODULES, WINGSDK_BYTES,
WINGSDK_DATETIME, WINGSDK_DURATION, WINGSDK_GENERIC, WINGSDK_IRESOURCE, WINGSDK_JSON, WINGSDK_MAP, WINGSDK_MUT_ARRAY,
WINGSDK_MUT_JSON, WINGSDK_MUT_MAP, WINGSDK_MUT_SET, WINGSDK_NODE, WINGSDK_REGEX, WINGSDK_RESOURCE, WINGSDK_SET,
WINGSDK_SIM_IRESOURCE_FQN, WINGSDK_STD_MODULE, WINGSDK_STRING, WINGSDK_STRUCT,
WINGSDK_MUT_JSON, WINGSDK_MUT_MAP, WINGSDK_MUT_SET, WINGSDK_NODE, WINGSDK_REGEX, WINGSDK_RESOURCE,
WINGSDK_RESOURCE_FQN, WINGSDK_SET, WINGSDK_SIM_IRESOURCE_FQN, WINGSDK_STD_MODULE, WINGSDK_STRING, WINGSDK_STRUCT,
};
use camino::{Utf8Path, Utf8PathBuf};
use derivative::Derivative;
Expand Down Expand Up @@ -4157,7 +4157,7 @@ This value is set by the CLI at compile time and can be used to conditionally co
let interface_spec = Interface {
name: iface.name.clone(),
fqn: format!("{}.{}", self.base_fqn_for_current_file(), iface.name),
docs: doc.as_ref().map_or(Docs::default(), |s| Docs::with_summary(s)),
docs: doc.as_ref().map_or(Docs::default(), |s: &String| Docs::with_summary(s)),
env: dummy_env,
extends: extend_interfaces.clone(),
phase: iface.phase,
Expand Down Expand Up @@ -4748,6 +4748,21 @@ This value is set by the CLI at compile time and can be used to conditionally co
}
}

let mut default_docs = Docs::default();
// if parent docs exist we use them as the defualt
if let Some(parent_class) = parent_class {
// New classes defined in Wing shouldn't inherit docs from std.Resource
let is_parent_resource = parent_class
.as_class()
.map(|c| c.fqn.as_deref() == Some(WINGSDK_RESOURCE_FQN))
.unwrap_or(false);
if !is_parent_resource {
if let Some(parent_docs) = parent_class.docs() {
default_docs = parent_docs.clone();
}
}
}

// Create the resource/class type and add it to the current environment (so class implementation can reference itself)
let class_spec = Class {
name: ast_class.name.clone(),
Expand All @@ -4758,7 +4773,7 @@ This value is set by the CLI at compile time and can be used to conditionally co
is_abstract: false,
phase: ast_class.phase,
defined_in_phase: env.phase,
docs: stmt.doc.as_ref().map_or(Docs::default(), |s| Docs::with_summary(s)),
docs: stmt.doc.as_ref().map_or(default_docs, |s| Docs::with_summary(s)),
std_construct_args: ast_class.phase == Phase::Preflight,
lifts: None,
uid: self.types.class_counter,
Expand Down Expand Up @@ -4821,6 +4836,7 @@ This value is set by the CLI at compile time and can be used to conditionally co
method_def.access,
&mut class_env,
method_name,
parent_class,
);
method_types.insert(&method_name, method_type);
}
Expand All @@ -4839,6 +4855,7 @@ This value is set by the CLI at compile time and can be used to conditionally co
ast_class.initializer.access,
&mut class_env,
&init_symb,
parent_class,
);
method_types.insert(&init_symb, init_func_type);

Expand All @@ -4857,6 +4874,7 @@ This value is set by the CLI at compile time and can be used to conditionally co
ast_class.inflight_initializer.access,
&mut class_env,
&inflight_init_symb,
parent_class,
);
method_types.insert(&inflight_init_symb, inflight_init_func_type);

Expand Down Expand Up @@ -5759,6 +5777,7 @@ This value is set by the CLI at compile time and can be used to conditionally co
access: AccessModifier,
class_env: &mut SymbolEnv,
method_name: &Symbol,
parent_class: Option<UnsafeRef<Type>>,
) {
// Modify the method's type based on the fact we know it's a method and not just a function
let method_sig = method_type
Expand Down Expand Up @@ -5829,6 +5848,16 @@ This value is set by the CLI at compile time and can be used to conditionally co

let method_phase = method_type.as_function_sig().unwrap().phase;

// use the parent's method docs as default, if exist.
let mut default_method_docs = None;
if let Some(parent_class) = parent_class {
if let Some(c) = parent_class.as_class() {
if let Some(parent_method) = c.methods(true).find(|m| m.1.name.eq(&method_name)) {
default_method_docs = parent_method.1.docs;
}
}
};

match class_env.define(
method_name,
SymbolKind::make_member_variable(
Expand All @@ -5838,7 +5867,11 @@ This value is set by the CLI at compile time and can be used to conditionally co
instance_type.is_none(),
method_phase,
access,
method_def.doc.as_ref().map(|s| Docs::with_summary(s)),
method_def
.doc
.as_ref()
.map(|s| Docs::with_summary(s))
.or(default_method_docs),
),
access,
StatementIdx::Top,
Expand Down
Loading