Skip to content
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
75 changes: 72 additions & 3 deletions src/codeblock/playground/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,41 @@ fn extract_inputs<C: comments::Comments>(
let mut result = Vec::new();

for member in &node.body {
let (key, decorators, value) = match member {
let (key, decorators, value, type_) = match member {
ast::ClassMember::AutoAccessor(ast::AutoAccessor {
key: ast::Key::Public(key),
decorators,
value,
type_ann,
..
})
| ast::ClassMember::ClassProp(ast::ClassProp {
key,
decorators,
value,
type_ann,
..
}) => (key, decorators, value),
}) => (
key,
decorators,
value,
type_ann.as_deref().and_then(type_ann_to_input_type),
),

ast::ClassMember::Method(ast::ClassMethod {
kind: ast::MethodKind::Setter,
key,
function,
..
}) => (key, &function.decorators, &None),
}) => (
key,
&function.decorators,
&None,
function
.params
.get(0)
.and_then(|param| extract_type_from_pat(&param.pat)),
),

_ => continue,
};
Expand All @@ -76,6 +91,10 @@ fn extract_inputs<C: comments::Comments>(
}
}

if let Some(type_) = type_ {
config = Some(config.extend(PlaygroundInputConfig::from_type(type_)));
}

let config = config.extend(
value
.as_ref()
Expand All @@ -93,6 +112,56 @@ fn extract_inputs<C: comments::Comments>(
Ok(result)
}

fn extract_type_from_pat(pat: &ast::Pat) -> Option<PlaygroundInputType> {
match pat {
ast::Pat::Object(ast::ObjectPat { type_ann, .. })
| ast::Pat::Ident(ast::BindingIdent { type_ann, .. })
| ast::Pat::Array(ast::ArrayPat { type_ann, .. }) => {
type_ann.as_deref().and_then(type_ann_to_input_type)
}

ast::Pat::Assign(ast::AssignPat { left, right, .. }) => extract_type_from_pat(left)
.or_else(|| evaluate(right).map(PlaygroundInputConfig::type_)),

_ => None,
}
}

fn type_ann_to_input_type(type_ann: &ast::TsTypeAnn) -> Option<PlaygroundInputType> {
match type_ann.type_ann.as_ref() {
ast::TsType::TsKeywordType(ast::TsKeywordType {
kind: ast::TsKeywordTypeKind::TsNumberKeyword,
..
}) => Some(PlaygroundInputType::Number),
ast::TsType::TsKeywordType(ast::TsKeywordType {
kind: ast::TsKeywordTypeKind::TsStringKeyword,
..
}) => Some(PlaygroundInputType::String),
ast::TsType::TsKeywordType(ast::TsKeywordType {
kind: ast::TsKeywordTypeKind::TsBooleanKeyword,
..
}) => Some(PlaygroundInputType::Boolean),

ast::TsType::TsUnionOrIntersectionType(ast::TsUnionOrIntersectionType::TsUnionType(
ast::TsUnionType { types, .. },
)) => {
let string_types: Vec<_> = types
.iter()
.filter_map(|t| t.as_ts_lit_type())
.filter_map(|t| t.lit.as_str())
.map(|v| v.value.to_string())
.collect();

if string_types.len() == types.len() {
Some(PlaygroundInputType::Enum(string_types))
} else {
None
}
}
_ => None,
}
}

fn extract_actions<C: comments::Comments>(
node: &ast::Class,
comments: &C,
Expand Down
5 changes: 5 additions & 0 deletions src/codeblock/playground/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ impl PlaygroundInputConfig {
default_: Some(default_),
}
}

#[inline]
pub(super) fn type_(self) -> PlaygroundInputType {
self.type_
}
}

pub(crate) struct PlaygroundInput {
Expand Down
6 changes: 2 additions & 4 deletions test-book/src/sample-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class AnnounceComponent {
* Person to tell it's working
*/
@Input()
name = 'Bram';
name: 'Bram' | 'reader' = 'Bram';
}

@Component({
Expand All @@ -27,12 +27,10 @@ export class ConvinceComponent {
@Input()
name = 'Bram';

exclaim = '!';
exclaim = '';

/**
* Number of exclamation points to write!
*
* @input {"type": "number", "default": 1}
*/
@Input()
set numberOfExclamationPoints(value: number) {
Expand Down