-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: emit warning for const assign code (#538)
* fix: forbit const assign for esbuild * fix: move to `visit_binding_identifier` * refactor * fix: update test
- Loading branch information
Showing
16 changed files
with
215 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
crates/rolldown/tests/esbuild/default/.forbid_const_assign_when_bundling/artifacts.snap
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
crates/rolldown/tests/esbuild/default/.forbid_const_assign_when_bundling/entry.js
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
crates/rolldown/tests/esbuild/default/forbid_const_assign_when_bundling/artifacts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
--- | ||
source: crates/rolldown/tests/common/case.rs | ||
expression: content | ||
input_file: crates/rolldown/tests/esbuild/default/forbid_const_assign_when_bundling | ||
--- | ||
# warnings | ||
|
||
## FORBID_CONST_ASSIGN | ||
|
||
```text | ||
[FORBID_CONST_ASSIGN] Warning: Unexpected re-assignment of const variable `x` | ||
╭─[tests/esbuild/default/forbid_const_assign_when_bundling/entry.js:4:3] | ||
│ | ||
3 │ const x = 1 | ||
│ ┬ | ||
│ ╰── `x` is declared here as const | ||
4 │ x = 2 | ||
│ ┬ | ||
│ ╰── `x` is re-assigned here | ||
───╯ | ||
``` | ||
## FORBID_CONST_ASSIGN | ||
|
||
```text | ||
[FORBID_CONST_ASSIGN] Warning: Unexpected re-assignment of const variable `y` | ||
╭─[tests/esbuild/default/forbid_const_assign_when_bundling/entry.js:14:5] | ||
│ | ||
13 │ const y = 1 | ||
│ ┬ | ||
│ ╰── `y` is declared here as const | ||
14 │ y = 2 | ||
│ ┬ | ||
│ ╰── `y` is re-assigned here | ||
────╯ | ||
``` | ||
# Assets | ||
|
||
## entry_js.mjs | ||
|
||
```js | ||
// entry.js | ||
let err; | ||
try{ | ||
const x = 1; | ||
x = 2; | ||
}catch(e){ | ||
err = e; | ||
}assert(typeof err !== 'undefined'); | ||
function foo() { | ||
let err$1; | ||
try{ | ||
const y = 1; | ||
y = 2; | ||
}catch(e){ | ||
err$1 = e; | ||
} assert(typeof err$1 !== 'undefined'); | ||
} | ||
foo(); | ||
``` |
21 changes: 21 additions & 0 deletions
21
crates/rolldown/tests/esbuild/default/forbid_const_assign_when_bundling/entry.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
let err | ||
try { | ||
const x = 1 | ||
x = 2 | ||
} catch (e) { | ||
err = e | ||
} | ||
assert(typeof err !== 'undefined') | ||
|
||
function foo() { | ||
let err | ||
try { | ||
const y = 1 | ||
y = 2 | ||
} catch (e) { | ||
err = e | ||
} | ||
assert(typeof err !== 'undefined') | ||
} | ||
|
||
foo() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
crates/rolldown_error/src/error_kind/forbid_const_assign.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::{path::Path, sync::Arc}; | ||
|
||
use ariadne::Label; | ||
use oxc::span::Span; | ||
|
||
use crate::{diagnostic::DiagnosticBuilder, PathExt}; | ||
|
||
use super::BuildErrorLike; | ||
|
||
#[derive(Debug)] | ||
pub struct ForbitConstAssign { | ||
pub filename: String, | ||
pub source: Arc<str>, | ||
pub name: String, | ||
pub reference_span: Span, | ||
pub re_assign_span: Span, | ||
} | ||
|
||
impl BuildErrorLike for ForbitConstAssign { | ||
// | ||
fn code(&self) -> &'static str { | ||
"FORBID_CONST_ASSIGN" | ||
} | ||
|
||
fn message(&self) -> String { | ||
format!("Unexpected re-assignment of const variable `{0}` at {1}", self.name, self.filename) | ||
} | ||
|
||
fn diagnostic_builder(&self) -> crate::diagnostic::DiagnosticBuilder { | ||
let filename = Path::new(&self.filename).relative_display(); | ||
DiagnosticBuilder { | ||
code: Some(self.code()), | ||
summary: Some(format!("Unexpected re-assignment of const variable `{0}`", self.name)), | ||
files: Some(vec![(filename.clone(), self.source.to_string())]), | ||
labels: Some(vec![ | ||
Label::new(( | ||
filename.clone(), | ||
(self.re_assign_span.start as usize..self.re_assign_span.end as usize), | ||
)) | ||
.with_message(format!("`{0}` is re-assigned here", self.name)), | ||
Label::new(( | ||
filename, | ||
(self.reference_span.start as usize..self.reference_span.end as usize), | ||
)) | ||
.with_message(format!("`{0}` is declared here as const", self.name)), | ||
]), | ||
..Default::default() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters