Commit 9abbb1c0 authored by Andrew Imm's avatar Andrew Imm

import.meta.glob args parsing

Following require.context as an example, this parses the args to `import.meta.glob()` and passes them to the analyzer code. The parser checks for a string in the first slot, and an optional object in the second slot. If `eager` is provided, it must be a bool constant; if `import` is provided, it must be a string constant.

I added a new diagnostic error number ("TP1008") for errors related to parsing these args, too.
parent 7ddd0ebb
...@@ -3601,6 +3601,80 @@ impl Hash for RequireContextValue { ...@@ -3601,6 +3601,80 @@ impl Hash for RequireContextValue {
} }
} }
#[derive(Debug, Clone)]
pub struct ImportMetaGlobOptions {
pub pattern: RcStr,
pub eager: bool,
pub import: Option<RcStr>,
}
/// Parse the arguments passed to an import.meta.glob invocation, validate them
/// and convert them to the appropriate rust values.
pub fn parse_import_meta_glob(args: &[JsValue]) -> Result<ImportMetaGlobOptions> {
if !(1..=2).contains(&args.len()) {
bail!("import.meta.glob() requires 1-2 arguments");
}
let Some(pattern) = args[0].as_str().map(|s| s.into()) else {
bail!("import.meta.glob(pattern, ...) requires pattern to be a constant string");
};
let mut eager = false;
let mut import = None;
if let Some(options_arg) = args.get(1) {
if let JsValue::Object { parts, .. } = options_arg {
for part in parts {
match part {
ObjectPart::KeyValue(key, value) => {
if let Some(key_str) = key.as_str() {
match key_str {
"eager" => {
if let Some(eager_val) = value.as_bool() {
eager = eager_val;
} else {
bail!(
"import.meta.glob(..., {{ eager }}) requires eager to \
be a constant boolean"
);
}
}
"import" => {
if let Some(import_val) = value.as_str() {
import = Some(import_val.into());
} else {
bail!(
"import.meta.glob(..., {{ import }}) requires import \
to be a constant string"
);
}
}
_ => {
// Ignore unknown options for forward compatibility
}
}
}
}
ObjectPart::Spread(_) => {
bail!(
"import.meta.glob(..., options) does not support spread operators in \
options"
);
}
}
}
} else {
bail!("import.meta.glob(..., options) requires options to be an object literal");
}
}
Ok(ImportMetaGlobOptions {
pattern,
eager,
import,
})
}
/// A list of well-known functions that have special meaning in the analysis. /// A list of well-known functions that have special meaning in the analysis.
#[derive(Debug, Clone, Hash, PartialEq, Eq)] #[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum WellKnownFunctionKind { pub enum WellKnownFunctionKind {
......
...@@ -7,6 +7,7 @@ pub mod failed_to_analyze { ...@@ -7,6 +7,7 @@ pub mod failed_to_analyze {
pub const CHILD_PROCESS_SPAWN: &str = "TP1005"; pub const CHILD_PROCESS_SPAWN: &str = "TP1005";
pub const PATH_METHOD: &str = "TP1006"; pub const PATH_METHOD: &str = "TP1006";
pub const REQUIRE_CONTEXT: &str = "TP1007"; pub const REQUIRE_CONTEXT: &str = "TP1007";
pub const IMPORT_META_GLOB: &str = "TP1008";
pub const NODE_PRE_GYP_FIND: &str = "TP1100"; pub const NODE_PRE_GYP_FIND: &str = "TP1100";
pub const NODE_GYP_BUILD: &str = "TP1101"; pub const NODE_GYP_BUILD: &str = "TP1101";
pub const NODE_BINDINGS: &str = "TP1102"; pub const NODE_BINDINGS: &str = "TP1102";
......
...@@ -112,7 +112,7 @@ use crate::{ ...@@ -112,7 +112,7 @@ use crate::{
}, },
imports::{ImportAnnotations, ImportAttributes, ImportedSymbol, Reexport}, imports::{ImportAnnotations, ImportAttributes, ImportedSymbol, Reexport},
linker::link, linker::link,
parse_require_context, side_effects, parse_import_meta_glob, parse_require_context, side_effects,
top_level_await::has_top_level_await, top_level_await::has_top_level_await,
well_known::replace_well_known, well_known::replace_well_known,
}, },
...@@ -2192,19 +2192,32 @@ where ...@@ -2192,19 +2192,32 @@ where
} }
WellKnownFunctionKind::ImportMetaGlob => { WellKnownFunctionKind::ImportMetaGlob => {
let _args = linked_args().await?; let args = linked_args().await?;
let options = match parse_import_meta_glob(args) {
let pattern = rcstr!("**"); Ok(options) => options,
let eager = false; Err(err) => {
let import = None; let (args, hints) = explain_args(args);
handler.span_err_with_code(
span,
&format!(
"import.meta.glob({args}) is not statically analyze-able: {}{hints}",
PrettyPrintError(&err)
),
DiagnosticId::Error(
errors::failed_to_analyze::ecmascript::IMPORT_META_GLOB.to_string(),
),
);
return Ok(());
}
};
analysis.add_reference_code_gen( analysis.add_reference_code_gen(
ImportMetaGlobAssetReference::new( ImportMetaGlobAssetReference::new(
source, source,
origin, origin,
pattern, options.pattern,
eager, options.eager,
import, options.import,
Some(issue_source(source, span)), Some(issue_source(source, span)),
in_try, in_try,
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment