Unverified Commit 2e3fb9bd authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: FreeVarReference::ReportUsage (#89302)

looking for a better name...
parent b0facee6
...@@ -7,7 +7,7 @@ use turbo_rcstr::RcStr; ...@@ -7,7 +7,7 @@ use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, Vc, trace::TraceRawVcs}; use turbo_tasks::{FxIndexMap, NonLocalValue, ResolvedVc, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use crate::environment::Environment; use crate::{environment::Environment, issue::IssueSeverity};
#[macro_export] #[macro_export]
macro_rules! definable_name_map_pattern_internal { macro_rules! definable_name_map_pattern_internal {
...@@ -313,7 +313,13 @@ pub enum FreeVarReference { ...@@ -313,7 +313,13 @@ pub enum FreeVarReference {
Member(RcStr, RcStr), Member(RcStr, RcStr),
Value(CompileTimeDefineValue), Value(CompileTimeDefineValue),
InputRelative(InputRelativeConstant), InputRelative(InputRelativeConstant),
Error(RcStr), // Report the replacement of this free var with the given severity and message, and
// potentially replace with the `inner` value.
ReportUsage {
message: RcStr,
severity: IssueSeverity,
inner: Option<Box<FreeVarReference>>,
},
} }
impl From<bool> for FreeVarReference { impl From<bool> for FreeVarReference {
......
...@@ -641,10 +641,16 @@ impl TryFrom<&FreeVarReference> for JsValue { ...@@ -641,10 +641,16 @@ impl TryFrom<&FreeVarReference> for JsValue {
false, false,
"compile time injected free var module", "compile time injected free var module",
)), )),
FreeVarReference::Error(_) => Ok(JsValue::unknown_empty( FreeVarReference::ReportUsage { inner, .. } => {
false, if let Some(inner) = &inner {
"compile time injected free var error", inner.as_ref().try_into()
)), } else {
Ok(JsValue::unknown_empty(
false,
"compile time injected free var error",
))
}
}
FreeVarReference::InputRelative(kind) => { FreeVarReference::InputRelative(kind) => {
use turbopack_core::compile_time_info::InputRelativeConstant; use turbopack_core::compile_time_info::InputRelativeConstant;
Ok(JsValue::unknown_empty( Ok(JsValue::unknown_empty(
......
...@@ -43,7 +43,7 @@ use swc_core::{ ...@@ -43,7 +43,7 @@ use swc_core::{
common::{ common::{
GLOBALS, Globals, Span, Spanned, SyntaxContext, GLOBALS, Globals, Span, Spanned, SyntaxContext,
comments::{CommentKind, Comments}, comments::{CommentKind, Comments},
errors::{DiagnosticId, HANDLER, Handler}, errors::{DiagnosticId, HANDLER, Handler, Level},
pass::AstNodePath, pass::AstNodePath,
source_map::SmallPos, source_map::SmallPos,
}, },
...@@ -2908,13 +2908,6 @@ async fn handle_free_var_reference( ...@@ -2908,13 +2908,6 @@ async fn handle_free_var_reference(
} }
match value { match value {
FreeVarReference::Error(error_message) => state.handler.span_err_with_code(
span,
error_message,
DiagnosticId::Error(
errors::failed_to_analyze::ecmascript::FREE_VAR_REFERENCE.to_string(),
),
),
FreeVarReference::Value(value) => { FreeVarReference::Value(value) => {
analysis.add_code_gen(ConstantValueCodeGen::new( analysis.add_code_gen(ConstantValueCodeGen::new(
value.clone(), value.clone(),
...@@ -2997,6 +2990,35 @@ async fn handle_free_var_reference( ...@@ -2997,6 +2990,35 @@ async fn handle_free_var_reference(
ast_path.to_vec().into(), ast_path.to_vec().into(),
)); ));
} }
FreeVarReference::ReportUsage {
message,
severity,
inner,
} => {
state.handler.emit_with_code(
&span.into(),
message,
DiagnosticId::Error(
errors::failed_to_analyze::ecmascript::FREE_VAR_REFERENCE.to_string(),
),
match severity {
IssueSeverity::Bug => Level::Bug,
IssueSeverity::Fatal => Level::Fatal,
IssueSeverity::Error => Level::Error,
IssueSeverity::Warning => Level::Warning,
IssueSeverity::Hint => Level::Help,
IssueSeverity::Info | IssueSeverity::Note => Level::Note,
IssueSeverity::Suggestion => Level::Cancelled,
},
);
if let Some(inner) = inner {
return Box::pin(handle_free_var_reference(
ast_path, inner, span, state, analysis,
))
.await;
}
}
} }
Ok(true) Ok(true)
} }
......
...@@ -33,14 +33,16 @@ use turbopack_core::{ ...@@ -33,14 +33,16 @@ use turbopack_core::{
EvaluatableAssets, MinifyType, SourceMapSourceType, availability_info::AvailabilityInfo, EvaluatableAssets, MinifyType, SourceMapSourceType, availability_info::AvailabilityInfo,
}, },
compile_time_defines, compile_time_defines,
compile_time_info::{CompileTimeDefineValue, CompileTimeInfo, DefinableNameSegment}, compile_time_info::{
CompileTimeDefineValue, CompileTimeInfo, DefinableNameSegment, FreeVarReference,
},
condition::ContextCondition, condition::ContextCondition,
context::AssetContext, context::AssetContext,
environment::{BrowserEnvironment, Environment, ExecutionEnvironment, NodeJsEnvironment}, environment::{BrowserEnvironment, Environment, ExecutionEnvironment, NodeJsEnvironment},
file_source::FileSource, file_source::FileSource,
free_var_references, free_var_references,
ident::Layer, ident::Layer,
issue::{CollectibleIssuesExt, IssueFilter}, issue::{CollectibleIssuesExt, IssueFilter, IssueSeverity},
module::Module, module::Module,
module_graph::{ module_graph::{
ModuleGraph, SingleModuleGraph, ModuleGraph, SingleModuleGraph,
...@@ -321,7 +323,19 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> { ...@@ -321,7 +323,19 @@ async fn run_test_operation(resource: RcStr) -> Result<Vc<FileSystemPath>> {
let compile_time_info = CompileTimeInfo::builder(env) let compile_time_info = CompileTimeInfo::builder(env)
.defines(defines.clone().resolved_cell()) .defines(defines.clone().resolved_cell())
.free_var_references(free_var_references!(..defines.into_iter()).resolved_cell()) .free_var_references(
free_var_references!(
..defines.into_iter(),
WARNED_VALUE = FreeVarReference::ReportUsage {
severity: IssueSeverity::Warning,
message: rcstr!("WARNED_VALUE is deprecated, use REPLACEMENT_VALUE instead"),
inner: Some(Box::new(FreeVarReference::Value(
CompileTimeDefineValue::String(rcstr!("replacement"))
))),
},
)
.resolved_cell(),
)
.cell() .cell()
.await?; .await?;
......
...@@ -61,3 +61,9 @@ p.env.NODE_ENV != 'production' && console.log('development') ...@@ -61,3 +61,9 @@ p.env.NODE_ENV != 'production' && console.log('development')
p.env.NODE_ENV == 'production' && console.log('production') p.env.NODE_ENV == 'production' && console.log('production')
console.log(__dirname) console.log(__dirname)
// Test that WARNED_VALUE triggers a warning but still gets replaced
console.log(WARNED_VALUE)
if (WARNED_VALUE === 'replacement') {
console.log('warning replacement works')
}
warning - [analysis] /turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js:66:12 error TP1202 WARNED_VALUE is deprecated, use REPLACEMENT_VALUE instead
62 |
63 | console.log(__dirname)
64 |
65 | // Test that WARNED_VALUE triggers a warning but still gets replaced
+ v----------v
66 + console.log(WARNED_VALUE)
+ ^----------^
67 | if (WARNED_VALUE === 'replacement') {
68 | console.log('warning replacement works')
69 | }
70 |
\ No newline at end of file
warning - [analysis] /turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js:67:4 error TP1202 WARNED_VALUE is deprecated, use REPLACEMENT_VALUE instead
63 | console.log(__dirname)
64 |
65 | // Test that WARNED_VALUE triggers a warning but still gets replaced
66 | console.log(WARNED_VALUE)
+ v----------v
67 + if (WARNED_VALUE === 'replacement') {
+ ^----------^
68 | console.log('warning replacement works')
69 | }
70 |
\ No newline at end of file
...@@ -58,6 +58,11 @@ if ("TURBOPACK compile-time falsy", 0) //TURBOPACK unreachable ...@@ -58,6 +58,11 @@ if ("TURBOPACK compile-time falsy", 0) //TURBOPACK unreachable
("TURBOPACK compile-time value", "development") != 'production' && console.log('development'); ("TURBOPACK compile-time value", "development") != 'production' && console.log('development');
("TURBOPACK compile-time value", "development") == 'production' && console.log('production'); ("TURBOPACK compile-time value", "development") == 'production' && console.log('production');
console.log(("TURBOPACK compile-time value", "/ROOT/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input")); console.log(("TURBOPACK compile-time value", "/ROOT/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input"));
// Test that WARNED_VALUE triggers a warning but still gets replaced
console.log(("TURBOPACK compile-time value", "replacement"));
if (("TURBOPACK compile-time value", "replacement") === 'replacement') {
console.log('warning replacement works');
}
}), }),
]); ]);
......
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
"version": 3, "version": 3,
"sources": [], "sources": [],
"sections": [ "sections": [
{"offset": {"line": 3, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js"],"sourcesContent":["if (DEFINED_VALUE) {\n console.log('DEFINED_VALUE')\n}\n\nif (DEFINED_TRUE) {\n console.log('DEFINED_VALUE')\n}\n\nif (!DEFINED_NULL) {\n console.log('DEFINED_NULL', DEFINED_NULL)\n}\n\nif (DEFINED_INT) {\n console.log('DEFINED_INT', DEFINED_INT)\n}\n\nif (DEFINED_FLOAT) {\n console.log('DEFINED_FLOAT', DEFINED_FLOAT)\n}\n\nif (DEFINED_ARRAY) {\n console.log('DEFINED_ARRAY', DEFINED_ARRAY)\n}\n\nif (DEFINED_EVALUATE) {\n console.log('DEFINED_EVALUATE', DEFINED_EVALUATE)\n}\n\nif (DEFINED_EVALUATE_NESTED) {\n console.log('DEFINED_EVALUATE_NESTED', DEFINED_EVALUATE_NESTED)\n}\n\nif (A.VERY.LONG.DEFINED.VALUE) {\n console.log('A.VERY.LONG.DEFINED.VALUE')\n}\n\nif (process.env.NODE_ENV) {\n console.log('something')\n}\n\nif (process.env.NODE_ENV === 'production') {\n console.log('production')\n}\n\nvar p = process\n\nconsole.log(A.VERY.LONG.DEFINED.VALUE)\nconsole.log(DEFINED_VALUE)\nconsole.log(p.env.NODE_ENV)\n\nif (p.env.NODE_ENV === 'production') {\n console.log('production')\n}\n\np.env.NODE_ENV == 'production'\n ? console.log('production')\n : console.log('development')\n\n// TODO short-circuit is not implemented yet\np.env.NODE_ENV != 'production' && console.log('development')\np.env.NODE_ENV == 'production' && console.log('production')\n\nconsole.log(__dirname)\n"],"names":["console","log","p","process"],"mappings":"AAAA,wCAAmB;IACjBA,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAkB;IAChBD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAmB;IACjBD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAiB;IACfD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAmB;IACjBD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAmB;IACjBD,QAAQC,GAAG,CAAC;;;;;;;;;AACd;AAEA,wCAAsB;IACpBD,QAAQC,GAAG,CAAC,qDAzBd,IAAI;AA0BJ;AAEA,wCAA6B;IAC3BD,QAAQC,GAAG,CAAC;;;yCA7Bd,IAAM;;AA8BN;AAEA,wCAA+B;IAC7BD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAA0B;IACxBD,QAAQC,GAAG,CAAC;AACd;AAEA;;AAIA,IAAIC,IAAIC;AAERH,QAAQC,GAAG;;;AACXD,QAAQC,GAAG;AACXD,QAAQC,GAAG;AAEX;;AAIA,sCACI,0BACAD,QAAQC,GAAG,CAAC;AAEhB,4CAA4C;AAC5C,mDAAkB,gBAAgBD,QAAQC,GAAG,CAAC;AAC9C,mDAAkB,gBAAgBD,QAAQC,GAAG,CAAC;AAE9CD,QAAQC,GAAG"}}] {"offset": {"line": 3, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/comptime/define/input/index.js"],"sourcesContent":["if (DEFINED_VALUE) {\n console.log('DEFINED_VALUE')\n}\n\nif (DEFINED_TRUE) {\n console.log('DEFINED_VALUE')\n}\n\nif (!DEFINED_NULL) {\n console.log('DEFINED_NULL', DEFINED_NULL)\n}\n\nif (DEFINED_INT) {\n console.log('DEFINED_INT', DEFINED_INT)\n}\n\nif (DEFINED_FLOAT) {\n console.log('DEFINED_FLOAT', DEFINED_FLOAT)\n}\n\nif (DEFINED_ARRAY) {\n console.log('DEFINED_ARRAY', DEFINED_ARRAY)\n}\n\nif (DEFINED_EVALUATE) {\n console.log('DEFINED_EVALUATE', DEFINED_EVALUATE)\n}\n\nif (DEFINED_EVALUATE_NESTED) {\n console.log('DEFINED_EVALUATE_NESTED', DEFINED_EVALUATE_NESTED)\n}\n\nif (A.VERY.LONG.DEFINED.VALUE) {\n console.log('A.VERY.LONG.DEFINED.VALUE')\n}\n\nif (process.env.NODE_ENV) {\n console.log('something')\n}\n\nif (process.env.NODE_ENV === 'production') {\n console.log('production')\n}\n\nvar p = process\n\nconsole.log(A.VERY.LONG.DEFINED.VALUE)\nconsole.log(DEFINED_VALUE)\nconsole.log(p.env.NODE_ENV)\n\nif (p.env.NODE_ENV === 'production') {\n console.log('production')\n}\n\np.env.NODE_ENV == 'production'\n ? console.log('production')\n : console.log('development')\n\n// TODO short-circuit is not implemented yet\np.env.NODE_ENV != 'production' && console.log('development')\np.env.NODE_ENV == 'production' && console.log('production')\n\nconsole.log(__dirname)\n\n// Test that WARNED_VALUE triggers a warning but still gets replaced\nconsole.log(WARNED_VALUE)\nif (WARNED_VALUE === 'replacement') {\n console.log('warning replacement works')\n}\n"],"names":["console","log","p","process"],"mappings":"AAAA,wCAAmB;IACjBA,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAkB;IAChBD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAmB;IACjBD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAiB;IACfD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAmB;IACjBD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAAmB;IACjBD,QAAQC,GAAG,CAAC;;;;;;;;;AACd;AAEA,wCAAsB;IACpBD,QAAQC,GAAG,CAAC,qDAzBd,IAAI;AA0BJ;AAEA,wCAA6B;IAC3BD,QAAQC,GAAG,CAAC;;;yCA7Bd,IAAM;;AA8BN;AAEA,wCAA+B;IAC7BD,QAAQC,GAAG,CAAC;AACd;AAEA,wCAA0B;IACxBD,QAAQC,GAAG,CAAC;AACd;AAEA;;AAIA,IAAIC,IAAIC;AAERH,QAAQC,GAAG;;;AACXD,QAAQC,GAAG;AACXD,QAAQC,GAAG;AAEX;;AAIA,sCACI,0BACAD,QAAQC,GAAG,CAAC;AAEhB,4CAA4C;AAC5C,mDAAkB,gBAAgBD,QAAQC,GAAG,CAAC;AAC9C,mDAAkB,gBAAgBD,QAAQC,GAAG,CAAC;AAE9CD,QAAQC,GAAG;AAEX,oEAAoE;AACpED,QAAQC,GAAG;AACX,IAAI,oDAAiB,eAAe;IAClCD,QAAQC,GAAG,CAAC;AACd"}}]
} }
\ No newline at end of file
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