Skip to content

Commit 7234ba4

Browse files
fix(estree): Adjust span for TSTypePredicate.typeAnnotation (#10711)
Part of #9705 Given code like this: ```ts function f(v): v is (1|2) {} ``` Currently, `TSTypePredicate`.`type_annotation`.span contains positions for `(` and `)`, but it should not when `preserveParens: false` for TS-ESTree. > https://playground.oxc.rs/#eNptj01vgzAMhv8KyqmVkAp03QfnqddN25VLCKaKFBxkJ2wV478vKR/bob7EVl4/ft9RKFGK1qNy2mLS7oZ9mQyJ5mSX/xT7ZJwqPByK48Pp8en5JcvvdFGQLZVvVWx1rLBCkQorylGQx/jwFZ38FqUjD6kwGp0oW2k4DKxsD+sPX7vamnVyJJFbS90inlLRS2KgiJTG2K8PcJ7wzTvWDZyXVOt6TxC0A7xLAuQ/Rjx/Y4R+OzFDnaQLBG8CuMjyU0ih2RrpoHkFZSTJyP+HUraBC9wiAsrawKf1pKCT/WwiSDqNutXrPWXRkTXn4D0uDUC15RB/Bk7TL/T+ijY= --- BTW, `TypePredicate`.`type_annotation` should be just `TSType` instead of `TSTypeAnnotation`(And generate extra `TSTypeAnnotation` for TS-ESTree)? https://github.com/oxc-project/oxc/blob/7d8efd872e9481821325d58c63f11fbd2ade2dc8/crates/oxc_ast/src/ast/ts.rs#L1123-L1135 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent c18c81f commit 7234ba4

File tree

2 files changed

+4
-17
lines changed

2 files changed

+4
-17
lines changed

crates/oxc_parser/src/ts/types.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use oxc_allocator::{Box, Vec};
22
use oxc_ast::{NONE, ast::*};
3+
use oxc_span::GetSpan;
34
use oxc_syntax::operator::UnaryOperator;
45

56
use crate::{
@@ -649,9 +650,8 @@ impl<'a> ParserImpl<'a> {
649650
self.bump_any(); // bump `is`
650651
// TODO: this should go through the ast builder.
651652
let parameter_name = TSTypePredicateName::This(this_ty);
652-
let type_span = self.start_span();
653653
let ty = self.parse_ts_type();
654-
let type_annotation = Some(self.ast.ts_type_annotation(self.end_span(type_span), ty));
654+
let type_annotation = Some(self.ast.ts_type_annotation(ty.span(), ty));
655655
self.ast.ts_type_type_predicate(self.end_span(span), parameter_name, false, type_annotation)
656656
}
657657

@@ -1051,10 +1051,9 @@ impl<'a> ParserImpl<'a> {
10511051
fn parse_type_or_type_predicate(&mut self) -> TSType<'a> {
10521052
let span = self.start_span();
10531053
let type_predicate_variable = self.try_parse(Self::parse_type_predicate_prefix);
1054-
let type_span = self.start_span();
10551054
let ty = self.parse_ts_type();
10561055
if let Some(parameter_name) = type_predicate_variable {
1057-
let type_annotation = Some(self.ast.ts_type_annotation(self.end_span(type_span), ty));
1056+
let type_annotation = Some(self.ast.ts_type_annotation(ty.span(), ty));
10581057
return self.ast.ts_type_type_predicate(
10591058
self.end_span(span),
10601059
parameter_name,

tasks/coverage/snapshots/estree_typescript.snap

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,11 @@ commit: 15392346
22

33
estree_typescript Summary:
44
AST Parsed : 6480/6481 (99.98%)
5-
Positive Passed: 6472/6481 (99.86%)
6-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/controlFlowInstanceofWithSymbolHasInstance.ts
7-
8-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/narrowingUnionToUnion.ts
9-
10-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/expressions/binaryOperators/instanceofOperator/instanceofOperatorWithRHSHasSymbolHasInstance.ts
11-
12-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsPrimitiveIntersection.ts
13-
14-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/expressions/typeGuards/typeGuardNarrowsToLiteralTypeUnion.ts
15-
5+
Positive Passed: 6478/6481 (99.95%)
166
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/jsx/jsxReactTestSuite.tsx
177
JSX expressions may not use the comma operator
188

199
Mismatch: tasks/coverage/typescript/tests/cases/conformance/jsx/tsxReactEmitEntities.tsx
2010

2111
Mismatch: tasks/coverage/typescript/tests/cases/conformance/jsx/tsxReactEmitNesting.tsx
2212

23-
Mismatch: tasks/coverage/typescript/tests/cases/conformance/types/stringLiteral/stringLiteralTypesAsTags02.ts
24-

0 commit comments

Comments
 (0)