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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<!-- add new items here -->

- Support setters with an `@Input()` decorator

## v0.2.0

- Allow extra flags passed via `{{#angular}}` block, just like in ` ```ts,angular ` code blocks
Expand Down
19 changes: 11 additions & 8 deletions src/angular/builder/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub(super) fn build(config: &Config, chapters: Vec<ChapterWithCodeBlocks>) -> Re
writer.write_tsconfig(config)?;

let Some(root_target_folder) = diff_paths(&config.target_folder, root) else {
return Err(Error::msg("Failed to find relative target folder"));
};
return Err(Error::msg("Failed to find relative target folder"));
};

let mut replacements = Vec::with_capacity(chapters.len());

Expand Down Expand Up @@ -104,17 +104,20 @@ fn run_replacements(
let chapter = fs::read_to_string(&chapter_path)?;
let path_to_root = path_to_root(&replacement.chapter_path);

let Some(main_filename) = fs::read_dir(config.target_folder.join(&replacement.project_folder))?
let Some(main_filename) =
fs::read_dir(config.target_folder.join(&replacement.project_folder))?
.filter_map(Result::ok)
.find(|entry| {
let file_name = entry.file_name();
let file_name = file_name.to_string_lossy();
file_name.ends_with(".js") && file_name.starts_with("main.")
}) else {
return Err(Error::msg(
format!("Failed to find angular application for chapter {:?}", replacement.chapter_path)
));
};
})
else {
return Err(Error::msg(format!(
"Failed to find angular application for chapter {:?}",
replacement.chapter_path
)));
};

let main_filename = format!(
"{}/{}",
Expand Down
13 changes: 7 additions & 6 deletions src/angular/builder/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub(super) fn build(config: &Config, chapters: Vec<ChapterWithCodeBlocks>) -> Re
writer.write_tsconfig(config)?;

let Some(output_path) = diff_paths(&config.target_folder, root) else {
return Err(Error::msg("Failed to find relative target folder"));
};
return Err(Error::msg("Failed to find relative target folder"));
};

let mut workspace = AngularWorkspace::new();

Expand Down Expand Up @@ -103,10 +103,11 @@ pub(super) fn run_replacements(
let path_to_root = path_to_root(&replacement.chapter_path);

let Some(main_filename) = scripts.get(&replacement.script_basename) else {
return Err(Error::msg(
format!("Failed to find angular application for chapter {:?}", &replacement.chapter_path)
));
};
return Err(Error::msg(format!(
"Failed to find angular application for chapter {:?}",
&replacement.chapter_path
)));
};

let app_script_src = format!(r#"src="{path_to_root}/{main_filename}""#);

Expand Down
28 changes: 17 additions & 11 deletions src/codeblock/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct CodeBlockVisitor {

impl CodeBlockVisitor {
fn get_selector(&mut self, decorator: &ast::ObjectLit, name: &str) -> Result<String> {
static INDENTATION: Lazy<Regex> = Lazy::new(|| Regex::new(r#"^\s+"#).unwrap());
static INDENTATION: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\s+").unwrap());

let selector = decorator
.props
Expand Down Expand Up @@ -75,11 +75,15 @@ impl CodeBlockVisitor {
}

let Some(generated_selector) = self.index.map(|i| format!("codeblock-{i}")) else {
return Err(Error::msg(format!("Coudldn't find selector on class {name}")));
return Err(Error::msg(format!(
"Coudldn't find selector on class {name}"
)));
};

let Some(first_prop) = decorator.props.first() else {
return Err(Error::msg(format!("Unexpected empty @Component annotation in {name}")));
return Err(Error::msg(format!(
"Unexpected empty @Component annotation in {name}"
)));
};

let span = first_prop.span();
Expand Down Expand Up @@ -128,10 +132,14 @@ impl CodeBlockVisitor {

debug!("found @Component on {name}");

let Some(component) = component.expr.as_call()
let Some(component) = component
.expr
.as_call()
.and_then(|call| call.args.get(0))
.and_then(|arg| arg.expr.as_object())
else { return Ok(()); };
else {
return Ok(());
};

if self.tag.is_some() {
return Err(Error::msg(format!(
Expand Down Expand Up @@ -265,12 +273,10 @@ pub(super) fn parse_codeblock(
HANDLER.set(&handler, || visitor.visit_program(&program))?;

let Some(class_name) = visitor.class_name else {
return Err(
match class_name {
Some(class_name) => Error::msg(format!("Failed to find class {class_name}")),
None => Error::msg("Failed to find component class")
}
);
return Err(match class_name {
Some(class_name) => Error::msg(format!("Failed to find class {class_name}")),
None => Error::msg("Failed to find component class"),
});
};

let Some(tag) = visitor.tag else {
Expand Down
15 changes: 13 additions & 2 deletions src/codeblock/playground/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ fn extract_inputs<C: comments::Comments>(
let mut result = Vec::new();

for member in &node.body {
let (ast::ClassMember::AutoAccessor(ast::AutoAccessor {
let (key, decorators, value) = match member {
ast::ClassMember::AutoAccessor(ast::AutoAccessor {
key: ast::Key::Public(key),
decorators,
value,
Expand All @@ -43,7 +44,17 @@ fn extract_inputs<C: comments::Comments>(
decorators,
value,
..
})) = member else { continue };
}) => (key, decorators, value),

ast::ClassMember::Method(ast::ClassMethod {
kind: ast::MethodKind::Setter,
key,
function,
..
}) => (key, &function.decorators, &None),

_ => continue,
};

if get_decorator(decorators, "Input").is_none() {
continue;
Expand Down
10 changes: 4 additions & 6 deletions src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ impl<'a> CodeBlockCollector<'a> {

fn process_event<'b>(&mut self, event: Event<'b>) -> ProcessedEvent<'b> {
static TAG_ANGULAR: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r#"\{\{#angular\s+(?<path>\S+?)(?:#(?<class_name>\S+))?(?<flags>\s+.*?)?\}\}"#,
)
.unwrap()
Regex::new(r"\{\{#angular\s+(?<path>\S+?)(?:#(?<class_name>\S+))?(?<flags>\s+.*?)?\}\}")
.unwrap()
});

if self.error.is_err() {
Expand Down Expand Up @@ -339,7 +337,7 @@ impl<'a> ProcessedEvent<'a> {
}

fn concat(self, other: Self) -> Self {
Self::Chain(Box::new(self.into_iter().chain(other.into_iter())))
Self::Chain(Box::new(self.into_iter().chain(other)))
}
}

Expand Down Expand Up @@ -394,7 +392,7 @@ pub(crate) fn process_markdown(
chapter: &mut Chapter,
) -> Result<Option<ChapterWithCodeBlocks>> {
let Some(source_path) = chapter.source_path.as_ref().map(Clone::clone) else {
return Ok(None)
return Ok(None);
};

let mut new_content: String = String::with_capacity(chapter.content.len());
Expand Down
10 changes: 6 additions & 4 deletions test-book/src/sample-2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ export class ConvinceComponent {
@Input()
name = 'Bram';

exclaim = '!';

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

get exclaim() {
return '!'.repeat(this.numberOfExclamationPoints);
set numberOfExclamationPoints(value: number) {
this.exclaim = '!'.repeat(value);
}
}