Commit 1155db53 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

skip odd members

parent 5e478e9f
...@@ -12,6 +12,55 @@ use super::{ ...@@ -12,6 +12,55 @@ use super::{
}; };
use crate::analyzer::RequireContextValue; use crate::analyzer::RequireContextValue;
pub fn is_well_known_function_prop(value: &str) -> bool {
// TODO this separate list is very brittle
matches!(
value,
"arch"
| "argv"
| "assign"
| "cache"
| "context"
| "createReadStream"
| "cwd"
| "default"
| "dirname"
| "endianness"
| "env"
| "execFile"
| "execFileSync"
| "exists"
| "existsSync"
| "filter"
| "find"
| "forEach"
| "fork"
| "join"
| "keys"
| "load"
| "loadSync"
| "map"
| "meta"
| "open"
| "openSync"
| "pathToFileURL"
| "platform"
| "promises"
| "readFile"
| "readFileSync"
| "realpath"
| "realpathSync"
| "resolve"
| "set"
| "SetRootDir"
| "silent"
| "spawn"
| "spawnSync"
| "stat"
| "statSync"
)
}
pub async fn replace_well_known( pub async fn replace_well_known(
value: JsValue, value: JsValue,
compile_time_info: Vc<CompileTimeInfo>, compile_time_info: Vc<CompileTimeInfo>,
......
...@@ -138,6 +138,7 @@ use crate::{ ...@@ -138,6 +138,7 @@ use crate::{
imports::{ImportAnnotations, ImportAttributes, ImportedSymbol, Reexport}, imports::{ImportAnnotations, ImportAttributes, ImportedSymbol, Reexport},
parse_require_context, parse_require_context,
top_level_await::has_top_level_await, top_level_await::has_top_level_await,
well_known::is_well_known_function_prop,
}, },
chunk::EcmascriptExports, chunk::EcmascriptExports,
code_gen::{CodeGen, CodeGens, IntoCodeGenReference}, code_gen::{CodeGen, CodeGens, IntoCodeGenReference},
...@@ -475,7 +476,7 @@ impl AnalysisState<'_> { ...@@ -475,7 +476,7 @@ impl AnalysisState<'_> {
value: JsValue, value: JsValue,
attributes: &ImportAttributes, attributes: &ImportAttributes,
earliest_toplevel_visitor: &impl Fn(JsValue) -> JsValue, earliest_toplevel_visitor: &impl Fn(JsValue) -> JsValue,
) -> Result<JsValue> { ) -> Result<(JsValue, u32)> {
link( link(
self.var_graph, self.var_graph,
value, value,
...@@ -495,8 +496,7 @@ impl AnalysisState<'_> { ...@@ -495,8 +496,7 @@ impl AnalysisState<'_> {
&self.fun_args_values, &self.fun_args_values,
&self.var_cache, &self.var_cache,
) )
.await? .await
.0)
} }
async fn link_truthy(&self, value: JsValue, attributes: &ImportAttributes) -> Result<JsValue> { async fn link_truthy(&self, value: JsValue, attributes: &ImportAttributes) -> Result<JsValue> {
...@@ -509,17 +509,30 @@ impl AnalysisState<'_> { ...@@ -509,17 +509,30 @@ impl AnalysisState<'_> {
attributes: &ImportAttributes, attributes: &ImportAttributes,
) -> Result<Vec<WellKnownFunctionKind>> { ) -> Result<Vec<WellKnownFunctionKind>> {
// We are only interested in well-known functions, the only JsValues that can evaluated to // We are only interested in well-known functions, the only JsValues that can evaluated to
// that are WellKnownFunction-s, and Variables that point to them. Anything else can be // that are WellKnownFunctions, FreeVars and Members, (and Variables) that point to them.
// short circuited. // Everything else can be short circuited.
// let original = value.clone();
let value = self let value = self
.unsafe_link_value_shallow(value, attributes, &|v| match v { .unsafe_link_value_shallow(value, attributes, &|v| match v {
JsValue::Alternatives { .. } | JsValue::FreeVar { .. } | JsValue::Member { .. } => { JsValue::Alternatives { .. }
| JsValue::WellKnownFunction { .. }
| JsValue::FreeVar { .. } => v,
JsValue::Member(_, _, ref prop) => {
// Filter out `potentially_hugely_complex_value.startsWith`
// This is unsafe because you could do `{foo: require}.foo("./file")` and this
// skipping would miss it.
if let Some(prop) = prop.as_str()
&& !is_well_known_function_prop(prop)
{
v.into_unknown(false, "earliest visitor well known function")
} else {
v v
} }
_ => JsValue::unknown(v, false, "earliest visitor well known function"), }
_ => v.into_unknown(false, "earliest visitor well known function"),
}) })
.await?; .await?;
Ok(match value { Ok(match value.0 {
JsValue::WellKnownFunction(kind) => vec![kind], JsValue::WellKnownFunction(kind) => vec![kind],
JsValue::Alternatives { values, .. } => values JsValue::Alternatives { values, .. } => values
.into_iter() .into_iter()
...@@ -528,7 +541,11 @@ impl AnalysisState<'_> { ...@@ -528,7 +541,11 @@ impl AnalysisState<'_> {
_ => None, _ => None,
}) })
.collect(), .collect(),
_ => vec![], JsValue::Unknown { .. } => vec![],
_ => {
// println!(" {:04} {:?} {:?}", value.1, value.0, original);
vec![]
}
}) })
} }
} }
......
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