Skip to content

Commit

Permalink
chore: update eval warning msg (#1221)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed May 23, 2024
1 parent 470137c commit b07fdde
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 21 deletions.
8 changes: 2 additions & 6 deletions crates/rolldown/src/ast_scanner/impl_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ impl<'me, 'ast> Visit<'ast> for AstScanner<'me> {
}
if ident.name == "eval" {
self.result.warnings.push(
BuildError::unsupported_eval(
self.file_path.to_string(),
Arc::clone(self.source),
ident.span,
)
.with_severity_warning(),
BuildError::eval(self.file_path.to_string(), Arc::clone(self.source), ident.span)
.with_severity_warning(),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown/tests/fixtures/warnings/eval/artifacts.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/fixtures/warnings/basic
input_file: crates/rolldown/tests/fixtures/warnings/eval
---
# warnings

## EVAL

```text
[EVAL] Warning: Rolldown does not support `eval` function currently.
[EVAL] Warning: Use of eval is strongly discouraged as it poses security risks and may cause issues with minification.
╭─[main.js:1:13]
1 │ console.log(eval)
Expand Down
9 changes: 4 additions & 5 deletions crates/rolldown_error/src/build_error/error_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ use rolldown_resolver::ResolveError;
use super::BuildError;

use crate::events::{
circular_dependency::CircularDependency, external_entry::ExternalEntry,
circular_dependency::CircularDependency, eval::Eval, external_entry::ExternalEntry,
forbid_const_assign::ForbidConstAssign, sourcemap_error::SourceMapError,
unresolved_entry::UnresolvedEntry, unresolved_import::UnresolvedImport,
unresolved_import_treated_as_external::UnresolvedImportTreatedAsExternal,
unsupported_eval::UnsupportedEval, NapiError,
unresolved_import_treated_as_external::UnresolvedImportTreatedAsExternal, NapiError,
};

impl BuildError {
Expand Down Expand Up @@ -72,7 +71,7 @@ impl BuildError {
Self::new_inner(NapiError { status, reason })
}

pub fn unsupported_eval(filename: String, source: Arc<str>, span: Span) -> Self {
Self::new_inner(UnsupportedEval { filename, eval_span: span, source })
pub fn eval(filename: String, source: Arc<str>, span: Span) -> Self {
Self::new_inner(Eval { filename, span, source })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ use crate::{diagnostic::Diagnostic, types::diagnostic_options::DiagnosticOptions
use super::BuildEvent;

#[derive(Debug)]
pub struct UnsupportedEval {
pub struct Eval {
pub filename: String,
pub source: Arc<str>,
pub eval_span: Span,
pub span: Span,
}

impl BuildEvent for UnsupportedEval {
impl BuildEvent for Eval {
fn kind(&self) -> crate::event_kind::EventKind {
crate::event_kind::EventKind::Eval
}

fn code(&self) -> &'static str {
"UNSUPPORTED_EVAL"
"EVAL"
}

fn message(&self, _opts: &DiagnosticOptions) -> String {
format!("Unsupported eval at {}", self.filename)
format!("Use of eval in '{}' is strongly discouraged as it poses security risks and may cause issues with minification.", self.filename)
}

fn on_diagnostic(&self, diagnostic: &mut Diagnostic, opts: &DiagnosticOptions) {
let filename = opts.stabilize_path(&self.filename);

diagnostic.title = "Rolldown does not support `eval` function currently.".to_string();
diagnostic.title = "Use of eval is strongly discouraged as it poses security risks and may cause issues with minification.".to_string();

let file_id = diagnostic.add_file(filename, Arc::clone(&self.source));

diagnostic.add_label(
&file_id,
self.eval_span.start..self.eval_span.end,
self.span.start..self.span.end,
"Used `eval` function here.".to_string(),
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown_error/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use crate::{
};

pub mod circular_dependency;
pub mod eval;
pub mod external_entry;
pub mod forbid_const_assign;
pub mod sourcemap_error;
pub mod unresolved_entry;
pub mod unresolved_import;
pub mod unresolved_import_treated_as_external;
pub mod unsupported_eval;

pub trait BuildEvent: Debug + Sync + Send {
fn kind(&self) -> EventKind;
Expand Down

0 comments on commit b07fdde

Please sign in to comment.