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

feat: new "runtime" compiler option #130

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
73 changes: 68 additions & 5 deletions src/deno_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ impl NodeModulesDirMode {
#[serde(rename_all = "camelCase")]
pub struct ConfigFileJson {
pub compiler_options: Option<Value>,
pub runtime: Option<Vec<String>>,
pub import_map: Option<String>,
pub imports: Option<Value>,
pub scopes: Option<Value>,
Expand Down Expand Up @@ -914,16 +915,76 @@ impl ConfigFile {
/// Parse `compilerOptions` and return a serde `Value`.
/// The result also contains any options that were ignored.
pub fn to_compiler_options(&self) -> Result<ParsedTsConfigOptions, AnyError> {
if let Some(compiler_options) = self.json.compiler_options.clone() {
let options: serde_json::Map<String, Value> =
serde_json::from_value(compiler_options)
.context("compilerOptions should be an object")?;
let compiler_options_exists = self.json.compiler_options.is_some();
let runtime_exists = self.json.runtime.is_some();

if compiler_options_exists || runtime_exists {
let mut options: serde_json::Map<String, Value> =
if let Some(compiler_options) = self.json.compiler_options.clone() {
serde_json::from_value(compiler_options)
.context("compilerOptions should be an object")?
} else {
serde_json::Map::new()
};

if runtime_exists {
self.resolve_runtime_libs(&mut options);
}

parse_compiler_options(options, Some(&self.specifier))
} else {
Ok(Default::default())
}
}

/// Resolves and updates the `lib` field in `compilerOptions` based on the `runtime` configuration.
fn resolve_runtime_libs(&self, options: &mut serde_json::Map<String, Value>) {
if let Some(runtime) = self.json.runtime.clone() {
let contains_deno = runtime.contains(&"deno".to_string());
let contains_browser = runtime.contains(&"browser".to_string());

if contains_deno && contains_browser {
self.insert_libs(
options,
vec![
"deno.ns".to_string(),
"esnext".to_string(),
"dom".to_string(),
"dom.iterable".to_string(),
],
);
} else if contains_deno {
self.insert_libs(options, vec!["deno.ns".to_string()]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be verified with TSC settings in deno, I think we enable more libs for Deno type checking.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure,
Let me know what other libs we need to set or point me so I find myself.

} else if contains_browser {
self.insert_libs(
options,
vec![
"esnext".to_string(),
"dom".to_string(),
"dom.iterable".to_string(),
],
);
}
}
}

/// Inserts the values into the `lib` field of `compilerOptions`.
fn insert_libs(
&self,
options: &mut serde_json::Map<String, Value>,
libs: Vec<String>,
) {
let lib_array = options
.entry("lib".to_string())
.or_insert(Value::Array(Vec::new())) // Ensure "lib" key exists as an array
.as_array_mut()
.unwrap();

for lib in libs {
lib_array.push(Value::String(lib));
}
}

pub fn to_import_map_path(&self) -> Result<Option<PathBuf>, AnyError> {
let Some(value) = self.json.import_map.as_ref() else {
return Ok(None);
Expand Down Expand Up @@ -1705,6 +1766,7 @@ mod tests {
// comments are allowed
"strict": true
},
"runtime": ["deno", "browser"],
"lint": {
"include": ["src/"],
"exclude": ["src/testdata/"],
Expand Down Expand Up @@ -1741,7 +1803,8 @@ mod tests {
maybe_ignored,
} = config_file.to_compiler_options().unwrap();
assert!(options.contains_key("strict"));
assert_eq!(options.len(), 1);
assert!(options.contains_key("lib"));
assert_eq!(options.len(), 2);
assert_eq!(
maybe_ignored,
Some(IgnoredCompilerOptions {
Expand Down