Skip to content

Commit 57eaecf

Browse files
Allow typeshare annotation using cfg_attr
1 parent 30dc9b5 commit 57eaecf

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[cfg_attr(feature = "typeshare-support", typeshare)]
2+
pub struct TestStruct1 {
3+
field: String,
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface TestStruct1 {
2+
field: string;
3+
}
4+

app/engine/src/driver.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{collections::HashMap, io};
33
use anyhow::Context as _;
44
use clap::{CommandFactory as _, FromArgMatches as _};
55
use clap_complete::generate as generate_completions;
6+
use flexi_logger::AdaptiveFormat;
67
use ignore::{overrides::OverrideBuilder, types::TypesBuilder, WalkBuilder};
78
use itertools::Itertools;
89
use lazy_format::lazy_format;
@@ -184,6 +185,11 @@ pub fn main_body<Helper>(personalizations: args::PersonalizeClap) -> anyhow::Res
184185
where
185186
Helper: LanguageHelper,
186187
{
188+
flexi_logger::Logger::try_with_env_or_str("info")?
189+
.adaptive_format_for_stderr(AdaptiveFormat::Opt)
190+
.adaptive_format_for_stdout(AdaptiveFormat::Opt)
191+
.start()?;
192+
187193
let language_metas = Helper::LanguageSet::compute_language_metas()?;
188194
let command = StandardArgs::command();
189195
let command = add_personalizations(command, personalizations);

app/engine/src/parser.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Source file parsing.
22
use ignore::Walk;
33
use itertools::Itertools;
4+
use log::debug;
45
use proc_macro2::{Delimiter, Group};
56
use rayon::iter::{IntoParallelIterator, ParallelIterator};
67
use std::{
@@ -185,6 +186,7 @@ pub fn parse_input(
185186
inputs
186187
.into_par_iter()
187188
.map(|parser_input| {
189+
debug!("Parsing file {:?}", parser_input.file_path);
188190
// Performance nit: we don't need to clone in the error case;
189191
// map_err is taking unconditional ownership unnecessarily
190192
let content = std::fs::read_to_string(&parser_input.file_path).map_err(|err| {
@@ -285,7 +287,8 @@ pub fn parse(
285287
) -> Result<Option<ParsedData>, ParseErrorSet> {
286288
// We will only produce output for files that contain the `#[typeshare]`
287289
// attribute, so this is a quick and easy performance win
288-
if !source_code.contains("#[typeshare") {
290+
if !source_code.contains("typeshare") {
291+
debug!("No typeshare found in file");
289292
return Ok(None);
290293
}
291294

@@ -296,7 +299,6 @@ pub fn parse(
296299
.map_err(|err| ParseError::new(&err.span(), ParseErrorKind::SynError(err)))?;
297300

298301
import_visitor.visit_file(&file_contents);
299-
300302
import_visitor.parsed_data().map(Some)
301303
}
302304

@@ -690,12 +692,16 @@ fn parse_const_expr(e: &Expr) -> Result<RustConstExpr, ParseError> {
690692

691693
// Helpers
692694

693-
/// Checks the given attrs for `#[typeshare]`
695+
/// Checks the given attrs for `#[typeshare]` or `#[cfg_attr(<cond>, typeshare)]`
694696
pub(crate) fn has_typeshare_annotation(attrs: &[syn::Attribute]) -> bool {
697+
let check_cfg_attr = |attr| {
698+
get_meta_items(attr, "cfg_attr").iter().any(|item| {
699+
matches!(item, Meta::Path(path) if path.segments.iter().any(|segment| segment.ident == TYPESHARE))
700+
})
701+
};
695702
attrs
696703
.iter()
697-
.flat_map(|attr| attr.path().segments.clone())
698-
.any(|segment| segment.ident == TYPESHARE)
704+
.any(|attr| attr.path().is_ident(TYPESHARE) || check_cfg_attr(attr))
699705
}
700706

701707
pub(crate) fn serde_rename_all(attrs: &[syn::Attribute]) -> Option<String> {
@@ -1096,4 +1102,10 @@ mod test_get_decorators {
10961102
);
10971103
assert_eq!(decorators.type_override_for_lang("kotlin"), None);
10981104
}
1105+
1106+
#[test]
1107+
fn test_cfg_attr() {
1108+
let attr = parse_attr(r#"#[cfg_attr(feature = "typeshare", typeshare)]"#);
1109+
assert!(has_typeshare_annotation(&attr));
1110+
}
10991111
}

0 commit comments

Comments
 (0)