Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,15 @@ pub fn declaration_single_statement(span: Span) -> OxcDiagnostic {
.with_label(span)
}

#[cold]
pub fn const_type_parameter(span: Span) -> OxcDiagnostic {
ts_error(
"1277",
"'const' modifier can only appear on a type parameter of a function, method or class",
)
.with_label(span)
}

#[cold]
pub fn async_function_declaration(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Async functions can only be declared at the top level or inside a block")
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_parser/src/ts/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ impl<'a, C: Config> ParserImpl<'a, C> {
let id = self.parse_binding_identifier();
self.check_reserved_type_name(&id, "Type alias");
let params = self.parse_ts_type_parameters();
// A `const` modifier is only valid on a type parameter of a function, method, or class
// (TS1277), so reject it on a type alias, e.g. `type T<const U> = ...`.
if let Some(type_params) = &params {
for param in &type_params.params {
if param.r#const {
self.error(diagnostics::const_type_parameter(param.span));
}
}
}
self.expect(Kind::Eq);

let intrinsic_token = self.cur_token();
Expand Down
10 changes: 7 additions & 3 deletions tasks/coverage/snapshots/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: 2688fbd1
parser_babel Summary:
AST Parsed : 2236/2236 (100.00%)
Positive Passed: 2222/2236 (99.37%)
Negative Passed: 1708/1723 (99.13%)
Negative Passed: 1709/1723 (99.19%)
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/cast/unparenthesized-assert-and-assign/input.ts

Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/cast/unparenthesized-type-assertion-and-assign/input.ts
Expand Down Expand Up @@ -32,8 +32,6 @@ Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/ty

Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/regression/keyword-qualified-type-2/input.ts

Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/const-type-parameters-invalid/input.ts

Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/class-private-method/typescript-invalid-abstract/input.ts

× TS(18019): 'abstract' modifier cannot be used with a private identifier.
Expand Down Expand Up @@ -14848,6 +14846,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
· ─────
╰────

× TS(1277): 'const' modifier can only appear on a type parameter of a function, method or class
╭─[babel/packages/babel-parser/test/fixtures/typescript/types/const-type-parameters-invalid/input.ts:1:8]
1 │ type T<const U> = {};
· ───────
╰────

× TS(1363): A type-only import can specify a default import or named bindings, but not both.
╭─[babel/packages/babel-parser/test/fixtures/typescript/types/import-type-declaration-error/input.ts:1:13]
1 │ import type FooDefault, { Bar, Baz } from "module";
Expand Down
12 changes: 9 additions & 3 deletions tasks/coverage/snapshots/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: 7539c04d
parser_typescript Summary:
AST Parsed : 9779/9779 (100.00%)
Positive Passed: 9779/9779 (100.00%)
Negative Passed: 1635/2640 (61.93%)
Negative Passed: 1636/2640 (61.97%)
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration3.ts

Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/FunctionDeclaration4.ts
Expand Down Expand Up @@ -1988,8 +1988,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/typ

Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/typeParameters/typeParameterLists/propertyAccessOnTypeParameterWithConstraints5.ts

Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts

Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/typeParameters/typeParameterLists/typesWithDuplicateTypeParameters.ts

Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/typeRelationships/assignmentCompatibility/nullAssignedToUndefined.ts
Expand Down Expand Up @@ -30474,6 +30472,14 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/wit
24 │ type T21 = typeof Array<string>; // new (...) => string[]
╰────

× TS(1277): 'const' modifier can only appear on a type parameter of a function, method or class
╭─[typescript/tests/cases/conformance/types/typeParameters/typeParameterLists/typeParameterConstModifiers.ts:55:9]
54 │
55 │ type T1<const T> = T; // Error
· ───────
56 │
╰────

× TS(1273): 'public' modifier cannot be used on a type parameter.
╭─[typescript/tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts:95:10]
94 │
Expand Down
Loading