Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f21c9b2

Browse files
committed
fix: allow assertion function with type guard
1 parent f6c56eb commit f21c9b2

File tree

10 files changed

+3353
-53
lines changed

10 files changed

+3353
-53
lines changed

packages/parser/tests/lib/__snapshots__/typescript.ts.snap

Lines changed: 660 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const assertString = (x: any): asserts x is string => {
2+
return
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function assertsStringGuard(x: any): asserts x is string {
2+
return
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
interface AssertFoo {
2+
isString(node: any): asserts node is string;
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class AssertsFoo {
2+
isBar(): asserts this is string {
3+
return;
4+
}
5+
isBaz = (): asserts this is string => {
6+
return;
7+
}
8+
}

packages/typescript-estree/src/convert.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,35 +2428,22 @@ export class Converter {
24282428
}
24292429

24302430
case SyntaxKind.TypePredicate: {
2431-
if (node.assertsModifier) {
2432-
const result = this.createNode<TSESTree.TSAssertsTypePredicate>(
2433-
node,
2434-
{
2435-
type: AST_NODE_TYPES.TSAssertsTypePredicate,
2436-
parameterName: this.convertChild(node.parameterName),
2437-
},
2438-
);
2439-
return result;
2440-
} else if (node.type) {
2441-
const result = this.createNode<TSESTree.TSIsTypePredicate>(node, {
2442-
type: AST_NODE_TYPES.TSIsTypePredicate,
2443-
parameterName: this.convertChild(node.parameterName),
2444-
typeAnnotation: this.convertTypeAnnotation(node.type, node),
2445-
});
2446-
/**
2447-
* Specific fix for type-guard location data
2448-
*/
2431+
const result = this.createNode<TSESTree.TSTypePredicate>(node, {
2432+
type: AST_NODE_TYPES.TSTypePredicate,
2433+
asserts: node.assertsModifier !== undefined,
2434+
parameterName: this.convertChild(node.parameterName),
2435+
typeAnnotation: null,
2436+
});
2437+
/**
2438+
* Specific fix for type-guard location data
2439+
*/
2440+
if (node.type) {
2441+
result.typeAnnotation = this.convertTypeAnnotation(node.type, node);
24492442
result.typeAnnotation.loc = result.typeAnnotation.typeAnnotation.loc;
24502443
result.typeAnnotation.range =
24512444
result.typeAnnotation.typeAnnotation.range;
2452-
return result;
24532445
}
2454-
2455-
// unknown predicate - probably from a new version of ts
2456-
// don't want to error on this, but also don't want to return anything resembling another node
2457-
return this.createNode<TSESTree.TSUnsupportedTypePredicate>(node, {
2458-
type: AST_NODE_TYPES.TSUnsupportedTypePredicate,
2459-
});
2446+
return result;
24602447
}
24612448

24622449
case SyntaxKind.ImportType:

packages/typescript-estree/src/ts-estree/ast-node-types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ export enum AST_NODE_TYPES {
9595
TSAnyKeyword = 'TSAnyKeyword',
9696
TSArrayType = 'TSArrayType',
9797
TSAsExpression = 'TSAsExpression',
98-
TSAssertsTypePredicate = 'TSAssertsTypePredicate',
9998
TSAsyncKeyword = 'TSAsyncKeyword',
10099
TSBooleanKeyword = 'TSBooleanKeyword',
101100
TSBigIntKeyword = 'TSBigIntKeyword',
@@ -114,7 +113,6 @@ export enum AST_NODE_TYPES {
114113
TSExternalModuleReference = 'TSExternalModuleReference',
115114
TSImportType = 'TSImportType',
116115
TSInferType = 'TSInferType',
117-
TSIsTypePredicate = 'TSIsTypePredicate',
118116
TSLiteralType = 'TSLiteralType',
119117
TSIndexedAccessType = 'TSIndexedAccessType',
120118
TSIndexSignature = 'TSIndexSignature',
@@ -154,7 +152,7 @@ export enum AST_NODE_TYPES {
154152
TSTypeParameter = 'TSTypeParameter',
155153
TSTypeParameterDeclaration = 'TSTypeParameterDeclaration',
156154
TSTypeParameterInstantiation = 'TSTypeParameterInstantiation',
157-
TSUnsupportedTypePredicate = 'TSUnsupportedTypePredicate',
155+
TSTypePredicate = 'TSTypePredicate',
158156
TSTypeReference = 'TSTypeReference',
159157
TSTypeQuery = 'TSTypeQuery',
160158
TSIntersectionType = 'TSIntersectionType',

packages/typescript-estree/src/ts-estree/ts-estree.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ export type Node =
162162
| TSAnyKeyword
163163
| TSArrayType
164164
| TSAsExpression
165-
| TSAssertsTypePredicate
166165
| TSAsyncKeyword
167166
| TSBigIntKeyword
168167
| TSBooleanKeyword
@@ -189,7 +188,6 @@ export type Node =
189188
| TSInterfaceBody
190189
| TSInterfaceHeritage
191190
| TSIntersectionType
192-
| TSIsTypePredicate
193191
| TSLiteralType
194192
| TSMappedType
195193
| TSMethodSignature
@@ -224,12 +222,12 @@ export type Node =
224222
| TSTypeParameter
225223
| TSTypeParameterDeclaration
226224
| TSTypeParameterInstantiation
225+
| TSTypePredicate
227226
| TSTypeQuery
228227
| TSTypeReference
229228
| TSUndefinedKeyword
230229
| TSUnionType
231230
| TSUnknownKeyword
232-
| TSUnsupportedTypePredicate
233231
| TSVoidKeyword
234232
| UpdateExpression
235233
| UnaryExpression
@@ -406,7 +404,6 @@ export type TypeNode =
406404
| ThisExpression
407405
| TSAnyKeyword
408406
| TSArrayType
409-
| TSAssertsTypePredicate
410407
| TSBigIntKeyword
411408
| TSBooleanKeyword
412409
| TSClassImplements
@@ -418,7 +415,6 @@ export type TypeNode =
418415
| TSInferType
419416
| TSInterfaceHeritage
420417
| TSIntersectionType
421-
| TSIsTypePredicate
422418
| TSLiteralType
423419
| TSMappedType
424420
| TSNeverKeyword
@@ -434,6 +430,7 @@ export type TypeNode =
434430
| TSTupleType
435431
| TSTypeLiteral
436432
| TSTypeOperator
433+
| TSTypePredicate
437434
| TSTypeReference
438435
| TSTypeQuery
439436
| TSUndefinedKeyword
@@ -1368,17 +1365,10 @@ export interface TSTypeParameterInstantiation extends BaseNode {
13681365
}
13691366

13701367
export interface TSTypePredicate extends BaseNode {
1368+
type: AST_NODE_TYPES.TSTypePredicate;
1369+
asserts: boolean;
13711370
parameterName: Identifier | TSThisType;
1372-
}
1373-
export interface TSAssertsTypePredicate extends TSTypePredicate {
1374-
type: AST_NODE_TYPES.TSAssertsTypePredicate;
1375-
}
1376-
export interface TSIsTypePredicate extends TSTypePredicate {
1377-
type: AST_NODE_TYPES.TSIsTypePredicate;
1378-
typeAnnotation: TSTypeAnnotation;
1379-
}
1380-
export interface TSUnsupportedTypePredicate extends BaseNode {
1381-
type: AST_NODE_TYPES.TSUnsupportedTypePredicate;
1371+
typeAnnotation: TSTypeAnnotation | null;
13821372
}
13831373

13841374
export interface TSTypeQuery extends BaseNode {

packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,14 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e
19421942

19431943
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/type-assertion-in-method.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
19441944

1945+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/type-assertion-with-guard-in-arrow-function.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
1946+
1947+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/type-assertion-with-guard-in-function.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
1948+
1949+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/type-assertion-with-guard-in-interface.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
1950+
1951+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/type-assertion-with-guard-in-method.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
1952+
19451953
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/type-guard-in-arrow-function.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
19461954

19471955
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/type-guard-in-function.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

0 commit comments

Comments
 (0)