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

Infer from type statements #164

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
52 changes: 43 additions & 9 deletions extractor/src/doc_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
tags::{validate_tags, Tag},
};
use full_moon::{
ast::{self, punctuated::Punctuated, Stmt},
ast::{self, luau::{TypeDeclaration, TypeInfo}, punctuated::Punctuated, Stmt},
node::Node,
};

Expand Down Expand Up @@ -40,6 +40,7 @@ enum DocEntryKind {
Type {
within: String,
name: String,
type_info: Option<TypeInfo>,
},
Class {
name: String,
Expand Down Expand Up @@ -101,12 +102,14 @@ fn get_explicit_kind(tags: &[Tag]) -> Result<Option<DocEntryKind>, Diagnostic> {
return Ok(Some(DocEntryKind::Type {
name: type_tag.name.as_str().to_owned(),
within: get_within_tag(tags, tag)?,
type_info: None,
}))
}
Tag::Interface(interface_tag) => {
return Ok(Some(DocEntryKind::Type {
name: interface_tag.name.as_str().to_owned(),
within: get_within_tag(tags, tag)?,
type_info: None,
}))
}
_ => (),
Expand Down Expand Up @@ -146,6 +149,26 @@ where
Ok(())
}

fn parse_type_declaration(
declaration: &TypeDeclaration,
doc_comment: &DocComment,
within_tag: Option<&crate::tags::WithinTag<'_>>
) -> Result<DocEntryKind, Diagnostic> {
let name = declaration.type_name().to_string();
EliTheGingerCat marked this conversation as resolved.
Show resolved Hide resolved

let within = if let Some(within) = within_tag {
within.name.as_str().to_owned()
} else {
return Err(doc_comment.diagnostic("Type requires @within tag"));
};

Ok(DocEntryKind::Type {
name,
within,
type_info: Some(declaration.type_definition().to_owned()),
})
}

fn determine_kind(
doc_comment: &DocComment,
stmt: Option<&Stmt>,
Expand Down Expand Up @@ -307,6 +330,14 @@ fn determine_kind(
_ => Err(doc_comment.diagnostic("Expression must be a function")),
}
}
Some(Stmt::TypeDeclaration(declaration)) => {
parse_type_declaration(declaration, doc_comment, within_tag)
}
Some(Stmt::ExportedTypeDeclaration(export_declaration)) => {
let declaration = export_declaration.type_declaration();

parse_type_declaration(declaration, doc_comment, within_tag)
}

_ => Err(doc_comment
.diagnostic("Explicitly specify a kind tag, like @function, @prop, or @class.")),
Expand Down Expand Up @@ -445,14 +476,17 @@ impl<'a> DocEntry<'a> {
})?),
all_tags,
),
DocEntryKind::Type { within, name } => (
DocEntry::Type(TypeDocEntry::parse(DocEntryParseArguments {
within: Some(within),
name,
desc,
tags,
source: doc_comment,
})?),
DocEntryKind::Type { within, name , type_info} => (
DocEntry::Type(TypeDocEntry::parse(
DocEntryParseArguments {
within: Some(within),
name,
desc,
tags,
source: doc_comment,
},
type_info,
)?),
all_tags,
),
DocEntryKind::Class { name } => (
Expand Down
23 changes: 21 additions & 2 deletions extractor/src/doc_entry/type_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
serde_util::is_false,
tags::{CustomTag, ExternalTag, FieldTag, Tag},
};
use full_moon::ast::luau::TypeInfo;
use serde::Serialize;

use super::DocEntryParseArguments;
Expand Down Expand Up @@ -55,7 +56,10 @@ pub struct TypeDocEntry<'a> {
}

impl<'a> TypeDocEntry<'a> {
pub(super) fn parse(args: DocEntryParseArguments<'a>) -> Result<Self, Diagnostics> {
pub(super) fn parse(
args: DocEntryParseArguments<'a>,
type_info: Option<TypeInfo>,
) -> Result<Self, Diagnostics> {
let DocEntryParseArguments {
name,
desc,
Expand Down Expand Up @@ -83,7 +87,7 @@ impl<'a> TypeDocEntry<'a> {
for tag in tags {
match tag {
Tag::Type(type_tag) => {
doc_entry.lua_type = Some(type_tag.lua_type.as_str().to_owned())
doc_entry.lua_type = Some(type_tag.lua_type.as_str().to_owned());
}

Tag::Field(field_tag) => doc_entry.fields.push(field_tag.into()),
Expand All @@ -107,6 +111,21 @@ impl<'a> TypeDocEntry<'a> {
return Err(Diagnostics::from(diagnostics));
}

if let Some(type_info) = type_info {
match type_info {
TypeInfo::Table { fields, .. } => {
for field in fields {
doc_entry.fields.push(Field {
name: field.key().to_string(),
EliTheGingerCat marked this conversation as resolved.
Show resolved Hide resolved
lua_type: field.value().to_string(),
desc: String::new(),
EliTheGingerCat marked this conversation as resolved.
Show resolved Hide resolved
});
}
},
_ => doc_entry.lua_type = Some(type_info.to_string())
EliTheGingerCat marked this conversation as resolved.
Show resolved Hide resolved
}
}

Ok(doc_entry)
}
}
33 changes: 33 additions & 0 deletions extractor/test-input/passing/type_statement_inference.lua
EliTheGingerCat marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--- @class TypeStatementInference
--- Infer types from type statements.

--- @within TypeStatementInference
--- Table gets converted to interface.
type baseData = {
id: number
}

--- @within TypeStatementInference
--- Everything else uses a basic string representation.
type pet = "cat" | "dog"

--- @within TypeStatementInference
--- Unions and intersections are not marked as interfaces.
type petData = baseData & {
pet: pet
}

--- @within TypeStatementInference
--- Exported types work the same.
export type response = petData?

--- @within TypeStatementInference
--- @type petFetcher (id: number) -> response
--- \@type can override.
type randomFetcher = any

--- @within TypeStatementInference
--- @interface petLibrary
--- @field toPet (baseData) -> petData
--- \@interface can override.
type randomLibrary = any
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: tests/test-inputs.rs
expression: stderr
---

EliTheGingerCat marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
source: tests/test-inputs.rs
expression: stdout
---
[
{
"functions": [],
"properties": [],
"types": [
{
"name": "baseData ",
"desc": "Table gets converted to interface.\r",
"fields": [
{
"name": "\tid",
EliTheGingerCat marked this conversation as resolved.
Show resolved Hide resolved
"lua_type": "number\r\n",
"desc": ""
}
],
"source": {
"line": 6,
"path": ""
}
},
{
"name": "pet ",
"desc": "Everything else uses a basic string representation.\r",
"lua_type": "\"cat\" | \"dog\"\r\n",
"source": {
"line": 12,
"path": ""
}
},
{
"name": "petData ",
"desc": "Unions and intersections are not marked as interfaces.\r",
"lua_type": "baseData & {\r\n\tpet: pet\r\n}\r\n",
"source": {
"line": 16,
"path": ""
}
},
{
"name": "response ",
"desc": "Exported types work the same.\r",
"lua_type": "petData?\r\n",
"source": {
"line": 22,
"path": ""
}
},
{
"name": "petFetcher",
"desc": "\\@type can override.\r",
"lua_type": "(id: number) -> response",
"source": {
"line": 27,
"path": ""
}
},
{
"name": "petLibrary",
"desc": "\\@interface can override.\r",
"fields": [
{
"name": "toPet",
"lua_type": "(baseData) -> petData",
"desc": ""
}
],
"source": {
"line": 33,
"path": ""
}
}
],
"name": "TypeStatementInference",
"desc": "Infer types from type statements.\r",
"source": {
"line": 3,
"path": ""
}
}
]
5 changes: 5 additions & 0 deletions extractor/tests/test-inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ fn anomymous_function_assignment() -> anyhow::Result<()> {
run_moonwave("passing/anonymous_function_assignment.lua", 0)
}

#[test]
fn type_statement_inference() -> anyhow::Result<()> {
run_moonwave("passing/type_statement_inference.lua", 0)
}

#[test]
fn failing_anomymous_function_assignment() -> anyhow::Result<()> {
run_moonwave("failing/anonymous_function_assignment.lua", 1)
Expand Down