Skip to content

Commit

Permalink
feat(parser): 'readonly' type modifier is only permitted on array and…
Browse files Browse the repository at this point in the history
… tuple literal types. (#7880)
  • Loading branch information
Boshen committed Dec 14, 2024
1 parent 7a087b9 commit 81eedb1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
7 changes: 7 additions & 0 deletions crates/oxc_parser/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,13 @@ pub fn cannot_appear_on_an_index_signature(modifier: &Modifier) -> OxcDiagnostic
.with_label(modifier.span)
}

/// TS(1354)
#[cold]
pub fn readonly_in_array_or_tuple_type(span: Span) -> OxcDiagnostic {
ts_error("1354", "'readonly' type modifier is only permitted on array and tuple literal types.")
.with_label(span)
}

/// TS(18010)
#[cold]
pub fn accessibility_modifier_on_private_property(modifier: &Modifier) -> OxcDiagnostic {
Expand Down
12 changes: 9 additions & 3 deletions crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ impl<'a> ParserImpl<'a> {
F: Fn(&mut Self) -> Result<TSType<'a>>,
{
let span = self.start_span();
// let is_union_type = kind == Kind::Pipe;
let has_leading_operator = self.eat(kind);
/* hasLeadingOperator && parseFunctionOrConstructorTypeToError(isUnionType) ||*/
let mut ty = parse_constituent_type(self)?;
Expand Down Expand Up @@ -253,8 +252,15 @@ impl<'a> ParserImpl<'a> {
fn parse_type_operator(&mut self, operator: TSTypeOperatorOperator) -> Result<TSType<'a>> {
let span = self.start_span();
self.bump_any(); // bump operator
let type_annotation = self.parse_type_operator_or_higher()?;
Ok(self.ast.ts_type_type_operator(self.end_span(span), operator, type_annotation))
let operator_span = self.end_span(span);
let ty = self.parse_type_operator_or_higher()?;
if operator == TSTypeOperatorOperator::Readonly
&& !matches!(ty, TSType::TSArrayType(_))
&& !matches!(ty, TSType::TSTupleType(_))
{
self.error(diagnostics::readonly_in_array_or_tuple_type(operator_span));
}
Ok(self.ast.ts_type_type_operator(self.end_span(span), operator, ty))
}

fn parse_infer_type(&mut self) -> Result<TSType<'a>> {
Expand Down
30 changes: 25 additions & 5 deletions tasks/coverage/snapshots/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: 54a8389f
parser_babel Summary:
AST Parsed : 2205/2218 (99.41%)
Positive Passed: 2190/2218 (98.74%)
Negative Passed: 1516/1634 (92.78%)
Negative Passed: 1520/1634 (93.02%)
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/annex-b/enabled/3.1-sloppy-labeled-functions-if-body/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/invalid-fn-decl-labeled-inside-if/input.js
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/core/categorized/invalid-fn-decl-labeled-inside-loop/input.js
Expand Down Expand Up @@ -115,10 +115,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/types/import-type-declaration-error/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/import-type-dynamic-errors/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/intrinsic-keyword-error/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/read-only-1/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/read-only-2/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/read-only-3/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/read-only-4/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/tuple-labeled-invalid-optional/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/tuple-optional-invalid/input.ts
Expect Syntax Error: tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/types/tuple-required-after-labeled-optional/input.ts
Expand Down Expand Up @@ -12487,6 +12483,30 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
· ────────
╰────

× TS(1354): 'readonly' type modifier is only permitted on array and tuple literal types.
╭─[babel/packages/babel-parser/test/fixtures/typescript/types/read-only-1/input.ts:1:12]
1 │ type T30 = readonly string; // Error
· ────────
╰────

× TS(1354): 'readonly' type modifier is only permitted on array and tuple literal types.
╭─[babel/packages/babel-parser/test/fixtures/typescript/types/read-only-2/input.ts:1:12]
1 │ type T31 = readonly T; // Error
· ────────
╰────

× TS(1354): 'readonly' type modifier is only permitted on array and tuple literal types.
╭─[babel/packages/babel-parser/test/fixtures/typescript/types/read-only-3/input.ts:1:12]
1 │ type T32 = readonly readonly string[]; // Error
· ────────
╰────

× TS(1354): 'readonly' type modifier is only permitted on array and tuple literal types.
╭─[babel/packages/babel-parser/test/fixtures/typescript/types/read-only-4/input.ts:1:12]
1 │ type T33 = readonly Array<string>; // Error
· ────────
╰────

× Expected `,` but found `:`
╭─[babel/packages/babel-parser/test/fixtures/typescript/types/tuple-invalid-label-1/input.ts:1:14]
1 │ type T = [x.y: A];
Expand Down
35 changes: 33 additions & 2 deletions tasks/coverage/snapshots/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ commit: d85767ab
parser_typescript Summary:
AST Parsed : 6494/6503 (99.86%)
Positive Passed: 6483/6503 (99.69%)
Negative Passed: 1239/5747 (21.56%)
Negative Passed: 1240/5747 (21.58%)
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration10.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration11.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/compiler/ClassDeclaration13.ts
Expand Down Expand Up @@ -4330,7 +4330,6 @@ Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/tup
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/tuple/indexerWithTuple.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/tuple/named/namedTupleMembersErrors.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/tuple/optionalTupleElements1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/tuple/restTupleElements1.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/tuple/strictTupleLength.ts
Expect Syntax Error: tasks/coverage/typescript/tests/cases/conformance/types/tuple/tupleLengthCheck.ts
Expand Down Expand Up @@ -24784,6 +24783,38 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private
166 │
╰────

× TS(1354): 'readonly' type modifier is only permitted on array and tuple literal types.
╭─[typescript/tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts:9:12]
8 │
9 │ type T30 = readonly string; // Error
· ────────
10 │ type T31<T> = readonly T; // Error
╰────

× TS(1354): 'readonly' type modifier is only permitted on array and tuple literal types.
╭─[typescript/tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts:10:15]
9 │ type T30 = readonly string; // Error
10 │ type T31<T> = readonly T; // Error
· ────────
11 │ type T32 = readonly readonly string[]; // Error
╰────

× TS(1354): 'readonly' type modifier is only permitted on array and tuple literal types.
╭─[typescript/tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts:11:12]
10 │ type T31<T> = readonly T; // Error
11 │ type T32 = readonly readonly string[]; // Error
· ────────
12 │ type T33 = readonly Array<string>; // Error
╰────

× TS(1354): 'readonly' type modifier is only permitted on array and tuple literal types.
╭─[typescript/tests/cases/conformance/types/tuple/readonlyArraysAndTuples.ts:12:12]
11 │ type T32 = readonly readonly string[]; // Error
12 │ type T33 = readonly Array<string>; // Error
· ────────
13 │
╰────

× Expected a semicolon or an implicit semicolon after a statement, but found none
╭─[typescript/tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts:6:5]
5 │ type string = I;
Expand Down

0 comments on commit 81eedb1

Please sign in to comment.