feat: Codegen feature flags (#28920)

This commit adds "deno_features" crate that contains definitions of all
unstable features in Deno.

Based on these definitions, both Rust and JS code is generated ensuring
that the two are always in sync.

In addition some of flag handling was rewritten to use the generated
definitions, instead of hand rolling these flag definitions.

---------

Co-authored-by: snek <snek@deno.com>
This commit is contained in:
Bartek Iwańczuk 2025-04-25 10:33:45 +02:00 committed by GitHub
parent 4924731ac3
commit 189ccffdb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 701 additions and 284 deletions

102
runtime/features/build.rs Normal file
View file

@ -0,0 +1,102 @@
// Copyright 2018-2025 the Deno authors. MIT license.
#![allow(clippy::disallowed_methods)]
mod data;
mod structs;
fn main() {
let crate_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut js_list = String::from(
"// Copyright 2018-2025 the Deno authors. MIT license.
/**
* Don't modify this file manually.
*
* This file is auto-generated by the build script, modify `data.rs` instead.
*/
export const unstableIds = {
",
);
let mut rs_list = String::from(
"// Copyright 2018-2025 the Deno authors. MIT license.
/// Don't modify this file manually.
///
/// This file is auto-generated by the build script, modify `data.rs` instead.
use crate::structs::UnstableFeatureDefinition;
use crate::structs::UnstableFeatureKind;
pub static UNSTABLE_FEATURES: &[UnstableFeatureDefinition] = &[",
);
let mut descriptions = data::FEATURE_DESCRIPTIONS.to_vec();
descriptions.sort_by_key(|desc| desc.name);
for (id, feature) in descriptions.into_iter().enumerate() {
let flag_name = format!("unstable-{}", feature.name);
let feature_kind = match feature.kind {
structs::UnstableFeatureKind::Cli => "UnstableFeatureKind::Cli",
structs::UnstableFeatureKind::Runtime => "UnstableFeatureKind::Runtime",
};
let env_var = match feature.env_var {
Some(var_name) => format!(r#"Some("{}")"#, var_name),
None => "None".to_string(),
};
rs_list += &format!(
r#" UnstableFeatureDefinition {{
name: "{}",
flag_name: "{}",
help_text: "{}",
show_in_help: {},
id: {},
kind: {},
env_var: {},
config_file_option: "{}",
}},
"#,
feature.name,
flag_name,
feature.help_text,
feature.show_in_help,
id,
feature_kind,
env_var,
match feature.config_option {
data::ConfigFileOption::SameAsFlagName => feature.name,
data::ConfigFileOption::Renamed(alias) => alias,
}
);
if matches!(feature.kind, structs::UnstableFeatureKind::Runtime) {
let camel = camel_case(feature.name);
js_list += &format!(" {}: {},\n", camel, id);
}
}
js_list += "};\n";
rs_list += "];\n";
std::fs::write(crate_dir.join("gen.js"), js_list).unwrap();
std::fs::write(crate_dir.join("gen.rs"), rs_list).unwrap();
}
fn camel_case(name: &str) -> String {
let mut output = String::new();
let mut upper = false;
for c in name.chars() {
if c == '-' {
upper = true;
} else if upper {
upper = false;
output.push(c.to_ascii_uppercase());
} else {
output.push(c);
}
}
output
}