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

Skip to content

Commit 16a89b1

Browse files
committed
feat: merge ast structure of as expression with type assertion
THIS IS BREAKING CHANGE
1 parent 5e2a993 commit 16a89b1

29 files changed

+98
-92
lines changed

packages/eslint-plugin/src/rules/consistent-type-assertions.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default util.createRule<Options, MessageIds>({
8686
}
8787

8888
function reportIncorrectAssertionType(
89-
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
89+
node: TSESTree.TSTypeAssertion,
9090
): void {
9191
// If this node is `as const`, then don't report an error.
9292
if (isConst(node.typeAnnotation)) {
@@ -123,9 +123,7 @@ export default util.createRule<Options, MessageIds>({
123123
}
124124
}
125125

126-
function checkExpression(
127-
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
128-
): void {
126+
function checkExpression(node: TSESTree.TSTypeAssertion): void {
129127
if (
130128
options.assertionStyle === 'never' ||
131129
options.objectLiteralTypeAssertions === 'allow' ||
@@ -159,15 +157,7 @@ export default util.createRule<Options, MessageIds>({
159157

160158
return {
161159
TSTypeAssertion(node): void {
162-
if (options.assertionStyle !== 'angle-bracket') {
163-
reportIncorrectAssertionType(node);
164-
return;
165-
}
166-
167-
checkExpression(node);
168-
},
169-
TSAsExpression(node): void {
170-
if (options.assertionStyle !== 'as') {
160+
if (options.assertionStyle !== node.kind) {
171161
reportIncorrectAssertionType(node);
172162
return;
173163
}

packages/eslint-plugin/src/rules/indent-new-do-not-use/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ const KNOWN_NODES = new Set([
133133
AST_NODE_TYPES.TSAbstractClassProperty,
134134
AST_NODE_TYPES.TSAbstractMethodDefinition,
135135
AST_NODE_TYPES.TSArrayType,
136-
AST_NODE_TYPES.TSAsExpression,
137136
AST_NODE_TYPES.TSCallSignatureDeclaration,
138137
AST_NODE_TYPES.TSConditionalType,
139138
AST_NODE_TYPES.TSConstructorType,

packages/eslint-plugin/src/rules/indent.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const KNOWN_NODES = new Set([
3636
AST_NODE_TYPES.TSAbstractClassProperty,
3737
AST_NODE_TYPES.TSAbstractMethodDefinition,
3838
AST_NODE_TYPES.TSArrayType,
39-
AST_NODE_TYPES.TSAsExpression,
4039
AST_NODE_TYPES.TSCallSignatureDeclaration,
4140
AST_NODE_TYPES.TSConditionalType,
4241
AST_NODE_TYPES.TSConstructorType,
@@ -177,6 +176,12 @@ export default util.createRule<Options, MessageIds>({
177176
return Object.assign({}, rules, {
178177
// overwrite the base rule here so we can use our KNOWN_NODES list instead
179178
'*:exit'(node: TSESTree.Node) {
179+
if (
180+
node.type === AST_NODE_TYPES.TSTypeAssertion &&
181+
node.kind === 'as'
182+
) {
183+
return;
184+
}
180185
// For nodes we care about, skip the default handling, because it just marks the node as ignored...
181186
if (!KNOWN_NODES.has(node.type)) {
182187
rules['*:exit'](node);
@@ -192,20 +197,22 @@ export default util.createRule<Options, MessageIds>({
192197
return rules.VariableDeclaration(node);
193198
},
194199

195-
TSAsExpression(node: TSESTree.TSAsExpression) {
196-
// transform it to a BinaryExpression
197-
return rules['BinaryExpression, LogicalExpression']({
198-
type: AST_NODE_TYPES.BinaryExpression,
199-
operator: 'as',
200-
left: node.expression,
201-
// the first typeAnnotation includes the as token
202-
right: node.typeAnnotation as any,
200+
TSTypeAssertion(node: TSESTree.TSTypeAssertion) {
201+
if (node.kind === 'as') {
202+
// transform it to a BinaryExpression
203+
return rules['BinaryExpression, LogicalExpression']({
204+
type: AST_NODE_TYPES.BinaryExpression,
205+
operator: 'as',
206+
left: node.expression,
207+
// the first typeAnnotation includes the as token
208+
right: node.typeAnnotation as any,
203209

204-
// location data
205-
parent: node.parent,
206-
range: node.range,
207-
loc: node.loc,
208-
});
210+
// location data
211+
parent: node.parent,
212+
range: node.range,
213+
loc: node.loc,
214+
});
215+
}
209216
},
210217

211218
TSConditionalType(node: TSESTree.TSConditionalType) {

packages/eslint-plugin/src/rules/keyword-spacing.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export default util.createRule<Options, MessageIds>({
3131
const baseRules = baseRule.create(context);
3232
return {
3333
...baseRules,
34-
TSAsExpression(node): void {
34+
TSTypeAssertion(node): void {
35+
if (node.kind !== 'as') {
36+
return;
37+
}
3538
const asToken = util.nullThrows(
3639
sourceCode.getTokenAfter(
3740
node.expression,

packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,7 @@ export default util.createRule<Options, MessageIds>({
206206
}
207207
}
208208
},
209-
'TSAsExpression, TSTypeAssertion'(
210-
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
211-
): void {
209+
TSTypeAssertion(node: TSESTree.TSTypeAssertion): void {
212210
if (
213211
options.typesToIgnore?.includes(
214212
sourceCode.getText(node.typeAnnotation),
@@ -239,7 +237,7 @@ export default util.createRule<Options, MessageIds>({
239237
node,
240238
messageId: 'unnecessaryAssertion',
241239
fix(fixer) {
242-
return originalNode.kind === ts.SyntaxKind.TypeAssertionExpression
240+
return node.kind === 'angle-bracket'
243241
? fixer.removeRange([
244242
node.range[0],
245243
node.expression.range[0] - 1,

packages/eslint-plugin/src/rules/no-var-requires.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export default util.createRule<Options, MessageIds>({
3636
parent &&
3737
(parent.type === AST_NODE_TYPES.VariableDeclarator ||
3838
parent.type === AST_NODE_TYPES.CallExpression ||
39-
parent.type === AST_NODE_TYPES.TSAsExpression ||
4039
parent.type === AST_NODE_TYPES.TSTypeAssertion ||
4140
parent.type === AST_NODE_TYPES.MemberExpression)
4241
) {

packages/eslint-plugin/src/rules/non-nullable-type-assertion-style.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ export default util.createRule({
7272
return true;
7373
};
7474

75-
const isConstAssertion = (
76-
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
77-
): boolean => {
75+
const isConstAssertion = (node: TSESTree.TSTypeAssertion): boolean => {
7876
return (
7977
node.typeAnnotation.type === AST_NODE_TYPES.TSTypeReference &&
8078
node.typeAnnotation.typeName.type === AST_NODE_TYPES.Identifier &&
@@ -83,9 +81,7 @@ export default util.createRule({
8381
};
8482

8583
return {
86-
'TSAsExpression, TSTypeAssertion'(
87-
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
88-
): void {
84+
TSTypeAssertion(node: TSESTree.TSTypeAssertion): void {
8985
if (isConstAssertion(node)) {
9086
return;
9187
}

packages/eslint-plugin/src/rules/prefer-as-const.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ export default util.createRule({
6363
}
6464

6565
return {
66-
TSAsExpression(node): void {
67-
compareTypes(node.expression, node.typeAnnotation, true);
68-
},
6966
TSTypeAssertion(node): void {
7067
compareTypes(node.expression, node.typeAnnotation, true);
7168
},

packages/eslint-plugin/src/rules/unbound-method.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ function isSafeUse(node: TSESTree.Node): boolean {
309309

310310
case AST_NODE_TYPES.ChainExpression:
311311
case AST_NODE_TYPES.TSNonNullExpression:
312-
case AST_NODE_TYPES.TSAsExpression:
313312
case AST_NODE_TYPES.TSTypeAssertion:
314313
return isSafeUse(parent);
315314

packages/eslint-plugin/tests/rules/indent/indent.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type foo = ArrType[];
7575
],
7676
},
7777
{
78-
node: AST_NODE_TYPES.TSAsExpression,
78+
node: AST_NODE_TYPES.TSTypeAssertion,
7979
code: [
8080
`
8181
const foo = {} as {

packages/experimental-utils/src/ast-utils/predicates.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,11 @@ function isLogicalOrOperator(
5656
*/
5757
function isTypeAssertion(
5858
node: TSESTree.Node | undefined | null,
59-
): node is TSESTree.TSAsExpression | TSESTree.TSTypeAssertion {
59+
): node is TSESTree.TSTypeAssertion {
6060
if (!node) {
6161
return false;
6262
}
63-
return (
64-
node.type === AST_NODE_TYPES.TSAsExpression ||
65-
node.type === AST_NODE_TYPES.TSTypeAssertion
66-
);
63+
return node.type === AST_NODE_TYPES.TSTypeAssertion;
6764
}
6865

6966
function isVariableDeclarator(

packages/experimental-utils/src/ts-eslint/Rule.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ interface RuleListener {
331331
TSAbstractMethodDefinition?: RuleFunction<TSESTree.TSAbstractMethodDefinition>;
332332
TSAnyKeyword?: RuleFunction<TSESTree.TSAnyKeyword>;
333333
TSArrayType?: RuleFunction<TSESTree.TSArrayType>;
334-
TSAsExpression?: RuleFunction<TSESTree.TSAsExpression>;
335334
TSAsyncKeyword?: RuleFunction<TSESTree.TSAsyncKeyword>;
336335
TSBigIntKeyword?: RuleFunction<TSESTree.TSBigIntKeyword>;
337336
TSBooleanKeyword?: RuleFunction<TSESTree.TSBooleanKeyword>;

packages/scope-manager/src/referencer/Referencer.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,6 @@ class Referencer extends Visitor {
297297
TypeVisitor.visit(this, node);
298298
}
299299

300-
protected visitTypeAssertion(
301-
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
302-
): void {
303-
this.visit(node.expression);
304-
this.visitType(node.typeAnnotation);
305-
}
306-
307300
/////////////////////
308301
// Visit selectors //
309302
/////////////////////
@@ -317,7 +310,6 @@ class Referencer extends Visitor {
317310
protected AssignmentExpression(node: TSESTree.AssignmentExpression): void {
318311
let left = node.left;
319312
switch (left.type) {
320-
case AST_NODE_TYPES.TSAsExpression:
321313
case AST_NODE_TYPES.TSTypeAssertion:
322314
// explicitly visit the type annotation
323315
this.visit(left.typeAnnotation);
@@ -603,10 +595,6 @@ class Referencer extends Visitor {
603595
this.visitType(node.typeParameters);
604596
}
605597

606-
protected TSAsExpression(node: TSESTree.TSAsExpression): void {
607-
this.visitTypeAssertion(node);
608-
}
609-
610598
protected TSDeclareFunction(node: TSESTree.TSDeclareFunction): void {
611599
this.visitFunction(node);
612600
}
@@ -707,7 +695,13 @@ class Referencer extends Visitor {
707695
}
708696

709697
protected TSTypeAssertion(node: TSESTree.TSTypeAssertion): void {
710-
this.visitTypeAssertion(node);
698+
if (node.kind === 'as') {
699+
this.visit(node.expression);
700+
this.visitType(node.typeAnnotation);
701+
} else {
702+
this.visitType(node.typeAnnotation);
703+
this.visit(node.expression);
704+
}
711705
}
712706

713707
protected UpdateExpression(node: TSESTree.UpdateExpression): void {

packages/scope-manager/tests/fixtures/type-assertion/angle-bracket.ts.shot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ ScopeManager {
2323
resolved: Variable$2,
2424
writeExpr: Literal$2,
2525
},
26-
Reference$2 {
26+
Reference$3 {
2727
identifier: Identifier<"x">,
2828
isRead: true,
2929
isTypeReference: false,
@@ -44,7 +44,7 @@ ScopeManager {
4444
],
4545
name: "T",
4646
references: Array [
47-
Reference$3 {
47+
Reference$2 {
4848
identifier: Identifier<"T">,
4949
isRead: true,
5050
isTypeReference: true,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ enum AST_NODE_TYPES {
9191
TSAbstractMethodDefinition = 'TSAbstractMethodDefinition',
9292
TSAnyKeyword = 'TSAnyKeyword',
9393
TSArrayType = 'TSArrayType',
94-
TSAsExpression = 'TSAsExpression',
9594
TSAsyncKeyword = 'TSAsyncKeyword',
9695
TSBigIntKeyword = 'TSBigIntKeyword',
9796
TSBooleanKeyword = 'TSBooleanKeyword',

packages/types/src/ts-estree.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ export type Node =
222222
| TSAbstractMethodDefinition
223223
| TSAnyKeyword
224224
| TSArrayType
225-
| TSAsExpression
226225
| TSAsyncKeyword
227226
| TSBigIntKeyword
228227
| TSBooleanKeyword
@@ -373,7 +372,7 @@ export type Expression =
373372
| RestElement
374373
| SequenceExpression
375374
| SpreadElement
376-
| TSAsExpression
375+
| TSTypeAssertion
377376
| TSUnaryExpression
378377
| YieldExpression;
379378
export type ForInitialiser = Expression | VariableDeclaration;
@@ -409,7 +408,7 @@ export type LeftHandSideExpression =
409408
| PrimaryExpression
410409
| TaggedTemplateExpression
411410
| TSNonNullExpression
412-
| TSAsExpression
411+
| TSTypeAssertion
413412
| ArrowFunctionExpression;
414413
export type Literal =
415414
| BigIntLiteral
@@ -1261,7 +1260,7 @@ export interface ThisExpression extends BaseNode {
12611260

12621261
export interface ThrowStatement extends BaseNode {
12631262
type: AST_NODE_TYPES.ThrowStatement;
1264-
argument: Statement | TSAsExpression | null;
1263+
argument: Statement | TSTypeAssertion | null;
12651264
}
12661265

12671266
export interface TryStatement extends BaseNode {
@@ -1304,12 +1303,6 @@ export interface TSArrayType extends BaseNode {
13041303
elementType: TypeNode;
13051304
}
13061305

1307-
export interface TSAsExpression extends BaseNode {
1308-
type: AST_NODE_TYPES.TSAsExpression;
1309-
expression: Expression;
1310-
typeAnnotation: TypeNode;
1311-
}
1312-
13131306
export interface TSAsyncKeyword extends BaseNode {
13141307
type: AST_NODE_TYPES.TSAsyncKeyword;
13151308
}
@@ -1647,6 +1640,7 @@ export interface TSTypeAssertion extends BaseNode {
16471640
type: AST_NODE_TYPES.TSTypeAssertion;
16481641
typeAnnotation: TypeNode;
16491642
expression: Expression;
1643+
kind: 'as' | 'angle-bracket';
16501644
}
16511645

16521646
export interface TSTypeLiteral extends BaseNode {

packages/typescript-estree/src/convert.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,10 +2619,11 @@ export class Converter {
26192619
});
26202620
}
26212621
case SyntaxKind.AsExpression: {
2622-
return this.createNode<TSESTree.TSAsExpression>(node, {
2623-
type: AST_NODE_TYPES.TSAsExpression,
2622+
return this.createNode<TSESTree.TSTypeAssertion>(node, {
2623+
type: AST_NODE_TYPES.TSTypeAssertion,
26242624
expression: this.convertChild(node.expression),
26252625
typeAnnotation: this.convertType(node.type),
2626+
kind: 'as',
26262627
});
26272628
}
26282629
case SyntaxKind.InferType: {
@@ -2656,6 +2657,7 @@ export class Converter {
26562657
type: AST_NODE_TYPES.TSTypeAssertion,
26572658
typeAnnotation: this.convertType(node.type),
26582659
expression: this.convertChild(node.expression),
2660+
kind: 'angle-bracket',
26592661
});
26602662
}
26612663
case SyntaxKind.ImportEqualsDeclaration: {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ export interface EstreeToTsNodeTypes {
156156
| ts.MethodDeclaration
157157
| ts.ConstructorDeclaration;
158158
[AST_NODE_TYPES.TSArrayType]: ts.ArrayTypeNode;
159-
[AST_NODE_TYPES.TSAsExpression]: ts.AsExpression;
160159
[AST_NODE_TYPES.TSCallSignatureDeclaration]: ts.PropertySignature;
161160
[AST_NODE_TYPES.TSClassImplements]: ts.ExpressionWithTypeArguments;
162161
[AST_NODE_TYPES.TSConditionalType]: ts.ConditionalTypeNode;
@@ -203,7 +202,7 @@ export interface EstreeToTsNodeTypes {
203202
[AST_NODE_TYPES.TSTemplateLiteralType]: ts.TemplateLiteralTypeNode;
204203
[AST_NODE_TYPES.TSTypeAliasDeclaration]: ts.TypeAliasDeclaration;
205204
[AST_NODE_TYPES.TSTypeAnnotation]: undefined;
206-
[AST_NODE_TYPES.TSTypeAssertion]: ts.TypeAssertion;
205+
[AST_NODE_TYPES.TSTypeAssertion]: ts.TypeAssertion | ts.AsExpression;
207206
[AST_NODE_TYPES.TSTypeLiteral]: ts.TypeLiteralNode;
208207
[AST_NODE_TYPES.TSTypeOperator]: ts.TypeOperatorNode;
209208
[AST_NODE_TYPES.TSTypeParameter]: ts.TypeParameterDeclaration;

0 commit comments

Comments
 (0)