feat(es/lints): Refine error message (#3952)

This commit is contained in:
magic-akari 2022-03-11 12:55:37 +08:00 committed by GitHub
parent 069326d0da
commit 579aeb7cfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 226 additions and 362 deletions

View File

@ -1,12 +1,12 @@
error: Cannot reassign to a variable declared with `const` error: cannot reassign to a variable declared with `const`
|
5 | a = 600;
| ^
|
note: a was declared here
| |
1 | const a = 500; 1 | const a = 500;
| ^ | -
| |
| const variable was declared here
| help: consider making this variable mutable: `let a`
...
5 | a = 600;
| ^ cannot reassign

View File

@ -1,12 +1,8 @@
error: Duplicate binding error: the name `hi` is defined multiple times
|
2 | import { hi } from 'foo';
| ^^
|
note: hi was declared at here
| |
1 | import { hi } from 'foo'; 1 | import { hi } from 'foo';
| ^^ | -- previous definition of `hi` here
2 | import { hi } from 'foo';
| ^^ `hi` redefined here

View File

@ -1,36 +1,24 @@
error: Duplicate binding error: the name `foo` is defined multiple times
|
2 | const foo = 1; // error
| ^^^
|
note: foo was declared at here
| |
1 | function foo() {} 1 | function foo() {}
| ^^^ | --- previous definition of `foo` here
2 | const foo = 1; // error
| ^^^ `foo` redefined here
error: Duplicate binding error: the name `bar` is defined multiple times
|
5 | var bar; // error
| ^^^
|
note: bar was declared at here
| |
4 | function bar() {} 4 | function bar() {}
| ^^^ | --- previous definition of `bar` here
5 | var bar; // error
| ^^^ `bar` redefined here
error: Duplicate binding error: the name `baz` is defined multiple times
|
8 | function baz() {} // error
| ^^^
|
note: baz was declared at here
| |
7 | function baz() {} 7 | function baz() {}
| ^^^ | --- previous definition of `baz` here
8 | function baz() {} // error
| ^^^ `baz` redefined here

View File

@ -1,12 +1,8 @@
error: Duplicate binding error: the name `foo` is defined multiple times
|
4 | function foo() {}
| ^^^
|
note: foo was declared at here
| |
3 | function foo() {} 3 | function foo() {}
| ^^^ | --- previous definition of `foo` here
4 | function foo() {}
| ^^^ `foo` redefined here

View File

@ -1,36 +1,24 @@
error: Duplicate binding error: the name `a` is defined multiple times
|
2 | let a = 2; // error
| ^
|
note: a was declared at here
| |
1 | const a = 5; 1 | const a = 5;
| ^ | - previous definition of `a` here
2 | let a = 2; // error
| ^ `a` redefined here
error: Duplicate binding error: the name `b` is defined multiple times
|
5 | var b = 2; // error
| ^
|
note: b was declared at here
| |
4 | const b = 1; 4 | const b = 1;
| ^ | - previous definition of `b` here
5 | var b = 2; // error
| ^ `b` redefined here
error: Duplicate binding error: the name `c` is defined multiple times
|
8 | var c = 4; // error
| ^
|
note: c was declared at here
| |
7 | let c = 3; 7 | let c = 3;
| ^ | - previous definition of `c` here
8 | var c = 4; // error
| ^ `c` redefined here

View File

@ -1,12 +1,8 @@
error: Duplicate binding error: the name `foo` is defined multiple times
|
2 | const foo = 1; // error
| ^^^
|
note: foo was declared at here
| |
1 | function foo() {} 1 | function foo() {}
| ^^^ | --- previous definition of `foo` here
2 | const foo = 1; // error
| ^^^ `foo` redefined here

View File

@ -1,16 +1,15 @@
error: Duplicate export error: the name `default` is exported multiple times
|
5 | / export default () => {
6 | | let b = 2;
7 | | }
| |_^
|
note: default was exported at here
| |
1 | / export default () => { 1 | / export default () => {
2 | | let a = 2; 2 | | let a = 2;
3 | | } 3 | | }
| |_^ | |_- previous exported here
4 |
5 | / export default () => {
6 | | let b = 2;
7 | | }
| |_^ exported more than once
|
= note: Exported identifiers must be unique

View File

@ -1,14 +1,13 @@
error: Duplicate export error: the name `default` is exported multiple times
| |
3 | export { a as default };
| ------- previous exported here
4 |
5 | / export default () => { 5 | / export default () => {
6 | | let b = 2; 6 | | let b = 2;
7 | | } 7 | | }
| |_^ | |_^ exported more than once
| |
note: default was exported at here = note: Exported identifiers must be unique
|
3 | export { a as default };
| ^^^^^^^

View File

@ -1,12 +1,10 @@
error: Duplicate export error: the name `b` is exported multiple times
|
9 | export { a as b }
| ^
|
note: b was exported at here
| |
8 | export { b } 8 | export { b }
| ^ | - previous exported here
9 | export { a as b }
| ^ exported more than once
|
= note: Exported identifiers must be unique

View File

@ -1,144 +1,96 @@
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
1 | function a(a, b, b) {} 1 | function a(a, b, b) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
1 | function a(a, b, b) {}
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
2 | function b(a, a, a) {} 2 | function b(a, a, a) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
2 | function b(a, a, a) {}
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
2 | function b(a, a, a) {} 2 | function b(a, a, a) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
2 | function b(a, a, a) {}
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
3 | function c(a, b, a) {} 3 | function c(a, b, a) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
3 | function c(a, b, a) {}
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
4 | function d(a, b, a, b) {} 4 | function d(a, b, a, b) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
4 | function d(a, b, a, b) {}
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
4 | function d(a, b, a, b) {} 4 | function d(a, b, a, b) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
4 | function d(a, b, a, b) {}
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
5 | var e = function (a, b, b) {}; 5 | var e = function (a, b, b) {};
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
5 | var e = function (a, b, b) {};
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
6 | var f = function (a, a, a) {}; 6 | var f = function (a, a, a) {};
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
6 | var f = function (a, a, a) {};
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
6 | var f = function (a, a, a) {}; 6 | var f = function (a, a, a) {};
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
6 | var f = function (a, a, a) {};
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
7 | var g = function (a, b, a) {}; 7 | var g = function (a, b, a) {};
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
7 | var g = function (a, b, a) {};
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
8 | var h = function (a, b, a, b) {}; 8 | var h = function (a, b, a, b) {};
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
8 | var h = function (a, b, a, b) {};
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
8 | var h = function (a, b, a, b) {}; 8 | var h = function (a, b, a, b) {};
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
8 | var h = function (a, b, a, b) {};
| ^

View File

@ -1,96 +1,64 @@
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
1 | const foo = (a, b, b) => {}; 1 | const foo = (a, b, b) => {};
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
1 | const foo = (a, b, b) => {};
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
2 | (a, b, b) => {}; 2 | (a, b, b) => {};
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
2 | (a, b, b) => {};
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
3 | ((a, b, b) => {})(); 3 | ((a, b, b) => {})();
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
3 | ((a, b, b) => {})();
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
6 | constructor(a, b, b) {} 6 | constructor(a, b, b) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
6 | constructor(a, b, b) {}
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
8 | foo = (a, b, b) => {}; 8 | foo = (a, b, b) => {};
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
8 | foo = (a, b, b) => {};
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
10 | bar(a, b, b) {} 10 | bar(a, b, b) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
10 | bar(a, b, b) {}
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
14 | foo(a, b, b) {}, 14 | foo(a, b, b) {},
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
14 | foo(a, b, b) {},
| ^
error: `b` used as parameter more than once error: the name `b` is bound more than once in this parameter list
| |
15 | bar: function (a, b, b) {}, 15 | bar: function (a, b, b) {},
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `b` here | previous definition here
|
15 | bar: function (a, b, b) {},
| ^

View File

@ -1,36 +1,24 @@
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
1 | function foo(a, b, [a]) {} 1 | function foo(a, b, [a]) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
1 | function foo(a, b, [a]) {}
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
2 | function bar(a, b, ...a) {} 2 | function bar(a, b, ...a) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
2 | function bar(a, b, ...a) {}
| ^
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
3 | function baz({ a }, b, { c: [a] }) {} 3 | function baz({ a }, b, { c: [a] }) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
3 | function baz({ a }, b, { c: [a] }) {}
| ^

View File

@ -1,36 +1,24 @@
error: `a` used as parameter more than once error: the name `a` is bound more than once in this parameter list
| |
1 | function foo(a, b, [a]) { 1 | function foo(a, b, [a]) {
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `a` here | previous definition here
|
1 | function foo(a, b, [a]) {
| ^
error: `d` used as parameter more than once error: the name `d` is bound more than once in this parameter list
| |
2 | function bar(c, d, ...d) { 2 | function bar(c, d, ...d) {
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `d` here | previous definition here
|
2 | function bar(c, d, ...d) {
| ^
error: `e` used as parameter more than once error: the name `e` is bound more than once in this parameter list
| |
3 | function baz({ e }, f, { f: [e] }) {} 3 | function baz({ e }, f, { f: [e] }) {}
| ^ | - ^ used as parameter more than once
| | |
note: previous definition of `e` here | previous definition here
|
3 | function baz({ e }, f, { f: [e] }) {}
| ^

View File

@ -1,26 +1,20 @@
error: Duplicate binding error: the name `a` is defined multiple times
|
56 | var a = 0;
| ^
|
note: a was declared at here
| |
36 | let a; for (let i in {}) { (function() { a; }); a = 1; } 36 | let a; for (let i in {}) { (function() { a; }); a = 1; }
| ^ | - previous definition of `a` here
...
56 | var a = 0;
| ^ `a` redefined here
error: Duplicate binding error: the name `ee` is defined multiple times
|
67 | let { aa, bb: { bb }, cc: [cc], ...ee } = obj;
| ^^
|
note: ee was declared at here
| |
49 | function ee() { 49 | function ee() {
| ^^ | -- previous definition of `ee` here
...
67 | let { aa, bb: { bb }, cc: [cc], ...ee } = obj;
| ^^ `ee` redefined here
error: Function declared in a loop contains unsafe references to variable i error: Function declared in a loop contains unsafe references to variable i

View File

@ -13,7 +13,6 @@ use std::{
cmp::{min, Reverse}, cmp::{min, Reverse},
collections::HashMap, collections::HashMap,
io::{self, prelude::*}, io::{self, prelude::*},
ops::Range,
}; };
#[cfg(feature = "tty-emitter")] #[cfg(feature = "tty-emitter")]
@ -775,16 +774,13 @@ impl EmitterWriter {
} }
fn get_max_line_num(&mut self, span: &MultiSpan, children: &[SubDiagnostic]) -> usize { fn get_max_line_num(&mut self, span: &MultiSpan, children: &[SubDiagnostic]) -> usize {
let mut max = 0;
let primary = self.get_multispan_max_line_num(span); let primary = self.get_multispan_max_line_num(span);
max = if primary > max { primary } else { max }; children
.iter()
for sub in children { .map(|sub| self.get_multispan_max_line_num(&sub.span))
let sub_result = self.get_multispan_max_line_num(&sub.span); .max()
max = if sub_result > max { primary } else { max }; .unwrap_or(0)
} .max(primary)
max
} }
// This "fixes" MultiSpans that contain Spans that are pointing to locations // This "fixes" MultiSpans that contain Spans that are pointing to locations
@ -886,15 +882,13 @@ impl EmitterWriter {
// `max_line_num_len` // `max_line_num_len`
let padding = " ".repeat(padding + label.len() + 5); let padding = " ".repeat(padding + label.len() + 5);
/// Return whether `style`, or the override if present and the style is /// Returns `override` if it is present and `style` is `NoStyle` or
/// `NoStyle`. /// `style` otherwise
fn style_or_override(style: Style, override_style: Option<Style>) -> Style { fn style_or_override(style: Style, override_: Option<Style>) -> Style {
if let Some(o) = override_style { match (style, override_) {
if style == Style::NoStyle { (Style::NoStyle, Some(override_)) => override_,
return o; _ => style,
}
} }
style
} }
let mut line_number = 0; let mut line_number = 0;
@ -1452,10 +1446,7 @@ fn num_overlap(
inclusive: bool, inclusive: bool,
) -> bool { ) -> bool {
let extra = if inclusive { 1 } else { 0 }; let extra = if inclusive { 1 } else { 0 };
fn contains(r: Range<usize>, value: usize) -> bool { (b_start..b_end + extra).contains(&a_start) || (a_start..a_end + extra).contains(&b_start)
r.start < value && value <= r.end
}
contains(b_start..b_end + extra, a_start) || contains(a_start..a_end + extra, b_start)
} }
fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool { fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {

View File

@ -27,9 +27,15 @@ impl ConstAssign {
handler handler
.struct_span_err( .struct_span_err(
id.span, id.span,
"Cannot reassign to a variable declared with `const`", "cannot reassign to a variable declared with `const`",
) )
.span_note(decl_span, &format!("{} was declared here", id.sym)) .span_label(decl_span, "const variable was declared here")
.span_suggestion(
decl_span,
"consider making this variable mutable",
format!("let {}", id.sym),
)
.span_label(id.span, "cannot reassign")
.emit(); .emit();
}); });
} }

View File

@ -38,10 +38,19 @@ impl DuplicateBindings {
match self.bindings.entry(id.to_id()) { match self.bindings.entry(id.to_id()) {
Entry::Occupied(mut prev) => { Entry::Occupied(mut prev) => {
if unique || prev.get().unique { if unique || prev.get().unique {
let name = &id.sym;
HANDLER.with(|handler| { HANDLER.with(|handler| {
handler handler
.struct_span_err(id.span, "Duplicate binding") .struct_span_err(
.span_note(prev.get().span, &format!("{} was declared at here", id.sym)) id.span,
&format!("the name `{}` is defined multiple times", name),
)
.span_label(
prev.get().span,
&format!("previous definition of `{}` here", name),
)
.span_label(id.span, &format!("`{}` redefined here", name))
.emit(); .emit();
}); });
} }

View File

@ -21,10 +21,17 @@ impl DuplicateExports {
fn add(&mut self, id: &Ident) { fn add(&mut self, id: &Ident) {
match self.exports.entry(id.sym.clone()) { match self.exports.entry(id.sym.clone()) {
Entry::Occupied(mut prev) => { Entry::Occupied(mut prev) => {
let name = &id.sym;
HANDLER.with(|handler| { HANDLER.with(|handler| {
handler handler
.struct_span_err(id.span, "Duplicate export") .struct_span_err(
.span_note(*prev.get(), &format!("{} was exported at here", id.sym)) id.span,
&format!("the name `{}` is exported multiple times", name),
)
.span_label(*prev.get(), "previous exported here")
.span_label(id.span, "exported more than once")
.note("Exported identifiers must be unique")
.emit(); .emit();
}); });

View File

@ -23,12 +23,13 @@ impl NoDupeArgs {
handler handler
.struct_span_err( .struct_span_err(
span, span,
&format!("`{}` used as parameter more than once", js_word), &format!(
) "the name `{}` is bound more than once in this parameter list",
.span_note( js_word
old_span, ),
&format!("previous definition of `{}` here", js_word),
) )
.span_label(old_span, "previous definition here".to_string())
.span_label(span, &"used as parameter more than once".to_string())
.emit(); .emit();
}); });
} }