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

Support conditions #9

Closed
stevefan1999-personal opened this issue Sep 16, 2023 · 1 comment · Fixed by #10
Closed

Support conditions #9

stevefan1999-personal opened this issue Sep 16, 2023 · 1 comment · Fixed by #10
Assignees

Comments

@stevefan1999-personal
Copy link

Right now I have to write this:

#[cfg(feature = "transpile")]
pub fn infer_transpile_syntax_by_extension(extension: &str) -> eyre::Result<Syntax> {
    trie_match::trie_match! {
        match extension {
            "js" | "mjs" => { Some(Syntax::Es(Default::default())) }
            "jsx" | "mjsx" => {
                if cfg!(feature = "react") {
                    Some(Syntax::Es(EsConfig { jsx: true, ..Default::default() }))
                } else {
                    None
                }
            }
            "ts" => {
                if cfg!(feature = "typescript") {
                    Some(Syntax::Typescript(Default::default()))
                } else {
                    None
                }
            }
            "tsx" => {
                if cfg!(all(feature = "typescript", feature = "react")) {
                    Some(Syntax::Typescript(TsConfig { tsx: true, ..Default::default() }))
                } else {
                    None
                }
            }
            _ => { None }
        }
    }
    .ok_or(eyre!("invalid extension"))
}

Rather than this:

pub fn infer_transpile_syntax_by_extension(extension: &str) -> eyre::Result<Syntax> {
    match extension {
        "js" | "mjs" => { Ok(Syntax::Es(Default::default())) }
        ("jsx" | "mjsx") if cfg!(feature = "react") => {
            Ok(Syntax::Es(EsConfig { jsx: true, ..Default::default() }))
        }
        "ts" if cfg!(feature = "typescript") => {
            Ok(Syntax::Typescript(Default::default()))
        }
        "tsx" if cfg!(all(feature = "typescript", feature = "react")) => {
            Ok(Syntax::Typescript(TsConfig { tsx: true, ..Default::default() }))
        }
        _ => { Err(eyre!("invalid extension")) }
    }
}

And neither this:

pub fn infer_transpile_syntax_by_extension(extension: &str) -> eyre::Result<Syntax> {
    match extension {
        "js" | "mjs" => Ok(Syntax::Es(Default::default())),
        #[cfg(feature = "react")]
        "jsx" | "mjsx" => {
            Ok(Syntax::Es(EsConfig {
                jsx: true,
                ..Default::default()
            }))
        }
        #[cfg(feature = "typescript")]
        "ts" => Ok(Syntax::Typescript(Default::default())),
        #[cfg(all(feature = "typescript", feature = "react"))]
        "tsx" => {
            Ok(Syntax::Typescript(TsConfig {
                tsx: true,
                ..Default::default()
            }))
        }
        _ => Err(eyre!("invalid extension")),
    }
}
@vbkaisetsu
Copy link
Member

vbkaisetsu commented Sep 16, 2023

@stevefan1999-personal Thank you for your suggestion.
I created a pull request #10 to support #[cfg(...)] attribute, but this feature is only available with Nightly Rust.

If there are no problems, I will ask another member of the team to review the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants