Skip to content

Commit

Permalink
refactor(parser): use error codes for ts diagnostics (#4335)
Browse files Browse the repository at this point in the history
Part of #4333
  • Loading branch information
DonIsaac committed Jul 18, 2024
1 parent 6068e6b commit a2eabe1
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 109 deletions.
8 changes: 8 additions & 0 deletions crates/oxc_diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ pub struct OxcCode {
pub scope: Option<Cow<'static, str>>,
pub number: Option<Cow<'static, str>>,
}
impl OxcCode {
pub fn is_some(&self) -> bool {
self.scope.is_some() || self.number.is_some()
}
}

impl fmt::Display for OxcCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -70,6 +75,9 @@ pub struct OxcDiagnosticInner {

impl fmt::Display for OxcDiagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
if self.code.is_some() {
write!(f, "{}: ", &self.code)?;
}
write!(f, "{}", &self.message)
}
}
Expand Down
48 changes: 28 additions & 20 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
use std::borrow::Cow;

use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;

use crate::modifiers::Modifier;

#[inline]
fn ts_error<C, M>(code: C, message: M) -> OxcDiagnostic
where
C: Into<Cow<'static, str>>,
M: Into<Cow<'static, str>>,
{
OxcDiagnostic::error(message).with_error_code("TS", code)
}

#[cold]
pub fn redeclaration(x0: &str, declare_span: Span, redeclare_span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Identifier `{x0}` has already been declared")).with_labels([
Expand Down Expand Up @@ -305,13 +316,12 @@ pub fn optional_chain_tagged_template(span0: Span) -> OxcDiagnostic {

#[cold]
pub fn ts_constructor_this_parameter(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error("TS2681: A constructor cannot have a `this` parameter.").with_label(span0)
ts_error("2681", "A constructor cannot have a `this` parameter.").with_label(span0)
}

#[cold]
pub fn ts_arrow_function_this_parameter(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error("TS2730: An arrow function cannot have a `this` parameter.")
.with_label(span0)
ts_error("2730", "An arrow function cannot have a `this` parameter.").with_label(span0)
}

#[cold]
Expand All @@ -335,19 +345,20 @@ pub fn expect_catch_finally(span0: Span) -> OxcDiagnostic {

#[cold]
pub fn a_set_accessor_cannot_have_a_return_type_annotation(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error("TS1095: A 'set' accessor cannot have a return type annotation")
.with_label(span0)
ts_error("1095", " A 'set' accessor cannot have a return type annotation.").with_label(span0)
}

#[cold]
pub fn return_statement_only_in_function_body(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error("TS1108: A 'return' statement can only be used within a function body")
ts_error("1108", "A 'return' statement can only be used within a function body.")
.with_label(span0)
}

#[cold]
pub fn jsx_expressions_may_not_use_the_comma_operator(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error("TS18007: JSX expressions may not use the comma operator.")
// OxcDiagnostic::error("TS18007: JSX expressions may not use the comma
// operator.")
ts_error("18007", "JSX expressions may not use the comma operator")
.with_help("Did you mean to write an array?")
.with_label(span0)
}
Expand Down Expand Up @@ -389,9 +400,10 @@ pub fn using_declarations_must_be_initialized(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Using declarations must have an initializer.").with_label(span0)
}

/// TS(1093)
#[cold]
pub fn static_constructor(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::error("TS1089: `static` modifier cannot appear on a constructor declaration.")
ts_error("1089", "`static` modifier cannot appear on a constructor declaration.")
.with_label(span0)
}

Expand All @@ -412,32 +424,28 @@ pub fn modifier_cannot_be_used_here(modifier: &Modifier) -> OxcDiagnostic {
/// TS(1030)
#[cold]
pub fn modifier_already_seen(modifier: &Modifier) -> OxcDiagnostic {
OxcDiagnostic::error(format!("TS1030: '{}' modifier already seen.", modifier.kind))
// OxcDiagnostic::error(format!("TS1030: '{}' modifier already seen.", modifier.kind))
ts_error("1030", format!("{}' modifier already seen.", modifier.kind))
.with_label(modifier.span)
.with_help("Remove the duplicate modifier.")
}

/// TS(1273)
#[cold]
pub fn cannot_appear_on_a_type_parameter(modifier: &Modifier) -> OxcDiagnostic {
OxcDiagnostic::error(format!(
"'{}' modifier cannot be used on a type parameter.",
modifier.kind
))
.with_label(modifier.span)
ts_error("1273", format!("'{}' modifier cannot be used on a type parameter.", modifier.kind))
.with_label(modifier.span)
}

/// TS(1090)
pub fn cannot_appear_on_a_parameter(modifier: &Modifier) -> OxcDiagnostic {
OxcDiagnostic::error(format!(
"TS1090: '{}' modifier cannot appear on a parameter.",
modifier.kind
))
.with_label(modifier.span)
ts_error("1090", format!("'{}' modifier cannot appear on a parameter.", modifier.kind))
.with_label(modifier.span)
}

/// TS(18010)
#[cold]
pub fn accessibility_modifier_on_private_property(modifier: &Modifier) -> OxcDiagnostic {
OxcDiagnostic::error("An accessibility modifier cannot be used with a private identifier.")
ts_error("18010", "An accessibility modifier cannot be used with a private identifier.")
.with_label(modifier.span)
}
48 changes: 24 additions & 24 deletions tasks/coverage/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
· ───
╰────

× TS1108: A 'return' statement can only be used within a function body
× TS(1108): A 'return' statement can only be used within a function body.
╭─[core/uncategorised/454/input.js:1:1]
1 │ return
· ──────
Expand Down Expand Up @@ -9045,7 +9045,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
· ─
╰────

× TS1108: A 'return' statement can only be used within a function body
× TS(1108): A 'return' statement can only be used within a function body.
╭─[esprima/invalid-syntax/migrated_0171/input.js:1:1]
1 │ return
· ──────
Expand Down Expand Up @@ -10062,7 +10062,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
7 │ }
╰────

× An accessibility modifier cannot be used with a private identifier.
× TS(18010): An accessibility modifier cannot be used with a private identifier.
╭─[typescript/class/accessor-invalid/input.ts:3:3]
2 │ declare accessor prop7: number;
3 │ private accessor #p: any;
Expand All @@ -10078,7 +10078,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Try insert a semicolon here

× TS1030: 'declare' modifier already seen.
× TS(1030): declare' modifier already seen.
╭─[typescript/class/duplicate-modifier-1/input.ts:2:18]
1 │ class A {
2 │ declare public declare foo;
Expand All @@ -10087,7 +10087,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Remove the duplicate modifier.

× TS1030: 'declare' modifier already seen.
× TS(1030): declare' modifier already seen.
╭─[typescript/class/duplicate-modifier-2/input.ts:2:25]
1 │ class A {
2 │ declare public static declare foo;
Expand All @@ -10096,7 +10096,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Remove the duplicate modifier.

× TS1030: 'public' modifier already seen.
× TS(1030): public' modifier already seen.
╭─[typescript/class/duplicates-accessibility/input.ts:2:10]
1 │ class Foo {
2 │ public public a;
Expand Down Expand Up @@ -10235,23 +10235,23 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
11 │ }
╰────

× An accessibility modifier cannot be used with a private identifier.
× TS(18010): An accessibility modifier cannot be used with a private identifier.
╭─[typescript/class/private-fields-modifier-private/input.ts:2:3]
1 │ class A {
2 │ private #a;
· ───────
3 │ }
╰────

× An accessibility modifier cannot be used with a private identifier.
× TS(18010): An accessibility modifier cannot be used with a private identifier.
╭─[typescript/class/private-fields-modifier-protected/input.ts:2:3]
1 │ class A {
2 │ protected #a;
· ─────────
3 │ }
╰────

× An accessibility modifier cannot be used with a private identifier.
× TS(18010): An accessibility modifier cannot be used with a private identifier.
╭─[typescript/class/private-fields-modifier-public/input.ts:2:3]
1 │ class A {
2 │ public #a;
Expand Down Expand Up @@ -10306,7 +10306,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
· ───
╰────

× TS1030: 'declare' modifier already seen.
× TS(1030): declare' modifier already seen.
╭─[typescript/export/double-declare/input.ts:1:16]
1 │ export declare declare var name;
· ───────
Expand Down Expand Up @@ -10427,15 +10427,15 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
· ─
╰────

× TS1095: A 'set' accessor cannot have a return type annotation
× TS(1095): A 'set' accessor cannot have a return type annotation.
╭─[typescript/interface/get-set-invalid-return-types/input.ts:2:17]
1 │ interface Foo {
2 │ set foo(param): string;
· ────────
3 │ }
╰────

× TS1095: A 'set' accessor cannot have a return type annotation
× TS(1095): A 'set' accessor cannot have a return type annotation.
╭─[typescript/interface/get-set-invalid-return-types-babel-7/input.ts:2:17]
1 │ interface Foo {
2 │ set foo(param): string;
Expand Down Expand Up @@ -10919,15 +10919,15 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
· ╰── `,` expected
╰────

× 'public' modifier cannot be used on a type parameter.
× TS(1273): 'public' modifier cannot be used on a type parameter.
╭─[typescript/types/variance-annotations/input.ts:95:10]
94 │
95 │ type T20<public T> = T; // Error
· ──────
96 │ type T21<in out in T> = T; // Error
╰────

× TS1030: 'in' modifier already seen.
× TS(1030): in' modifier already seen.
╭─[typescript/types/variance-annotations/input.ts:96:17]
95 │ type T20<public T> = T; // Error
96 │ type T21<in out in T> = T; // Error
Expand All @@ -10936,7 +10936,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Remove the duplicate modifier.

× TS1030: 'out' modifier already seen.
× TS(1030): out' modifier already seen.
╭─[typescript/types/variance-annotations/input.ts:97:17]
96 │ type T21<in out in T> = T; // Error
97 │ type T22<in out out T> = T; // Error
Expand All @@ -10945,15 +10945,15 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Remove the duplicate modifier.

× 'public' modifier cannot be used on a type parameter.
× TS(1273): 'public' modifier cannot be used on a type parameter.
╭─[typescript/types/variance-annotations-babel-7/input.ts:95:10]
94 │
95 │ type T20<public T> = T; // Error
· ──────
96 │ type T21<in out in T> = T; // Error
╰────

× TS1030: 'in' modifier already seen.
× TS(1030): in' modifier already seen.
╭─[typescript/types/variance-annotations-babel-7/input.ts:96:17]
95 │ type T20<public T> = T; // Error
96 │ type T21<in out in T> = T; // Error
Expand All @@ -10962,7 +10962,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Remove the duplicate modifier.

× TS1030: 'out' modifier already seen.
× TS(1030): out' modifier already seen.
╭─[typescript/types/variance-annotations-babel-7/input.ts:97:17]
96 │ type T21<in out in T> = T; // Error
97 │ type T22<in out out T> = T; // Error
Expand All @@ -10971,15 +10971,15 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Remove the duplicate modifier.

× 'public' modifier cannot be used on a type parameter.
× TS(1273): 'public' modifier cannot be used on a type parameter.
╭─[typescript/types/variance-annotations-with-jsx/input.tsx:98:10]
97 │
98 │ type T20<public T> = T; // Error
· ──────
99 │ type T21<in out in T> = T; // Error
╰────

× TS1030: 'in' modifier already seen.
× TS(1030): in' modifier already seen.
╭─[typescript/types/variance-annotations-with-jsx/input.tsx:99:17]
98 │ type T20<public T> = T; // Error
99 │ type T21<in out in T> = T; // Error
Expand All @@ -10988,7 +10988,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Remove the duplicate modifier.

× TS1030: 'out' modifier already seen.
× TS(1030): out' modifier already seen.
╭─[typescript/types/variance-annotations-with-jsx/input.tsx:100:17]
99 │ type T21<in out in T> = T; // Error
100 │ type T22<in out out T> = T; // Error
Expand All @@ -10997,15 +10997,15 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Remove the duplicate modifier.

× 'public' modifier cannot be used on a type parameter.
× TS(1273): 'public' modifier cannot be used on a type parameter.
╭─[typescript/types/variance-annotations-with-jsx-babel-7/input.tsx:98:10]
97 │
98 │ type T20<public T> = T; // Error
· ──────
99 │ type T21<in out in T> = T; // Error
╰────

× TS1030: 'in' modifier already seen.
× TS(1030): in' modifier already seen.
╭─[typescript/types/variance-annotations-with-jsx-babel-7/input.tsx:99:17]
98 │ type T20<public T> = T; // Error
99 │ type T21<in out in T> = T; // Error
Expand All @@ -11014,7 +11014,7 @@ Expect to Parse: "typescript/types/const-type-parameters-babel-7/input.ts"
╰────
help: Remove the duplicate modifier.

× TS1030: 'out' modifier already seen.
× TS(1030): out' modifier already seen.
╭─[typescript/types/variance-annotations-with-jsx-babel-7/input.tsx:100:17]
99 │ type T21<in out in T> = T; // Error
100 │ type T22<in out out T> = T; // Error
Expand Down
Loading

0 comments on commit a2eabe1

Please sign in to comment.