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 {

0 commit comments

Comments
 (0)