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`
|
5 | a = 600;
| ^
|
note: a was declared here
error: cannot reassign to a variable declared with `const`
|
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
|
2 | import { hi } from 'foo';
| ^^
|
note: hi was declared at here
error: the name `hi` is defined multiple times
|
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
|
2 | const foo = 1; // error
| ^^^
|
note: foo was declared at here
error: the name `foo` is defined multiple times
|
1 | function foo() {}
| ^^^
| --- previous definition of `foo` here
2 | const foo = 1; // error
| ^^^ `foo` redefined here
error: Duplicate binding
|
5 | var bar; // error
| ^^^
|
note: bar was declared at here
error: the name `bar` is defined multiple times
|
4 | function bar() {}
| ^^^
| --- previous definition of `bar` here
5 | var bar; // error
| ^^^ `bar` redefined here
error: Duplicate binding
|
8 | function baz() {} // error
| ^^^
|
note: baz was declared at here
error: the name `baz` is defined multiple times
|
7 | function baz() {}
| ^^^
| --- previous definition of `baz` here
8 | function baz() {} // error
| ^^^ `baz` redefined here

View File

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

View File

@ -1,36 +1,24 @@
error: Duplicate binding
|
2 | let a = 2; // error
| ^
|
note: a was declared at here
error: the name `a` is defined multiple times
|
1 | const a = 5;
| ^
| - previous definition of `a` here
2 | let a = 2; // error
| ^ `a` redefined here
error: Duplicate binding
|
5 | var b = 2; // error
| ^
|
note: b was declared at here
error: the name `b` is defined multiple times
|
4 | const b = 1;
| ^
| - previous definition of `b` here
5 | var b = 2; // error
| ^ `b` redefined here
error: Duplicate binding
|
8 | var c = 4; // error
| ^
|
note: c was declared at here
error: the name `c` is defined multiple times
|
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
|
2 | const foo = 1; // error
| ^^^
|
note: foo was declared at here
error: the name `foo` is defined multiple times
|
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
|
5 | / export default () => {
6 | | let b = 2;
7 | | }
| |_^
|
note: default was exported at here
error: the name `default` is exported multiple times
|
1 | / export default () => {
2 | | let a = 2;
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
|
5 | / export default () => {
6 | | let b = 2;
7 | | }
| |_^
|
note: default was exported at here
error: the name `default` is exported multiple times
|
3 | export { a as default };
| ^^^^^^^
| ------- 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,12 +1,10 @@
error: Duplicate export
|
9 | export { a as b }
| ^
|
note: b was exported at here
error: the name `b` is exported multiple times
|
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) {}
| ^
|
note: previous definition of `b` here
| - ^ used as parameter more than once
| |
| 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) {}
| ^
|
note: previous definition of `a` here
| - ^ used as parameter more than once
| |
| previous definition here
error: the name `a` is bound more than once in this parameter list
|
2 | function b(a, a, a) {}
| ^
| - ^ used as parameter more than once
| |
| previous definition here
error: `a` used as parameter more than once
|
2 | function b(a, a, a) {}
| ^
|
note: previous definition of `a` 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) {}
| ^
|
note: previous definition of `a` here
| - ^ used as parameter more than once
| |
| 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) {}
| ^
|
note: previous definition of `a` here
| - ^ used as parameter more than once
| |
| previous definition here
error: the name `b` is bound more than once in this parameter list
|
4 | function d(a, b, a, b) {}
| ^
| - ^ used as parameter more than once
| |
| previous definition here
error: `b` used as parameter more than once
|
4 | function d(a, b, a, b) {}
| ^
|
note: previous definition of `b` 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) {};
| ^
|
note: previous definition of `b` here
| - ^ used as parameter more than once
| |
| 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) {};
| ^
|
note: previous definition of `a` here
| - ^ used as parameter more than once
| |
| previous definition here
error: the name `a` is bound more than once in this parameter list
|
6 | var f = function (a, a, a) {};
| ^
| - ^ used as parameter more than once
| |
| previous definition here
error: `a` used as parameter more than once
|
6 | var f = function (a, a, a) {};
| ^
|
note: previous definition of `a` 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) {};
| ^
|
note: previous definition of `a` here
| - ^ used as parameter more than once
| |
| 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) {};
| ^
|
note: previous definition of `a` here
| - ^ used as parameter more than once
| |
| previous definition here
error: the name `b` is bound more than once in this parameter list
|
8 | var h = function (a, b, a, b) {};
| ^
error: `b` used as parameter more than once
|
8 | var h = function (a, b, a, b) {};
| ^
|
note: previous definition of `b` here
|
8 | var h = function (a, b, a, b) {};
| ^
| - ^ used as parameter more than once
| |
| previous definition here

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) => {};
| ^
|
note: previous definition of `b` here
| - ^ used as parameter more than once
| |
| 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) => {};
| ^
|
note: previous definition of `b` here
| - ^ used as parameter more than once
| |
| 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) => {})();
| ^
|
note: previous definition of `b` here
| - ^ used as parameter more than once
| |
| 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) {}
| ^
|
note: previous definition of `b` here
| - ^ used as parameter more than once
| |
| 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) => {};
| ^
|
note: previous definition of `b` here
| - ^ used as parameter more than once
| |
| 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) {}
| ^
|
note: previous definition of `b` here
| - ^ used as parameter more than once
| |
| 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) {},
| ^
|
note: previous definition of `b` here
| - ^ used as parameter more than once
| |
| 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) {},
| ^
|
note: previous definition of `b` here
|
15 | bar: function (a, b, b) {},
| ^
| - ^ used as parameter more than once
| |
| previous definition here

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]) {}
| ^
|
note: previous definition of `a` here
| - ^ used as parameter more than once
| |
| 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) {}
| ^
|
note: previous definition of `a` here
| - ^ used as parameter more than once
| |
| 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] }) {}
| ^
|
note: previous definition of `a` here
|
3 | function baz({ a }, b, { c: [a] }) {}
| ^
| - ^ used as parameter more than once
| |
| previous definition here

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]) {
| ^
|
note: previous definition of `a` here
| - ^ used as parameter more than once
| |
| 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) {
| ^
|
note: previous definition of `d` here
| - ^ used as parameter more than once
| |
| 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] }) {}
| ^
|
note: previous definition of `e` here
|
3 | function baz({ e }, f, { f: [e] }) {}
| ^
| - ^ used as parameter more than once
| |
| previous definition here

View File

@ -1,26 +1,20 @@
error: Duplicate binding
|
56 | var a = 0;
| ^
|
note: a was declared at here
error: the name `a` is defined multiple times
|
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
|
67 | let { aa, bb: { bb }, cc: [cc], ...ee } = obj;
| ^^
|
note: ee was declared at here
error: the name `ee` is defined multiple times
|
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

View File

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

View File

@ -27,9 +27,15 @@ impl ConstAssign {
handler
.struct_span_err(
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();
});
}

View File

@ -38,10 +38,19 @@ impl DuplicateBindings {
match self.bindings.entry(id.to_id()) {
Entry::Occupied(mut prev) => {
if unique || prev.get().unique {
let name = &id.sym;
HANDLER.with(|handler| {
handler
.struct_span_err(id.span, "Duplicate binding")
.span_note(prev.get().span, &format!("{} was declared at here", id.sym))
.struct_span_err(
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();
});
}

View File

@ -21,10 +21,17 @@ impl DuplicateExports {
fn add(&mut self, id: &Ident) {
match self.exports.entry(id.sym.clone()) {
Entry::Occupied(mut prev) => {
let name = &id.sym;
HANDLER.with(|handler| {
handler
.struct_span_err(id.span, "Duplicate export")
.span_note(*prev.get(), &format!("{} was exported at here", id.sym))
.struct_span_err(
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();
});

View File

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